diff options
Diffstat (limited to 'java/sca/distribution/tomcat/tomcat-hook')
2 files changed, 66 insertions, 29 deletions
diff --git a/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java b/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java index ff5d9f2f47..05c77f3396 100644 --- a/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java +++ b/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java @@ -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 @@ import org.apache.catalina.startup.HostConfig; * 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 class TuscanyLifecycleListener implements LifecycleListener { 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 @@ public class TuscanyLifecycleListener implements LifecycleListener { 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 @@ public class TuscanyLifecycleListener implements LifecycleListener { 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)
diff --git a/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java b/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java index dbfe397a82..14b9e65e2b 100644 --- a/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java +++ b/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java @@ -41,12 +41,15 @@ import org.apache.catalina.deploy.FilterDef; 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 @@ public class TuscanyStandardContext extends StandardContext { 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 @@ public class TuscanyStandardContext extends StandardContext { @Override
public boolean listenerStart() {
- if (tuscanyClassLoader != null) {
+ if (isSCAApp) {
enableTuscany();
}
return super.listenerStart();
@@ -78,15 +89,15 @@ public class TuscanyStandardContext extends StandardContext { 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 @@ public class TuscanyStandardContext extends StandardContext { } catch (NamingException e) {
}
}
- if(o==null) {
+ if (o == null) {
return false;
}
@@ -140,16 +151,20 @@ public class TuscanyStandardContext extends StandardContext { 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 @@ public class TuscanyStandardContext extends StandardContext { }
return tuscanyClassLoader;
}
+
}
|