    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 java.util.EventListener;
    013    import java.util.HashMap;
    014    import java.util.Map;
    015
    016    import javax.servlet.Filter;
    017    import javax.servlet.Servlet;
    018
    019    import org.mortbay.jetty.handler.ErrorHandler;
    020    import org.mortbay.jetty.servlet.Context;
    021    import org.mortbay.jetty.servlet.DefaultServlet;
    022    import org.mortbay.jetty.servlet.ErrorPageErrorHandler;
    023    import org.mortbay.jetty.servlet.FilterHolder;
    024    import org.mortbay.jetty.servlet.ServletHolder;
    025    import org.picocontainer.PicoContainer;
    026    import org.picocontainer.defaults.DefaultPicoContainer;
    027
    028    public class PicoContext {
    029
    030        private final Context context;
    031        private final PicoContainer parentContainer;
    032        private final boolean withSessionHandler;
    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 PicoContext(Context context, PicoContainer parentContainer, boolean withSessionHandler) {
    042            this.context = context;
    043            this.parentContainer = parentContainer;
    044            this.withSessionHandler = withSessionHandler;
    045        }
    046
    047        public PicoServletHolder addServletWithMapping(Class servletClass, String pathMapping) {
    048            PicoServletHolder holder = new PicoServletHolder(servletClass, parentContainer);
    049            context.addServlet(holder, pathMapping);
    050            return holder;
    051        }
    052
    053        public Servlet addServletWithMapping(Servlet servlet, String pathMapping) {
    054            ServletHolder holder = new ServletHolder(servlet);
    055            context.addServlet(holder, pathMapping);
    056            return servlet;
    057        }
    058
    059        public PicoFilterHolder addFilterWithMapping(Class filterClass, String pathMapping, int dispatchers) {
    060            PicoFilterHolder filterHolder = new PicoFilterHolder(filterClass, parentContainer);
    061            context.addFilter(filterHolder, pathMapping, 0);
    062            return filterHolder;
    063        }
    064
    065        public Filter addFilterWithMapping(Filter filter, String pathMapping, int dispatchers) {
    066            context.addFilter(new FilterHolder(filter), pathMapping, dispatchers);
    067            return filter;
    068        }
    069
    070        public void addInitParam(String param, String value) {
    071            Map params = new HashMap(context.getInitParams());
    072            params.put(param, value);
    073            context.setInitParams(params);
    074        }
    075
    076
    077        public EventListener addListener(Class listenerClass) {
    078            DefaultPicoContainer child = new DefaultPicoContainer(parentContainer);
    079            child.registerComponentImplementation(EventListener.class, listenerClass);
    080            EventListener instance = (EventListener) child.getComponentInstance(EventListener.class);
    081            return addListener(instance);
    082        }
    083
    084        public EventListener addListener(EventListener listener) {
    085            context.addEventListener(listener);
    086            return listener;
    087        }
    088
    089
    090        public void setStaticContext(String absolutePath) {
    091            context.addServlet(DefaultServlet.class.getName(), "/");
    092            context.setResourceBase(absolutePath);
    093        }
    094
    095        public void setStaticContext(String absolutePath, String welcomePage) {
    096            context.addServlet(DefaultServlet.class.getName(), "/");
    097            context.setResourceBase(absolutePath);
    098            context.setWelcomeFiles(new String[]{welcomePage});
    099        }
    100
    101        public void setDefaultHandling(final String absolutePath, String scratchDir, String pageSuffix) {
    102            context.setResourceBase(absolutePath);
    103            ServletHolder jspHolder = new PicoServletHolder(parentContainer);
    104            jspHolder.setName("jsp");
    105            jspHolder.setClassName("org.apache.jasper.servlet.JspServlet");
    106            jspHolder.setInitParameter("scratchdir", scratchDir);
    107            jspHolder.setInitParameter("logVerbosityLevel", "DEBUG");
    108            jspHolder.setInitParameter("fork", "false");
    109            jspHolder.setInitParameter("xpoweredBy", "false");
    110            jspHolder.setForcedPath(null);
    111            jspHolder.setInitOrder(0);
    112
    113            context.addServlet(jspHolder, "*.jsp");
    114            context.addServlet(DefaultServlet.class.getName(), "/");
    115
    116        }
    117
    118        public void addErrorHandler() {
    119            addErrorHandler(new ErrorPageErrorHandler());
    120        }
    121
    122        public void addErrorHandler(ErrorHandler handler) {
    123            context.setErrorHandler(handler);
    124        }
    125
    126    }