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 @Override 058 public void activate(ComponentContext context) { 059 try { 060 configurator = (ServerConfigurator) Class.forName(CONFIGURATOR_CLASS).getConstructor().newInstance(); 061 } catch (ReflectiveOperationException e) { 062 throw new RuntimeServiceException(e); 063 } 064 // config from test features 065 int p = -1; 066 String configPort = System.getProperty(PORT_SYSTEM_PROP); 067 if (StringUtils.isNotBlank(configPort)) { 068 try { 069 p = Integer.parseInt(configPort); 070 } catch (NumberFormatException e) { 071 log.error("Invalid port for embedded servlet container: " + configPort); 072 } 073 } 074 configurator.initialize(p); 075 } 076 077 @Override 078 public void deactivate(ComponentContext context) { 079 configurator.close(); 080 } 081 082 @Override 083 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 084 if (XP_WEB_APP.equals(extensionPoint)) { 085 configurator.addWepApp((WebApplication) contribution); 086 } else if (XP_FILTER.equals(extensionPoint)) { 087 configurator.addFilter((FilterDescriptor) contribution); 088 } else if (XP_SERVLET.equals(extensionPoint)) { 089 configurator.addServlet((ServletDescriptor) contribution); 090 } else if (XP_LISTENER.equals(extensionPoint)) { 091 configurator.addLifecycleListener((ServletContextListenerDescriptor) contribution); 092 } 093 } 094 095 @Override 096 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 097 // we don't do anything special as this is a test component 098 } 099 100 @Override 101 public int getApplicationStartedOrder() { 102 return -100; 103 } 104 105 @Override 106 public void start(ComponentContext context) { 107 configurator.start(); 108 } 109 110 @Override 111 public void stop(ComponentContext context) { 112 configurator.stop(); 113 } 114 115}