001/*
002 * (C) Copyright 2006-2018 Nuxeo (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 *     Bogdan Stefanescu
018 *     Florent Guillaume
019 */
020package org.nuxeo.runtime.server;
021
022import org.apache.commons.lang3.StringUtils;
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.common.server.WebApplication;
026import org.nuxeo.runtime.RuntimeServiceException;
027import org.nuxeo.runtime.model.ComponentContext;
028import org.nuxeo.runtime.model.ComponentInstance;
029import org.nuxeo.runtime.model.DefaultComponent;
030
031/**
032 * This component registers and configures an embedded servlet container.
033 * <p>
034 * The actual implementation is delegated to a specific embedded server by implementing the interface
035 * {@link ServerConfigurator}.
036 *
037 * @since 10.2
038 */
039public class ServerComponent extends DefaultComponent {
040
041    private static final Log log = LogFactory.getLog(ServerComponent.class);
042
043    public static final String XP_WEB_APP = "webapp";
044
045    public static final String XP_SERVLET = "servlet";
046
047    public static final String XP_FILTER = "filter";
048
049    public static final String XP_LISTENER = "listener";
050
051    public static final String PORT_SYSTEM_PROP = "nuxeo.servlet-container.port";
052
053    protected static final String CONFIGURATOR_CLASS = "org.nuxeo.runtime.server.tomcat.TomcatServerConfigurator";
054
055    protected ServerConfigurator configurator;
056
057    protected int port;
058
059    @Override
060    public void activate(ComponentContext context) {
061        try {
062            configurator = (ServerConfigurator) Class.forName(CONFIGURATOR_CLASS).getConstructor().newInstance();
063        } catch (ReflectiveOperationException e) {
064            throw new RuntimeServiceException(e);
065        }
066        // config from test features
067        int p = -1;
068        String configPort = System.getProperty(PORT_SYSTEM_PROP);
069        if (StringUtils.isNotBlank(configPort)) {
070            try {
071                p = Integer.parseInt(configPort);
072            } catch (NumberFormatException e) {
073                log.error("Invalid port for embedded servlet container: " + configPort);
074            }
075        }
076        port = configurator.initialize(p);
077    }
078
079    @Override
080    public void deactivate(ComponentContext context) {
081        configurator.close();
082    }
083
084    @Override
085    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
086        if (XP_WEB_APP.equals(extensionPoint)) {
087            configurator.addWepApp((WebApplication) contribution);
088        } else if (XP_FILTER.equals(extensionPoint)) {
089            configurator.addFilter((FilterDescriptor) contribution);
090        } else if (XP_SERVLET.equals(extensionPoint)) {
091            configurator.addServlet((ServletDescriptor) contribution);
092        } else if (XP_LISTENER.equals(extensionPoint)) {
093            configurator.addLifecycleListener((ServletContextListenerDescriptor) contribution);
094        }
095    }
096
097    @Override
098    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
099        // we don't do anything special as this is a test component
100    }
101
102    @Override
103    public int getApplicationStartedOrder() {
104        return -100;
105    }
106
107    @Override
108    public void start(ComponentContext context) {
109        configurator.start();
110    }
111
112    @Override
113    public void stop(ComponentContext context) {
114        configurator.stop();
115    }
116
117    /**
118     * Gets the port which is used by the server.
119     *
120     * @return the port
121     */
122    public int getPort() {
123        return port;
124    }
125
126}