diff options
Diffstat (limited to 'sca-java-2.x/contrib/modules/host-tomcat/src/main')
2 files changed, 88 insertions, 22 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 13a3deb90d..b62235a183 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 @@ -70,8 +70,12 @@ import org.apache.tuscany.sca.work.WorkScheduler; 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 ServletHost, LifeCycleListener { } } - 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 ServletHost, LifeCycleListener { } public void setDefaultPort(int port) { - defaultPortNumber = port; + defaultPort = port; } public int getDefaultPort() { - return defaultPortNumber; + return defaultPort; } /** @@ -213,9 +222,7 @@ public class TomcatServer implements ServletHost, LifeCycleListener { } 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 ServletHost, LifeCycleListener { 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 ServletHost, LifeCycleListener { 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 ServletHost, LifeCycleListener { } int portNumber = uri.getPort(); if (portNumber == -1) { - portNumber = defaultPortNumber; + portNumber = defaultPort; } // Get the host @@ -459,7 +471,7 @@ public class TomcatServer implements ServletHost, LifeCycleListener { // 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 ServletHost, LifeCycleListener { // 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 ServletHost, LifeCycleListener { } 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 ServletHost, LifeCycleListener { 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; + } + } diff --git a/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost b/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost index 2d85169f97..00350bbf6b 100644 --- a/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost +++ b/sca-java-2.x/contrib/modules/host-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.ServletHost @@ -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
|