KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > start > Monitor


1 // ========================================================================
2
// $Id: Monitor.java,v 1.6 2004/05/09 20:32:46 gregwilkins Exp $
3
// Copyright 2003-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.start;
17 import java.io.InputStreamReader JavaDoc;
18 import java.io.LineNumberReader JavaDoc;
19 import java.net.InetAddress JavaDoc;
20 import java.net.ServerSocket JavaDoc;
21 import java.net.Socket JavaDoc;
22
23 /*-------------------------------------------*/
24 /** Monitor thread.
25  * This thread listens on the port specified by the STOP.PORT system parameter
26  * (defaults to 8079) for request authenticated with the key given by the STOP.KEY
27  * system parameter (defaults to "mortbay") for admin requests. Commands "stop" and
28  * "status" are currently supported.
29  */

30 public class Monitor extends Thread JavaDoc
31 {
32     private int _port = Integer.getInteger("STOP.PORT",8079).intValue();
33     private String JavaDoc _key = System.getProperty("STOP.KEY","mortbay");
34
35     ServerSocket JavaDoc _socket;
36     
37     Monitor()
38     {
39         try
40         {
41             if(_port<0)
42                 return;
43             setDaemon(true);
44             _socket=new ServerSocket JavaDoc(_port,1,InetAddress.getByName("127.0.0.1"));
45             if (_port==0)
46             {
47                 _port=_socket.getLocalPort();
48                 System.out.println(_port);
49             }
50             if (!"mortbay".equals(_key))
51             {
52                 _key=Long.toString((long)(Long.MAX_VALUE*Math.random()),36);
53                 System.out.println(_key);
54             }
55         }
56         catch(Exception JavaDoc e)
57         {
58             if (Main._debug)
59                 e.printStackTrace();
60             else
61                 System.err.println(e.toString());
62         }
63         if (_socket!=null)
64             this.start();
65         else
66             System.err.println("WARN: Not listening on monitor port: "+_port);
67     }
68     
69     public void run()
70     {
71         while (true)
72         {
73             Socket JavaDoc socket=null;
74             try{
75                 socket=_socket.accept();
76                 
77                 LineNumberReader JavaDoc lin=
78                     new LineNumberReader JavaDoc(new InputStreamReader JavaDoc(socket.getInputStream()));
79                 String JavaDoc key=lin.readLine();
80                 if (!_key.equals(key))
81                     continue;
82                 
83                 String JavaDoc cmd=lin.readLine();
84                 if (Main._debug) System.err.println("command="+cmd);
85                 if ("stop".equals(cmd))
86                 {
87                     try {socket.close();}catch(Exception JavaDoc e){e.printStackTrace();}
88                     try {_socket.close();}catch(Exception JavaDoc e){e.printStackTrace();}
89                     System.exit(0);
90                 }
91                 else if ("status".equals(cmd))
92                 {
93                     socket.getOutputStream().write("OK\r\n".getBytes());
94                     socket.getOutputStream().flush();
95                 }
96             }
97             catch(Exception JavaDoc e)
98             {
99                 if (Main._debug)
100                     e.printStackTrace();
101                 else
102                     System.err.println(e.toString());
103             }
104             finally
105             {
106                 if (socket!=null)
107                 {
108                     try{socket.close();}catch(Exception JavaDoc e){}
109                 }
110                 socket=null;
111             }
112         }
113     }
114
115     /** Start a Monitor.
116      * This static method starts a monitor that listens for admin requests.
117      */

118     public static void monitor()
119     {
120         new Monitor();
121     }
122  
123 }
124
Popular Tags