From 381a103a148d1aacb19f0afb14f66d9d6e298878 Mon Sep 17 00:00:00 2001 From: rfeng Date: Tue, 6 Oct 2009 16:55:15 +0000 Subject: Enable lazy loading of ServletHost extensions git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@822343 13f79535-47bb-0310-9956-ffa450edef68 --- java/sca/modules/host-http/META-INF/MANIFEST.MF | 7 +- .../http/DefaultServletHostExtensionPoint.java | 151 ++++++++++++++++++++- .../http/jetty/JettyRuntimeModuleActivator.java | 81 ----------- .../apache/tuscany/sca/http/jetty/JettyServer.java | 133 +++++++++--------- .../org.apache.tuscany.sca.core.ModuleActivator | 18 --- .../org.apache.tuscany.sca.host.http.ServletHost | 18 +++ .../sca/http/jetty/JettyServerTestCase.java | 9 ++ .../tuscany/sca/host/webapp/ServletHostHelper.java | 9 +- .../sca/host/webapp/WebAppModuleActivator.java | 57 -------- .../org.apache.tuscany.sca.core.ModuleActivator | 18 --- 10 files changed, 257 insertions(+), 244 deletions(-) delete mode 100644 java/sca/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyRuntimeModuleActivator.java delete mode 100644 java/sca/modules/host-jetty/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator create mode 100644 java/sca/modules/host-jetty/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost delete mode 100644 java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java delete mode 100644 java/sca/modules/host-webapp/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator (limited to 'java/sca/modules') diff --git a/java/sca/modules/host-http/META-INF/MANIFEST.MF b/java/sca/modules/host-http/META-INF/MANIFEST.MF index 238b322f35..b4ad3f121e 100644 --- a/java/sca/modules/host-http/META-INF/MANIFEST.MF +++ b/java/sca/modules/host-http/META-INF/MANIFEST.MF @@ -11,8 +11,11 @@ Bundle-ManifestVersion: 2 Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt Bundle-Description: Apache Tuscany SCA HTTP Servlet Host Extension Poi nt -Import-Package: javax.servlet,javax.servlet.http,org.apache.tuscany.sc - a.host.http;version="2.0.0" +Import-Package: javax.servlet, + javax.servlet.http, + org.apache.tuscany.sca.core;version="2.0.0", + org.apache.tuscany.sca.extensibility;version="2.0.0", + org.apache.tuscany.sca.host.http;version="2.0.0" Bundle-SymbolicName: org.apache.tuscany.sca.host.http Bundle-DocURL: http://www.apache.org/ Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6 diff --git a/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/DefaultServletHostExtensionPoint.java b/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/DefaultServletHostExtensionPoint.java index 5e6de0b29b..7573141656 100644 --- a/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/DefaultServletHostExtensionPoint.java +++ b/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/DefaultServletHostExtensionPoint.java @@ -19,27 +19,176 @@ package org.apache.tuscany.sca.host.http; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.net.URL; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import javax.servlet.RequestDispatcher; +import javax.servlet.Servlet; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.LifeCycleListener; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; + /** * Default implementation of a Servlet host extension point. * * @version $Rev$ $Date$ */ -public class DefaultServletHostExtensionPoint implements ServletHostExtensionPoint { +public class DefaultServletHostExtensionPoint implements ServletHostExtensionPoint, LifeCycleListener { private List servletHosts = new ArrayList(); + private boolean loaded; + + private ExtensionPointRegistry registry; + + public DefaultServletHostExtensionPoint(ExtensionPointRegistry registry) { + this.registry = registry; + } public void addServletHost(ServletHost servletHost) { servletHosts.add(servletHost); + if (servletHost instanceof LifeCycleListener) { + ((LifeCycleListener)servletHost).start(); + } } public void removeServletHost(ServletHost servletHost) { servletHosts.remove(servletHost); + if (servletHost instanceof LifeCycleListener) { + ((LifeCycleListener)servletHost).stop(); + } } public List getServletHosts() { + loadServletHosts(); return servletHosts; } + + private synchronized void loadServletHosts() { + if (loaded) + return; + + // Get the databinding service declarations + Collection sds; + try { + sds = ServiceDiscovery.getInstance().getServiceDeclarations(ServletHost.class, true); + } catch (IOException e) { + throw new IllegalStateException(e); + } + + // Load data bindings + for (ServiceDeclaration sd : sds) { + // Create a data binding wrapper and register it + ServletHost servletHost = new LazyServletHost(sd); + addServletHost(servletHost); + } + + loaded = true; + } + + /** + * A data binding facade allowing data bindings to be lazily loaded and + * initialized. + */ + private class LazyServletHost implements ServletHost, LifeCycleListener { + private ServiceDeclaration sd; + private ServletHost host; + + /** + * @param sd + */ + public LazyServletHost(ServiceDeclaration sd) { + super(); + this.sd = sd; + } + + private synchronized ServletHost getServletHost() { + if (host == null) { + try { + Class cls = sd.loadClass(); + Constructor ctor = null; + try { + ctor = cls.getConstructor(ExtensionPointRegistry.class); + host = (ServletHost)ctor.newInstance(registry); + } catch (NoSuchMethodException e) { + ctor = cls.getConstructor(); + host = (ServletHost)ctor.newInstance(); + } + if(host instanceof LifeCycleListener) { + ((LifeCycleListener) host).start(); + } + } catch (Throwable e) { + throw new IllegalStateException(e); + } + } + return host; + } + + public void addServletMapping(String uri, Servlet servlet) throws ServletMappingException { + getServletHost().addServletMapping(uri, servlet); + } + + public String getContextPath() { + return getServletHost().getContextPath(); + } + + public int getDefaultPort() { + return getServletHost().getDefaultPort(); + } + + public RequestDispatcher getRequestDispatcher(String uri) throws ServletMappingException { + return getServletHost().getRequestDispatcher(uri); + } + + public Servlet getServletMapping(String uri) throws ServletMappingException { + return getServletHost().getServletMapping(uri); + } + + public URL getURLMapping(String uri) { + return getServletHost().getURLMapping(uri); + } + + public Servlet removeServletMapping(String uri) throws ServletMappingException { + return getServletHost().removeServletMapping(uri); + } + + public void setAttribute(String name, Object value) { + getServletHost().setAttribute(name, value); + } + + public void setContextPath(String path) { + getServletHost().setContextPath(path); + } + + public void setDefaultPort(int port) { + getServletHost().setDefaultPort(port); + } + + public void start() { + } + + public void stop() { + if (host instanceof LifeCycleListener) { + ((LifeCycleListener)host).stop(); + } + } + } + + public void start() { + } + + public void stop() { + for (ServletHost host : servletHosts) { + if (host instanceof LifeCycleListener) { + ((LifeCycleListener)host).stop(); + } + } + servletHosts.clear(); + registry = null; + } } diff --git a/java/sca/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyRuntimeModuleActivator.java b/java/sca/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyRuntimeModuleActivator.java deleted file mode 100644 index dde99a1fac..0000000000 --- a/java/sca/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyRuntimeModuleActivator.java +++ /dev/null @@ -1,81 +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.jetty; - -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -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.ServletHost; -import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint; -import org.apache.tuscany.sca.work.WorkScheduler; - -/** - * @version $Rev$ $Date$ - */ -public class JettyRuntimeModuleActivator implements ModuleActivator { - private static final Logger logger = Logger.getLogger(JettyRuntimeModuleActivator.class.getName()); - - private JettyServer server; - - public void start(ExtensionPointRegistry extensionPointRegistry) { - - // Register a Jetty Servlet host - ServletHostExtensionPoint servletHosts = - extensionPointRegistry.getExtensionPoint(ServletHostExtensionPoint.class); - - List hosts = servletHosts.getServletHosts(); - if (hosts != null) { - // Clear out any other hosts (eg webapp or tomcat) and add this jetty host is default - hosts.clear(); - UtilityExtensionPoint utilities = extensionPointRegistry.getExtensionPoint(UtilityExtensionPoint.class); - final WorkScheduler workScheduler = utilities.getUtility(WorkScheduler.class); - // Allow privileged access to start MBeans. Requires MBeanPermission in security policy. - try { - server = AccessController.doPrivileged(new PrivilegedAction() { - public JettyServer run() { - return new JettyServer(workScheduler); - } - }); - servletHosts.addServletHost(server); - } catch (Exception e) { - logger.log(Level.WARNING, "Exception creating JettyServer", e); - } - } - } - - public void stop(ExtensionPointRegistry registry) { - // Allow privileged access to stop MBeans. Requires MBeanPermission in security policy. - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - if (server != null) { - server.stop(); - } - return null; - } - }); - } - -} diff --git a/java/sca/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java b/java/sca/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java index fdd33217c3..70a3455a6e 100644 --- a/java/sca/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java +++ b/java/sca/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java @@ -40,6 +40,9 @@ import javax.servlet.RequestDispatcher; import javax.servlet.Servlet; import javax.servlet.ServletException; +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.ServletHost; import org.apache.tuscany.sca.host.http.ServletMappingException; @@ -62,7 +65,7 @@ import org.mortbay.thread.ThreadPool; * * @version $Rev$ $Date$ */ -public class JettyServer implements ServletHost { +public class JettyServer implements ServletHost, LifeCycleListener { private static final Logger logger = Logger.getLogger(JettyServer.class.getName()); private final Object joinLock = new Object(); @@ -74,7 +77,6 @@ public class JettyServer implements ServletHost { private String keyStoreType; private String trustStoreType; - private boolean sendServerVersion; private WorkScheduler workScheduler; private int defaultPort = portDefault; @@ -86,7 +88,7 @@ public class JettyServer implements ServletHost { private class Port { private Server server; private ServletHandler servletHandler; - + private Port(Server server, ServletHandler servletHandler) { this.server = server; this.servletHandler = servletHandler; @@ -95,26 +97,23 @@ public class JettyServer implements ServletHost { public Server getServer() { return server; } - + public ServletHandler getServletHandler() { return servletHandler; } } - + private Map ports = new HashMap(); private String contextPath = "/"; private org.mortbay.log.Logger jettyLogger; - public JettyServer(WorkScheduler workScheduler) { + public JettyServer(ExtensionPointRegistry registry) { + this(registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(WorkScheduler.class)); + } + + protected JettyServer(WorkScheduler workScheduler) { this.workScheduler = workScheduler; - try { - jettyLogger = Log.getLog(); - } catch (Throwable e) { - // Ignore - } finally { - Log.setLog(new JettyLogger()); - } AccessController.doPrivileged(new PrivilegedAction() { public Object run() { trustStore = System.getProperty("javax.net.ssl.trustStore"); @@ -128,11 +127,11 @@ public class JettyServer implements ServletHost { } }); } - + public void setDefaultPort(int port) { defaultPort = port; } - + public int getDefaultPort() { return defaultPort; } @@ -150,7 +149,7 @@ public class JettyServer implements ServletHost { } try { Set> entries = new HashSet>(ports.entrySet()); - for (Entry entry: entries) { + for (Entry entry : entries) { Port port = entry.getValue(); Server server = port.getServer(); server.stop(); @@ -166,7 +165,7 @@ public class JettyServer implements ServletHost { } } } - + private void configureSSL(SslSocketConnector connector) { connector.setProtocol("TLS"); connector.setKeystore(keyStore); @@ -186,7 +185,7 @@ public class JettyServer implements ServletHost { public void addServletMapping(String suri, Servlet servlet) throws ServletMappingException { URI uri = URI.create(suri); - + // Get the URI scheme and port String scheme = uri.getScheme(); if (scheme == null) { @@ -206,8 +205,8 @@ public class JettyServer implements ServletHost { Server server = new Server(); server.setThreadPool(new WorkSchedulerThreadPool()); if ("https".equals(scheme)) { -// Connector httpConnector = new SelectChannelConnector(); -// httpConnector.setPort(portNumber); + // Connector httpConnector = new SelectChannelConnector(); + // httpConnector.setPort(portNumber); SslSocketConnector sslConnector = new SslSocketConnector(); sslConnector.setPort(portNumber); configureSSL(sslConnector); @@ -217,26 +216,26 @@ public class JettyServer implements ServletHost { selectConnector.setPort(portNumber); server.setConnectors(new Connector[] {selectConnector}); } - + ContextHandler contextHandler = new ContextHandler(); //contextHandler.setContextPath(contextPath); contextHandler.setContextPath("/"); server.setHandler(contextHandler); - + SessionHandler sessionHandler = new SessionHandler(); ServletHandler servletHandler = new ServletHandler(); sessionHandler.addHandler(servletHandler); - + contextHandler.setHandler(sessionHandler); - + server.setStopAtShutdown(true); server.setSendServerVersion(sendServerVersion); server.start(); - + // Keep track of the new server and Servlet handler port = new Port(server, servletHandler); ports.put(portNumber, port); - + } catch (Exception e) { throw new ServletMappingException(e); } @@ -246,44 +245,44 @@ public class JettyServer implements ServletHost { ServletHandler servletHandler = port.getServletHandler(); ServletHolder holder; if (servlet instanceof DefaultResourceServlet) { - + // Optimize the handling of resource requests, use the Jetty default Servlet // instead of our default resource Servlet String servletPath = uri.getPath(); if (servletPath.endsWith("*")) { - servletPath = servletPath.substring(0, servletPath.length()-1); + servletPath = servletPath.substring(0, servletPath.length() - 1); } if (servletPath.endsWith("/")) { - servletPath = servletPath.substring(0, servletPath.length()-1); - } + servletPath = servletPath.substring(0, servletPath.length() - 1); + } if (!servletPath.startsWith("/")) { servletPath = '/' + servletPath; - } - + } + DefaultResourceServlet resourceServlet = (DefaultResourceServlet)servlet; DefaultServlet defaultServlet = new JettyDefaultServlet(servletPath, resourceServlet.getDocumentRoot()); holder = new ServletHolder(defaultServlet); - + } else { holder = new ServletHolder(servlet); } servletHandler.addServlet(holder); - + ServletMapping mapping = new ServletMapping(); mapping.setServletName(holder.getName()); String path = uri.getPath(); - + if (!path.startsWith("/")) { path = '/' + path; } - + if (!path.startsWith(contextPath)) { path = contextPath + path; - } - + } + mapping.setPathSpec(path); servletHandler.addServletMapping(mapping); - + // Compute the complete URL String host; try { @@ -299,7 +298,7 @@ public class JettyServer implements ServletHost { } logger.info("Added Servlet mapping: " + addedURL); } - + public URL getURLMapping(String suri) throws ServletMappingException { URI uri = URI.create(suri); @@ -312,7 +311,7 @@ public class JettyServer implements ServletHost { if (portNumber == -1) { portNumber = defaultPort; } - + // Get the host String host; try { @@ -320,11 +319,10 @@ public class JettyServer implements ServletHost { } catch (UnknownHostException e) { host = "localhost"; } - + // Construct the URL String path = uri.getPath(); - if (!path.startsWith("/")) { path = '/' + path; } @@ -332,7 +330,6 @@ public class JettyServer implements ServletHost { if (!path.startsWith(contextPath)) { path = contextPath + path; } - URL url; try { @@ -342,15 +339,15 @@ public class JettyServer implements ServletHost { } return url; } - + public Servlet getServletMapping(String suri) throws ServletMappingException { - - if (suri == null){ + + if (suri == null) { return null; } - + URI uri = URI.create(suri); - + // Get the URI port int portNumber = uri.getPort(); if (portNumber == -1) { @@ -362,22 +359,22 @@ public class JettyServer implements ServletHost { if (port == null) { return null; } - + // Remove the Servlet mapping for the given Servlet ServletHandler servletHandler = port.getServletHandler(); Servlet servlet = null; List mappings = new ArrayList(Arrays.asList(servletHandler.getServletMappings())); String path = uri.getPath(); - + if (!path.startsWith("/")) { path = '/' + path; } - + if (!path.startsWith(contextPath)) { path = contextPath + path; } - + for (ServletMapping mapping : mappings) { if (Arrays.asList(mapping.getPathSpecs()).contains(path)) { try { @@ -393,7 +390,7 @@ public class JettyServer implements ServletHost { public Servlet removeServletMapping(String suri) { URI uri = URI.create(suri); - + // Get the URI port int portNumber = uri.getPort(); if (portNumber == -1) { @@ -410,22 +407,22 @@ public class JettyServer implements ServletHost { logger.warning("No servlet registered at this URI: " + suri); return null; } - + // Remove the Servlet mapping for the given Servlet ServletHandler servletHandler = port.getServletHandler(); Servlet removedServlet = null; List mappings = new ArrayList(Arrays.asList(servletHandler.getServletMappings())); String path = uri.getPath(); - + if (!path.startsWith("/")) { path = '/' + path; } - + if (!path.startsWith(contextPath)) { path = contextPath + path; } - + for (ServletMapping mapping : mappings) { if (Arrays.asList(mapping.getPathSpecs()).contains(path)) { try { @@ -440,7 +437,7 @@ public class JettyServer implements ServletHost { } if (removedServlet != null) { servletHandler.setServletMappings(mappings.toArray(new ServletMapping[mappings.size()])); - + // Stop the port if there are no servlet mappings on it anymore if (mappings.size() == 0) { try { @@ -452,11 +449,11 @@ public class JettyServer implements ServletHost { } ports.remove(portNumber); } - + } else { logger.warning("Trying to Remove servlet mapping: " + path + " where mapping is not registered"); } - + return removedServlet; } @@ -464,11 +461,11 @@ public class JettyServer implements ServletHost { //FIXME implement this later return null; } - + public String getContextPath() { return contextPath; } - + public void setContextPath(String path) { this.contextPath = path; } @@ -506,4 +503,14 @@ public class JettyServer implements ServletHost { throw new UnsupportedOperationException(); } + public void start() { + try { + jettyLogger = Log.getLog(); + } catch (Throwable e) { + // Ignore + } finally { + Log.setLog(new JettyLogger()); + } + } + } diff --git a/java/sca/modules/host-jetty/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator b/java/sca/modules/host-jetty/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator deleted file mode 100644 index e826f5a496..0000000000 --- a/java/sca/modules/host-jetty/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator +++ /dev/null @@ -1,18 +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. -# Implementation class for the ModuleActivator -org.apache.tuscany.sca.http.jetty.JettyRuntimeModuleActivator diff --git a/java/sca/modules/host-jetty/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost b/java/sca/modules/host-jetty/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost new file mode 100644 index 0000000000..b1b8ddc387 --- /dev/null +++ b/java/sca/modules/host-jetty/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost @@ -0,0 +1,18 @@ +# 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. +# Implementation class for the ServletHost +org.apache.tuscany.sca.http.jetty.JettyServer;ranking=100 \ No newline at end of file diff --git a/java/sca/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java b/java/sca/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java index 020dd4af0e..2ab9e46b10 100644 --- a/java/sca/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java +++ b/java/sca/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java @@ -87,6 +87,7 @@ public class JettyServerTestCase extends TestCase { */ public void testRegisterServletMapping() throws Exception { JettyServer service = new JettyServer(workScheduler); + service.start(); TestServlet servlet = new TestServlet(); service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet); Socket client = new Socket("127.0.0.1", HTTP_PORT); @@ -103,6 +104,7 @@ public class JettyServerTestCase extends TestCase { System.setProperty("javax.net.ssl.keyStorePassword", "apache"); System.setProperty("jetty.ssl.password", "apache"); JettyServer service = new JettyServer(workScheduler); + service.start(); TestServlet servlet = new TestServlet(); try { service.addServletMapping("https://127.0.0.1:" + HTTP_PORT + "/foo", servlet); @@ -134,6 +136,7 @@ public class JettyServerTestCase extends TestCase { */ public void testRegisterMultiplePorts() throws Exception { JettyServer service = new JettyServer(workScheduler); + service.start(); TestServlet servlet = new TestServlet(); service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet); TestServlet servlet2 = new TestServlet(); @@ -160,6 +163,7 @@ public class JettyServerTestCase extends TestCase { public void testUnregisterMapping() throws Exception { JettyServer service = new JettyServer(workScheduler); + service.start(); TestServlet servlet = new TestServlet(); String uri = "http://127.0.0.1:" + HTTP_PORT + "/foo"; service.addServletMapping(uri, servlet); @@ -179,6 +183,7 @@ public class JettyServerTestCase extends TestCase { public void testRequestSession() throws Exception { JettyServer service = new JettyServer(workScheduler); + service.start(); TestServlet servlet = new TestServlet(); service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet); Socket client = new Socket("127.0.0.1", HTTP_PORT); @@ -193,12 +198,14 @@ public class JettyServerTestCase extends TestCase { public void testRestart() throws Exception { JettyServer service = new JettyServer(workScheduler); + service.start(); service.stop(); service.stop(); } public void testNoMappings() throws Exception { JettyServer service = new JettyServer(workScheduler); + service.start(); Exception ex = null; try { new Socket("127.0.0.1", HTTP_PORT); @@ -211,6 +218,7 @@ public class JettyServerTestCase extends TestCase { public void testResourceServlet() throws Exception { JettyServer service = new JettyServer(workScheduler); + service.start(); String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString(); documentRoot = documentRoot.substring(0, documentRoot.lastIndexOf('/')); @@ -231,6 +239,7 @@ public class JettyServerTestCase extends TestCase { public void testDefaultServlet() throws Exception { JettyServer service = new JettyServer(workScheduler); + service.start(); String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString(); documentRoot = documentRoot.substring(0, documentRoot.lastIndexOf('/')); diff --git a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java index 22bbde2a00..1205e86684 100644 --- a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java +++ b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java @@ -162,11 +162,12 @@ public class ServletHostHelper { if (hosts == null || hosts.size() < 1) { throw new IllegalStateException("No ServletHost found"); } - ServletHost servletHost = hosts.get(0); - if (!(servletHost instanceof WebAppServletHost)) { - throw new IllegalStateException("unexpected ServletHost type: " + servletHost); + for (ServletHost servletHost : hosts) { + if ((servletHost instanceof WebAppServletHost)) { + return (WebAppServletHost)servletHost; + } } - return (WebAppServletHost)servletHost; + throw new IllegalStateException("No WebApp Servlet host is configured"); } private static Node createNode(final ServletContext servletContext) throws ServletException { diff --git a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java deleted file mode 100644 index 356d14fb83..0000000000 --- a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java +++ /dev/null @@ -1,57 +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.host.webapp; - -import java.util.List; - -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.ModuleActivator; -import org.apache.tuscany.sca.host.http.ServletHost; -import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint; - -/** - * Activates the webapp host by registering the webapp ServletHost impl - * - * @version $Rev$ $Date$ - */ -public class WebAppModuleActivator implements ModuleActivator { - - public void start(ExtensionPointRegistry extensionPointRegistry) { - - ServletHostExtensionPoint servletHosts = - extensionPointRegistry.getExtensionPoint(ServletHostExtensionPoint.class); - - List hosts = servletHosts.getServletHosts(); - // Only add webapp host if no other host already registered (eg jetty in standalone) - if (hosts != null && hosts.size() < 1) { - hosts.add(new WebAppServletHost()); - } - } - - public void stop(ExtensionPointRegistry registry) { - // as we know we are running in a webapp remove all of the servlet - // hosts. There will just be one - see start method - ServletHostExtensionPoint servletHosts = - registry.getExtensionPoint(ServletHostExtensionPoint.class); - List hosts = servletHosts.getServletHosts(); - hosts.clear(); - } - -} diff --git a/java/sca/modules/host-webapp/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator b/java/sca/modules/host-webapp/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator deleted file mode 100644 index d99fdf257e..0000000000 --- a/java/sca/modules/host-webapp/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator +++ /dev/null @@ -1,18 +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. -# Implementation class for the ModuleActivator -org.apache.tuscany.sca.host.webapp.WebAppModuleActivator -- cgit v1.2.3