diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-04-05 18:28:06 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-04-05 18:28:06 +0000 |
commit | 331477b79355c30312e87f6c5a88bc5a7c7b8c61 (patch) | |
tree | f511fc062ba43e99e57158c32c9393f5dcca0977 /sca-java-2.x/contrib/modules/host-tomcat/src/main/java | |
parent | 27cb23a013720ea9b39ff221db00e11067fbc051 (diff) |
Bring up host-tomcat for 2.x based on the Embedded class from Tomcat
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@930925 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/contrib/modules/host-tomcat/src/main/java')
2 files changed, 169 insertions, 267 deletions
diff --git a/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java b/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java index 5ea1701c05..13a3deb90d 100644 --- a/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java +++ b/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java @@ -31,12 +31,8 @@ import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; -import java.util.Map.Entry; -import java.util.concurrent.Executor; import java.util.logging.Logger; import javax.servlet.RequestDispatcher; @@ -45,19 +41,23 @@ import javax.servlet.ServletException; import org.apache.catalina.Container; import org.apache.catalina.Context; +import org.apache.catalina.Engine; +import org.apache.catalina.Host; import org.apache.catalina.Lifecycle; import org.apache.catalina.LifecycleException; import org.apache.catalina.Loader; import org.apache.catalina.connector.Connector; -import org.apache.catalina.core.StandardContext; import org.apache.catalina.core.StandardEngine; -import org.apache.catalina.core.StandardHost; import org.apache.catalina.startup.ContextConfig; +import org.apache.catalina.startup.Embedded; import org.apache.coyote.http11.Http11Protocol; import org.apache.tomcat.util.buf.MessageBytes; import org.apache.tomcat.util.http.mapper.MappingData; -import org.apache.tomcat.util.net.JIoEndpoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.LifeCycleListener; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.host.http.DefaultResourceServlet; +import org.apache.tuscany.sca.host.http.SecurityContext; import org.apache.tuscany.sca.host.http.ServletHost; import org.apache.tuscany.sca.host.http.ServletMappingException; import org.apache.tuscany.sca.work.WorkScheduler; @@ -67,10 +67,10 @@ import org.apache.tuscany.sca.work.WorkScheduler; * * @version $Rev$ $Date$ */ -@SuppressWarnings("deprecation") -public class TomcatServer implements ServletHost { +public class TomcatServer implements ServletHost, LifeCycleListener { private static final Logger logger = Logger.getLogger(TomcatServer.class.getName()); + private Embedded embedded; private int defaultPortNumber = 8080; private final class TuscanyLoader implements Loader { @@ -145,21 +145,21 @@ public class TomcatServer implements ServletHost { * Represents a port and the server that serves it. */ private class Port { - private StandardEngine engine; - private StandardHost host; + private Engine engine; + private Host host; private Connector connector; - private Port(StandardEngine engine, StandardHost host, Connector connector) { + private Port(Engine engine, Host host, Connector connector) { this.engine = engine; this.host = host; this.connector = connector; } - public StandardEngine getEngine() { + public Engine getEngine() { return engine; } - public StandardHost getHost() { + public Host getHost() { return host; } @@ -177,9 +177,13 @@ public class TomcatServer implements ServletHost { /** * Constructs a new embedded Tomcat server. * - * @param workScheduler the WorkScheduler to use to process requests. */ - public TomcatServer(WorkScheduler workScheduler) { + public TomcatServer(ExtensionPointRegistry registry) { + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.workScheduler = utilities.getUtility(WorkScheduler.class); + } + + protected TomcatServer(WorkScheduler workScheduler) { this.workScheduler = workScheduler; } @@ -194,23 +198,24 @@ public class TomcatServer implements ServletHost { /** * Stop all the started servers. */ - public void stop() throws ServletMappingException { - if (!ports.isEmpty()) { + public void stop() { + if (embedded != null) { try { - Set<Entry<Integer, Port>> entries = new HashSet<Entry<Integer, Port>>(ports.entrySet()); - for (Entry<Integer, Port> entry : entries) { - Port port = entry.getValue(); - port.getConnector().stop(); - port.getEngine().stop(); - ports.remove(entry.getKey()); + // embedded.stop(); + embedded.destroy(); + for (Port port : ports.values()) { + port.connector.stop(); } - } catch (Exception e) { - throw new ServletMappingException(e); + } catch (LifecycleException e) { + throw new IllegalStateException(e); } } } - public void addServletMapping(String suri, Servlet servlet) { + public synchronized String addServletMapping(String suri, Servlet servlet) { + if (embedded == null) { + start(); + } URI uri = URI.create(suri); // Get the URI scheme and port @@ -224,108 +229,7 @@ public class TomcatServer implements ServletHost { Port port = ports.get(portNumber); if (port == null) { - // Create an engine - // Allow privileged access to read properties. Requires PropertiesPermission read in - // security policy. - final StandardEngine engine = AccessController.doPrivileged(new PrivilegedAction<StandardEngine>() { - public StandardEngine run() { - return new StandardEngine(); - } - }); - - engine.setBaseDir(""); - engine.setDefaultHost("localhost"); - engine.setName("engine/" + portNumber); - - // Create a host - final StandardHost host = new StandardHost(); - host.setAppBase(""); - host.setName("localhost"); - // Allow privileged access to read properties. Requires PropertiesPermission read in - // security policy. - AccessController.doPrivileged(new PrivilegedAction<Object>() { - public Object run() { - engine.addChild(host); - return null; - } - }); - - // Create the root context - StandardContext context = new StandardContext(); - final ClassLoader tccl = Thread.currentThread().getContextClassLoader(); - context.setLoader(new TuscanyLoader(tccl)); - // context.setParentClassLoader(tccl.getParent()); - context.setDocBase(""); - context.setPath(""); - ContextConfig config = new ContextConfig(); - ((Lifecycle)context).addLifecycleListener(config); - host.addChild(context); - - // Install an HTTP connector - // Allow privileged access to read properties. Requires PropertiesPermission read in - // security policy. - try { - AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { - public Object run() throws LifecycleException { - engine.start(); - return null; - } - }); - } catch (PrivilegedActionException e) { - // throw (LifecycleException)e.getException(); - throw new ServletMappingException(e); - } - Connector connector; - // Allow privileged access to read properties. Requires PropertiesPermission read in - // security policy. - try { - final String protocol = scheme; - connector = AccessController.doPrivileged(new PrivilegedExceptionAction<CustomConnector>() { - public CustomConnector run() throws Exception { - CustomConnector customConnector = new CustomConnector(); - customConnector.setPort(portNumber); - customConnector.setContainer(engine); - - if ("https".equalsIgnoreCase(protocol)) { - configureSSL(customConnector); - ((Http11Protocol) customConnector.getProtocolHandler()).setSSLEnabled(true); - } - customConnector.initialize(); - customConnector.start(); - return customConnector; - } - - private void configureSSL(CustomConnector customConnector) { - String trustStore = System.getProperty("javax.net.ssl.trustStore"); - String trustStorePass = System.getProperty("javax.net.ssl.trustStorePassword"); - String keyStore = System.getProperty("javax.net.ssl.keyStore"); - String keyStorePass = System.getProperty("javax.net.ssl.keyStorePassword"); - - customConnector.setProperty("protocol", "TLS"); - - customConnector.setProperty("keystore", keyStore); - customConnector.setProperty("keypass", keyStorePass); - String keyStoreType = - System.getProperty("javax.net.ssl.keyStoreType", KeyStore.getDefaultType()); - String trustStoreType = - System.getProperty("javax.net.ssl.trustStoreType", KeyStore.getDefaultType()); - customConnector.setProperty("keytype", keyStoreType); - customConnector.setProperty("trusttype", trustStoreType); - customConnector.setProperty("truststore", trustStore); - customConnector.setProperty("trustpass", trustStorePass); - - customConnector.setProperty("clientauth", "false"); - customConnector.setProtocol("HTTP/1.1"); - customConnector.setScheme(protocol); - customConnector.setProperty("backlog", "10"); - customConnector.setSecure(true); - } - }); - } catch (Exception e) { - throw new ServletMappingException(e); - } - // Keep track of the running server - port = new Port(engine, host, connector); + port = createInstance(scheme, portNumber); ports.put(portNumber, port); } @@ -388,6 +292,120 @@ public class TomcatServer implements ServletHost { throw new ServletMappingException(e); } logger.info("Added Servlet mapping: " + addedURL); + return addedURL.toString(); + } + + private Port createInstance(String scheme, final int portNumber) { + Port port; + // Create an engine + // Allow privileged access to read properties. Requires PropertiesPermission read in + // security policy. + final Engine engine = AccessController.doPrivileged(new PrivilegedAction<Engine>() { + public Engine run() { + return embedded.createEngine(); + } + }); + + ((StandardEngine)engine).setBaseDir(""); + engine.setDefaultHost("localhost"); + engine.setName("engine/" + portNumber); + + // Create a host + // Allow privileged access to read properties. Requires PropertiesPermission read in + // security policy. + final Host host = AccessController.doPrivileged(new PrivilegedAction<Host>() { + public Host run() { + Host host = embedded.createHost("localhost", ""); + engine.addChild(host); + return host; + } + }); + + // Create the root context + Context context = embedded.createContext("", ""); + final ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + context.setLoader(new TuscanyLoader(tccl)); + // context.setParentClassLoader(tccl.getParent()); + ContextConfig config = new ContextConfig(); + ((Lifecycle)context).addLifecycleListener(config); + host.addChild(context); + + embedded.addEngine(engine); + + // Install an HTTP connector + + Connector connector; + // Allow privileged access to read properties. Requires PropertiesPermission read in + // security policy. + try { + final String protocol = scheme; + connector = AccessController.doPrivileged(new PrivilegedExceptionAction<Connector>() { + public Connector run() throws Exception { + Connector customConnector = new Connector(); + customConnector.setPort(portNumber); + + if ("https".equalsIgnoreCase(protocol)) { + configureSSL(customConnector); + ((Http11Protocol)customConnector.getProtocolHandler()).setSSLEnabled(true); + } + return customConnector; + } + + private void configureSSL(Connector customConnector) { + String trustStore = System.getProperty("javax.net.ssl.trustStore"); + String trustStorePass = System.getProperty("javax.net.ssl.trustStorePassword"); + String keyStore = System.getProperty("javax.net.ssl.keyStore"); + String keyStorePass = System.getProperty("javax.net.ssl.keyStorePassword"); + + customConnector.setProperty("protocol", "TLS"); + + customConnector.setProperty("keystore", keyStore); + customConnector.setProperty("keypass", keyStorePass); + String keyStoreType = System.getProperty("javax.net.ssl.keyStoreType", KeyStore.getDefaultType()); + String trustStoreType = + System.getProperty("javax.net.ssl.trustStoreType", KeyStore.getDefaultType()); + customConnector.setProperty("keytype", keyStoreType); + customConnector.setProperty("trusttype", trustStoreType); + customConnector.setProperty("truststore", trustStore); + customConnector.setProperty("trustpass", trustStorePass); + + customConnector.setProperty("clientauth", "false"); + customConnector.setProtocol("HTTP/1.1"); + customConnector.setScheme(protocol); + customConnector.setProperty("backlog", "10"); + customConnector.setSecure(true); + } + }); + } catch (Exception e) { + throw new ServletMappingException(e); + } + + embedded.addConnector(connector); + try { + connector.start(); + } catch (LifecycleException e) { + throw new ServletMappingException(e); + } + + // Keep track of the running server + port = new Port(engine, host, connector); + return port; + } + + private void startEmbedded() { + // Allow privileged access to read properties. Requires PropertiesPermission read in + // security policy. + try { + AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { + public Object run() throws LifecycleException { + embedded.start(); + return null; + } + }); + } catch (PrivilegedActionException e) { + // throw (LifecycleException)e.getException(); + throw new ServletMappingException(e); + } } public URL getURLMapping(String suri) throws ServletMappingException { @@ -484,7 +502,7 @@ public class TomcatServer implements ServletHost { } } - public Servlet removeServletMapping(String suri) { + public synchronized Servlet removeServletMapping(String suri) { URI uri = URI.create(suri); // Get the URI port @@ -552,7 +570,9 @@ public class TomcatServer implements ServletHost { if (contextNames == null || contextNames.length == 0) { try { port.getConnector().stop(); - port.getEngine().stop(); + ((StandardEngine)port.getEngine()).stop(); + embedded.removeEngine(port.getEngine()); + embedded.removeConnector(port.getConnector()); ports.remove(portNumber); } catch (LifecycleException e) { throw new IllegalStateException(e); @@ -575,79 +595,30 @@ public class TomcatServer implements ServletHost { return contextPath; } - /** - * A custom connector that uses our WorkScheduler to schedule - * worker threads. - */ - private class CustomConnector extends Connector { - - private class CustomHttpProtocolHandler extends Http11Protocol { - - /** - * An Executor wrapping our WorkScheduler - */ - private class WorkSchedulerExecutor implements Executor { - public void execute(Runnable command) { - workScheduler.scheduleWork(command); - } - } - - /** - * A custom Endpoint that waits for its acceptor thread to - * terminate before stopping. - */ - private class CustomEndpoint extends JIoEndpoint { - private Thread acceptorThread; - - private class CustomAcceptor extends Acceptor { - CustomAcceptor() { - super(); - } - } - - @Override - public void start() throws Exception { - if (!initialized) - init(); - if (!running) { - running = true; - paused = false; - acceptorThread = new Thread(new CustomAcceptor(), getName() + "-Acceptor-" + 0); - acceptorThread.setPriority(threadPriority); - acceptorThread.setDaemon(daemon); - acceptorThread.start(); - } - } + public void setContextPath(String path) { + this.contextPath = path; + } - @Override - public void stop() { - super.stop(); - try { - acceptorThread.join(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } + public void start() { + embedded = new Embedded(); + embedded.setAwait(true); + startEmbedded(); + } - @Override - public int getCurrentThreadsBusy() { - return 0; - } - } + public String addServletMapping(String uri, Servlet servlet, SecurityContext securityContext) + throws ServletMappingException { + return addServletMapping(uri, servlet); + } - CustomHttpProtocolHandler() { - endpoint = new CustomEndpoint(); - endpoint.setExecutor(new WorkSchedulerExecutor()); - } - } + public String getName() { + return "tomcat"; + } - CustomConnector() throws Exception { - protocolHandler = new CustomHttpProtocolHandler(); - } + public URL getURLMapping(String arg0, SecurityContext arg1) { + return null; } - public void setContextPath(String path) { - this.contextPath = path; + public void setAttribute(String arg0, Object arg1) { } } diff --git a/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java b/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java deleted file mode 100644 index 070dffdce6..0000000000 --- a/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.http.tomcat.module; - -import java.security.AccessController; -import java.security.PrivilegedAction; - -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.ModuleActivator; -import org.apache.tuscany.sca.core.UtilityExtensionPoint; -import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint; -import org.apache.tuscany.sca.http.tomcat.TomcatServer; -import org.apache.tuscany.sca.work.WorkScheduler; - -/** - * @version $Rev$ $Date$ - */ -public class TomcatRuntimeModuleActivator implements ModuleActivator { - - private TomcatServer server; - - public void start(ExtensionPointRegistry extensionPointRegistry) { - - // Register a Tomcat Servlet host - ServletHostExtensionPoint servletHosts = - extensionPointRegistry.getExtensionPoint(ServletHostExtensionPoint.class); - - if (servletHosts.getServletHosts().size() < 1) { - UtilityExtensionPoint utilities = extensionPointRegistry.getExtensionPoint(UtilityExtensionPoint.class); - final WorkScheduler workScheduler = utilities.getUtility(WorkScheduler.class); - // Allow privileged access to start MBeans. Requires MBeanPermission in security policy. - server = AccessController.doPrivileged(new PrivilegedAction<TomcatServer>() { - public TomcatServer run() { - return new TomcatServer(workScheduler); - } - }); - servletHosts.addServletHost(server); - } - } - - public void stop(ExtensionPointRegistry registry) { - // Allow privileged access to stop MBeans. Requires MBeanPermission in security policy. - AccessController.doPrivileged(new PrivilegedAction<Object>() { - public Object run() { - if (server != null) { - server.stop(); - } - return null; - } - }); - } -} |