001/*
002 * Copyright 2012 Atteo.
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 */
016package org.atteo.moonshine.jetty.handlers;
017
018import java.util.Collections;
019
020import javax.servlet.ServletContainerInitializer;
021import javax.servlet.ServletException;
022import javax.xml.bind.annotation.XmlElement;
023import javax.xml.bind.annotation.XmlIDREF;
024import javax.xml.bind.annotation.XmlRootElement;
025
026import org.atteo.moonshine.services.ImportService;
027import org.atteo.moonshine.services.Service;
028import org.atteo.moonshine.webserver.ServletContainer;
029import org.eclipse.jetty.server.Handler;
030import org.eclipse.jetty.servlet.DefaultServlet;
031import org.eclipse.jetty.servlet.ServletContextHandler;
032import org.eclipse.jetty.util.component.AbstractLifeCycle;
033import org.eclipse.jetty.util.component.LifeCycle;
034
035import com.google.inject.Module;
036import com.google.inject.servlet.ServletModule;
037
038
039/**
040 * Jetty servlet handler.
041 * <p>
042 * Handles servlets and filters registered using {@link ServletContainer}
043 * or using {@link ServletModule}.
044 * </p>
045 */
046@XmlRootElement(name = "servlets")
047public class ServletContextHandlerConfig extends HandlerConfig implements Service {
048    @ImportService
049    @XmlElement
050    @XmlIDREF
051    private ServletContainer servletContainer;
052
053    @Override
054    public Handler getHandler() {
055        final ServletContextHandler handler = new ServletContextHandler();
056        handler.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() {
057            @Override
058            public void lifeCycleStarting(LifeCycle event) {
059                handler.getServletContext().setExtendedListenerTypes(true);
060                Iterable<ServletContainerInitializer> initializers = servletContainer.getInitializers();
061                for (ServletContainerInitializer servletContainerInitializer : initializers) {
062                    try {
063                        servletContainerInitializer.onStartup(Collections.<Class<?>>emptySet(),
064                                handler.getServletContext());
065                    } catch (ServletException ex) {
066                        throw new RuntimeException(ex);
067                    }
068                }
069                handler.addServlet(DefaultServlet.class, "/");
070                handler.getServletContext().setExtendedListenerTypes(false);
071            }
072        });
073        return handler;
074    }
075
076    @Override
077    public Module configure() {
078        return null;
079    }
080
081    @Override
082    public void start() {
083    }
084
085    @Override
086    public void stop() {
087    }
088
089    @Override
090    public void close() {
091    }
092
093    @Override
094    public Iterable<? extends Service> getSubServices() {
095        return Collections.emptyList();
096    }
097}