package org.xins.common.servlet.container;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
class CommandLineArguments {
private int port;
private File warFile;
private int loaderMode = -1;
private boolean showGUI;
CommandLineArguments(String[] args) {
port = HTTPServletStarter.DEFAULT_PORT_NUMBER;
showGUI = false;
if (args.length == 1 && args[0].equals("-help")) {
System.out.println("Usage: java [-Dorg.xins.server.config=<xins properties>] -jar <api name>.war [-port:<port number>] [-gui] [-war:<war file>] [-loader:<classloader mode>]");
System.out.println(" if port number = -1, the Servlet is not started.");
System.exit(0);
}
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (arg.startsWith("-port:") || arg.startsWith("-port=")) {
try {
port = Integer.parseInt(arg.substring(6));
} catch (NumberFormatException nfe) {
System.err.println("Warning: Incorrect port number \"" + args[1] +
"\", using " + HTTPServletStarter.DEFAULT_PORT_NUMBER + " as port number.");
}
} else if (arg.startsWith("-war:") || arg.startsWith("-war=")) {
warFile = new File(arg.substring(5));
} else if (arg.startsWith("-loader:") || arg.startsWith("-loader=")) {
try {
loaderMode = Integer.parseInt(args[2]);
} catch (NumberFormatException nfe) {
System.err.println("Warning: Incorrect ClassLoader \"" + args[2] +
"\", using " + ServletClassLoader.USE_WAR_LIB + " as default.");
}
} else if (arg.equalsIgnoreCase("-gui")) {
showGUI = true;
} else if (arg.endsWith(".war") && warFile == null) {
warFile = new File(arg);
} else if (port == HTTPServletStarter.DEFAULT_PORT_NUMBER) {
try {
port = Integer.parseInt(arg);
} catch (NumberFormatException nfe) {
}
}
}
if (warFile == null) {
URL codeLocation = HTTPServletStarter.class.getProtectionDomain().getCodeSource().getLocation();
System.out.println("No WAR file passed as argument, using: " + codeLocation);
try {
warFile = new File(new URI(codeLocation.toString()));
} catch (URISyntaxException murlex) {
murlex.printStackTrace();
}
}
if (warFile == null || !warFile.exists()) {
System.err.println("WAR file \"" + warFile + "\" not found.");
System.exit(-1);
}
if (loaderMode == -1) {
String classPath = System.getProperty("java.class.path");
if (classPath.indexOf("xins-common.jar") != -1 && classPath.indexOf("servlet.jar") != -1 &&
classPath.indexOf("xins-server.jar") != -1 && classPath.indexOf("xmlenc.jar") != -1) {
loaderMode = ServletClassLoader.USE_WAR_EXTERNAL_LIB;
} else {
loaderMode = ServletClassLoader.USE_WAR_LIB;
}
}
}
int getPort() {
return port;
}
File getWarFile() {
return warFile;
}
int getLoaderMode() {
return loaderMode;
}
boolean showGUI() {
return showGUI;
}
}