001: /*
002: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: */
007: package winstone.tools;
008:
009: import java.io.IOException;
010: import java.io.ObjectOutputStream;
011: import java.io.OutputStream;
012: import java.net.Socket;
013: import java.util.Map;
014:
015: import winstone.Launcher;
016: import winstone.Logger;
017: import winstone.WebAppConfiguration;
018: import winstone.WinstoneResourceBundle;
019:
020: /**
021: * Included so that we can control winstone from the command line a little more
022: * easily.
023: *
024: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
025: * @version $Id: WinstoneControl.java,v 1.6 2006/03/13 15:37:29 rickknowles Exp $
026: */
027: public class WinstoneControl {
028: private final static WinstoneResourceBundle TOOLS_RESOURCES = new WinstoneResourceBundle(
029: "winstone.tools.LocalStrings");
030:
031: final static String OPERATION_SHUTDOWN = "shutdown";
032: final static String OPERATION_RELOAD = "reload:";
033: static int TIMEOUT = 10000;
034:
035: /**
036: * Parses command line parameters, and calls the appropriate method for
037: * executing the winstone operation required.
038: */
039: public static void main(String argv[]) throws Exception {
040:
041: // Load args from the config file
042: Map options = Launcher.loadArgsFromCommandLineAndConfig(argv,
043: "operation");
044: String operation = (String) options.get("operation");
045: if (options.containsKey("controlPort")
046: && !options.containsKey("port")) {
047: options.put("port", options.get("controlPort"));
048: }
049:
050: if (operation.equals("")) {
051: printUsage();
052: return;
053: }
054:
055: Logger.setCurrentDebugLevel(Integer
056: .parseInt(WebAppConfiguration.stringArg(options,
057: "debug", "5")));
058:
059: String host = WebAppConfiguration.stringArg(options, "host",
060: "localhost");
061: String port = WebAppConfiguration.stringArg(options, "port",
062: "8081");
063:
064: Logger.log(Logger.INFO, TOOLS_RESOURCES,
065: "WinstoneControl.UsingHostPort", new String[] { host,
066: port });
067:
068: // Check for shutdown
069: if (operation.equalsIgnoreCase(OPERATION_SHUTDOWN)) {
070: Socket socket = new Socket(host, Integer.parseInt(port));
071: socket.setSoTimeout(TIMEOUT);
072: OutputStream out = socket.getOutputStream();
073: out.write(Launcher.SHUTDOWN_TYPE);
074: out.close();
075: Logger.log(Logger.INFO, TOOLS_RESOURCES,
076: "WinstoneControl.ShutdownOK", new String[] { host,
077: port });
078: }
079:
080: // check for reload
081: else if (operation.toLowerCase().startsWith(
082: OPERATION_RELOAD.toLowerCase())) {
083: String webappName = operation.substring(OPERATION_RELOAD
084: .length());
085: Socket socket = new Socket(host, Integer.parseInt(port));
086: socket.setSoTimeout(TIMEOUT);
087: OutputStream out = socket.getOutputStream();
088: out.write(Launcher.RELOAD_TYPE);
089: ObjectOutputStream objOut = new ObjectOutputStream(out);
090: objOut.writeUTF(host);
091: objOut.writeUTF(webappName);
092: objOut.close();
093: out.close();
094: Logger.log(Logger.INFO, TOOLS_RESOURCES,
095: "WinstoneControl.ReloadOK", new String[] { host,
096: port });
097: } else {
098: printUsage();
099: }
100: }
101:
102: /**
103: * Displays the usage message
104: */
105: private static void printUsage() throws IOException {
106: System.out.println(TOOLS_RESOURCES
107: .getString("WinstoneControl.Usage"));
108: }
109: }
|