001/*
002 * (C) Copyright 2006-2011 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.ecm.webengine.jaxrs.servlet;
020
021import java.io.IOException;
022
023import javax.servlet.ServletConfig;
024import javax.servlet.ServletException;
025import javax.servlet.http.HttpServlet;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029import org.nuxeo.ecm.webengine.jaxrs.BundleNotFoundException;
030import org.nuxeo.ecm.webengine.jaxrs.servlet.config.ListenerSetDescriptor;
031import org.nuxeo.ecm.webengine.jaxrs.servlet.config.ServletDescriptor;
032import org.nuxeo.ecm.webengine.jaxrs.servlet.config.ServletRegistry;
033
034/**
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class ServletHolder extends HttpServlet {
038
039    private static final long serialVersionUID = 1L;
040
041    protected RequestChain chain;
042
043    protected ServletDescriptor descriptor;
044
045    protected volatile boolean initDone = false;
046
047    protected String getName(ServletConfig config) {
048        String name = config.getInitParameter(ServletRegistry.SERVLET_NAME);
049        if (name == null) {
050            name = config.getServletName();
051        }
052        return name;
053    }
054
055    protected ServletDescriptor getDescriptor(ServletConfig config) throws ServletException {
056        String name = getName(config);
057        if (name == null) {
058            throw new ServletException("No name defined for the ServletHolder. Check your servlet contributions.");
059        }
060        ServletDescriptor desc = ServletRegistry.getInstance().getServletDescriptor(name);
061        if (desc == null) {
062            throw new ServletException("No such servlet descriptor: " + name);
063        }
064        return desc;
065    }
066
067    @Override
068    public void init(ServletConfig config) throws ServletException {
069        try {
070            descriptor = getDescriptor(config);
071            chain = new RequestChain(descriptor.getServlet(), descriptor.getFilters());
072            ListenerSetDescriptor listeners = descriptor.getListenerSet();
073            if (listeners != null) {
074                // initialize listeners if not already initialized
075                listeners.init(config);
076            }
077            super.init(config);
078            // lazy chain.init(descriptor, config);
079        } catch (ServletException e) {
080            throw e;
081        } catch (ReflectiveOperationException | BundleNotFoundException e) {
082            throw new ServletException("Initialization exception for servlet " + config.getServletName(), e);
083        }
084    }
085
086    @Override
087    public void destroy() {
088        super.destroy();
089        initDone = false;
090        if (chain != null) {
091            chain.destroy();
092            chain = null;
093        }
094    }
095
096    @Override
097    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
098            IOException {
099        Thread t = Thread.currentThread();
100        ClassLoader cl = t.getContextClassLoader();
101        try {
102            if (!initDone) {
103                lazyInit();
104            }
105            // use servlet class loader as the context class loader
106            t.setContextClassLoader(chain.servlet.getClass().getClassLoader());
107            chain.execute(request, response);
108        } finally {
109            t.setContextClassLoader(cl);
110        }
111    }
112
113    protected synchronized void lazyInit() throws ServletException {
114        try {
115            chain.init(descriptor, getServletConfig());
116        } finally {
117            initDone = true;
118        }
119    }
120
121}