001/*
002 * Copyright 2013 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.tomcat;
017
018import java.util.Collections;
019
020import javax.servlet.ServletContainerInitializer;
021import javax.xml.bind.annotation.XmlElement;
022import javax.xml.bind.annotation.XmlIDREF;
023
024import org.apache.catalina.Context;
025import org.apache.catalina.Wrapper;
026import org.apache.catalina.servlets.DefaultServlet;
027import org.apache.catalina.startup.Tomcat;
028import org.atteo.evo.config.Configurable;
029import org.atteo.moonshine.services.ImportService;
030import org.atteo.moonshine.services.Service;
031import org.atteo.moonshine.webserver.ServletContainer;
032
033import com.google.inject.Module;
034
035/**
036 * Context configuration.
037 */
038public class ContextConfig extends Configurable implements Service {
039    @ImportService
040    @XmlIDREF
041    @XmlElement
042    private ServletContainer servletContainer;
043
044    /**
045     * Context path.
046     */
047    @XmlElement
048    private String path = "/";
049
050    /**
051     * Context base directory.
052     */
053    @XmlElement
054    private String baseDir = ".";
055
056    public String getPath() {
057        return path;
058    }
059
060    public String getBaseDir() {
061        return baseDir;
062    }
063
064    public void configure(Context context) {
065        for (ServletContainerInitializer servletContainerInitializer : servletContainer.getInitializers()) {
066            context.addServletContainerInitializer(servletContainerInitializer, Collections.<Class<?>>emptySet());
067        }
068        Wrapper wrapper = new Tomcat.ExistingStandardWrapper(new DefaultServlet());
069        wrapper.setName("default");
070        context.addChild(wrapper);
071        context.addServletMapping("/", "default");
072    }
073
074    @Override
075    public Module configure() {
076        return null;
077    }
078
079    @Override
080    public void start() {
081    }
082
083    @Override
084    public void stop() {
085    }
086
087    @Override
088    public void close() {
089    }
090
091    @Override
092    public Iterable<? extends Service> getSubServices() {
093        return Collections.emptyList();
094    }
095}