1
2
3
4
5
6
7
8
9
10
11
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
23
24
25
26
27
28
29
30
31
32
33 public class Monitor extends Thread
34 {
35 private int _port = Integer.getInteger("STOP.PORT", -1).intValue();
36 private String _key = System.getProperty("STOP.KEY", null);
37
38 ServerSocket _socket;
39
40 Monitor()
41 {
42 try
43 {
44 if(_port<0)
45 return;
46 setDaemon(true);
47 setName("StopMonitor");
48 _socket=new ServerSocket(_port,1,InetAddress.getByName("127.0.0.1"));
49 if (_port==0)
50 {
51 _port=_socket.getLocalPort();
52 System.out.println(_port);
53 }
54
55 if (_key==null)
56 {
57 _key=Long.toString((long)(Long.MAX_VALUE*Math.random()+this.hashCode()+System.currentTimeMillis()),36);
58 System.out.println("-DSTOP.KEY="+_key);
59 }
60 }
61 catch(Exception e)
62 {
63 if (Main._debug)
64 e.printStackTrace();
65 else
66 System.err.println(e.toString());
67 }
68 if (_socket!=null)
69 this.start();
70 else
71 System.err.println("WARN: Not listening on monitor port: "+_port);
72 }
73
74 public void run()
75 {
76 while (true)
77 {
78 Socket socket=null;
79 try{
80 socket=_socket.accept();
81
82 LineNumberReader lin=
83 new LineNumberReader(new InputStreamReader(socket.getInputStream()));
84 String key=lin.readLine();
85 if (!_key.equals(key))
86 continue;
87
88 String cmd=lin.readLine();
89 if (Main._debug) System.err.println("command="+cmd);
90 if ("stop".equals(cmd))
91 {
92 try {socket.close();}catch(Exception e){e.printStackTrace();}
93 try {_socket.close();}catch(Exception e){e.printStackTrace();}
94 System.exit(0);
95 }
96 else if ("status".equals(cmd))
97 {
98 socket.getOutputStream().write("OK\r\n".getBytes());
99 socket.getOutputStream().flush();
100 }
101 }
102 catch(Exception e)
103 {
104 if (Main._debug)
105 e.printStackTrace();
106 else
107 System.err.println(e.toString());
108 }
109 finally
110 {
111 if (socket!=null)
112 {
113 try{socket.close();}catch(Exception e){}
114 }
115 socket=null;
116 }
117 }
118 }
119
120
121
122
123 public static void monitor()
124 {
125 new Monitor();
126 }
127
128 }