summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca
diff options
context:
space:
mode:
Diffstat (limited to 'sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca')
-rw-r--r--sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyAnnotationsProcessor.java85
-rw-r--r--sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyContextConfig.java183
-rw-r--r--sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyHostConfig.java167
-rw-r--r--sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java161
-rw-r--r--sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java196
5 files changed, 0 insertions, 792 deletions
diff --git a/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyAnnotationsProcessor.java b/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyAnnotationsProcessor.java
deleted file mode 100644
index 54947d6492..0000000000
--- a/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyAnnotationsProcessor.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * 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.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-
-import javax.naming.NamingException;
-import javax.servlet.ServletContext;
-
-import org.apache.AnnotationProcessor;
-import org.apache.catalina.util.DefaultAnnotationProcessor;
-
-public class TuscanyAnnotationsProcessor implements AnnotationProcessor {
-
- private TuscanyStandardContext tuscanyStandardContext;
- private AnnotationProcessor tomcatAnnotationProcessor;
- private Method tuscanyInjectMethod;
-
- public TuscanyAnnotationsProcessor(TuscanyStandardContext tuscanyStandardContext, javax.naming.Context context) {
- this.tuscanyStandardContext = tuscanyStandardContext;
- this.tomcatAnnotationProcessor = new DefaultAnnotationProcessor(context);
- initInjectMethod(tuscanyStandardContext);
- }
-
- private void initInjectMethod(TuscanyStandardContext tuscanyStandardContext) {
-
- // this needs to use reflection as the tuscany-hook module can't have any
- // dependencies on the tuscany runtime modules as they're not
- // in the server classpath
- // TODO: is there a nicer way ?
-
- ClassLoader cl = tuscanyStandardContext.getParentClassLoader();
- try {
- Class<?> c = Class.forName("org.apache.tuscany.sca.implementation.web.runtime.utils.ContextHelper", true, cl);
- if (c != null) {
- this.tuscanyInjectMethod = c.getMethod("inject", new Class[]{Object.class, ServletContext.class});
- }
- } catch (Exception e){
- // ignore
- }
- }
-
- public void postConstruct(Object instance) throws IllegalAccessException, InvocationTargetException {
- tomcatAnnotationProcessor.postConstruct(instance);
- }
-
- public void preDestroy(Object instance) throws IllegalAccessException, InvocationTargetException {
- tomcatAnnotationProcessor.preDestroy(instance);
- }
-
- public void processAnnotations(Object instance) throws IllegalAccessException, InvocationTargetException, NamingException {
- if (tuscanyInjectMethod != null) {
- ServletContext sc = tuscanyStandardContext.getServletContext();
- if (sc != null) {
- Object rc = sc.getAttribute("org.apache.tuscany.sca.implementation.web.RuntimeComponent");
- if (rc != null) {
- try {
- tuscanyInjectMethod.invoke(null, instance, sc);
- } catch (Exception e) {
- throw new InvocationTargetException(e);
- }
- }
- }
- }
- tomcatAnnotationProcessor.processAnnotations(instance);
- }
-}
diff --git a/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyContextConfig.java b/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyContextConfig.java
deleted file mode 100644
index 5e7905d246..0000000000
--- a/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyContextConfig.java
+++ /dev/null
@@ -1,183 +0,0 @@
-/*
- * 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.io.IOException;
-import java.net.URL;
-
-import org.apache.catalina.Host;
-import org.apache.catalina.core.StandardContext;
-import org.apache.catalina.core.StandardHost;
-import org.apache.catalina.startup.ContextConfig;
-import org.apache.catalina.startup.ExpandWar;
-
-public class TuscanyContextConfig extends ContextConfig{
-
- /**
- * Return the location of the default deployment descriptor
- *
- * Override the super class method to use the Tuscany specific
- * default web.xml which has the Tuscany listener and filer config
- */
- @Override
- public String getDefaultWebXml() {
- if( defaultWebXml == null ) {
- defaultWebXml="conf/tuscany-web.xml";
- }
- return (this.defaultWebXml);
- }
-
- /**
- * Adjust docBase.
- *
- * This is cutNpaste of the Tomcat method but changed on the lines marked with // TUSCANY:
- * to override the default that only works for file names ending with .war
- *
- */
- @Override
- protected void fixDocBase()
- throws IOException {
-
- Host host = (Host) context.getParent();
- String appBase = host.getAppBase();
-
- boolean unpackWARs = true;
- if (host instanceof StandardHost) {
- unpackWARs = ((StandardHost) host).isUnpackWARs()
- && ((StandardContext) context).getUnpackWAR();
- }
-
- File canonicalAppBase = new File(appBase);
- if (canonicalAppBase.isAbsolute()) {
- canonicalAppBase = canonicalAppBase.getCanonicalFile();
- } else {
- canonicalAppBase =
- new File(System.getProperty("catalina.base"), appBase)
- .getCanonicalFile();
- }
-
- String docBase = context.getDocBase();
- if (docBase == null) {
- // Trying to guess the docBase according to the path
- String path = context.getPath();
- if (path == null) {
- return;
- }
- if (path.equals("")) {
- docBase = "ROOT";
- } else {
- if (path.startsWith("/")) {
- docBase = path.substring(1);
- } else {
- docBase = path;
- }
- }
- }
-
- File file = new File(docBase);
- if (!file.isAbsolute()) {
- docBase = (new File(canonicalAppBase, docBase)).getPath();
- } else {
- docBase = file.getCanonicalPath();
- }
- file = new File(docBase);
- String origDocBase = docBase;
-
- String contextPath = context.getPath();
- if (contextPath.equals("")) {
- contextPath = "ROOT";
- } else {
- if (contextPath.lastIndexOf('/') > 0) {
- contextPath = "/" + contextPath.substring(1).replace('/','#');
- }
- }
- // TUSCANY: update from .war to also support .jar and .zip SCA contributions
- if ((docBase.toLowerCase().endsWith(".war") || docBase.toLowerCase().endsWith(".jar")||docBase.toLowerCase().endsWith(".zip")) && !file.isDirectory() && unpackWARs) {
- URL war = new URL("jar:" + (new File(docBase)).toURI().toURL() + "!/");
- docBase = ExpandWar.expand(host, war, contextPath);
- file = new File(docBase);
- docBase = file.getCanonicalPath();
- if (context instanceof StandardContext) {
- ((StandardContext) context).setOriginalDocBase(origDocBase);
- }
- } else {
- File docDir = new File(docBase);
- if (!docDir.exists()) {
- // TUSCANY: update from .war to also support .jar and .zip SCA contributions
- File warFile = new File(docBase + ".war");
- if (warFile.exists()) {
- if (unpackWARs) {
- URL war =
- new URL("jar:" + warFile.toURI().toURL() + "!/");
- docBase = ExpandWar.expand(host, war, contextPath);
- file = new File(docBase);
- docBase = file.getCanonicalPath();
- } else {
- docBase = warFile.getCanonicalPath();
- }
- } else {
- warFile = new File(docBase + ".jar");
- if (warFile.exists()) {
- if (unpackWARs) {
- URL war =
- new URL("jar:" + warFile.toURI().toURL() + "!/");
- docBase = ExpandWar.expand(host, war, contextPath);
- file = new File(docBase);
- docBase = file.getCanonicalPath();
- } else {
- docBase = warFile.getCanonicalPath();
- }
- } else {
- warFile = new File(docBase + ".zip");
- if (warFile.exists()) {
- if (unpackWARs) {
- URL war =
- new URL("jar:" + warFile.toURI().toURL() + "!/");
- docBase = ExpandWar.expand(host, war, contextPath);
- file = new File(docBase);
- docBase = file.getCanonicalPath();
- } else {
- docBase = warFile.getCanonicalPath();
- }
- }
- }
- }
- if (context instanceof StandardContext) {
- ((StandardContext) context).setOriginalDocBase(origDocBase);
- }
- }
- }
-
- if (docBase.startsWith(canonicalAppBase.getPath())) {
- docBase = docBase.substring(canonicalAppBase.getPath().length());
- docBase = docBase.replace(File.separatorChar, '/');
- if (docBase.startsWith("/")) {
- docBase = docBase.substring(1);
- }
- } else {
- docBase = docBase.replace(File.separatorChar, '/');
- }
-
- context.setDocBase(docBase);
-
- }
-
-}
diff --git a/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyHostConfig.java b/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyHostConfig.java
deleted file mode 100644
index e92d7a0bf6..0000000000
--- a/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyHostConfig.java
+++ /dev/null
@@ -1,167 +0,0 @@
-/*
- * 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.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
-
-import org.apache.catalina.Context;
-import org.apache.catalina.Lifecycle;
-import org.apache.catalina.LifecycleListener;
-import org.apache.catalina.startup.HostConfig;
-
-/**
- * A Tuscany customized HostConfig that adds support for SCA contributions
- * to be deployed along with the usual .war files.
- */
-public class TuscanyHostConfig extends HostConfig {
-
- protected File scaBase = null;
-
- @Override
- protected void deployApps() {
-
- File appBase = appBase();
- File configBase = configBase();
- // Deploy XML descriptors from configBase
- deployDescriptors(configBase, configBase.list());
- // Deploy WARs, and loop if additional descriptors are found
- deployWARs(appBase, appBase.list());
- // TUSCANY: Deploy any SCA contibutions
- deploySCAContributions(appBase, appBase.list());
- // Deploy expanded folders
- deployDirectories(appBase, appBase.list());
- }
-
- protected void deploySCAContributions(File appBase, String[] files) {
- if (files == null)
- return;
-
- for (int i = 0; i < files.length; i++) {
-
- File scafile = new File(appBase, files[i]);
-
- if (scafile.isFile() && isSCAContribution(scafile)) {
-
- // Calculate the context path and make sure it is unique
- String contextPath = "/" + files[i].replace('#','/');
- int period = contextPath.lastIndexOf(".");
- if (period >= 0)
- contextPath = contextPath.substring(0, period);
-
- if (isServiced(contextPath))
- continue;
-
- String file = files[i];
-
- deploySCAContribution(contextPath, scafile, file);
-
- }
-
- }
-
- }
-
- protected String tuscanyContextClass = "org.apache.tuscany.sca.tomcat.TuscanyContextConfig";
-
- protected void deploySCAContribution(String contextPath, File dir, String file) {
- if (deploymentExists(contextPath))
- return;
-
- DeployedApplication deployedApp = new DeployedApplication(contextPath);
-
- // Deploy the application in this WAR file
- if(log.isInfoEnabled())
- log.info("Deploying SCA contibution: " + file);
-
- // Populate redeploy resources with the WAR file
- deployedApp.redeployResources.put(dir.getAbsolutePath(), new Long(dir.lastModified()));
-
- try {
- Context context = (Context) Class.forName(contextClass).newInstance();
- if (context instanceof Lifecycle) {
-
- // Tuscany: change to use the Tuscany ContextConfig class
- Class clazz = Class.forName(tuscanyContextClass);
-
- LifecycleListener listener =
- (LifecycleListener) clazz.newInstance();
- ((Lifecycle) context).addLifecycleListener(listener);
- }
- context.setPath(contextPath);
- context.setDocBase(file);
-
- host.addChild(context);
- // If we're unpacking WARs, the docBase will be mutated after
- // starting the context
- if (unpackWARs && (context.getDocBase() != null)) {
- String name = null;
- String path = context.getPath();
- if (path.equals("")) {
- name = "ROOT";
- } else {
- if (path.startsWith("/")) {
- name = path.substring(1);
- } else {
- name = path;
- }
- }
- name = name.replace('/', '#');
- File docBase = new File(name);
- if (!docBase.isAbsolute()) {
- docBase = new File(appBase(), name);
- }
- deployedApp.redeployResources.put(docBase.getAbsolutePath(),
- new Long(docBase.lastModified()));
- addWatchedResources(deployedApp, docBase.getAbsolutePath(), context);
- } else {
- addWatchedResources(deployedApp, null, context);
- }
- } catch (Throwable t) {
- log.error(sm.getString("hostConfig.deployJar.error", file), t);
- }
-
- deployed.put(contextPath, deployedApp);
- }
-
- protected boolean isSCAContribution(File file) {
- ZipFile zip = null;
- ZipEntry entry = null;
- try {
- try {
- zip = new ZipFile(file);
- entry = zip.getEntry("META-INF/sca-contribution.xml");
- } catch (Exception e) {
- }
-
- return (entry != null);
-
- } finally {
- if (zip != null) {
- try {
- zip.close();
- } catch (Throwable t) {
- }
- }
- }
- }
-
-}
diff --git a/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java b/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java
deleted file mode 100644
index a9eee9687e..0000000000
--- a/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java
+++ /dev/null
@@ -1,161 +0,0 @@
-/*
- * 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.util.logging.Logger;
-
-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
- * <Listener className="org.apache.tuscany.sca.tomcat.TuscanyLifecycleListener"/>
- */
-public class TuscanyLifecycleListener implements LifecycleListener {
- private static final Logger log = Logger.getLogger(TuscanyLifecycleListener.class.getName());
-
- 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";
-
- private static String domainURI = "tuscany:default";
-
- 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();
- if (webappDir == null) {
- log.severe("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("Tuscany enabled on 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, "tomcat-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;
- }
-
- public void setDomainURI(String domainURI) {
- TuscanyLifecycleListener.domainURI = domainURI;
- }
-
- public static String getDomainURI() {
- return domainURI;
- }
-}
diff --git a/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java b/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java
deleted file mode 100644
index ca9c81f1c3..0000000000
--- a/sca-java-2.x/tags/2.0-Beta1-RC3/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java
+++ /dev/null
@@ -1,196 +0,0 @@
-/*
- * 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.lang.reflect.Method;
-import java.net.URI;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.logging.Logger;
-
-import javax.naming.NameClassPair;
-import javax.naming.NamingEnumeration;
-import javax.naming.NamingException;
-
-import org.apache.catalina.LifecycleException;
-import org.apache.catalina.Loader;
-import org.apache.catalina.core.StandardContext;
-
-/**
- * A Tuscany StandardContext to initilize SCA applications.
- * There is a StandardContext instance for each webapp and its
- * called to handle all start/stop/etc requests. This intercepts
- * the start and inserts any required Tuscany configuration.
- */
-public class TuscanyStandardContext extends StandardContext {
- 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 webapp, work out how to have only one per server
- // ?? is that comment still true?
- private static URLClassLoader tuscanyClassLoader;
- private static Object node;
- private static Class<?> nodeClass;
- private static Method nodeStopMethod;
-
- public TuscanyStandardContext() {
- }
-
- /**
- * Overrides the getLoader method in the Tomcat StandardContext as its a convenient
- * point to insert the Tuscany initilization. This gets called the first time during
- * StandardContext.start after the webapp resources have been created so this can
- * use getResources() to look for the SCA web.composite or sca-contribution.xml files,
- * but its still early enough in start to insert the required Tuscany config.
- */
- @Override
- public Loader getLoader() {
- if (loader != null) {
- return loader;
- }
-
- ClassLoader parent = getParentClassLoader();
- if (isSCAApp = isSCAApplication()) {
- if (tuscanyClassLoader == null) {
- initTuscany();
- }
- setParentClassLoader(getTuscanyClassloader(parent));
- setDefaultWebXml("conf/tuscany-web.xml");
- }
-
- return super.getLoader();
- }
-
- @Override
- public boolean listenerStart() {
- if (isSCAApp) {
- enableTuscany();
- }
- return super.listenerStart();
- }
-
- private void enableTuscany() {
-
- if (isUseNaming() && getNamingContextListener() != null) {
- setAnnotationProcessor(new TuscanyAnnotationsProcessor(this, getNamingContextListener().getEnvContext()));
- } else {
- setAnnotationProcessor(new TuscanyAnnotationsProcessor(this, null));
- }
-
- log.info("Tuscany SCA is enabled for: " + this.getName());
- }
-
- private boolean isSCAApplication() {
- Object o = null;
- try {
- o = getResources().lookup("WEB-INF/web.composite");
- } catch (NamingException e) {
- }
- if (o == null) {
- try {
- o = getResources().lookup("META-INF/sca-contribution.xml");
- } catch (NamingException e) {
- }
- }
- if (o == null) {
- return false;
- }
-
- // Try to see if the Tuscany jars are packaged in the webapp
- NamingEnumeration<NameClassPair> enumeration;
- try {
- enumeration = getResources().list("WEB-INF/lib");
- while (enumeration.hasMoreElements()) {
- String jar = enumeration.nextElement().getName();
- if (jar.startsWith("tuscany-")) {
- // Do not alter is
- log.info("Tuscany SCA ignoring webapp with embedded Tuscany runtime: " + this.getName());
- return false;
- }
- }
- } catch (NamingException e) {
- }
- return true;
- }
-
- private URLClassLoader getTuscanyClassloader(ClassLoader parent) {
- return tuscanyClassLoader;
- }
-
- private void initTuscany() {
- initTuscanyClassloader(getParentClassLoader());
- initDomain();
- }
-
- private void initTuscanyClassloader(ClassLoader parent) {
- if (tuscanyClassLoader == null) {
- 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++) {
- jarURLs[i] = runtimeJars[i].toURI().toURL();
- }
- tuscanyClassLoader = new URLClassLoader(jarURLs, parent);
-
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- private void initDomain() {
- ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
- try {
- Thread.currentThread().setContextClassLoader(tuscanyClassLoader);
- Class<?> nodeFactoryClass = Class.forName("org.apache.tuscany.sca.node.NodeFactory", true, tuscanyClassLoader);
- Method getInstanceMethod = nodeFactoryClass.getMethod("getInstance", new Class[0]);
- Object instance = getInstanceMethod.invoke(null);
- Method createNodeMethod = nodeFactoryClass.getMethod("createNode", new Class[]{URI.class, new String[0].getClass()});
- URI domainURI = URI.create(TuscanyLifecycleListener.getDomainURI());
- this.node = createNodeMethod.invoke(instance, new Object[]{domainURI, new String[0]});
- this.nodeClass = Class.forName("org.apache.tuscany.sca.node.Node", true, tuscanyClassLoader);
- Method nodeStartMethod = nodeClass.getMethod("start", new Class[0]);
- this.nodeStopMethod = nodeClass.getMethod("stop", new Class[0]);
- nodeStartMethod.invoke(node);
- } catch (Exception e) {
- throw new RuntimeException(e);
- } finally {
- Thread.currentThread().setContextClassLoader(oldCL);
- }
- }
-
- @Override
- public synchronized void stop() throws LifecycleException {
- super.stop();
-
- if (node != null && nodeStopMethod != null) {
- try {
- nodeStopMethod.invoke(node);
- node = null;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
-}