View Javadoc

    1   //========================================================================
    2   //$Id: Monitor.java 4005 2008-11-06 22:31:53Z janb $
    3   //Copyright 2008 Mort Bay Consulting Pty. Ltd.
    4   //------------------------------------------------------------------------
    5   //Licensed under the Apache License, Version 2.0 (the "License");
    6   //you may not use this file except in compliance with the License.
    7   //You may obtain a copy of the License at
    8   //http://www.apache.org/licenses/LICENSE-2.0
    9   //Unless required by applicable law or agreed to in writing, software
    10  //distributed under the License is distributed on an "AS IS" BASIS,
    11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //See the License for the specific language governing permissions and
    13  //limitations under the License.
    14  //========================================================================
    15
    16
    17  package org.mortbay.jetty.plugin.util;
    18
    19  import java.io.IOException;
    20  import java.io.InputStreamReader;
    21  import java.io.LineNumberReader;
    22  import java.net.InetAddress;
    23  import java.net.ServerSocket;
    24  import java.net.Socket;
    25  import java.net.UnknownHostException;
    26
    27  import org.mortbay.jetty.Server;
    28
    29
    30
    31
    32  /**
    33   * Monitor
    34   *
    35   * Listens for stop commands eg via mvn jetty:stop and
    36   * causes jetty to stop either by exiting the jvm, or
    37   * by stopping the Server instances. The choice of
    38   * behaviour is controlled by either passing true
    39   * (exit jvm) or false (stop Servers) in the constructor.
    40   *
    41   */
    42  public class Monitor extends Thread
    43  {
    44      private String _key;
    45      private Server[] _servers;
    46
    47      ServerSocket _serverSocket;
    48      boolean _kill;
    49
    50      public Monitor(int port, String key, Server[] servers, boolean kill)
    51      throws UnknownHostException, IOException
    52      {
    53          if (port <= 0)
    54              throw new IllegalStateException ("Bad stop port");
    55          if (key==null)
    56              throw new IllegalStateException("Bad stop key");
    57
    58          _key = key;
    59          _servers = servers;
    60          _kill = kill;
    61          setDaemon(true);
    62          setName("StopJettyPluginMonitor");
    63          _serverSocket=new ServerSocket(port,1,InetAddress.getByName("127.0.0.1"));
    64          _serverSocket.setReuseAddress(true);
    65      }
    66
    67      public void run()
    68      {
    69          while (_serverSocket != null)
    70          {
    71              Socket socket = null;
    72              try
    73              {
    74                  socket = _serverSocket.accept();
    75                  socket.setSoLinger(false, 0);
    76                  LineNumberReader lin = new LineNumberReader(new InputStreamReader(socket.getInputStream()));
    77
    78                  String key = lin.readLine();
    79                  if (!_key.equals(key)) continue;
    80                  String cmd = lin.readLine();
    81                  if ("stop".equals(cmd))
    82                  {
    83                      try{socket.close();}catch (Exception e){PluginLog.getLog().debug(e);}
    84                      try{socket.close();}catch (Exception e){PluginLog.getLog().debug(e);}
    85                      try{_serverSocket.close();}catch (Exception e){PluginLog.getLog().debug(e);}
    86
    87                      _serverSocket = null;
    88
    89                      if (_kill)
    90                      {
    91                          PluginLog.getLog().info("Killing Jetty");
    92                          System.exit(0);
    93                      }
    94                      else
    95                      {
    96                          for (int i=0; _servers != null && i < _servers.length; i++)
    97                          {
    98                              try
    99                              {
    100                                 PluginLog.getLog().info("Stopping server "+i);
    101                                 _servers[i].stop();
    102                             }
    103                             catch (Exception e)
    104                             {
    105                                 PluginLog.getLog().error(e);
    106                             }
    107                         }
    108                     }
    109                 }
    110                 else
    111                     PluginLog.getLog().info("Unsupported monitor operation");
    112             }
    113             catch (Exception e)
    114             {
    115                 PluginLog.getLog().error(e);
    116             }
    117             finally
    118             {
    119                 if (socket != null)
    120                 {
    121                     try
    122                     {
    123                         socket.close();
    124                     }
    125                     catch (Exception e)
    126                     {
    127                         PluginLog.getLog().debug(e);
    128                     }
    129                 }
    130                 socket = null;
    131             }
    132         }
    133     }
    134 }