View Javadoc

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