View Javadoc

1   // ========================================================================
2   // Copyright (c) 2003-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.start;
15  import java.io.InputStreamReader;
16  import java.io.LineNumberReader;
17  import java.net.InetAddress;
18  import java.net.ServerSocket;
19  import java.net.Socket;
20  
21  /*-------------------------------------------*/
22  /** Monitor thread.
23   * This thread listens on the port specified by the STOP.PORT system parameter
24   * (defaults to -1 for not listening) for request authenticated with the key given by the STOP.KEY
25   * system parameter (defaults to "eclipse") for admin requests. 
26   * <p>
27   * If the stop port is set to zero, then a random port is assigned and the port number
28   * is printed to stdout.
29   * <p>
30   * Commands "stop" and * "status" are currently supported.
31   *
32   */
33  public class Monitor extends Thread
34  {
35      private int _port;
36      private String _key;
37  
38      ServerSocket _socket;
39      
40      Monitor(int port,String key)
41      {
42          _port=port;
43          _key=key;
44          
45          try
46          {
47              if(_port<0)
48                  return;
49              setDaemon(true);
50  	    setName("StopMonitor");
51              _socket=new ServerSocket(_port,1,InetAddress.getByName("127.0.0.1"));
52              if (_port==0)
53              {
54                  _port=_socket.getLocalPort();
55                  System.out.println(_port);
56              }
57              
58              if (_key==null)
59              {
60                  _key=Long.toString((long)(Long.MAX_VALUE*Math.random()+this.hashCode()+System.currentTimeMillis()),36);
61                  System.out.println("STOP.KEY="+_key);
62              }
63          }
64          catch(Exception e)
65          {
66              if (Main.DEBUG)
67                  e.printStackTrace();
68              else
69                  System.err.println(e.toString());
70          }
71          if (_socket!=null)
72              this.start();
73          else
74              System.err.println("WARN: Not listening on monitor port: "+_port);
75      }
76      
77      public void run()
78      {
79          while (true)
80          {
81              Socket socket=null;
82              try{
83                  socket=_socket.accept();
84                  
85                  LineNumberReader lin=
86                      new LineNumberReader(new InputStreamReader(socket.getInputStream()));
87                  String key=lin.readLine();
88                  if (!_key.equals(key))
89                      continue;
90                  
91                  String cmd=lin.readLine();
92                  if (Main.DEBUG) System.err.println("command="+cmd);
93                  if ("stop".equals(cmd))
94                  {
95                      try {socket.close();}catch(Exception e){e.printStackTrace();}
96                      try {_socket.close();}catch(Exception e){e.printStackTrace();}
97                      System.exit(0);
98                  }
99                  else if ("status".equals(cmd))
100                 {
101                     socket.getOutputStream().write("OK\r\n".getBytes());
102                     socket.getOutputStream().flush();
103                 }
104             }
105             catch(Exception e)
106             {
107                 if (Main.DEBUG)
108                     e.printStackTrace();
109                 else
110                     System.err.println(e.toString());
111             }
112             finally
113             {
114                 if (socket!=null)
115                 {
116                     try{socket.close();}catch(Exception e){}
117                 }
118                 socket=null;
119             }
120         }
121     }
122 
123     /** Start a Monitor.
124      * This static method starts a monitor that listens for admin requests.
125      */
126     public static void monitor(int port,String key)
127     {
128         new Monitor(port,key);
129     }
130  
131 }