001: /*
002: * $Id: HTTPServletStarter.java,v 1.26 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.lang.reflect.Constructor;
011: import javax.swing.JFrame;
012:
013: /**
014: * HTTP Server used to invoke the XINS Servlet.
015: *
016: * @version $Revision: 1.26 $ $Date: 2007/09/18 08:45:08 $
017: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
018: */
019: public class HTTPServletStarter {
020:
021: /**
022: * The default port number.
023: */
024: public static final int DEFAULT_PORT_NUMBER = 8080;
025:
026: /**
027: * Creates a new <code>HTTPServletStarter</code> for the specified WAR
028: * file, on the default port, as a daemon thread.
029: *
030: * <p>A listener is started on the port immediately.
031: *
032: * @param warFile
033: * the WAR file of the application to deploy, cannot be
034: * <code>null</code>.
035: *
036: * @throws ServletException
037: * if the servlet cannot be initialized.
038: *
039: * @throws IOException
040: * if the servlet container cannot be started.
041: */
042: // Exception is thrown as ServletException is not in the classpath
043: public HTTPServletStarter(File warFile) throws Exception {
044: this (warFile, DEFAULT_PORT_NUMBER, true);
045: }
046:
047: /**
048: * Creates a new <code>HTTPServletStarter</code> for the specified WAR
049: * file, on the specified port, as a daemon thread.
050: *
051: * <p>A listener is started on the port immediately.
052: *
053: * @param warFile
054: * the WAR file of the application to deploy, cannot be
055: * <code>null</code>.
056: *
057: * @param port
058: * the port to run the web server on.
059: *
060: * @throws ServletException
061: * if the servlet cannot be initialized.
062: *
063: * @throws IOException
064: * if the servlet container cannot be started.
065: */
066: public HTTPServletStarter(File warFile, int port) throws Exception {
067: this (warFile, port, true);
068: }
069:
070: /**
071: * Creates a new <code>HTTPServletStarter</code> for the specified WAR
072: * file, on the specified port, optionally as a daemon thread.
073: *
074: * <p>A listener is started on the port immediately.
075: *
076: * @param warFile
077: * The war file of the application to deploy, cannot be <code>null</code>.
078: *
079: * @param port
080: * The port of the web server, cannot be <code>null</code>.
081: *
082: * @param deamon
083: * <code>true</code> if the thread listening to connection should be a
084: * deamon thread, <code>false</code> otherwise.
085: *
086: * @throws ServletException
087: * if the servlet cannot be initialized.
088: *
089: * @throws IOException
090: * if the servlet container cannot be started.
091: */
092: public HTTPServletStarter(File warFile, int port, boolean deamon)
093: throws Exception {
094: this (warFile, port, deamon,
095: ServletClassLoader.USE_WAR_EXTERNAL_LIB);
096: }
097:
098: /**
099: * Creates a new <code>HTTPServletStarter</code> for the specified servlet
100: * class, on the specified port, optionally as a daemon thread.
101: *
102: * <p>A listener is started on the port immediately.
103: *
104: * @param servletClassName
105: * The name of the servlet to load, cannot be <code>null</code>.
106: *
107: * @param port
108: * The port of the web server, cannot be <code>null</code>.
109: *
110: * @param deamon
111: * <code>true</code> if the thread listening to connection should be a
112: * deamon thread, <code>false</code> otherwise.
113: *
114: * @param loaderMode
115: * the way the ClassLoader should locate and load the classes.
116: * See {@link ServletClassLoader].
117: *
118: * @throws ServletException
119: * if the servlet cannot be initialized.
120: *
121: * @throws IOException
122: * if the servlet container cannot be started.
123: *
124: * @since XINS 2.1.
125: */
126: public HTTPServletStarter(File warFile, int port, boolean deamon,
127: int loaderMode) throws Exception {
128:
129: // Create the servlet
130: ClassLoader loader = ServletClassLoader.getServletClassLoader(
131: warFile, loaderMode);
132:
133: Class[] constClasses = { File.class, Integer.TYPE, Boolean.TYPE };
134: Object[] constArgs = { warFile, new Integer(port),
135: deamon ? Boolean.TRUE : Boolean.FALSE };
136: try {
137: Class delegate = loader
138: .loadClass("org.xins.common.servlet.container.HTTPServletHandler");
139: Constructor constructor = delegate
140: .getConstructor(constClasses);
141: constructor.newInstance(constArgs);
142: } catch (Exception ex) {
143: ex.printStackTrace();
144: }
145: }
146:
147: /**
148: * Creates a new <code>HTTPServletStarter</code> for the specified servlet
149: * class, on the specified port, optionally as a daemon thread.
150: *
151: * <p>A listener is started on the port immediately.
152: *
153: * @param servletClassName
154: * The name of the servlet to load, cannot be <code>null</code>.
155: *
156: * @param port
157: * The port of the web server, cannot be <code>null</code>.
158: *
159: * @param deamon
160: * <code>true</code> if the thread listening to connection should be a
161: * deamon thread, <code>false</code> otherwise.
162: *
163: * @throws ServletException
164: * if the servlet cannot be initialized.
165: *
166: * @throws IOException
167: * if the servlet container cannot be started.
168: */
169: public HTTPServletStarter(String servletClassName, int port,
170: boolean deamon) throws Exception {
171:
172: // Create the servlet
173: Class[] constClasses = { String.class, Integer.TYPE,
174: Boolean.TYPE };
175: Object[] constArgs = { servletClassName, new Integer(port),
176: Boolean.valueOf(deamon) };
177: try {
178: Class delegate = getClass()
179: .getClassLoader()
180: .loadClass(
181: "org.xins.common.servlet.container.HTTPServletHandler");
182: Constructor constructor = delegate
183: .getConstructor(constClasses);
184: constructor.newInstance(constArgs);
185: } catch (Exception ex) {
186: ex.printStackTrace();
187: }
188: }
189:
190: /**
191: * Starts the Servlet container for the specific API.
192: *
193: * @param args
194: * The command line arguments, the first argument should be the location
195: * of the WAR file or the name of the class of the servlet to load,
196: * the optional second argument is the port number.
197: * If no port number is specified, 8080 is used as default.
198: */
199: public static void main(String[] args) {
200:
201: CommandLineArguments cmdArgs = new CommandLineArguments(args);
202: if (cmdArgs.getPort() == -1) {
203: try {
204: ClassLoader loader = ServletClassLoader
205: .getServletClassLoader(cmdArgs.getWarFile(),
206: cmdArgs.getLoaderMode());
207: loader.loadClass("org.xins.common.spec.SpecGUI")
208: .newInstance();
209: } catch (Exception ex) {
210: ex.printStackTrace();
211: }
212: } else {
213: if (cmdArgs.showGUI()) {
214: JFrame apiFrame = new JFrame();
215: new ConsoleGUI(apiFrame, cmdArgs);
216: apiFrame.setVisible(true);
217: }
218: try {
219: // Starts the server and wait for connections
220: new HTTPServletStarter(cmdArgs.getWarFile(), cmdArgs
221: .getPort(), false, cmdArgs.getLoaderMode());
222: } catch (Exception ioe) {
223: ioe.printStackTrace();
224: }
225: }
226: }
227: }
|