Author: antelder
Date: Thu Feb 26 07:29:05 2009
New Revision: 748050

URL: http://svn.apache.org/viewvc?rev=748050&view=rev
Log:
More TUSCANY-2858 work on bringing up the webapp support. Separate out the 
runtime initilization into a helper class so the common code can be shared, add 
a TuscanyContextListener that can start the Tuscany runtime, clean up the use 
of ThreadLocals. Just need to fix TUSCANY-2881 now

Added:
    
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java
    
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java
   (contents, props changed)
      - copied, changed from r747774, 
tuscany/branches/sca-java-1.x/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java
Modified:
    
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java
    
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java
    
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java

Added: 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java?rev=748050&view=auto
==============================================================================
--- 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java
 (added)
+++ 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java
 Thu Feb 26 07:29:05 2009
@@ -0,0 +1,187 @@
+/*
+ * 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.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.logging.Logger;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+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.Contribution;
+import org.apache.tuscany.sca.node.Node;
+import org.apache.tuscany.sca.node.NodeFactory;
+import org.oasisopen.sca.ServiceRuntimeException;
+
+public class ServletHostHelper {
+    private static final Logger logger = 
Logger.getLogger(ServletHostHelper.class.getName());
+
+    public static final String SCA_NODE_ATTRIBUTE = Node.class.getName();
+
+    public static ServletHost getServletHost() {
+        return WebAppServletHost.getInstance();
+    }
+
+    public static void init(ServletConfig servletConfig) {
+        init(servletConfig.getServletContext());
+    }
+    
+    public static void init(final ServletContext servletContext) {
+        if (servletContext.getAttribute(SCA_NODE_ATTRIBUTE) == null) {
+            try {
+                servletContext.setAttribute(SCA_NODE_ATTRIBUTE, 
createNode(servletContext));
+                WebAppServletHost.getInstance().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);
+            }
+        }
+    }
+    
+    private static Node createNode(final ServletContext servletContext) throws 
ServletException {
+        String contextPath = initContextPath(servletContext);
+        String contributionRoot = getContributionRoot(servletContext);
+        NodeFactory factory = NodeFactory.newInstance();
+        String webComposite = getWebComposite(servletContext);
+        Node node = factory.createNode(contextPath, webComposite, new 
Contribution(contributionRoot, contributionRoot));
+        node.start();
+        return node;
+    }
+    
+    private static String getWebComposite(ServletContext servletContext) {
+        InputStream stream = 
servletContext.getResourceAsStream("/WEB-INF/web.composite");
+        BufferedReader reader = new BufferedReader(new 
InputStreamReader(stream));
+
+        StringBuilder sb = new StringBuilder();
+        String s = null;
+        try {
+            while ((s = reader.readLine()) != null) {
+                sb.append(s + "\n");
+            }
+        } catch (IOException e) {
+            throw new ServiceRuntimeException(e);
+        } finally {
+            try {
+                stream.close();
+            } catch (IOException e) {
+                throw new ServiceRuntimeException(e);
+            }
+        }
+ 
+        return sb.toString();
+    }
+
+    private static String getContributionRoot(ServletContext servletContext) {
+        String contributionRoot = null;
+        try {
+
+            InitialContext ic = new InitialContext();
+            URL repoURL = (URL)ic.lookup("java:comp/env/url/contributions");
+
+            contributionRoot = repoURL.toString();
+
+        } catch (NamingException e) {
+
+            // ignore exception and use default location
+
+            try {
+                
+                String root = 
servletContext.getInitParameter("contributionRoot");
+                if (root == null || root.length() < 1) {
+                    root = "/";
+                }
+                URL rootURL = servletContext.getResource(root);
+                if (rootURL.getProtocol().equals("jndi")) {
+                    //this is Tomcat case, we should use getRealPath
+                    File warRootFile = new 
File(servletContext.getRealPath(root));
+                    contributionRoot = warRootFile.toURI().toString();
+                } else {
+                    //this is Jetty case
+                    contributionRoot = rootURL.toString();
+                }
+
+            } catch (MalformedURLException mf) {
+                //ignore, pass null
+            }
+        }
+
+        logger.info("contributionRoot: " + contributionRoot);
+        return contributionRoot;
+    }
+
+    /**
+     * Initializes the contextPath
+     * The 2.5 Servlet API has a getter for this, for pre 2.5 Servlet
+     * containers use an init parameter.
+     */
+    @SuppressWarnings("unchecked")
+    private static String initContextPath(ServletContext context) {
+        String contextPath;
+        if 
(Collections.list(context.getInitParameterNames()).contains("contextPath")) {
+            contextPath = context.getInitParameter("contextPath");
+        } else {
+            try {
+                // Try to get the method anyway since some ServletContext impl 
has this method even before 2.5
+                Method m = context.getClass().getMethod("getContextPath", new 
Class[] {});
+                contextPath = (String)m.invoke(context, new Object[] {});
+            } catch (Exception e) {
+                logger.warning("Servlet level is: " + 
context.getMajorVersion() + "." + context.getMinorVersion());
+                throw new IllegalStateException("'contextPath' init parameter 
must be set for pre-2.5 servlet container");
+            }
+        }
+        logger.info("ContextPath: " + contextPath);
+        return contextPath;
+    }
+
+    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);
+        }
+    }
+}

Copied: 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java
 (from r747774, 
tuscany/branches/sca-java-1.x/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java)
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java?p2=tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java&p1=tuscany/branches/sca-java-1.x/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java&r1=747774&r2=748050&rev=748050&view=diff
==============================================================================
--- 
tuscany/branches/sca-java-1.x/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java
 (original)
+++ 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java
 Thu Feb 26 07:29:05 2009
@@ -19,55 +19,22 @@
 
 package org.apache.tuscany.sca.host.webapp;
 
-import java.util.Enumeration;
-
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
-import javax.servlet.ServletException;
 
-import org.apache.tuscany.sca.host.embedded.SCADomain;
 
 /**
  * A ServletContextListener to create and close the SCADomain
  * when the webapp is initialized or destroyed.
- *
- * @version $Rev$ $Date$
  */
 public class TuscanyContextListener implements ServletContextListener {
 
     public void contextInitialized(ServletContextEvent event) {
-        final ServletContext servletContext = event.getServletContext();
-        try {
-            WebAppServletHost.getInstance().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 null;
-                }
-            });
-        } catch (ServletException e) {
-            throw new RuntimeException(e);
-        }
+        ServletHostHelper.init(event.getServletContext());
     }
 
     public void contextDestroyed(ServletContextEvent event) {
-        ServletContext servletContext = event.getServletContext();
-        SCADomain scaDomain = (SCADomain) 
servletContext.getAttribute(WebAppServletHost.SCA_DOMAIN_ATTRIBUTE);
-        if (scaDomain != null) {
-            scaDomain.close();
-        }
+        ServletHostHelper.stop(event.getServletContext());
     }
 
 }

Propchange: 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java
------------------------------------------------------------------------------
--- svn:mergeinfo (added)
+++ svn:mergeinfo Thu Feb 26 07:29:05 2009
@@ -0,0 +1 @@
+/tuscany/branches/sca-java-1.3/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java:671193

Modified: 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java?rev=748050&r1=748049&r2=748050&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java
 (original)
+++ 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java
 Thu Feb 26 07:29:05 2009
@@ -20,22 +20,16 @@
 package org.apache.tuscany.sca.host.webapp;
 
 import java.io.IOException;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.Map;
-import java.util.WeakHashMap;
 
 import javax.servlet.Filter;
 import javax.servlet.FilterConfig;
 import javax.servlet.RequestDispatcher;
-import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 
-import org.apache.tuscany.sca.host.http.ServletHost;
 
 /**
  * A Servlet filter that forwards service requests to the Servlets registered 
with
@@ -45,50 +39,16 @@
  */
 public class TuscanyServletFilter implements Filter {
     private static final long serialVersionUID = 1L;
-    //private static final Logger logger = 
Logger.getLogger(WebAppServletHost.class.getName());
-
-    private static Map<ClassLoader, ServletHost> servletHosts =
-        Collections.synchronizedMap(new WeakHashMap<ClassLoader, 
ServletHost>());
-
-    // [REVIEW] Assume the filter class is per webapp
-    private static WebAppServletHost servletHost;
-
-    // Test if the servletHost == null to know if the filter is called (webapp)
-    static ServletHost getServletHost() {
-        return 
servletHosts.get(Thread.currentThread().getContextClassLoader());
-    }
+    
+    private transient ServletContext context;
 
     public void init(final FilterConfig config) throws ServletException {
-        // TODO: must be a better way to get this than using a static
-        servletHost = new WebAppServletHost();
-        servletHosts.put(Thread.currentThread().getContextClassLoader(), 
servletHost);
-
-        // Initialize the Servlet host
-        servletHost.init(new ServletConfig() {
-            public String getInitParameter(String name) {
-                return config.getInitParameter(name);
-            }
-
-            public Enumeration getInitParameterNames() {
-                return config.getInitParameterNames();
-            }
-
-            public ServletContext getServletContext() {
-                return config.getServletContext();
-            }
-
-            public String getServletName() {
-                return config.getFilterName();
-            }
-        });
+        context = config.getServletContext();
+        ServletHostHelper.init(context);
     }
 
     public void destroy() {
-        if (servletHost != null) {
-            servletHost.destroy();
-            servletHost = null;
-            
servletHosts.remove(Thread.currentThread().getContextClassLoader());
-        }
+        ServletHostHelper.stop(context);
     }
 
     public void doFilter(ServletRequest request, ServletResponse response, 
javax.servlet.FilterChain chain)
@@ -105,7 +65,7 @@
         }
 
         // Get a request dispatcher for the Servlet mapped to that path
-        RequestDispatcher dispatcher = servletHost.getRequestDispatcher(path);
+        RequestDispatcher dispatcher = 
ServletHostHelper.getServletHost().getRequestDispatcher(path);
         if (dispatcher != null) {
 
             // Let the dispatcher forward the request to the Servlet 

Modified: 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java?rev=748050&r1=748049&r2=748050&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java
 (original)
+++ 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java
 Thu Feb 26 07:29:05 2009
@@ -41,7 +41,7 @@
         List<ServletHost> hosts = servletHosts.getServletHosts();
         ServletHost host = null;
         try {
-           host = TuscanyServletFilter.getServletHost();
+           host = ServletHostHelper.getServletHost();
         } catch (NoClassDefFoundError e) {
                // ignore 
         }

Modified: 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java?rev=748050&r1=748049&r2=748050&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java
 (original)
+++ 
tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java
 Thu Feb 26 07:29:05 2009
@@ -19,24 +19,15 @@
 
 package org.apache.tuscany.sca.host.webapp;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-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.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.logging.Logger;
 
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
 import javax.servlet.RequestDispatcher;
 import javax.servlet.Servlet;
 import javax.servlet.ServletConfig;
@@ -45,14 +36,12 @@
 
 import org.apache.tuscany.sca.host.http.ServletHost;
 import org.apache.tuscany.sca.host.http.ServletMappingException;
-import org.apache.tuscany.sca.node.Contribution;
 import org.apache.tuscany.sca.node.Node;
-import org.apache.tuscany.sca.node.NodeFactory;
 
 /**
  * ServletHost implementation for use in a webapp environment.
  * 
- * FIXME: using a static singleton seems a big hack but how should it be 
shared?
+ * FIXME: TUSCANY-2881: using a static singleton seems a big hack but how 
should it be shared?
  * Need some way for TuscanyServlet to pull it out.
  *
  * @version $Rev$ $Date$
@@ -65,7 +54,6 @@
     private static final WebAppServletHost instance = new WebAppServletHost();
 
     private Map<String, Servlet> servlets;
-    private Node node;
     private String contextPath = "/";
     private int defaultPortNumber = 8080;
     private String contributionRoot;
@@ -222,15 +210,8 @@
         for (String name : tempAttributes.keySet()) {
             servletContext.setAttribute(name, tempAttributes.get(name));
         }
-
-        if (servletContext.getAttribute(SCA_NODE_ATTRIBUTE) == null) {
-            initContextPath(config);
-            contributionRoot = getContributionRoot(servletContext);
-            NodeFactory factory = NodeFactory.newInstance();
-            node = factory.createNode(contextPath, 
getWebComposite(servletContext), new Contribution(contributionRoot, 
contributionRoot));
-            node.start();
-            servletContext.setAttribute(SCA_NODE_ATTRIBUTE, node);
-        }
+        
+        ServletHostHelper.init(servletContext);
 
         // Initialize the registered Servlets
         for (Servlet servlet : servlets.values()) {
@@ -239,93 +220,6 @@
         
     }
     
-    protected String getWebComposite(ServletContext servletContext) throws 
ServletException {
-        InputStream stream = 
servletContext.getResourceAsStream("/WEB-INF/web.composite");
-        BufferedReader reader = new BufferedReader(new 
InputStreamReader(stream));
-
-        StringBuilder sb = new StringBuilder();
-        String s = null;
-        try {
-            while ((s = reader.readLine()) != null) {
-                sb.append(s + "\n");
-            }
-        } catch (IOException e) {
-            throw new ServletException(e);
-        } finally {
-            try {
-                stream.close();
-            } catch (IOException e) {
-                throw new ServletException(e);
-            }
-        }
- 
-        return sb.toString();
-    }
-
-    protected String getContributionRoot(ServletContext servletContext) {
-        String contributionRoot = null;
-        try {
-
-            InitialContext ic = new InitialContext();
-            URL repoURL = (URL)ic.lookup("java:comp/env/url/contributions");
-
-            contributionRoot = repoURL.toString();
-
-        } catch (NamingException e) {
-
-            // ignore exception and use default location
-
-            try {
-                
-                String root = 
servletContext.getInitParameter("contributionRoot");
-                if (root == null || root.length() < 1) {
-                    root = "/";
-                }
-                URL rootURL = servletContext.getResource(root);
-                if (rootURL.getProtocol().equals("jndi")) {
-                    //this is Tomcat case, we should use getRealPath
-                    File warRootFile = new 
File(servletContext.getRealPath(root));
-                    contributionRoot = warRootFile.toURI().toString();
-                } else {
-                    //this is Jetty case
-                    contributionRoot = rootURL.toString();
-                }
-
-            } catch (MalformedURLException mf) {
-                //ignore, pass null
-            }
-        }
-
-        logger.info("contributionRoot: " + contributionRoot);
-        return contributionRoot;
-    }
-
-    /**
-     * Initializes the contextPath
-     * The 2.5 Servlet API has a getter for this, for pre 2.5 Servlet
-     * containers use an init parameter.
-     */
-    @SuppressWarnings("unchecked")
-    public void initContextPath(ServletConfig config) {
-        
-        if 
(Collections.list(config.getInitParameterNames()).contains("contextPath")) {
-            contextPath = config.getInitParameter("contextPath");
-        } else {
-            // The getContextPath() is introduced since Servlet 2.5
-            ServletContext context = config.getServletContext();
-            try {
-                // Try to get the method anyway since some ServletContext impl 
has this method even before 2.5
-                Method m = context.getClass().getMethod("getContextPath", new 
Class[] {});
-                contextPath = (String)m.invoke(context, new Object[] {});
-            } catch (Exception e) {
-                logger.warning("Servlet level is: " + 
context.getMajorVersion() + "." + context.getMinorVersion());
-                throw new IllegalStateException("'contextPath' init parameter 
must be set for pre-2.5 servlet container");
-            }
-        }
-
-        logger.info("ContextPath: " + contextPath);
-    }
-
     void destroy() {
 
         // Destroy the registered Servlets
@@ -334,9 +228,7 @@
         }
 
         // Close the SCA domain
-        if (node != null) {
-            node.stop();
-        }
+        ServletHostHelper.stop(servletContext);
     }
 
     public String getContextPath() {


Reply via email to