001: // ========================================================================
002: // Copyright (c) 2003 Mort Bay Consulting (Australia) Pty. Ltd.
003: // $Id: Monitor.java 6177 2007-02-19 10:11:27Z aaime $
004: // ========================================================================
005: package org.mortbay.start;
006:
007: import java.io.InputStreamReader;
008: import java.io.LineNumberReader;
009: import java.net.InetAddress;
010: import java.net.ServerSocket;
011: import java.net.Socket;
012:
013: /*-------------------------------------------*/
014: /** Monitor thread.
015: * This thread listens on the port specified by the STOP.PORT system parameter
016: * (defaults to 8079) for request authenticated with the key given by the STOP.KEY
017: * system parameter (defaults to "mortbay") for admin requests. Commands "stop" and
018: * "status" are currently supported.
019: */
020: public class Monitor extends Thread {
021: private int _port = Integer.getInteger("STOP.PORT", 8079)
022: .intValue();
023: private String _key = System.getProperty("STOP.KEY", "mortbay");
024: ServerSocket _socket;
025:
026: Monitor() {
027: try {
028: if (_port < 0) {
029: return;
030: }
031:
032: setDaemon(true);
033: _socket = new ServerSocket(_port, 1, InetAddress
034: .getByName("127.0.0.1"));
035:
036: if (_port == 0) {
037: _port = _socket.getLocalPort();
038: System.out.println(_port);
039: }
040:
041: if (!"mortbay".equals(_key)) {
042: _key = Long.toString((long) (Long.MAX_VALUE * Math
043: .random()), 36);
044: System.out.println(_key);
045: }
046: } catch (Exception e) {
047: if (Main._debug) {
048: e.printStackTrace();
049: } else {
050: System.err.println(e.toString());
051: }
052: }
053:
054: if (_socket != null) {
055: this .start();
056: } else {
057: System.err.println("WARN: Not listening on monitor port: "
058: + _port);
059: }
060: }
061:
062: public void run() {
063: while (true) {
064: Socket socket = null;
065:
066: try {
067: socket = _socket.accept();
068:
069: LineNumberReader lin = new LineNumberReader(
070: new InputStreamReader(socket.getInputStream()));
071: String key = lin.readLine();
072:
073: if (!_key.equals(key)) {
074: continue;
075: }
076:
077: String cmd = lin.readLine();
078:
079: if (Main._debug) {
080: System.err.println("command=" + cmd);
081: }
082:
083: if ("stop".equals(cmd)) {
084: try {
085: socket.close();
086: } catch (Exception e) {
087: e.printStackTrace();
088: }
089:
090: try {
091: _socket.close();
092: } catch (Exception e) {
093: e.printStackTrace();
094: }
095:
096: System.exit(0);
097: } else if ("status".equals(cmd)) {
098: socket.getOutputStream().write("OK\r\n".getBytes());
099: socket.getOutputStream().flush();
100: }
101: } catch (Exception e) {
102: if (Main._debug) {
103: e.printStackTrace();
104: } else {
105: System.err.println(e.toString());
106: }
107: } finally {
108: if (socket != null) {
109: try {
110: socket.close();
111: } catch (Exception e) {
112: }
113: }
114:
115: socket = null;
116: }
117: }
118: }
119:
120: /** Start a Monitor.
121: * This static method starts a monitor that listens for admin requests.
122: */
123: public static void monitor() {
124: new Monitor();
125: }
126: }
|