    001    /*****************************************************************************
    002     * Copyright (C) NanoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     *****************************************************************************/
    009
    010    package org.nanocontainer.webcontainer;
    011
    012    import org.mortbay.jetty.Connector;
    013    import org.mortbay.jetty.RequestLog;
    014    import org.mortbay.jetty.Server;
    015    import org.mortbay.jetty.handler.HandlerList;
    016    import org.mortbay.jetty.handler.RequestLogHandler;
    017    import org.mortbay.jetty.nio.BlockingChannelConnector;
    018    import org.mortbay.jetty.servlet.Context;
    019    import org.picocontainer.PicoContainer;
    020    import org.picocontainer.Startable;
    021    import org.picocontainer.alternatives.EmptyPicoContainer;
    022
    023    public class PicoJettyServer extends EmptyPicoContainer implements PicoContainer, Startable {
    024
    025        private final Server server;
    026        private final PicoContainer parentContainer;
    027
    028        public PicoJettyServer(PicoContainer parentContainer) {
    029            this.parentContainer = parentContainer;
    030            server = new Server();
    031            server.setHandler(new HandlerList());
    032        }
    033
    034        public PicoJettyServer(String host, int port, PicoContainer parentContainer) {
    035            this(parentContainer);
    036            createBlockingChannelConnector(host, port);
    037        }
    038
    039        public Connector createBlockingChannelConnector(String host, int port) {
    040            BlockingChannelConnector connector = new BlockingChannelConnector();
    041            connector.setHost(host);
    042            connector.setPort(port);
    043            server.addConnector(connector);
    044            return connector;
    045        }
    046
    047        public PicoContext createContext(String contextPath, boolean withSessionHandler) {
    048            Context context = new Context(server, contextPath, Context.SESSIONS);
    049            return new PicoContext(context, parentContainer, withSessionHandler);
    050        }
    051
    052        public PicoWebAppContext addWebApplication(String contextPath, String warFile) {
    053            PicoWebAppContext context = new PicoWebAppContext(parentContainer);
    054            context.setContextPath(contextPath);
    055            context.setExtractWAR(true);
    056            context.setWar(warFile);
    057            context.setParentLoaderPriority(true);
    058            server.addHandler(context);
    059            return context;
    060        }
    061
    062        public void start() {
    063            try {
    064                server.start();
    065            } catch (Exception e) {
    066                throw new JettyServerLifecycleException("Jetty couldn't start", e);
    067            }
    068        }
    069
    070        public void stop() {
    071            try {
    072                server.stop();
    073            } catch (Exception e) {
    074                throw new JettyServerLifecycleException("Jetty couldn't stop", e);
    075            }
    076        }
    077
    078        public void addRequestLog(RequestLog requestLog) {
    079            RequestLogHandler handler = new RequestLogHandler();
    080            handler.setRequestLog(requestLog);
    081            server.addHandler(handler);
    082        }
    083
    084    }