001: /*
002: * $Id: CommandLineArguments.java,v 1.4 2007/09/18 08:45:08 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common.servlet.container;
008:
009: import java.io.File;
010: import java.net.URI;
011: import java.net.URISyntaxException;
012: import java.net.URL;
013:
014: /**
015: * Class used to parse and get the command line arguments when the internal
016: * Servlet container is started.
017: *
018: * @version $Revision: 1.4 $ $Date: 2007/09/18 08:45:08 $
019: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
020: */
021: class CommandLineArguments {
022:
023: /**
024: * The port number to use for the Servlet container or -1 if the Servlet
025: * container should not be started.
026: */
027: private int port;
028:
029: /**
030: * The WAR file to execute.
031: */
032: private File warFile;
033:
034: /**
035: * The ClassLoader mode with which the Servlet should be loaded.
036: */
037: private int loaderMode = -1;
038:
039: /**
040: * <code>true</code> if the graphical user interface should be started,
041: * <code>false</code> to run the Servlet in the console.
042: */
043: private boolean showGUI;
044:
045: /**
046: * Parses the command line arguments.
047: *
048: * @param args
049: * the command line arguments as passed to the <code>main()</code> method.
050: */
051: CommandLineArguments(String[] args) {
052: port = HTTPServletStarter.DEFAULT_PORT_NUMBER;
053: showGUI = false;
054: if (args.length == 1 && args[0].equals("-help")) {
055: System.out
056: .println("Usage: java [-Dorg.xins.server.config=<xins properties>] -jar <api name>.war [-port:<port number>] [-gui] [-war:<war file>] [-loader:<classloader mode>]");
057: System.out
058: .println(" if port number = -1, the Servlet is not started.");
059: System.exit(0);
060: }
061: for (int i = 0; i < args.length; i++) {
062: String arg = args[i];
063: if (arg.startsWith("-port:") || arg.startsWith("-port=")) {
064: try {
065: port = Integer.parseInt(arg.substring(6));
066: } catch (NumberFormatException nfe) {
067: System.err
068: .println("Warning: Incorrect port number \""
069: + args[1]
070: + "\", using "
071: + HTTPServletStarter.DEFAULT_PORT_NUMBER
072: + " as port number.");
073: }
074: } else if (arg.startsWith("-war:")
075: || arg.startsWith("-war=")) {
076: warFile = new File(arg.substring(5));
077: } else if (arg.startsWith("-loader:")
078: || arg.startsWith("-loader=")) {
079: try {
080: loaderMode = Integer.parseInt(args[2]);
081: } catch (NumberFormatException nfe) {
082: System.err
083: .println("Warning: Incorrect ClassLoader \""
084: + args[2]
085: + "\", using "
086: + ServletClassLoader.USE_WAR_LIB
087: + " as default.");
088: }
089: } else if (arg.equalsIgnoreCase("-gui")) {
090: showGUI = true;
091:
092: // for backward compatibility
093: } else if (arg.endsWith(".war") && warFile == null) {
094: warFile = new File(arg);
095: } else if (port == HTTPServletStarter.DEFAULT_PORT_NUMBER) {
096: try {
097: port = Integer.parseInt(arg);
098: } catch (NumberFormatException nfe) {
099: }
100: }
101: }
102:
103: // Detect the location of the WAR file if needed.
104: if (warFile == null) {
105: URL codeLocation = HTTPServletStarter.class
106: .getProtectionDomain().getCodeSource()
107: .getLocation();
108: System.out
109: .println("No WAR file passed as argument, using: "
110: + codeLocation);
111: try {
112: warFile = new File(new URI(codeLocation.toString()));
113: } catch (URISyntaxException murlex) {
114: murlex.printStackTrace();
115: }
116: }
117:
118: if (warFile == null || !warFile.exists()) {
119: System.err.println("WAR file \"" + warFile
120: + "\" not found.");
121: System.exit(-1);
122: }
123:
124: // Detect the ClassLoader mode
125: if (loaderMode == -1) {
126: String classPath = System.getProperty("java.class.path");
127: if (classPath.indexOf("xins-common.jar") != -1
128: && classPath.indexOf("servlet.jar") != -1
129: && classPath.indexOf("xins-server.jar") != -1
130: && classPath.indexOf("xmlenc.jar") != -1) {
131: loaderMode = ServletClassLoader.USE_WAR_EXTERNAL_LIB;
132: } else {
133: loaderMode = ServletClassLoader.USE_WAR_LIB;
134: }
135: }
136: }
137:
138: /**
139: * Gets the port number specified. If no default port number is specified
140: * return the default port number.
141: *
142: * @return
143: * the port number.
144: */
145: int getPort() {
146: return port;
147: }
148:
149: /**
150: * Gets the location of the WAR file to execute.
151: *
152: * @return
153: * the WAR file or <code>null</code> if not found.
154: */
155: File getWarFile() {
156: return warFile;
157: }
158:
159: /**
160: * Gets the class loader mode.
161: *
162: * @return
163: * the class loader mode to use to load the WAR classes.
164: */
165: int getLoaderMode() {
166: return loaderMode;
167: }
168:
169: /**
170: * Indicates whether to run it in console mode or with the Swing user interface.
171: *
172: * @return
173: * <code>true</code> for the graphical user interface mode,
174: * <code>false</code> for the console mode.
175: */
176: boolean showGUI() {
177: return showGUI;
178: }
179: }
|