diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-12-04 15:11:00 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-12-04 15:11:00 +0000 |
commit | a10633370206b6060f58a5723478e2cbf72c1248 (patch) | |
tree | 3170f26824b9f9a409bf5b175c69f3834c24f713 /sca-java-2.x/trunk/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany | |
parent | 62c97ce41ae36c5dc3ee3a90cc72ed859fb76776 (diff) |
Update the Tomcat distribution so that SCA contributions can be deployed to exsiting Hosts instead of running in the Tuscany webapp. This means they run just like a regular web application and can be managed with the usual Tomcat admin and use all the usual Tomcat Host and Context features. Also update to configure the domain from the context so the domain nad be customized fro the Tomcat instance and each contribution via a context parameter. That removes the need for the singleton install option so that is removed. Also update the install page to have a bit of text about whats going on in the intsall
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@887223 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany')
2 files changed, 350 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyContextConfig.java b/sca-java-2.x/trunk/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyContextConfig.java new file mode 100644 index 0000000000..5e7905d246 --- /dev/null +++ b/sca-java-2.x/trunk/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyContextConfig.java @@ -0,0 +1,183 @@ +/*
+ * 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/trunk/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyHost.java b/sca-java-2.x/trunk/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyHost.java new file mode 100644 index 0000000000..d6042c5902 --- /dev/null +++ b/sca-java-2.x/trunk/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyHost.java @@ -0,0 +1,167 @@ +/*
+ * 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 TuscanyHost 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) {
+ }
+ }
+ }
+ }
+
+}
|