    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.Server;
    013    import org.mortbay.jetty.Handler;
    014    import org.mortbay.jetty.handler.ContextHandler;
    015    import org.mortbay.jetty.handler.ResourceHandler;
    016    import org.mortbay.jetty.handler.ErrorHandler;
    017    import org.mortbay.jetty.servlet.*;
    018    import org.picocontainer.PicoContainer;
    019    import org.picocontainer.defaults.DefaultPicoContainer;
    020
    021    import javax.servlet.Filter;
    022    import javax.servlet.Servlet;
    023    import java.util.EventListener;
    024    import java.io.IOException;
    025
    026    public class PicoContextHandler {
    027
    028        private final ContextHandler context;
    029        private final Server server;
    030        private final PicoContainer parentContainer;
    031        private final boolean withSessionHandler;
    032        private PicoServletHandler servletHandler;
    033
    034        public static final int DEFAULT = 0;
    035        public static final int REQUEST = 1;
    036        public static final int FORWARD = 2;
    037        public static final int INCLUDE = 4;
    038        public static final int ERROR = 8;
    039        public static final int ALL = 15;
    040
    041        public PicoContextHandler(ContextHandler context, Server server, PicoContainer parentContainer, boolean sessionManager) {
    042            this.context = context;
    043            this.server = server;
    044            this.parentContainer = parentContainer;
    045            this.withSessionHandler = sessionManager;
    046        }
    047
    048        public PicoServletHolder addServletWithMapping(Class servletClass, String pathMapping) {
    049            PicoServletHandler handler = getServletHandler();
    050            return (PicoServletHolder) handler.addServletWithMapping(servletClass, pathMapping);
    051        }
    052
    053        public Servlet addServletWithMapping(Servlet servlet, String pathMapping) {
    054            PicoServletHandler handler = getServletHandler();
    055            handler.addServletWithMapping(new ServletHolder(servlet), pathMapping);
    056            return servlet;
    057        }
    058
    059
    060        private synchronized PicoServletHandler getServletHandler() {
    061            if (servletHandler == null) {
    062                servletHandler = new PicoServletHandler(parentContainer);
    063                servletHandler.setServer(server);
    064                if (withSessionHandler) {
    065                    SessionHandler sessionHandler = new SessionHandler();
    066                    sessionHandler.setServer(server);
    067                    context.addHandler(sessionHandler);
    068                    sessionHandler.addHandler(servletHandler);
    069                } else {
    070                    context.addHandler(servletHandler);
    071                }
    072            }
    073            return servletHandler;
    074        }
    075
    076        public PicoFilterHolder addFilterWithMapping(Class filterClass, String pathMapping, int dispatchers) {
    077            PicoServletHandler handler = getServletHandler();
    078            return (PicoFilterHolder) handler.addFilterWithMapping(filterClass, pathMapping, dispatchers);
    079        }
    080
    081        public Filter addFilterWithMapping(Filter filter, String pathMapping, int dispatchers) {
    082            PicoServletHandler servletHandler = getServletHandler();
    083            servletHandler.addFilterWithMapping(new FilterHolder(filter), pathMapping, dispatchers);
    084            return filter;
    085        }
    086
    087
    088        public EventListener addListener(Class listenerClass) {
    089
    090            DefaultPicoContainer child = new DefaultPicoContainer(parentContainer);
    091            child.registerComponentImplementation(EventListener.class, listenerClass);
    092            EventListener instance = (EventListener) child.getComponentInstance(EventListener.class);
    093
    094            return addListener(instance);
    095
    096        }
    097
    098        public EventListener addListener(EventListener listener) {
    099            context.addEventListener(listener);
    100            return listener;
    101        }
    102
    103
    104        public void setStaticContext(String absolutePath) {
    105            ResourceHandler resourceHandler = new ResourceHandler();
    106            resourceHandler.setResourceBase(absolutePath);
    107            context.addHandler(resourceHandler);
    108        }
    109
    110        public void setStaticContext(String absolutePath, String welcomePage) {
    111            ResourceHandler resourceHandler = new ResourceHandler();
    112            resourceHandler.setResourceBase(absolutePath);
    113            resourceHandler.setWelcomeFiles(new String[]{welcomePage});
    114            context.addHandler(resourceHandler);
    115        }
    116
    117        public void setDefaultHandling(final String absolutePath, String scratchDir, String pageSuffix) {
    118            context.setResourceBase(absolutePath);
    119            PicoServletHandler handler = getServletHandler();
    120            ServletHolder jspHolder = new PicoServletHolder(parentContainer);
    121            jspHolder.setName("jsp");
    122            jspHolder.setClassName("org.apache.jasper.servlet.JspServlet");
    123            jspHolder.setInitParameter("scratchdir", scratchDir);
    124            jspHolder.setInitParameter("logVerbosityLevel", "DEBUG");
    125            jspHolder.setInitParameter("fork", "false");
    126            jspHolder.setInitParameter("xpoweredBy", "false");
    127            jspHolder.setForcedPath(null);
    128            jspHolder.setInitOrder(0);
    129
    130
    131            handler.addServletWithMapping(jspHolder, pageSuffix);
    132            handler.addServletWithMapping(DefaultServlet.class.getName(), "/");
    133
    134        }
    135
    136        public void addErrorHandler() {
    137            addErrorHandler(new ErrorPageErrorHandler());
    138        }
    139
    140        public void addErrorHandler(ErrorHandler handler) {
    141            context.setErrorHandler(handler);
    142        }
    143
    144        //     protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)
    145    //        throws IOException
    146      //  {
    147        //    writeErrorPage(request, writer, code, message, _showStacks);
    148       // }
    149
    150    }