tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rf...@apache.org
Subject svn commit: r930925 - in /tuscany/sca-java-2.x/contrib/modules/host-tomcat: ./ META-INF/ src/main/java/org/apache/tuscany/sca/http/tomcat/ src/main/java/org/apache/tuscany/sca/http/tomcat/module/ src/main/resources/META-INF/services/
Date Mon, 05 Apr 2010 18:28:07 GMT
Author: rfeng
Date: Mon Apr  5 18:28:06 2010
New Revision: 930925

URL: http://svn.apache.org/viewvc?rev=930925&view=rev
Log:
Bring up host-tomcat for 2.x based on the Embedded class from Tomcat

Added:
    tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost
Removed:
    tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/
    tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator
Modified:
    tuscany/sca-java-2.x/contrib/modules/host-tomcat/META-INF/MANIFEST.MF
    tuscany/sca-java-2.x/contrib/modules/host-tomcat/pom.xml
    tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java

Modified: tuscany/sca-java-2.x/contrib/modules/host-tomcat/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/host-tomcat/META-INF/MANIFEST.MF?rev=930925&r1=930924&r2=930925&view=diff
==============================================================================
--- tuscany/sca-java-2.x/contrib/modules/host-tomcat/META-INF/MANIFEST.MF (original)
+++ tuscany/sca-java-2.x/contrib/modules/host-tomcat/META-INF/MANIFEST.MF Mon Apr  5 18:28:06
2010
@@ -10,14 +10,24 @@ Bnd-LastModified: 1222833663765
 Bundle-ManifestVersion: 2
 Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
 Bundle-Description: Apache Tuscany SCA Tomcat Servlet Host Extension
-Import-Package: javax.naming,javax.naming.directory,javax.servlet,java
- x.servlet.http,org.apache.catalina,org.apache.catalina.connector,org.
- apache.catalina.core,org.apache.catalina.servlets,org.apache.catalina
- .startup,org.apache.coyote,org.apache.coyote.http11,org.apache.naming
- .resources,org.apache.tomcat.util.buf,org.apache.tomcat.util.http.map
- per,org.apache.tomcat.util.net,org.apache.tuscany.sca.core;version="1
- .4",org.apache.tuscany.sca.host.http;version="2.0.0",org.apache.tuscany
- .sca.work;version="2.0.0"
+Import-Package: javax.naming,
+ javax.naming.directory,
+ javax.servlet,
+ javax.servlet.http,
+ org.apache.catalina,
+ org.apache.catalina.connector,
+ org.apache.catalina.core,
+ org.apache.catalina.servlets,
+ org.apache.catalina.startup,
+ org.apache.coyote,
+ org.apache.coyote.http11,
+ org.apache.naming.resources,
+ org.apache.tomcat.util.buf,
+ org.apache.tomcat.util.http.mapper,
+ org.apache.tomcat.util.net,
+ org.apache.tuscany.sca.core;version="2.0.0",
+ org.apache.tuscany.sca.host.http;version="2.0.0",
+ org.apache.tuscany.sca.work;version="2.0.0"
 Bundle-SymbolicName: org.apache.tuscany.sca.host.tomcat
 Bundle-DocURL: http://www.apache.org/
 

Modified: tuscany/sca-java-2.x/contrib/modules/host-tomcat/pom.xml
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/host-tomcat/pom.xml?rev=930925&r1=930924&r2=930925&view=diff
==============================================================================
--- tuscany/sca-java-2.x/contrib/modules/host-tomcat/pom.xml (original)
+++ tuscany/sca-java-2.x/contrib/modules/host-tomcat/pom.xml Mon Apr  5 18:28:06 2010
@@ -44,7 +44,7 @@
         <dependency>
             <groupId>org.apache.tomcat</groupId>
             <artifactId>catalina</artifactId>
-            <version>6.0.14</version>
+            <version>6.0.26</version>
             <exclusions>
                 <exclusion>
                     <groupId>org.apache.tomcat</groupId>
@@ -56,7 +56,7 @@
         <dependency>
             <groupId>org.apache.tomcat</groupId>
             <artifactId>coyote</artifactId>
-            <version>6.0.14</version>
+            <version>6.0.26</version>
             <exclusions>
                 <exclusion>
                     <groupId>org.apache.tomcat</groupId>

Modified: tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java?rev=930925&r1=930924&r2=930925&view=diff
==============================================================================
--- tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
(original)
+++ tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
Mon Apr  5 18:28:06 2010
@@ -31,12 +31,8 @@ import java.security.PrivilegedActionExc
 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.WorkS
  *
  * @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 Ser
      * 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 Ser
     /**
      * 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 Ser
     /**
      * 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 Ser
         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 Ser
             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 Ser
         }
     }
 
-    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 Ser
             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 Ser
         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) {
     }
 
 }

Added: tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost?rev=930925&view=auto
==============================================================================
--- tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost
(added)
+++ tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost
Mon Apr  5 18:28:06 2010
@@ -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.tomcat.TomcatServer;name=tomcat,ranking=50



Mime
View raw message