KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > tools > WinstoneControl


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone.tools;
8
9 import java.io.IOException JavaDoc;
10 import java.io.ObjectOutputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.net.Socket JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import winstone.Launcher;
16 import winstone.Logger;
17 import winstone.WebAppConfiguration;
18 import winstone.WinstoneResourceBundle;
19
20 /**
21  * Included so that we can control winstone from the command line a little more
22  * easily.
23  *
24  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
25  * @version $Id: WinstoneControl.java,v 1.6 2006/03/13 15:37:29 rickknowles Exp $
26  */

27 public class WinstoneControl {
28     private final static WinstoneResourceBundle TOOLS_RESOURCES = new WinstoneResourceBundle("winstone.tools.LocalStrings");
29     
30     final static String JavaDoc OPERATION_SHUTDOWN = "shutdown";
31     final static String JavaDoc OPERATION_RELOAD = "reload:";
32     static int TIMEOUT = 10000;
33
34     /**
35      * Parses command line parameters, and calls the appropriate method for
36      * executing the winstone operation required.
37      */

38     public static void main(String JavaDoc argv[]) throws Exception JavaDoc {
39
40         // Load args from the config file
41
Map JavaDoc options = Launcher.loadArgsFromCommandLineAndConfig(argv, "operation");
42         String JavaDoc operation = (String JavaDoc) options.get("operation");
43         if (options.containsKey("controlPort") && !options.containsKey("port")) {
44             options.put("port", options.get("controlPort"));
45         }
46
47         if (operation.equals("")) {
48             printUsage();
49             return;
50         }
51
52         Logger.setCurrentDebugLevel(Integer.parseInt(WebAppConfiguration
53                 .stringArg(options, "debug", "5")));
54
55         String JavaDoc host = WebAppConfiguration.stringArg(options, "host", "localhost");
56         String JavaDoc port = WebAppConfiguration.stringArg(options, "port", "8081");
57
58         Logger.log(Logger.INFO, TOOLS_RESOURCES, "WinstoneControl.UsingHostPort",
59                 new String JavaDoc[] { host, port });
60
61         // Check for shutdown
62
if (operation.equalsIgnoreCase(OPERATION_SHUTDOWN)) {
63             Socket JavaDoc socket = new Socket JavaDoc(host, Integer.parseInt(port));
64             socket.setSoTimeout(TIMEOUT);
65             OutputStream JavaDoc out = socket.getOutputStream();
66             out.write(Launcher.SHUTDOWN_TYPE);
67             out.close();
68             Logger.log(Logger.INFO, TOOLS_RESOURCES, "WinstoneControl.ShutdownOK",
69                     new String JavaDoc[] { host, port });
70         }
71
72         // check for reload
73
else if (operation.toLowerCase().startsWith(OPERATION_RELOAD.toLowerCase())) {
74             String JavaDoc webappName = operation.substring(OPERATION_RELOAD.length());
75             Socket JavaDoc socket = new Socket JavaDoc(host, Integer.parseInt(port));
76             socket.setSoTimeout(TIMEOUT);
77             OutputStream JavaDoc out = socket.getOutputStream();
78             out.write(Launcher.RELOAD_TYPE);
79             ObjectOutputStream JavaDoc objOut = new ObjectOutputStream JavaDoc(out);
80             objOut.writeUTF(host);
81             objOut.writeUTF(webappName);
82             objOut.close();
83             out.close();
84             Logger.log(Logger.INFO, TOOLS_RESOURCES, "WinstoneControl.ReloadOK",
85                     new String JavaDoc[] { host, port });
86         }
87         else {
88             printUsage();
89         }
90     }
91
92     /**
93      * Displays the usage message
94      */

95     private static void printUsage() throws IOException JavaDoc {
96         System.out.println(TOOLS_RESOURCES.getString("WinstoneControl.Usage"));
97     }
98 }
99
Popular Tags