ServerUtil.java
/***************************************************************************
Copyright 2009 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.web.servlets;
import java.io.File;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import net.metanotion.util.ConstantDispatcher;
import net.metanotion.util.Dispatcher;
import net.metanotion.web.RequestObject;
import net.metanotion.web.SessionFactory;
import net.metanotion.web.SessionHandler;
import net.metanotion.web.SessionInitializer;
import net.metanotion.web.concrete.SimpleSessionHandler;
/** A collection of convenience methods for starting up a Jetty based application server with the web framework. */
public final class ServerUtil {
/** Create an instance of a Jetty HTTP server.
@param port The port to listen to for HTTP requests.
@param mapper The Dispatcher implementation to use for this application.
@param application The SessionHandler implementation that represents the web application.
@return a Jetty Server object, however, Jetty starts in a separate thread, and this method ,join's to that
thread, and so it will not return until Jetty's listening thread terminates.
*/
public static Server makeJettyServer(final int port,
final Dispatcher<? extends Object, RequestObject> mapper,
final SessionHandler application) {
return makeJettyServer(port, new RequestHandler(mapper, application));
}
/** Create an instance of a Jetty HTTP server.
@param port The port to listen to for HTTP requests.
@param handler The RequestHandler implementation to embed in a Jetty Servlet context.
@return The Jetty Server object. When this method returns, the server is ready to be started but it is NOT yet
listening for requests or running in it's own thread. To do that, you must call <code>server.start()</code>.
*/
public static Server makeJettyServer(final int port, final RequestHandler handler) {
final Server server = new Server(port);
final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new EmbeddedServlet(handler)), "/*");
return server;
}
/** Start up Jetty listening for HTTP requests.
@param port The port to listen to for HTTP requests.
@param mapper The Dispatcher implementation to use for this application.
@param application The SessionFactory implementation that represents the web application.
@param tempFolder The temporary folder to use for file uploads.
@param maxMemFile The maximum size of file uploads to cache in memory.
@param maxDiskFile The maximum size of file uploads to cache on disk.
@throws Exception if the server fails to start or the thread it is listening on is interrupted.
*/
public static void launchJettyServer(final int port,
final Dispatcher<? extends Object, RequestObject> mapper,
final SessionFactory<RequestObject> application,
final File tempFolder,
final int maxMemFile,
final long maxDiskFile) throws Exception {
final SessionHandler handler = new SimpleSessionHandler(application, new ServletSessionStore());
ServerUtil.launchJettyServer(port, mapper, handler, tempFolder, maxMemFile, maxDiskFile);
}
/** Start up Jetty listening for HTTP requests.
@param port The port to listen to for HTTP requests.
@param mapper The Dispatcher implementation to use for this application.
@param application The SessionHandler implementation that represents the web application.
@param tempFolder The temporary folder to use for file uploads.
@param maxMemFile The maximum size of file uploads to cache in memory.
@param maxDiskFile The maximum size of file uploads to cache on disk.
@throws Exception if the server fails to start or the thread it is listening on is interrupted.
*/
public static void launchJettyServer(final int port,
final Dispatcher<? extends Object, RequestObject> mapper,
final SessionHandler application,
final File tempFolder,
final int maxMemFile,
final long maxDiskFile) throws Exception {
final Server server = makeJettyServer(port, new RequestHandler(mapper, application, tempFolder, maxMemFile, maxDiskFile));
server.start();
server.join();
}
/** Start up Jetty listening for HTTP requests without a dispatcher to decode requests(i.e. the SessionHandler
instance will receive a message of "null".
@param port The port to listen to for HTTP requests.
@param application The SessionHandler implementation that represents the web application.
@throws Exception if the server fails to start or the thread it is listening on is interrupted.
*/
public static void launchJettyServer(final int port, final SessionHandler application) throws Exception {
ServerUtil.launchJettyServer(port, new ConstantDispatcher<Object,RequestObject>(), application);
}
/** Create an HTTP server listening on the port provided, using the dispatcher provided with a standard servlet
session store that uses the SessionFactory provided. This method will block until the HTTP server thread
terminates(i.e. it will run forever by default).
@param port The TCP port to listen on.
@param mapper The dispatcher to process a request.
@param application The session factory which generates new sessions when the
{@link net.metanotion.web.servlets.ServletSessionStore}/{@link net.metanotion.web.concrete.SimpleSessionHandler}
decide one is needed.
@throws Exception if the server fails to start or the thread it is listening on is interrupted.
*/
public static void launchJettyServer(final int port,
final Dispatcher<? extends Object, RequestObject> mapper,
final SessionFactory<RequestObject> application) throws Exception {
final SessionHandler handler = new SimpleSessionHandler(application, new ServletSessionStore());
ServerUtil.launchJettyServer(port, mapper, handler);
}
/** Start up Jetty listening for HTTP requests.
@param port The port to listen to for HTTP requests.
@param mapper The dispatcher to process a request.
@param application The SessionInitializer implementation that represents the web application.
@throws Exception if the server fails to start or the thread it is listening on is interrupted.
*/
public static void launchJettyServer(final int port,
final Dispatcher<? extends Object, RequestObject> mapper,
final SessionInitializer application) throws Exception {
ServerUtil.launchJettyServer(port, mapper, new SimpleSessionHandler(application));
}
/** Create an HTTP server listening on the provided port, using the dispatcher provided with a SessionHandler to
process the messages generated by the dispatcher. This method will block until the HTTP server thread
terminates (i.e. it will run forever by default).
@param port The TCP port to listen on.
@param mapper The dispatcher to process a request.
@param application The SessionHandler implementation to process messages and return HTTP Responses.
@throws Exception if the server fails to start or the thread it is listening on is interrupted.
*/
public static void launchJettyServer(final int port,
final Dispatcher<? extends Object, RequestObject> mapper,
final SessionHandler application) throws Exception {
final Server server = makeJettyServer(port, new RequestHandler(mapper, application));
server.start();
server.join();
}
}