diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-10-02 18:47:49 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-10-02 18:47:49 +0000 |
commit | 3415d6a774a65984893639921ce8d3665102e53e (patch) | |
tree | 97e392b4faa68d604ac5874c3ce9d46ea2c7d4aa /sca-java-2.x/branches/sca-java-2.0-M5/modules/host-webapp/src/main | |
parent | cc41fab8cd8282b7e3ef86053ed511ee632076f7 (diff) |
TUSCANY-3667 - InetAddress is not allowed in GoogleAppEngine, so try to load it dynamically and fail gracefully
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1003855 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/branches/sca-java-2.0-M5/modules/host-webapp/src/main')
-rw-r--r-- | sca-java-2.x/branches/sca-java-2.0-M5/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java | 52 |
1 files changed, 29 insertions, 23 deletions
diff --git a/sca-java-2.x/branches/sca-java-2.0-M5/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java b/sca-java-2.x/branches/sca-java-2.0-M5/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java index 0c5431a7d8..caf7fc7942 100644 --- a/sca-java-2.x/branches/sca-java-2.0-M5/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java +++ b/sca-java-2.x/branches/sca-java-2.0-M5/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java @@ -6,30 +6,29 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.host.webapp; import java.lang.reflect.Method; -import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.RequestDispatcher; @@ -45,12 +44,14 @@ import org.apache.tuscany.sca.node.Node; /** * ServletHost implementation for use in a webapp environment. - * + * * @version $Rev$ $Date$ */ public class WebAppServletHost implements ServletHost { private static final Logger logger = Logger.getLogger(WebAppServletHost.class.getName()); + private static final String INETADDRESS = "java.net.InetAddress"; + public static final String SCA_NODE_ATTRIBUTE = Node.class.getName(); private Map<String, Servlet> servlets; @@ -73,14 +74,14 @@ public class WebAppServletHost implements ServletHost { public int getDefaultPort() { return defaultPortNumber; } - + public String getName() { return "webapp"; } - + public String addServletMapping(String suri, Servlet servlet) throws ServletMappingException { return addServletMapping(suri, servlet, null); - } + } public String addServletMapping(String suri, Servlet servlet, SecurityContext securityContext) throws ServletMappingException { URI pathURI = URI.create(suri); @@ -94,7 +95,7 @@ public class WebAppServletHost implements ServletHost { // String relativeURI = suri; if (!suri.startsWith(contextPath + "/")) { suri = contextPath + suri; - } + } if (!servlets.values().contains(servlet)) { // The same servlet can be registred more than once @@ -104,7 +105,7 @@ public class WebAppServletHost implements ServletHost { 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); @@ -169,8 +170,13 @@ public class WebAppServletHost implements ServletHost { String host = uri.getHost(); if (host == null) { try { - host = InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { + //TUSCANY-3667 - InetAddress is not allowed in GoogleAppEngine + //host = InetAddress.getLocalHost().getHostName(); + Class<?> clazz = Class.forName(INETADDRESS); + Object inetAddress = clazz.getMethod("getLocalHost").invoke(null); + host = (String) clazz.getMethod("getHostName").invoke(inetAddress); + } catch (Throwable t) { + logger.log(Level.WARNING, "Error retrieving host information : " + t.getMessage()); host = "localhost"; } } @@ -233,22 +239,22 @@ 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)); } - + // WebAppHelper.init(servletContext); - + initContextPath(config); // Initialize the registered Servlets for (Servlet servlet : servlets.values()) { servlet.init(config); } - + } - + /** * Initializes the contextPath * The 2.5 Servlet API has a getter for this, for pre 2.5 Servlet @@ -256,9 +262,9 @@ public class WebAppServletHost implements ServletHost { */ @SuppressWarnings("unchecked") public void initContextPath(ServletConfig config) { - + String oldContextPath = contextPath; - + if (Collections.list(config.getInitParameterNames()).contains("contextPath")) { contextPath = config.getInitParameter("contextPath"); } else { @@ -292,9 +298,9 @@ public class WebAppServletHost implements ServletHost { servlets.put(ns, servlets.remove(oldURI)); } } - - } - + + } + void destroy() { // Destroy the registered Servlets |