001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.runtime.jetty;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import javax.servlet.ServletContextListener;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.mortbay.jetty.Handler;
029import org.mortbay.jetty.Server;
030import org.mortbay.jetty.handler.HandlerCollection;
031import org.mortbay.jetty.servlet.Context;
032import org.mortbay.jetty.servlet.FilterHolder;
033import org.mortbay.jetty.servlet.ServletHolder;
034import org.mortbay.jetty.webapp.WebAppContext;
035
036/**
037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
038 */
039public class ContextManager {
040
041    protected Log log = LogFactory.getLog(ContextManager.class);
042
043    protected Server server;
044
045    protected Map<String, Context> contexts;
046
047    protected Map<String, ServletContextListenerDescriptor> listeners;
048
049    public ContextManager(Server server) {
050        this.server = server;
051        this.contexts = new HashMap<String, Context>();
052        this.listeners = new HashMap<String, ServletContextListenerDescriptor>();
053    }
054
055    public Server getServer() {
056        return server;
057    }
058
059    public synchronized void addFilter(FilterDescriptor descriptor) {
060        String path = descriptor.getContext();
061        Context ctx = contexts.get(path);
062        if (ctx == null) {
063            ctx = new Context(server, path, Context.SESSIONS | Context.NO_SECURITY);
064            contexts.put(path, ctx);
065        }
066        FilterHolder holder = new FilterHolder(descriptor.getClazz());
067        String name = descriptor.getName();
068        if (name != null) {
069            holder.setName(name);
070        }
071        String desc = descriptor.getDescription();
072        if (desc != null) {
073            holder.setDisplayName(desc);
074        }
075        Map<String, String> params = descriptor.getInitParams();
076        if (params != null) {
077            holder.setInitParameters(params);
078        }
079        ctx.addFilter(holder, descriptor.getPath(), org.mortbay.jetty.Handler.DEFAULT);
080    }
081
082    public synchronized void addServlet(ServletDescriptor descriptor) {
083        String path = descriptor.getContext();
084        Context ctx = contexts.get(path);
085        if (ctx == null) {
086            ctx = new Context(server, path, Context.SESSIONS | Context.NO_SECURITY);
087            contexts.put(path, ctx);
088        }
089        ServletHolder holder = new ServletHolder(descriptor.getClazz());
090        String name = descriptor.getName();
091        if (name != null) {
092            holder.setName(name);
093        }
094        String desc = descriptor.getDescription();
095        if (desc != null) {
096            holder.setDisplayName(desc);
097        }
098        Map<String, String> params = descriptor.getInitParams();
099        if (params != null) {
100            holder.setInitParameters(params);
101        }
102        ctx.addServlet(holder, descriptor.getPath());
103    }
104
105    public synchronized void addLifecycleListener(ServletContextListenerDescriptor descriptor) {
106        listeners.put(descriptor.name, descriptor);
107    }
108
109    public synchronized void removeLifecycleListener(ServletContextListenerDescriptor descriptor) {
110        listeners.remove(descriptor.name);
111    }
112
113    public synchronized void removeFilter(FilterDescriptor descriptor) {
114        // TODO not yet implemented
115    }
116
117    public synchronized void removeServlet(ServletDescriptor descriptor) {
118        // TODO not yet implemented
119    }
120
121    public void applyLifecycleListeners() {
122        HandlerCollection hc = (HandlerCollection) server.getHandler();
123        Handler[] handlers = hc.getChildHandlersByClass(WebAppContext.class);
124        for (ServletContextListenerDescriptor desc : listeners.values()) {
125            ServletContextListener listener;
126            try {
127                listener = desc.clazz.newInstance();
128            } catch (ReflectiveOperationException e) {
129                log.error("Cannot add life cycle listener " + desc.name, e);
130                continue;
131            }
132            for (Handler handler : handlers) {
133                WebAppContext context = (WebAppContext) handler;
134                if (context.getContextPath().matches(desc.context)) {
135                    context.addEventListener(listener);
136                }
137            }
138        }
139    }
140}