tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rf...@apache.org
Subject svn commit: r930949 - in /tuscany/sca-java-2.x/contrib/modules/host-tomcat/src: main/java/org/apache/tuscany/sca/http/tomcat/ main/resources/META-INF/services/ test/java/org/apache/tuscany/sca/http/tomcat/
Date Mon, 05 Apr 2010 20:11:32 GMT
Author: rfeng
Date: Mon Apr  5 20:11:32 2010
New Revision: 930949

URL: http://svn.apache.org/viewvc?rev=930949&view=rev
Log:
All the ServletHost impl class to receive attributes  and add a test

Modified:
    tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
    tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost
    tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java

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=930949&r1=930948&r2=930949&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 20:11:32 2010
@@ -70,8 +70,12 @@ import org.apache.tuscany.sca.work.WorkS
 public class TomcatServer implements ServletHost, LifeCycleListener {
     private static final Logger logger = Logger.getLogger(TomcatServer.class.getName());
 
+    private WorkScheduler workScheduler;
     private Embedded embedded;
-    private int defaultPortNumber = 8080;
+    private Map<Integer, Port> ports = new HashMap<Integer, Port>();
+    private String contextPath = "/";
+    private int defaultPort = 8080;
+    private int defaultSSLPort = 8443;
 
     private final class TuscanyLoader implements Loader {
         private final ClassLoader tccl;
@@ -168,19 +172,24 @@ public class TomcatServer implements Ser
         }
     }
 
-    private Map<Integer, Port> ports = new HashMap<Integer, Port>();
-
-    private WorkScheduler workScheduler;
-
-    private String contextPath = "/";
-
     /**
      * Constructs a new embedded Tomcat server.
      *
      */
-    public TomcatServer(ExtensionPointRegistry registry) {
+    public TomcatServer(ExtensionPointRegistry registry, Map<String, String> attributes)
{
         UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class);
         this.workScheduler = utilities.getUtility(WorkScheduler.class);
+        String port = attributes.get("defaultPort");
+        if (port != null) {
+            defaultPort = Integer.parseInt(port);
+        }
+        String sslPort = attributes.get("defaultSSLPort");
+        if (sslPort != null) {
+            defaultPort = Integer.parseInt(sslPort);
+        }
+        if (attributes.containsKey("contextPath")) {
+            contextPath = attributes.get("contextPath");
+        }
     }
 
     protected TomcatServer(WorkScheduler workScheduler) {
@@ -188,11 +197,11 @@ public class TomcatServer implements Ser
     }
 
     public void setDefaultPort(int port) {
-        defaultPortNumber = port;
+        defaultPort = port;
     }
 
     public int getDefaultPort() {
-        return defaultPortNumber;
+        return defaultPort;
     }
 
     /**
@@ -213,9 +222,7 @@ public class TomcatServer implements Ser
     }
 
     public synchronized String addServletMapping(String suri, Servlet servlet) {
-        if (embedded == null) {
-            start();
-        }
+        init();
         URI uri = URI.create(suri);
 
         // Get the URI scheme and port
@@ -223,7 +230,7 @@ public class TomcatServer implements Ser
         if (scheme == null) {
             scheme = "http";
         }
-        final int portNumber = (uri.getPort() == -1 ? defaultPortNumber : uri.getPort());
+        final int portNumber = (uri.getPort() == -1 ? defaultPort : uri.getPort());
 
         // Get the port object associated with the given port number
         Port port = ports.get(portNumber);
@@ -392,7 +399,12 @@ public class TomcatServer implements Ser
         return port;
     }
 
-    private void startEmbedded() {
+    private synchronized void init() {
+        if (embedded != null) {
+            return;
+        }
+        embedded = new Embedded();
+        embedded.setAwait(true);
         // Allow privileged access to read properties. Requires PropertiesPermission read
in
         // security policy.
         try {
@@ -418,7 +430,7 @@ public class TomcatServer implements Ser
         }
         int portNumber = uri.getPort();
         if (portNumber == -1) {
-            portNumber = defaultPortNumber;
+            portNumber = defaultPort;
         }
 
         // Get the host
@@ -459,7 +471,7 @@ public class TomcatServer implements Ser
         // Get the URI port
         int portNumber = uri.getPort();
         if (portNumber == -1) {
-            portNumber = defaultPortNumber;
+            portNumber = defaultPort;
         }
 
         // Get the port object associated with the given port number
@@ -508,7 +520,7 @@ public class TomcatServer implements Ser
         // Get the URI port
         int portNumber = uri.getPort();
         if (portNumber == -1) {
-            portNumber = defaultPortNumber;
+            portNumber = defaultPort;
         }
 
         // Get the port object associated with the given port number
@@ -600,9 +612,7 @@ public class TomcatServer implements Ser
     }
 
     public void start() {
-        embedded = new Embedded();
-        embedded.setAwait(true);
-        startEmbedded();
+
     }
 
     public String addServletMapping(String uri, Servlet servlet, SecurityContext securityContext)
@@ -621,4 +631,60 @@ public class TomcatServer implements Ser
     public void setAttribute(String arg0, Object arg1) {
     }
 
+    private URL map(String suri, SecurityContext securityContext, boolean resolve) throws
ServletMappingException {
+        URI uri = URI.create(suri);
+
+        // Get the URI scheme and port
+        String scheme = null;
+        if (securityContext != null && securityContext.isSSLEnabled()) {
+            scheme = "https";
+        } else {
+            scheme = uri.getScheme();
+            if (scheme == null) {
+                scheme = "http";
+            }
+        }
+
+        int portNumber = uri.getPort();
+        if (portNumber == -1) {
+            if ("http".equals(scheme)) {
+                portNumber = defaultPort;
+            } else {
+                portNumber = defaultSSLPort;
+            }
+        }
+
+        // Get the host
+        String host = uri.getHost();
+        if (host == null) {
+            host = "0.0.0.0";
+            if (resolve) {
+                try {
+                    host = InetAddress.getLocalHost().getHostAddress();
+                } catch (UnknownHostException e) {
+                    host = "localhost";
+                }
+            }
+        }
+
+        // Construct the URL
+        String path = uri.getPath();
+
+        if (!path.startsWith("/")) {
+            path = '/' + path;
+        }
+
+        if (!path.startsWith(contextPath)) {
+            path = contextPath + path;
+        }
+
+        URL url;
+        try {
+            url = new URL(scheme, host, portNumber, path);
+        } catch (MalformedURLException e) {
+            throw new ServletMappingException(e);
+        }
+        return url;
+    }
+
 }

Modified: 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=930949&r1=930948&r2=930949&view=diff
==============================================================================
--- tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost
(original)
+++ 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 20:11:32 2010
@@ -15,4 +15,4 @@
 # 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
+org.apache.tuscany.sca.http.tomcat.TomcatServer;name=tomcat,ranking=50,defaultPort=8085

Modified: tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java?rev=930949&r1=930948&r2=930949&view=diff
==============================================================================
--- tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java
(original)
+++ tuscany/sca-java-2.x/contrib/modules/host-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java
Mon Apr  5 20:11:32 2010
@@ -37,9 +37,15 @@ import javax.servlet.http.HttpServletRes
 
 import junit.framework.TestCase;
 
+import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.host.http.DefaultResourceServlet;
+import org.apache.tuscany.sca.host.http.ExtensibleServletHost;
+import org.apache.tuscany.sca.host.http.ServletHost;
 import org.apache.tuscany.sca.work.NotificationListener;
 import org.apache.tuscany.sca.work.WorkScheduler;
+import org.junit.Assert;
+import org.junit.Test;
 
 /**
  * @version $Rev$ $Date$
@@ -350,4 +356,14 @@ public class TomcatServerTestCase extend
             delegate.destroy();
         }
     }
+    
+    @Test
+    public void testDiscovery() {
+        ExtensionPointRegistry registry = new DefaultExtensionPointRegistry();
+        ExtensibleServletHost host = ExtensibleServletHost.getInstance(registry);
+        ServletHost me = host.getDefaultServletHost();
+        Assert.assertEquals("tomcat", me.getName());
+        Assert.assertEquals(8085, me.getDefaultPort());
+        registry.stop();
+    }
 }



Mime
View raw message