001: // ========================================================================
002: // Copyright 2003-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.start;
016:
017: import java.io.InputStreamReader;
018: import java.io.LineNumberReader;
019: import java.net.InetAddress;
020: import java.net.ServerSocket;
021: import java.net.Socket;
022:
023: /*-------------------------------------------*/
024: /** Monitor thread.
025: * This thread listens on the port specified by the STOP.PORT system parameter
026: * (defaults to 0 or not listening) for request authenticated with the key given by the STOP.KEY
027: * system parameter (defaults to "mortbay") for admin requests. Commands "stop" and
028: * "status" are currently supported.
029: */
030: public class Monitor extends Thread {
031: private int _port = Integer.getInteger("STOP.PORT", -1).intValue();
032: private String _key = System.getProperty("STOP.KEY", null);
033:
034: ServerSocket _socket;
035:
036: Monitor() {
037: try {
038: if (_port < 0)
039: return;
040: setDaemon(true);
041: setName("StopMonitor");
042: _socket = new ServerSocket(_port, 1, InetAddress
043: .getByName("127.0.0.1"));
044: if (_port == 0) {
045: _port = _socket.getLocalPort();
046: System.out.println(_port);
047: }
048:
049: if (_key == null) {
050: _key = Long.toString((long) (Long.MAX_VALUE
051: * Math.random() + this .hashCode() + System
052: .currentTimeMillis()), 36);
053: System.out.println("-DSTOP.KEY=" + _key);
054: }
055: } catch (Exception e) {
056: if (Main._debug)
057: e.printStackTrace();
058: else
059: System.err.println(e.toString());
060: }
061: if (_socket != null)
062: this .start();
063: else
064: System.err.println("WARN: Not listening on monitor port: "
065: + _port);
066: }
067:
068: public void run() {
069: while (true) {
070: Socket socket = null;
071: try {
072: socket = _socket.accept();
073:
074: LineNumberReader lin = new LineNumberReader(
075: new InputStreamReader(socket.getInputStream()));
076: String key = lin.readLine();
077: if (!_key.equals(key))
078: continue;
079:
080: String cmd = lin.readLine();
081: if (Main._debug)
082: System.err.println("command=" + cmd);
083: if ("stop".equals(cmd)) {
084: try {
085: socket.close();
086: } catch (Exception e) {
087: e.printStackTrace();
088: }
089: try {
090: _socket.close();
091: } catch (Exception e) {
092: e.printStackTrace();
093: }
094: System.exit(0);
095: } else if ("status".equals(cmd)) {
096: socket.getOutputStream().write("OK\r\n".getBytes());
097: socket.getOutputStream().flush();
098: }
099: } catch (Exception e) {
100: if (Main._debug)
101: e.printStackTrace();
102: else
103: System.err.println(e.toString());
104: } finally {
105: if (socket != null) {
106: try {
107: socket.close();
108: } catch (Exception e) {
109: }
110: }
111: socket = null;
112: }
113: }
114: }
115:
116: /** Start a Monitor.
117: * This static method starts a monitor that listens for admin requests.
118: */
119: public static void monitor() {
120: new Monitor();
121: }
122:
123: }
|