diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-03-14 00:50:38 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-03-14 00:50:38 +0000 |
commit | b4f48f407a8fefba16395eceb34de6bd48e74d12 (patch) | |
tree | 7e1bcf4456ef1dfd3b4d337ed7ea262f1259ffef /sca-java-2.x/trunk/modules/host-webapp/src/main | |
parent | 014971ccb5b2041147cf9d84d498cce64ea0493e (diff) |
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
Diffstat (limited to 'sca-java-2.x/trunk/modules/host-webapp/src/main')
-rw-r--r-- | sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java | 4 | ||||
-rw-r--r-- | sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java | 4 | ||||
-rw-r--r-- | sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java (renamed from sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java) | 73 | ||||
-rw-r--r-- | sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java | 51 |
4 files changed, 81 insertions, 51 deletions
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/ServletHostHelper.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java index d157b5f565..7d086cd0f3 100644 --- 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/WebAppHelper.java @@ -29,16 +29,18 @@ 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; -import org.apache.tuscany.sca.node.impl.NodeImpl; -public class ServletHostHelper { +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); @@ -125,50 +127,57 @@ public class ServletHostHelper { 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); + public synchronized static ServletHost init(final ServletContext servletContext) { + if (host == null) { try { - String domainName = (String)servletContext.getAttribute(DOMAIN_NAME_ATTR); factory = NodeFactory.getInstance(); + ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); + ServletHostExtensionPoint servletHosts = registry.getExtensionPoint(ServletHostExtensionPoint.class); + servletHosts.setWebApp(true); for (Enumeration<String> e = servletContext.getInitParameterNames(); e.hasMoreElements();) { String name = e.nextElement(); String value = servletContext.getInitParameter(name); servletContext.setAttribute(name, value); } - node = createNode(servletContext); + host = getServletHost(servletContext); + Node node = createAndStartNode(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(); - } - }); + return host; } catch (ServletException e) { throw new RuntimeException(e); } } - return getServletHost(node); + 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(Node node) { - NodeImpl nodeImpl = (NodeImpl)node; - return (WebAppServletHost) org.apache.tuscany.sca.host.http.ServletHostHelper.getServletHost(nodeImpl.getExtensionPoints()); + private static WebAppServletHost getServletHost(NodeFactory factory) { + ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); + return (WebAppServletHost) org.apache.tuscany.sca.host.http.ServletHostHelper.getServletHost(registry); } - private static Node createNode(final ServletContext servletContext) throws ServletException { + private static Node createAndStartNode(final ServletContext servletContext) throws ServletException { NodeConfiguration configuration; try { configuration = getNodeConfiguration(servletContext); @@ -180,10 +189,10 @@ public class ServletHostHelper { } public static void stop(ServletContext servletContext) { - Node node = (Node)servletContext.getAttribute(ServletHostHelper.SCA_NODE_ATTRIBUTE); + Node node = (Node)servletContext.getAttribute(WebAppHelper.SCA_NODE_ATTRIBUTE); if (node != null) { node.stop(); - servletContext.setAttribute(ServletHostHelper.SCA_NODE_ATTRIBUTE, null); + 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<String, Object> tempAttributes = new HashMap<String, Object>(); @@ -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() { |