From a76f2c08ce76d75096b875d25e55deda9c9a8644 Mon Sep 17 00:00:00 2001 From: antelder Date: Mon, 25 May 2009 07:17:54 +0000 Subject: Add module for Tomcat deep integration git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@778316 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/tomcat/TuscanyLifecycleListener.java | 146 +++++++++++++++++++++ .../tuscany/sca/tomcat/TuscanyStandardContext.java | 84 ++++++++++++ 2 files changed, 230 insertions(+) create mode 100644 java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java create mode 100644 java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java (limited to 'java/sca/distribution/tomcat/tomcat-hook/src') 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 new file mode 100644 index 0000000000..9533c43acb --- /dev/null +++ b/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java @@ -0,0 +1,146 @@ +/* + * 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.tomcat; + +import java.io.File; + +import org.apache.catalina.Container; +import org.apache.catalina.LifecycleEvent; +import org.apache.catalina.LifecycleListener; +import org.apache.catalina.ServerFactory; +import org.apache.catalina.Service; +import org.apache.catalina.connector.Connector; +import org.apache.catalina.core.StandardEngine; +import org.apache.catalina.core.StandardHost; +import org.apache.catalina.core.StandardServer; +import org.apache.catalina.core.StandardService; +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 + * + */ +public class TuscanyLifecycleListener implements LifecycleListener { +// private static transient Log log = LogFactory.getLog(StandardContext.class); + + public static final String TUSCANY_WAR_PROP = "org.apache.tuscany.sca.tomcat.war"; + + private static boolean running; + public static boolean isRunning() { + return running; + } + + public TuscanyLifecycleListener() { + running = true; + } + + public void lifecycleEvent(LifecycleEvent event) { + if ("init".equals(event.getType()) && (event.getSource() instanceof StandardServer)) { + File webappDir = findTuscanyWar(); + if (webappDir == null) { +// log.warn("Tuscany disabled as Tuscany webapp not found"); + } else { + System.setProperty(TUSCANY_WAR_PROP, webappDir.getAbsolutePath()); +// 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()) { + 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("TuscanyStandardContext set for connector: " + container.getName() + ":" + connector.getPort()); + } + } + } + } + } + } + } + } + + private static File findTuscanyWar() { + + // in Tomcat 5.5 the Tuscany war is in the server/webapps director + String catalinaBase = System.getProperty("catalina.base"); + File serverWebapps = new File(catalinaBase, "server/webapps"); + File tuscanyWar = findTuscanyWar(serverWebapps); + if (tuscanyWar != null) { + return tuscanyWar; + } + + // in Tomcat 6 the Tuscany war is normally in webapps, but we just scan all hosts directories + for (Service service : ServerFactory.getServer().findServices()) { + Container container = service.getContainer(); + if (container instanceof StandardEngine) { + StandardEngine engine = (StandardEngine) container; + for (Container child : engine.findChildren()) { + if (child instanceof StandardHost) { + StandardHost host = (StandardHost) child; + String appBase = host.getAppBase(); + + // determine the host dir (normally webapps) + File hostDir = new File(appBase); + if (!hostDir.isAbsolute()) { + hostDir = new File(catalinaBase, appBase); + } + + tuscanyWar = findTuscanyWar(hostDir); + if (tuscanyWar != null) { + return tuscanyWar; + } + } + } + } + } + return null; + } + + private static File findTuscanyWar(File hostDir) { + if (!hostDir.isDirectory()) { + return null; + } + + // iterate over the contexts + for (File contextDir : hostDir.listFiles()) { + // does this war have a web-inf lib dir + File hookLib = new File(contextDir, "server-lib"); + if (!hookLib.isDirectory()) { + continue; + } + // iterate over the libs looking for the tuscany-tomcat-*.jar + for (File file : hookLib.listFiles()) { + if (file.getName().startsWith("tuscany-tomcat-hook-") && file.getName().endsWith(".jar")) { + // this should be in the Tuscany war... + // make sure it has a runtime directory + if (new File(contextDir, "tuscany-lib").isDirectory()) { + return contextDir; + } + } + } + } + return null; + } +} 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 new file mode 100644 index 0000000000..0d6404fff9 --- /dev/null +++ b/java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java @@ -0,0 +1,84 @@ +/* + * 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.tomcat; + +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; + +import javax.naming.NamingException; + +import org.apache.catalina.Loader; +import org.apache.catalina.core.StandardContext; +import org.apache.catalina.deploy.FilterDef; + +public class TuscanyStandardContext extends StandardContext { + private static final long serialVersionUID = 1L; + + private ClassLoader tuscanyClassLoader; + + public Loader getLoader() { + if (loader != null) { + return loader; + } + + initTuscany(); + + return super.getLoader(); + } + + private void initTuscany() { + String scaVersion = getSCAVersion(); + if ("1.1".equals(scaVersion)) { + setParentClassLoader(getTuscanyClassloader(scaVersion)); + addApplicationListener("org.apache.tuscany.sca.host.webapp.TuscanyContextListener"); + FilterDef filterDef = new FilterDef(); + filterDef.setFilterName("TuscanyFilter"); + filterDef.setFilterClass("org.apache.tuscany.sca.host.webapp.TuscanyServletFilter"); + addFilterDef(filterDef); + } + } + + private String getSCAVersion() { + Object o = null; + try { + o = getResources().lookup("WEB-INF/web.composite"); + } catch (NamingException e) { + } + return o != null ? "1.1" : null; + } + + private ClassLoader getTuscanyClassloader(String version) { + if (tuscanyClassLoader == null) { + File tuscanyWar = new File(System.getProperty("org.apache.tuscany.sca.tomcat.war")); + File[] runtimeJars = new File(tuscanyWar, "tuscany-lib").listFiles(); + try { + URL[] jarURLs = new URL[runtimeJars.length]; + for (int i=0; i< jarURLs.length; i++) { + jarURLs[i] = runtimeJars[i].toURI().toURL(); + } + return new URLClassLoader(jarURLs, getParentClassLoader()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + return tuscanyClassLoader; + } +} -- cgit v1.2.3