From b4f48f407a8fefba16395eceb34de6bd48e74d12 Mon Sep 17 00:00:00 2001 From: rfeng Date: Sun, 14 Mar 2010 00:50:38 +0000 Subject: Improve the IP address binding to be based on the host from the uri Having servlet host to return deployed uri git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@922701 13f79535-47bb-0310-9956-ffa450edef68 --- .../tuscany/sca/host/webapp/ServletHostHelper.java | 189 -------------------- .../sca/host/webapp/TuscanyContextListener.java | 4 +- .../sca/host/webapp/TuscanyServletFilter.java | 4 +- .../tuscany/sca/host/webapp/WebAppHelper.java | 198 +++++++++++++++++++++ .../tuscany/sca/host/webapp/WebAppServletHost.java | 51 ++++-- 5 files changed, 238 insertions(+), 208 deletions(-) delete mode 100644 sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java create mode 100644 sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java (limited to 'sca-java-2.x/trunk/modules/host-webapp/src/main/java/org') diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java deleted file mode 100644 index d157b5f565..0000000000 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java +++ /dev/null @@ -1,189 +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.io.File; -import java.io.IOException; -import java.net.URI; -import java.net.URL; -import java.util.Enumeration; - -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; - -import org.apache.tuscany.sca.host.http.ServletHost; -import org.apache.tuscany.sca.node.Node; -import org.apache.tuscany.sca.node.NodeFactory; -import org.apache.tuscany.sca.node.configuration.NodeConfiguration; -import org.apache.tuscany.sca.node.impl.NodeImpl; - -public class ServletHostHelper { - public static final String DOMAIN_NAME_ATTR = "org.apache.tuscany.sca.domain.name"; - public static final String SCA_NODE_ATTRIBUTE = Node.class.getName(); - private static NodeFactory factory; - - private static URL getResource(ServletContext servletContext, String location) throws IOException { - URI uri = URI.create(location); - if (uri.isAbsolute()) { - return uri.toURL(); - } else { - String path = location; - if (!path.startsWith("/")) { - path = "/" + path; - } - URL url = servletContext.getResource(path); - if (url != null && url.getProtocol().equals("jndi")) { - //this is Tomcat case, we should use getRealPath - File warRootFile = new File(servletContext.getRealPath(path)); - return warRootFile.toURI().toURL(); - } else { - //this is Jetty case - return url; - } - } - } - - @SuppressWarnings("unchecked") - private static NodeConfiguration getNodeConfiguration(ServletContext servletContext) throws IOException { - NodeConfiguration configuration = null; - String nodeConfigURI = (String) servletContext.getAttribute("node.configuration"); - if (nodeConfigURI != null) { - URL url = getResource(servletContext, nodeConfigURI); - configuration = factory.loadConfiguration(url.openStream(), url); - } else { - configuration = factory.createNodeConfiguration(); - Enumeration names = servletContext.getAttributeNames(); - while (names.hasMoreElements()) { - String name = names.nextElement(); - if (name.startsWith("contribution.")) { - String contrib = (String) servletContext.getAttribute(name); - if (contrib != null) { - configuration.addContribution(getResource(servletContext, contrib)); - } - } - } - if (configuration.getContributions().isEmpty()) { - // TODO: Which path should be the default root - configuration.addContribution(getResource(servletContext, "/")); - } - URL composite = getResource(servletContext, "/WEB-INF/web.composite"); - if (composite != null) { - configuration.getContributions().get(0).addDeploymentComposite(composite); - } - String nodeURI = (String) servletContext.getAttribute("node.uri"); - if (nodeURI == null) { - nodeURI = new File(servletContext.getRealPath("/")).getName(); - } - configuration.setURI(nodeURI); - String domainURI = (String) servletContext.getAttribute("domain.uri"); - if (domainURI != null) { - configuration.setDomainURI(domainURI); - } else { - domainURI = servletContext.getInitParameter("org.apache.tuscany.sca.defaultDomainURI"); - if (domainURI != null) { - configuration.setDomainURI(getDomainName(domainURI)); - configuration.setDomainRegistryURI(domainURI); - } - } - } - return configuration; - } - - // TODO: Temp for now to get the old samples working till i clean up all the domain uri/name after the ML discussion. - private static String getDomainName(String configURI) { - String domainName; - if (configURI.startsWith("tuscany:vm:")) { - domainName = configURI.substring("tuscany:vm:".length()); - } else if (configURI.startsWith("tuscany:")) { - int i = configURI.indexOf('?'); - if (i == -1) { - domainName = configURI.substring("tuscany:".length()); - } else{ - domainName = configURI.substring("tuscany:".length(), i); - } - } else { - domainName = configURI; - } - return domainName; - } - - public static ServletHost init(final ServletContext servletContext) { - Node node = (Node)servletContext.getAttribute(SCA_NODE_ATTRIBUTE); - if (node == null) { - org.apache.tuscany.sca.host.http.ServletHostHelper.setWebappHost(true); - try { - String domainName = (String)servletContext.getAttribute(DOMAIN_NAME_ATTR); - factory = NodeFactory.getInstance(); - for (Enumeration e = servletContext.getInitParameterNames(); e.hasMoreElements();) { - String name = e.nextElement(); - String value = servletContext.getInitParameter(name); - servletContext.setAttribute(name, value); - } - node = createNode(servletContext); - servletContext.setAttribute(SCA_NODE_ATTRIBUTE, node); - getServletHost(node).init(new ServletConfig() { - public String getInitParameter(String name) { - return servletContext.getInitParameter(name); - } - - public Enumeration getInitParameterNames() { - return servletContext.getInitParameterNames(); - } - - public ServletContext getServletContext() { - return servletContext; - } - - public String getServletName() { - return servletContext.getServletContextName(); - } - }); - } catch (ServletException e) { - throw new RuntimeException(e); - } - } - return getServletHost(node); - } - - private static WebAppServletHost getServletHost(Node node) { - NodeImpl nodeImpl = (NodeImpl)node; - return (WebAppServletHost) org.apache.tuscany.sca.host.http.ServletHostHelper.getServletHost(nodeImpl.getExtensionPoints()); - } - - private static Node createNode(final ServletContext servletContext) throws ServletException { - NodeConfiguration configuration; - try { - configuration = getNodeConfiguration(servletContext); - } catch (IOException e) { - throw new ServletException(e); - } - Node node = factory.createNode(configuration).start(); - return node; - } - - public static void stop(ServletContext servletContext) { - Node node = (Node)servletContext.getAttribute(ServletHostHelper.SCA_NODE_ATTRIBUTE); - if (node != null) { - node.stop(); - servletContext.setAttribute(ServletHostHelper.SCA_NODE_ATTRIBUTE, null); - } - } -} diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java index 62c238e135..45c1bbf3da 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java @@ -36,7 +36,7 @@ public class TuscanyContextListener implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { logger.info(event.getServletContext().getServletContextName() + " is starting."); try { - ServletHostHelper.init(event.getServletContext()); + WebAppHelper.init(event.getServletContext()); } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); } @@ -49,7 +49,7 @@ public class TuscanyContextListener implements ServletContextListener { return; } try { - ServletHostHelper.stop(event.getServletContext()); + WebAppHelper.stop(event.getServletContext()); } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); } diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java index 72a76a789e..a12466a9fd 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java @@ -60,7 +60,7 @@ public class TuscanyServletFilter implements Filter { String value = config.getInitParameter(name); context.setAttribute(name, value); } - servletHost = ServletHostHelper.init(context); + servletHost = WebAppHelper.init(context); } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); context.log(e.getMessage(), e); @@ -69,7 +69,7 @@ public class TuscanyServletFilter implements Filter { } public void destroy() { - ServletHostHelper.stop(context); + WebAppHelper.stop(context); servletHost = null; } diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java new file mode 100644 index 0000000000..7d086cd0f3 --- /dev/null +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java @@ -0,0 +1,198 @@ +/* + * 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.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URL; +import java.util.Enumeration; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; +import org.apache.tuscany.sca.node.configuration.NodeConfiguration; + +public class WebAppHelper { + public static final String DOMAIN_NAME_ATTR = "org.apache.tuscany.sca.domain.name"; + public static final String SCA_NODE_ATTRIBUTE = Node.class.getName(); + private static NodeFactory factory; + private static WebAppServletHost host; + + private static URL getResource(ServletContext servletContext, String location) throws IOException { + URI uri = URI.create(location); + if (uri.isAbsolute()) { + return uri.toURL(); + } else { + String path = location; + if (!path.startsWith("/")) { + path = "/" + path; + } + URL url = servletContext.getResource(path); + if (url != null && url.getProtocol().equals("jndi")) { + //this is Tomcat case, we should use getRealPath + File warRootFile = new File(servletContext.getRealPath(path)); + return warRootFile.toURI().toURL(); + } else { + //this is Jetty case + return url; + } + } + } + + @SuppressWarnings("unchecked") + private static NodeConfiguration getNodeConfiguration(ServletContext servletContext) throws IOException { + NodeConfiguration configuration = null; + String nodeConfigURI = (String) servletContext.getAttribute("node.configuration"); + if (nodeConfigURI != null) { + URL url = getResource(servletContext, nodeConfigURI); + configuration = factory.loadConfiguration(url.openStream(), url); + } else { + configuration = factory.createNodeConfiguration(); + Enumeration names = servletContext.getAttributeNames(); + while (names.hasMoreElements()) { + String name = names.nextElement(); + if (name.startsWith("contribution.")) { + String contrib = (String) servletContext.getAttribute(name); + if (contrib != null) { + configuration.addContribution(getResource(servletContext, contrib)); + } + } + } + if (configuration.getContributions().isEmpty()) { + // TODO: Which path should be the default root + configuration.addContribution(getResource(servletContext, "/")); + } + URL composite = getResource(servletContext, "/WEB-INF/web.composite"); + if (composite != null) { + configuration.getContributions().get(0).addDeploymentComposite(composite); + } + String nodeURI = (String) servletContext.getAttribute("node.uri"); + if (nodeURI == null) { + nodeURI = new File(servletContext.getRealPath("/")).getName(); + } + configuration.setURI(nodeURI); + String domainURI = (String) servletContext.getAttribute("domain.uri"); + if (domainURI != null) { + configuration.setDomainURI(domainURI); + } else { + domainURI = servletContext.getInitParameter("org.apache.tuscany.sca.defaultDomainURI"); + if (domainURI != null) { + configuration.setDomainURI(getDomainName(domainURI)); + configuration.setDomainRegistryURI(domainURI); + } + } + } + return configuration; + } + + // TODO: Temp for now to get the old samples working till i clean up all the domain uri/name after the ML discussion. + private static String getDomainName(String configURI) { + String domainName; + if (configURI.startsWith("tuscany:vm:")) { + domainName = configURI.substring("tuscany:vm:".length()); + } else if (configURI.startsWith("tuscany:")) { + int i = configURI.indexOf('?'); + if (i == -1) { + domainName = configURI.substring("tuscany:".length()); + } else{ + domainName = configURI.substring("tuscany:".length(), i); + } + } else { + domainName = configURI; + } + return domainName; + } + + public synchronized static ServletHost init(final ServletContext servletContext) { + if (host == null) { + try { + factory = NodeFactory.getInstance(); + ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); + ServletHostExtensionPoint servletHosts = registry.getExtensionPoint(ServletHostExtensionPoint.class); + servletHosts.setWebApp(true); + for (Enumeration e = servletContext.getInitParameterNames(); e.hasMoreElements();) { + String name = e.nextElement(); + String value = servletContext.getInitParameter(name); + servletContext.setAttribute(name, value); + } + host = getServletHost(servletContext); + Node node = createAndStartNode(servletContext); + servletContext.setAttribute(SCA_NODE_ATTRIBUTE, node); + return host; + } catch (ServletException e) { + throw new RuntimeException(e); + } + } + return host; + } + + private static WebAppServletHost getServletHost(final ServletContext servletContext) throws ServletException { + WebAppServletHost host = (WebAppServletHost) getServletHost(factory); + host.init(new ServletConfig() { + public String getInitParameter(String name) { + return servletContext.getInitParameter(name); + } + + public Enumeration getInitParameterNames() { + return servletContext.getInitParameterNames(); + } + + public ServletContext getServletContext() { + return servletContext; + } + + public String getServletName() { + return servletContext.getServletContextName(); + } + }); + return host; + } + + private static WebAppServletHost getServletHost(NodeFactory factory) { + ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); + return (WebAppServletHost) org.apache.tuscany.sca.host.http.ServletHostHelper.getServletHost(registry); + } + + private static Node createAndStartNode(final ServletContext servletContext) throws ServletException { + NodeConfiguration configuration; + try { + configuration = getNodeConfiguration(servletContext); + } catch (IOException e) { + throw new ServletException(e); + } + Node node = factory.createNode(configuration).start(); + return node; + } + + public static void stop(ServletContext servletContext) { + Node node = (Node)servletContext.getAttribute(WebAppHelper.SCA_NODE_ATTRIBUTE); + if (node != null) { + node.stop(); + servletContext.setAttribute(WebAppHelper.SCA_NODE_ATTRIBUTE, null); + } + } +} diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java index f1d485b6c2..0c5431a7d8 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java @@ -58,6 +58,7 @@ public class WebAppServletHost implements ServletHost { private int defaultPortNumber = 8080; private String contributionRoot; + private ServletConfig servletConfig; private ServletContext servletContext; private Map tempAttributes = new HashMap(); @@ -77,11 +78,11 @@ public class WebAppServletHost implements ServletHost { return "webapp"; } - public void addServletMapping(String suri, Servlet servlet) throws ServletMappingException { - addServletMapping(suri, servlet, null); + public String addServletMapping(String suri, Servlet servlet) throws ServletMappingException { + return addServletMapping(suri, servlet, null); } - public void addServletMapping(String suri, Servlet servlet, SecurityContext securityContext) throws ServletMappingException { + public String addServletMapping(String suri, Servlet servlet, SecurityContext securityContext) throws ServletMappingException { URI pathURI = URI.create(suri); // Make sure that the path starts with a / @@ -90,15 +91,27 @@ public class WebAppServletHost implements ServletHost { suri = '/' + suri; } - if (!suri.startsWith(contextPath)) { + // String relativeURI = suri; + if (!suri.startsWith(contextPath + "/")) { suri = contextPath + suri; - } + } + if (!servlets.values().contains(servlet)) { + // The same servlet can be registred more than once + try { + servlet.init(servletConfig); + } catch (ServletException e) { + throw new ServletMappingException(e); + } + } + // In a webapp just use the given path and ignore the host and port // as they are fixed by the Web container servlets.put(suri, servlet); - logger.info("Added Servlet mapping: " + suri); + URL url = getURLMapping(suri, securityContext); + logger.info("Added Servlet mapping: " + url); + return url.toString(); } public Servlet removeServletMapping(String suri) throws ServletMappingException { @@ -116,7 +129,13 @@ public class WebAppServletHost implements ServletHost { // In a webapp just use the given path and ignore the host and port // as they are fixed by the Web container - return servlets.remove(suri); + Servlet servlet = servlets.remove(suri); + /* + if (servlet != null) { + servlet.destroy(); + } + */ + return servlet; } public Servlet getServletMapping(String suri) throws ServletMappingException { @@ -147,11 +166,13 @@ public class WebAppServletHost implements ServletHost { } // Get the host - String host; - try { - host = InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - host = "localhost"; + String host = uri.getHost(); + if (host == null) { + try { + host = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + host = "localhost"; + } } // Construct the URL @@ -210,14 +231,14 @@ public class WebAppServletHost implements ServletHost { } public void init(ServletConfig config) throws ServletException { - + this.servletConfig = config; servletContext = config.getServletContext(); for (String name : tempAttributes.keySet()) { servletContext.setAttribute(name, tempAttributes.get(name)); } - ServletHostHelper.init(servletContext); + // WebAppHelper.init(servletContext); initContextPath(config); @@ -282,7 +303,7 @@ public class WebAppServletHost implements ServletHost { } // Close the SCA domain - ServletHostHelper.stop(servletContext); + WebAppHelper.stop(servletContext); } public String getContextPath() { -- cgit v1.2.3