Author: rfeng
Date: Thu May 28 21:24:41 2009
New Revision: 779778

URL: http://svn.apache.org/viewvc?rev=779778&view=rev
Log:
Add a checkbox on the jsp to configure if Tuscany is shared or isolated by the 
webapps

Modified:
    
tuscany/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java
    
tuscany/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java
    
tuscany/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/Installer.java
    
tuscany/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java
    
tuscany/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/installer.jsp

Modified: 
tuscany/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java?rev=779778&r1=779777&r2=779778&view=diff
==============================================================================
--- 
tuscany/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java
 (original)
+++ 
tuscany/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java
 Thu May 28 21:24:41 2009
@@ -6,20 +6,23 @@
  * 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.tomcat;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Properties;
 import java.util.logging.Logger;
 
 import org.apache.catalina.Container;
@@ -38,7 +41,7 @@
  * A Tomcat LifecycleListener that initilizes the Tuscany Tomcat integration.
  * It sets a System property with the location of the Tuscany runtime .war
  * and configures each Tomcat Connector to use the TuscanyStandardContext.
- * 
+ *
  * To configure Tomcat to use this add the following to the Tomcat 
conf/server.xml
  *   <Listener 
className="org.apache.tuscany.sca.tomcat.TuscanyLifecycleListener"/>
  */
@@ -48,15 +51,18 @@
     public static final String TUSCANY_WAR_PROP = 
"org.apache.tuscany.sca.tomcat.war";
 
     private static boolean running;
+
     public static boolean isRunning() {
         return running;
     }
-    
+
+    static final String TUSCANY_SHARED_PROP = 
"org.apache.tuscany.sca.tomcat.shared";
+
     public TuscanyLifecycleListener() {
         running = true;
         log.info("Apache Tuscany initilizing");
     }
-    
+
     public void lifecycleEvent(LifecycleEvent event) {
         if ("init".equals(event.getType()) && (event.getSource() instanceof 
StandardServer)) {
             File webappDir = findTuscanyWar();
@@ -64,18 +70,33 @@
                 log.severe("Tuscany disabled as Tuscany webapp not found");
             } else {
                 System.setProperty(TUSCANY_WAR_PROP, 
webappDir.getAbsolutePath());
+                File propFile = new File(webappDir, "tuscany.properties");
+                if (propFile.isFile()) {
+                    try {
+                        FileInputStream is = new FileInputStream(propFile);
+                        Properties props = new Properties();
+                        props.load(is);
+                        is.close();
+                        System.setProperty(TUSCANY_SHARED_PROP, 
props.getProperty("singleton", "false"));
+                    } catch (IOException e) {
+                        // Ignore
+                    }
+                }
                 log.info("Using Tuscany webapp: " + 
webappDir.getAbsolutePath());
                 StandardServer server = (StandardServer)event.getSource();
                 StandardService catalina = 
(StandardService)server.findService("Catalina");
                 for (Connector connector : catalina.findConnectors()) {
-                    for (Container container: 
connector.getContainer().findChildren()) {
+                    for (Container container : 
connector.getContainer().findChildren()) {
                         if (container instanceof StandardHost) {
-                           for (LifecycleListener listener : 
((StandardHost)container).findLifecycleListeners()) {
-                               if (listener instanceof HostConfig) {
-                                   
((HostConfig)listener).setContextClass("org.apache.tuscany.sca.tomcat.TuscanyStandardContext");
-                                   log.info("Tuscany enabled on connector: " + 
container.getName() + ":" + connector.getPort());
-                               }
-                           }
+                            for (LifecycleListener listener : 
((StandardHost)container).findLifecycleListeners()) {
+                                if (listener instanceof HostConfig) {
+                                    ((HostConfig)listener)
+                                        
.setContextClass("org.apache.tuscany.sca.tomcat.TuscanyStandardContext");
+                                    log.info("Tuscany enabled on connector: " 
+ container.getName()
+                                        + ":"
+                                        + connector.getPort());
+                                }
+                            }
                         }
                     }
                 }
@@ -97,10 +118,10 @@
         for (Service service : ServerFactory.getServer().findServices()) {
             Container container = service.getContainer();
             if (container instanceof StandardEngine) {
-                StandardEngine engine = (StandardEngine) container;
+                StandardEngine engine = (StandardEngine)container;
                 for (Container child : engine.findChildren()) {
                     if (child instanceof StandardHost) {
-                        StandardHost host = (StandardHost) child;
+                        StandardHost host = (StandardHost)child;
                         String appBase = host.getAppBase();
 
                         // determine the host dir (normally webapps)

Modified: 
tuscany/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java?rev=779778&r1=779777&r2=779778&view=diff
==============================================================================
--- 
tuscany/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java
 (original)
+++ 
tuscany/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java
 Thu May 28 21:24:41 2009
@@ -41,12 +41,15 @@
 public class TuscanyStandardContext extends StandardContext {
     protected static final String TUSCANY_FILTER_NAME = "TuscanyFilter";
     protected static final String TUSCANY_SERVLET_FILTER = 
"org.apache.tuscany.sca.host.webapp.TuscanyServletFilter";
-    protected static final String TUSCANY_CONTEXT_LISTENER = 
"org.apache.tuscany.sca.host.webapp.TuscanyContextListener";
+    protected static final String TUSCANY_CONTEXT_LISTENER =
+        "org.apache.tuscany.sca.host.webapp.TuscanyContextListener";
     private static final long serialVersionUID = 1L;
     private static final Logger log = 
Logger.getLogger(TuscanyStandardContext.class.getName());
 
+    private boolean isSCAApp;
+
     // TODO: this gives an instance per connector, work out how to have only 
one per server
-    private ClassLoader tuscanyClassLoader;
+    private static URLClassLoader tuscanyClassLoader;
 
     /**
      * Overrides the getLoader method in the Tomcat StandardContext as its a 
convenient
@@ -61,8 +64,16 @@
             return loader;
         }
 
-        if(isSCAApplication()) {
-            setParentClassLoader(getTuscanyClassloader());
+        ClassLoader parent = getParentClassLoader();
+        if (isSCAApp = isSCAApplication()) {
+            String sharedProp = 
System.getProperty(TuscanyLifecycleListener.TUSCANY_SHARED_PROP, "false");
+            boolean shared = "true".equalsIgnoreCase(sharedProp);
+            if (!shared) {
+                setParentClassLoader(copy(getTuscanyClassloader(parent)));
+            } else {
+                // The default parent classloader is the one for the webapp
+                setParentClassLoader(getTuscanyClassloader(parent));
+            }
         }
 
         return super.getLoader();
@@ -70,7 +81,7 @@
 
     @Override
     public boolean listenerStart() {
-        if (tuscanyClassLoader != null) {
+        if (isSCAApp) {
             enableTuscany();
         }
         return super.listenerStart();
@@ -78,15 +89,15 @@
 
     private void enableTuscany() {
 
-        for(String listener: findApplicationListeners()) {
-            if(TUSCANY_CONTEXT_LISTENER.equals(listener)) {
+        for (String listener : findApplicationListeners()) {
+            if (TUSCANY_CONTEXT_LISTENER.equals(listener)) {
                 // The web application already has the context listener 
configured
                 return;
             }
         }
 
-        for(FilterDef filterDef: findFilterDefs()) {
-            if(TUSCANY_SERVLET_FILTER.equals(filterDef.getFilterClass())) {
+        for (FilterDef filterDef : findFilterDefs()) {
+            if (TUSCANY_SERVLET_FILTER.equals(filterDef.getFilterClass())) {
                 // The web application already has the filter configured
                 return;
             }
@@ -120,7 +131,7 @@
             } catch (NamingException e) {
             }
         }
-        if(o==null) {
+        if (o == null) {
             return false;
         }
 
@@ -140,16 +151,20 @@
         return true;
     }
 
-    private ClassLoader getTuscanyClassloader() {
+    private static URLClassLoader copy(URLClassLoader classLoader) {
+        return new URLClassLoader(classLoader.getURLs(), 
classLoader.getParent());
+    }
+
+    private synchronized URLClassLoader getTuscanyClassloader(ClassLoader 
parent) {
         if (tuscanyClassLoader == null) {
-            File tuscanyWar = new 
File(System.getProperty("org.apache.tuscany.sca.tomcat.war"));
+            File tuscanyWar = new 
File(System.getProperty(TuscanyLifecycleListener.TUSCANY_WAR_PROP));
             File[] runtimeJars = new File(tuscanyWar, 
"tuscany-lib").listFiles();
             try {
                 URL[] jarURLs = new URL[runtimeJars.length];
-                for (int i=0; i< jarURLs.length; i++) {
+                for (int i = 0; i < jarURLs.length; i++) {
                     jarURLs[i] = runtimeJars[i].toURI().toURL();
                 }
-                tuscanyClassLoader =  new URLClassLoader(jarURLs, 
getParentClassLoader());
+                tuscanyClassLoader = new URLClassLoader(jarURLs, parent);
                 return tuscanyClassLoader;
             } catch (Exception e) {
                 throw new RuntimeException(e);
@@ -157,4 +172,5 @@
         }
         return tuscanyClassLoader;
     }
+
 }

Modified: 
tuscany/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/Installer.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/Installer.java?rev=779778&r1=779777&r2=779778&view=diff
==============================================================================
--- 
tuscany/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/Installer.java
 (original)
+++ 
tuscany/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/Installer.java
 Thu May 28 21:24:41 2009
@@ -6,15 +6,15 @@
  * 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.war;
@@ -29,13 +29,14 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintWriter;
+import java.util.Properties;
 
 import org.apache.tuscany.sca.tomcat.TuscanyLifecycleListener;
 import org.codehaus.swizzle.stream.DelimitedTokenReplacementInputStream;
 import org.codehaus.swizzle.stream.StringTokenHandler;
 
 public class Installer {
-    
+
     private static boolean restartRequired;
     private static boolean tuscanyHookRunning;
     static {
@@ -45,7 +46,7 @@
             tuscanyHookRunning = false;
         }
     }
-    
+
     public static boolean isTuscanyHookRunning() {
         return tuscanyHookRunning;
     }
@@ -66,15 +67,15 @@
     public static boolean isInstalled() {
         return false;
     }
-    
+
     public String getStatus() {
         return status;
     }
 
-    public boolean install() {
+    public boolean install(boolean singleton) {
         try {
 
-            doInstall();
+            doInstall(singleton);
             status = "Install successful, Tomcat restart required.";
             restartRequired = true;
             return true;
@@ -92,9 +93,10 @@
 
     public boolean uninstall() {
         try {
-            
-            doUnintsall();            
-            status = "Tuscany removed from server.xml, please restart Tomcat 
and manually remove Tuscany jars from Tomcat lib";
+
+            doUnintsall();
+            status =
+                "Tuscany removed from server.xml, please restart Tomcat and 
manually remove Tuscany jars from Tomcat lib";
             restartRequired = true;
             return true;
 
@@ -114,9 +116,14 @@
             throw new IllegalStateException("conf/server.xml not found: " + 
serverXml.getAbsolutePath());
         }
         removeServerXml(serverXml);
+        File propFile = new File(tuscanyWAR, "tuscany.properties");
+        if (propFile.isFile()) {
+            propFile.delete();
+        }
+
     }
-    
-    private boolean doInstall() {
+
+    private boolean doInstall(boolean singleton) {
         // First verify all the file locations are as expected
         if (!tuscanyWAR.exists()) {
             throw new IllegalStateException("Tuscany war missing: " + 
tuscanyWAR.getAbsolutePath());
@@ -129,7 +136,7 @@
             // try Tomcat 5 server/lib
             if (new File(catalinaBase, "/server").exists()) {
                 serverLib = new File(new File(catalinaBase, "/server"), 
"/lib");
-            } 
+            }
         }
         if (!(serverLib.exists())) {
             throw new IllegalStateException("Tomcat lib not found: " + 
serverLib.getAbsolutePath());
@@ -139,17 +146,31 @@
             throw new IllegalStateException("conf/server.xml not found: " + 
serverXml.getAbsolutePath());
         }
 
-        File tuscanyTomcatJar = findTuscanyTomcatJar(tuscanyWAR);        
+        File tuscanyTomcatJar = findTuscanyTomcatJar(tuscanyWAR);
         if (tuscanyTomcatJar == null || !tuscanyTomcatJar.exists()) {
             throw new IllegalStateException("Can't find tuscany-tomcat-*.jar 
in: " + tuscanyWAR.getAbsolutePath());
         }
 
-        // Copy tuscany-tomcat jar from the tuscany webapp web-inf/lib to 
Tomcat server/lib 
+        // Copy tuscany-tomcat jar from the tuscany webapp web-inf/lib to 
Tomcat server/lib
         copyFile(tuscanyTomcatJar, new File(serverLib, 
tuscanyTomcatJar.getName()));
 
+        if (singleton) {
+            try {
+                // Write out a property file
+                File propFile = new File(tuscanyWAR, "tuscany.properties");
+                FileOutputStream os = new FileOutputStream(propFile);
+                Properties props = new Properties();
+                props.put("singleton", "true");
+                props.store(os, "Apache Tuscany properties for Tomcat");
+                os.close();
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+        }
+
         // Add Tuscany LifecycleListener to Tomcat server.xml
         updateServerXml(serverXml);
-        
+
         return true;
     }
 
@@ -163,35 +184,27 @@
         return null;
     }
 
-    static final String tuscanyListener = "\r\n" + "  <!-- Tuscany plugin for 
Tomcat -->\r\n" + "<Listener 
className=\"org.apache.tuscany.sca.tomcat.TuscanyLifecycleListener\" />";
+    static final String tuscanyListener =
+        "\r\n" + "  <!-- Tuscany plugin for Tomcat -->\r\n"
+            + "<Listener 
className=\"org.apache.tuscany.sca.tomcat.TuscanyLifecycleListener\" />";
 
     private void updateServerXml(File serverXmlFile) {
         String serverXML = readAll(serverXmlFile);
         if (!serverXML.contains(tuscanyListener)) {
-            String newServerXml = replace(
-               serverXML,
-               "<Server",
-               "<Server",
-               ">",
-               ">" + tuscanyListener);
+            String newServerXml = replace(serverXML, "<Server", "<Server", 
">", ">" + tuscanyListener);
             backup(serverXmlFile);
             writeAll(serverXmlFile, newServerXml);
         }
-        
+
     }
 
     private void removeServerXml(File serverXmlFile) {
         String serverXML = readAll(serverXmlFile);
         if (serverXML.contains(tuscanyListener)) {
-            String newServerXml = replace(
-               serverXML,
-               "<Server",
-               "<Server",
-               ">" + tuscanyListener,
-               ">");
+            String newServerXml = replace(serverXML, "<Server", "<Server", ">" 
+ tuscanyListener, ">");
             writeAll(serverXmlFile, newServerXml);
         }
-        
+
     }
 
     private String replace(String inputText, String begin, String newBegin, 
String end, String newEnd) {
@@ -225,13 +238,14 @@
             close(in);
         }
     }
+
     private String readAll(InputStream in) {
         try {
             // SwizzleStream block read methods are broken so read byte at a 
time
             StringBuilder sb = new StringBuilder();
             int i = in.read();
             while (i != -1) {
-                sb.append((char) i);
+                sb.append((char)i);
                 i = in.read();
             }
             return sb.toString();
@@ -276,7 +290,7 @@
         }
         out.flush();
     }
-    
+
     private void close(Closeable thing) {
         if (thing != null) {
             try {

Modified: 
tuscany/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java?rev=779778&r1=779777&r2=779778&view=diff
==============================================================================
--- 
tuscany/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java
 (original)
+++ 
tuscany/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java
 Thu May 28 21:24:41 2009
@@ -6,15 +6,15 @@
  * 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.war;
@@ -56,10 +56,11 @@
     protected void doIt(HttpServletRequest req, HttpServletResponse res) 
throws ServletException, IOException {
 
         if ("Install".equalsIgnoreCase(req.getParameter("action"))) {
-            installer.install();
+            String singleton = req.getParameter("singleton");
+            installer.install(singleton!=null && 
singleton.equalsIgnoreCase("true"));
         } else if ("Uninstall".equalsIgnoreCase(req.getParameter("action"))) {
             installer.uninstall();
-        } 
+        }
 
         req.setAttribute("installer", installer);
         RequestDispatcher rd = 
servletConfig.getServletContext().getRequestDispatcher("/installer.jsp");

Modified: 
tuscany/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/installer.jsp
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/installer.jsp?rev=779778&r1=779777&r2=779778&view=diff
==============================================================================
--- 
tuscany/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/installer.jsp 
(original)
+++ 
tuscany/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/installer.jsp 
Thu May 28 21:24:41 2009
@@ -48,6 +48,7 @@
        <B>Install Tuscany</B><BR>
        To install Tuscany into Tomcat, click:
                 <form action='installer' method='post'>
+                <input type='checkbox' name='singleton' value='true'>The 
Tuscany runtime is shared by all web applications if checked.<p>
                 <input type='submit' name='action' value='Install'>
                 </form>
        <BR>


Reply via email to