    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.Server;
    014    import org.mortbay.jetty.RequestLog;
    015    import org.mortbay.jetty.servlet.ErrorPageErrorHandler;
    016    import org.mortbay.jetty.handler.ContextHandler;
    017    import org.mortbay.jetty.handler.HandlerList;
    018    import org.mortbay.jetty.handler.RequestLogHandler;
    019    import org.mortbay.jetty.handler.ErrorHandler;
    020    import org.mortbay.jetty.nio.BlockingChannelConnector;
    021    import org.picocontainer.PicoContainer;
    022    import org.picocontainer.alternatives.EmptyPicoContainer;
    023
    024    public class PicoJettyServer extends EmptyPicoContainer implements PicoContainer {
    025
    026        private final Server server;
    027        private final PicoContainer parentContainer;
    028        private ErrorHandler errorHandler;
    029
    030        public PicoJettyServer(PicoContainer parentContainer) {
    031            this.parentContainer = parentContainer;
    032            server = new Server();
    033            server.setHandler(new HandlerList());
    034        }
    035
    036        public PicoJettyServer(String host, int port, PicoContainer parentContainer) {
    037            this(parentContainer);
    038            createBlockingChannelConnector(host, port);
    039        }
    040
    041        public Connector createBlockingChannelConnector(String host, int port) {
    042            BlockingChannelConnector connector = new BlockingChannelConnector();
    043            connector.setHost(host);
    044            connector.setPort(port);
    045            server.addConnector(connector);
    046            return connector;
    047        }
    048
    049        public PicoContextHandler createContext(String contextPath, boolean withSessionHandler) {
    050            ContextHandler context = new ContextHandler();
    051            context.setContextPath(contextPath);
    052            server.addHandler(context);
    053            return new PicoContextHandler(context, server, parentContainer, withSessionHandler);
    054        }
    055
    056
    057        public PicoWebAppContext addWebApplication(String contextPath, String warFile) {
    058            PicoWebAppContext wah = new PicoWebAppContext(parentContainer);
    059            wah.setContextPath(contextPath);
    060            wah.setExtractWAR(true);
    061            wah.setWar(warFile);
    062            wah.setParentLoaderPriority(true);
    063            server.addHandler(wah);
    064            return wah;
    065        }
    066
    067
    068        public void start() {
    069            try {
    070                server.start();
    071            } catch (Exception e) {
    072                e.printStackTrace();
    073                throw new JettyServerLifecycleException("Jetty couldn't start", e);
    074            }
    075        }
    076
    077        public void stop() {
    078            try {
    079                server.stop();
    080            } catch (Exception e) {
    081                throw new JettyServerLifecycleException("Jetty couldn't stop", e);
    082            }
    083        }
    084
    085        public void addRequestLog(RequestLog requestLog) {
    086            RequestLogHandler requestLogHandler = new RequestLogHandler();
    087            requestLogHandler.setRequestLog(requestLog);
    088            server.addHandler(requestLogHandler);
    089
    090        }
    091
    092    }