01: // ========================================================================
02: // Copyright (c) 2002 Mort Bay Consulting (Australia) Pty. Ltd.
03: // $Id: Main.java 6177 2007-02-19 10:11:27Z aaime $
04: // ========================================================================
05: package org.mortbay.stop;
06:
07: import java.io.OutputStream;
08: import java.net.InetAddress;
09: import java.net.Socket;
10:
11: /*-------------------------------------------*/
12: /** Main stop class.
13: * This class is intended to be the main class listed in the MANIFEST.MF of
14: * the stop.jar archive. It allows an application started with the
15: * command "java -jar start.jar" to be stopped.
16: *
17: * Programs started with start.jar may be stopped with the stop.jar, which connects
18: * via a local port to stop the server. The default port can be set with the
19: * STOP.PORT system property (a port of < 0 disables the stop mechanism). If the STOP.KEY
20: * system property is set, then a random key is generated and written to stdout. This key
21: * must be passed to the stop.jar.
22: *
23: * @author Greg Wilkins
24: * @version $Revision: 1.1 $
25: */
26: public class Main {
27: private boolean _debug = System.getProperty("DEBUG", null) != null;
28: private String _config = System.getProperty("START",
29: "org/mortbay/start/start.config");
30: private int _port = Integer.getInteger("STOP.PORT", 8079)
31: .intValue();
32: private String _key = System.getProperty("STOP.KEY", "mortbay");
33:
34: public static void main(String[] args) {
35: new Main().stop();
36: }
37:
38: void stop() {
39: try {
40: if (_port <= 0) {
41: System.err
42: .println("START.PORT system property must be specified");
43: }
44:
45: if (_key == null) {
46: _key = "";
47: System.err.println("Using empty key");
48: }
49:
50: Socket s = new Socket(InetAddress.getByName("127.0.0.1"),
51: _port);
52: OutputStream out = s.getOutputStream();
53: out.write((_key + "\r\nstop\r\n").getBytes());
54: out.flush();
55: s.shutdownOutput();
56: s.close();
57: } catch (Exception e) {
58: e.printStackTrace();
59: }
60: }
61: }
|