Author: antelder
Date: Thu May  8 03:26:21 2008
New Revision: 654467

URL: http://svn.apache.org/viewvc?rev=654467&view=rev
Log:
Update the tomcat deep integration to work with the latest uscany code

Modified:
    
incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java

Modified: 
incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java?rev=654467&r1=654466&r2=654467&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java
 Thu May  8 03:26:21 2008
@@ -20,25 +20,22 @@
 package org.apache.tuscany.sca.runtime.tomcat;
 
 import java.io.File;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
+import java.io.IOException;
+import java.util.jar.JarFile;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import org.apache.catalina.Container;
 import org.apache.catalina.LifecycleException;
-import org.apache.catalina.Wrapper;
 import org.apache.catalina.core.StandardContext;
+import org.apache.catalina.core.StandardEngine;
 import org.apache.catalina.core.StandardHost;
-import org.apache.tuscany.sca.runtime.Launcher;
+import org.apache.catalina.deploy.FilterDef;
+import org.apache.catalina.deploy.FilterMap;
 
 /**
  * To use this copy all the Tuscany jars to the Tomcat lib folder and update
- * the Tomcat conf/server.xml <Host> to include 
className="org.apache.tuscany.sca.runtime.tomcat.TomcatHost"
+ * the Tomcat conf/server.xml <Host> to include 
className="org.apache.tuscany.sca.runtime.tomcat.TomcatHost2"
  * 
  * For example: 
  * 
@@ -50,89 +47,99 @@
  */
 public class TuscanyHost extends StandardHost {
     private static final long serialVersionUID = 1L;
+    private final static Logger logger = 
Logger.getLogger(TuscanyHost.class.getName());
 
-    private static final String REPO = "../sca-contributions";
-
-    protected Launcher launcher;
-
-    private String contextPath = "/tuscany";
-    
     public synchronized void start() throws LifecycleException {
         try {
-
-            launcher = initTuscany();
+            logger.log(Level.INFO, "Starting Tuscany/SCA runtime");
 
             super.start();
             
-            launcher.start();
-
         } catch (Exception e) {
-            e.printStackTrace();
+            logger.log(Level.SEVERE, "Exception starting Tuscany/SCA runtime");
         }
     }
 
     public synchronized void stop() throws LifecycleException {
-        super.stop();
-        stopRuntime();
-    }
-
-    private Launcher initTuscany() throws ServletException {
-        StandardContext tc = new TuscanyContext();
-        tc.setPath(contextPath);
-        super.addChild(tc);
-        
-        TuscanyServlet s = new TuscanyServlet();
-        s.init(new MockServletConfig(contextPath));
-        Wrapper wrapper = new TuscanyWrapper(s);
-        wrapper.setName("TuscanyServlet");
-        tc.addChild(wrapper);
-        tc.addServletMapping("/*", "TuscanyServlet", true);
-
-        Launcher launcher = new Launcher(new File(REPO));
+        try {
+            logger.log(Level.INFO, "Stopping Tuscany/SCA runtime");
 
-        return launcher;
-    }
+            super.stop();
 
-    private void stopRuntime() {
-        System.out.println("XXXXXXXX TomcatHost.stopRuntime");
-        if (launcher != null) {
-            launcher.stop();
+        } catch (Exception e) {
+            logger.log(Level.SEVERE, "Exception Stopping Tuscany/SCA runtime");
         }
     }
 
     public synchronized void addChild(Container child) {
-        if (!(child instanceof StandardContext)) {
-            throw new 
IllegalArgumentException(sm.getString("tuscanyHost.notContext"));
+        try {
+            if (isSCAApp(child)) {
+                initSCAApplication((StandardContext)child);
+            }
+        } catch (Exception e) {
+            logger.log(Level.WARNING, "Exception detecting SCA application " + 
child.getName(), e);
         }
         super.addChild(child);
     }
 
-}
-
-class MockServletConfig implements ServletConfig {
-
-    Map<String, String> initParams;
-    
-    public MockServletConfig(String contextPath) {
-        initParams = new HashMap<String, String>();
-        initParams.put("contextPath", contextPath);
-    }
-
-    public String getInitParameter(String initParam) {
-        return initParams.get(initParam);
-    }
-
-    @SuppressWarnings("unchecked")
-    public Enumeration getInitParameterNames() {
-        return Collections.enumeration(initParams.keySet());
+    /**
+     * Tests if the child is an SCA application by checking for the presence
+     * of either an sca-contribution.xml file or a sca-deployables folder
+     */
+    protected boolean isSCAApp(Container child) throws IOException {
+        if (child instanceof StandardContext) {
+            StandardContext sc = (StandardContext) child;
+            
+            if (sc.getDocBase().endsWith(".war")) {
+                JarFile jar = null;
+                try {
+                    jar = new 
JarFile(((StandardEngine)this.getParent()).getBaseDir() + "/" + getAppBase() + 
"/" + sc.getDocBase());
+
+                    if (jar.getEntry("META-INF/sca-deployables/") != null) {
+                        return true;
+                    }
+
+                    if (jar.getEntry("META-INF/sca-contribution.xml") != null) 
{
+                        return true;
+                    }
+
+                } finally {
+                    if (jar != null) {
+                        jar.close();
+                    }
+                }
+            } else {
+                File webappRoot = new 
File(sc.getConfigFile()).getParentFile().getParentFile();
+
+                File scaDeployables = new File(webappRoot, 
"META-INF/sca-deployables");
+                if (scaDeployables.exists()) {
+                    return true;
+                }
+
+                File scaContribution = new File(webappRoot, 
"META-INF/sca-contribution.xml");
+                if (scaContribution.exists()) {
+                    return true;
+                }
+            }
+        }
+        return false;
     }
 
-    public ServletContext getServletContext() {
-        return null;
+    protected void initSCAApplication(StandardContext scaApp) {
+        logger.log(Level.INFO, "Initilizing SCA application: " + 
scaApp.getName());
+        
+        // Add the Tuscany ContextListener
+        
scaApp.addApplicationListener(org.apache.tuscany.sca.host.webapp.TuscanyContextListener.class.getName());
+        
+        // Add the Tuscany Servlet Filter
+        FilterDef filterDef = new FilterDef();
+        
filterDef.setFilterClass(org.apache.tuscany.sca.host.webapp.TuscanyServletFilter.class.getName());
+        
filterDef.setFilterName(org.apache.tuscany.sca.host.webapp.TuscanyServletFilter.class.getName());
+        scaApp.addFilterDef(filterDef);
+        FilterMap filterMap = new FilterMap();
+        filterMap.setFilterName(filterDef.getFilterName());
+        filterMap.addURLPattern("/*");
+        scaApp.addFilterMap(filterMap);
     }
 
-    public String getServletName() {
-        return null;
-    }
-    
 }


Reply via email to