KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > boot > ControlThread


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2005 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.boot;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.net.ServerSocket JavaDoc;
26 import java.net.Socket JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32  * @version $Id: ControlThread.java,v 1.2 2006/01/27 20:52:15 ddimon Exp $
33  */

34 final class ControlThread extends Thread JavaDoc {
35     static boolean isApplicationRunning(final InetAddress JavaDoc host,
36             final int port) {
37         try {
38             Socket JavaDoc socket = new Socket JavaDoc(host, port);
39             try {
40                 socket.setKeepAlive(true);
41                 String JavaDoc test = "" + System.currentTimeMillis(); //$NON-NLS-1$
42
OutputStream JavaDoc out = socket.getOutputStream();
43                 InputStream JavaDoc in = null;
44                 try {
45                     System.out.println("found running control service on " //$NON-NLS-1$
46
+ host + ":" + port); //$NON-NLS-1$
47
out.write(("PING " + test).getBytes()); //$NON-NLS-1$
48
out.flush();
49                     socket.shutdownOutput();
50                     in = socket.getInputStream();
51                     StringBuffer JavaDoc commandResult = new StringBuffer JavaDoc();
52                     byte[] buf = new byte[16];
53                     int len;
54                     while ((len = in.read(buf)) != -1) {
55                         commandResult.append(new String JavaDoc(buf, 0, len));
56                     }
57                     socket.shutdownInput();
58                     if (commandResult.toString().startsWith("OK") //$NON-NLS-1$
59
&& (commandResult.toString().indexOf(test) != -1)) {
60                         System.out.println("PING command succeed"); //$NON-NLS-1$
61
return true;
62                     }
63                     System.out.println("PING command failed"); //$NON-NLS-1$
64
} finally {
65                     try {
66                         out.close();
67                     } catch (IOException JavaDoc ioe) {
68                         // ignore
69
}
70                     if (in != null) {
71                         try {
72                             in.close();
73                         } catch (IOException JavaDoc ioe) {
74                             // ignore
75
}
76                     }
77                 }
78             } finally {
79                 socket.close();
80             }
81         } catch (IOException JavaDoc ioe) {
82             System.out.println(
83                     "seems that there is no control service running on " //$NON-NLS-1$
84
+ host + ":" + port); //$NON-NLS-1$
85
//ioe.printStackTrace();
86
}
87         return false;
88     }
89     
90     static boolean stopRunningApplication(final InetAddress JavaDoc host,
91             final int port) {
92         boolean result = false;
93         try {
94             Socket JavaDoc socket = new Socket JavaDoc(host, port);
95             try {
96                 socket.setKeepAlive(true);
97                 OutputStream JavaDoc out = socket.getOutputStream();
98                 InputStream JavaDoc in = null;
99                 try {
100                     System.out.println("found running control service on " //$NON-NLS-1$
101
+ host + ":" + port); //$NON-NLS-1$
102
out.write("STOP".getBytes()); //$NON-NLS-1$
103
out.flush();
104                     socket.shutdownOutput();
105                     in = socket.getInputStream();
106                     StringBuffer JavaDoc commandResult = new StringBuffer JavaDoc();
107                     byte[] buf = new byte[16];
108                     int len;
109                     while ((len = in.read(buf)) != -1) {
110                         commandResult.append(new String JavaDoc(buf, 0, len));
111                     }
112                     socket.shutdownInput();
113                     if (commandResult.toString().startsWith("OK")) { //$NON-NLS-1$
114
System.out.println("STOP command succeed"); //$NON-NLS-1$
115
result = true;
116                     } else {
117                         System.out.println("STOP command failed"); //$NON-NLS-1$
118
}
119                 } finally {
120                     try {
121                         out.close();
122                     } catch (IOException JavaDoc ioe) {
123                         // ignore
124
}
125                     if (in != null) {
126                         try {
127                             in.close();
128                         } catch (IOException JavaDoc ioe) {
129                             // ignore
130
}
131                     }
132                 }
133             } finally {
134                 socket.close();
135             }
136         } catch (IOException JavaDoc ioe) {
137             System.out.println(
138                     "seems that there is no control service running on " //$NON-NLS-1$
139
+ host + ":" + port); //$NON-NLS-1$
140
//ioe.printStackTrace();
141
}
142         if (result) {
143             try {
144                 Thread.sleep(2000);
145             } catch (InterruptedException JavaDoc ie) {
146                 // ignore
147
}
148         }
149         return result;
150     }
151
152     private Log log;
153
154     private ServerSocket JavaDoc serverSocket;
155     private final ServiceApplication application;
156     private boolean appRunning;
157     
158     ControlThread(final InetAddress JavaDoc host, final int port,
159             final ServiceApplication server) throws Exception JavaDoc {
160         log = LogFactory.getLog(this.getClass());
161         application = server;
162         serverSocket = new ServerSocket JavaDoc(port, 1, host);
163         appRunning = true;
164         setName("jpf-application-control-thread"); //$NON-NLS-1$
165
}
166     
167     /**
168      * @see java.lang.Runnable#run()
169      */

170     public void run() {
171         try {
172             while (true) {
173                 try {
174                     Socket JavaDoc clientSocket = serverSocket.accept();
175                     try {
176                         if (handleRequest(clientSocket)) {
177                             break;
178                         }
179                     } finally {
180                         try {
181                             clientSocket.close();
182                         } catch (IOException JavaDoc ioe) {
183                             // ignore
184
}
185                     }
186                 } catch (Exception JavaDoc e) {
187                     warn("error on server socket", e); //$NON-NLS-1$
188
break;
189                 }
190             }
191         } catch (Exception JavaDoc e) {
192             error(e);
193         } finally {
194             try {
195                 serverSocket.close();
196             } catch (IOException JavaDoc ioe) {
197                 warn("error closing server socket", ioe); //$NON-NLS-1$
198
}
199             if (appRunning) {
200                 stopApplication();
201             }
202         }
203     }
204     
205     private synchronized boolean handleRequest(final Socket JavaDoc clientSocket) {
206         debug("handling control request"); //$NON-NLS-1$
207
if (!isValidRemoteHost(clientSocket.getInetAddress())) {
208             warn("incoming connection to control socket registered" //$NON-NLS-1$
209
+ " from REMOTE address " + clientSocket.getInetAddress() //$NON-NLS-1$
210
+ ", attempt to execute command was IGNORED"); //$NON-NLS-1$
211
try {
212                 clientSocket.close();
213             } catch (IOException JavaDoc e) {
214                 // ignore
215
}
216             return false;
217         }
218         debug("processing control request"); //$NON-NLS-1$
219
boolean result = false;
220         try {
221             String JavaDoc commandResult;
222             InputStream JavaDoc in = clientSocket.getInputStream();
223             OutputStream JavaDoc out = null;
224             try {
225                 StringBuffer JavaDoc command = new StringBuffer JavaDoc();
226                 byte[] buf = new byte[16];
227                 int len;
228                 while ((len = in.read(buf)) != -1) {
229                     command.append(new String JavaDoc(buf, 0, len));
230                 }
231                 clientSocket.shutdownInput();
232                 debug("got command - " + command); //$NON-NLS-1$
233
if ("STOP".equals(command.toString())) { //$NON-NLS-1$
234
stopApplication();
235                     result = true;
236                     commandResult = "OK: stop done"; //$NON-NLS-1$
237
} else if (command.toString().startsWith("PING")) { //$NON-NLS-1$
238
commandResult = "OK: " //$NON-NLS-1$
239
+ command.substring("PING".length()); //$NON-NLS-1$
240
} else {
241                     commandResult = "ERROR: unknown command"; //$NON-NLS-1$
242
}
243                 //debug("command executed");
244
//debug("sending command result - " + commandResult);
245
out = clientSocket.getOutputStream();
246                 out.write(commandResult.getBytes());
247                 out.flush();
248                 clientSocket.shutdownOutput();
249                 //debug("command result sent");
250
} finally {
251                 try {
252                     in.close();
253                 } catch (IOException JavaDoc ioe) {
254                     // ignore
255
}
256                 if (out != null) {
257                     try {
258                         out.close();
259                     } catch (IOException JavaDoc ioe) {
260                         // ignore
261
}
262                 }
263             }
264         } catch (IOException JavaDoc ioe) {
265             error("error processing control request", ioe); //$NON-NLS-1$
266
}
267         return result;
268     }
269     
270     private void stopApplication() {
271         if (!appRunning) {
272             debug("application not running"); //$NON-NLS-1$
273
return;
274         }
275         appRunning = false;
276         debug("stopping application"); //$NON-NLS-1$
277
try {
278             Boot.stopApplication(application);
279             log = null;
280         } catch (Exception JavaDoc e) {
281             error("an error has occurred while stopping" //$NON-NLS-1$
282
+ " application", e); //$NON-NLS-1$
283
}
284         debug("application stopped from control thread"); //$NON-NLS-1$
285
}
286     
287     private boolean isValidRemoteHost(final InetAddress JavaDoc addr) {
288         byte[] localAddr = serverSocket.getInetAddress().getAddress();
289         byte[] remoteAddr = addr.getAddress();
290         if (localAddr.length != remoteAddr.length) {
291             return false;
292         }
293         for (int i = 0; i < remoteAddr.length; i++) {
294             if (localAddr[i] != remoteAddr[i]) {
295                 return false;
296             }
297         }
298         return true;
299     }
300     
301     private void debug(final String JavaDoc msg) {
302         if (log != null) {
303             log.debug(msg);
304         } else {
305             System.out.println(msg);
306         }
307     }
308     
309     private void warn(final String JavaDoc msg) {
310         if (log != null) {
311             log.warn(msg);
312         } else {
313             System.out.println(msg);
314         }
315     }
316     
317     private void warn(final String JavaDoc msg, final Exception JavaDoc e) {
318         if (log != null) {
319             log.warn(msg, e);
320         } else {
321             System.out.println(msg);
322             e.printStackTrace();
323         }
324     }
325     
326     private void error(final String JavaDoc msg, final Exception JavaDoc e) {
327         if (log != null) {
328             log.error(msg, e);
329         } else {
330             System.err.println(msg);
331             e.printStackTrace();
332         }
333     }
334     
335     private void error(final Exception JavaDoc e) {
336         if (log != null) {
337             log.error(e);
338         } else {
339             e.printStackTrace();
340         }
341     }
342 }
343
Popular Tags