summaryrefslogtreecommitdiffstats
path: root/java/sca
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-05-28 21:24:41 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-05-28 21:24:41 +0000
commit489f4354333e244564fc279bbf554cdd44652c1d (patch)
treed57cf862abf08fdca413bb32f254457da334c03a /java/sca
parent717185ace58cc017538097c811f896a770ab3e22 (diff)
Add a checkbox on the jsp to configure if Tuscany is shared or isolated by the webapps
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@779778 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca')
-rw-r--r--java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyLifecycleListener.java51
-rw-r--r--java/sca/distribution/tomcat/tomcat-hook/src/main/java/org/apache/tuscany/sca/tomcat/TuscanyStandardContext.java44
-rw-r--r--java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/Installer.java82
-rw-r--r--java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java11
-rw-r--r--java/sca/distribution/tomcat/tomcat-war/src/main/webapp/installer.jsp1
5 files changed, 121 insertions, 68 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;
}
+
}
diff --git a/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/Installer.java b/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/Installer.java
index 19879175d6..9ad06832e5 100644
--- a/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/Installer.java
+++ b/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/Installer.java
@@ -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.IOException;
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 @@ public class Installer {
tuscanyHookRunning = false;
}
}
-
+
public static boolean isTuscanyHookRunning() {
return tuscanyHookRunning;
}
@@ -66,15 +67,15 @@ public class Installer {
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 class Installer {
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 @@ public class Installer {
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 @@ public class Installer {
// 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 @@ public class Installer {
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 @@ public class Installer {
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 @@ public class Installer {
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 @@ public class Installer {
}
out.flush();
}
-
+
private void close(Closeable thing) {
if (thing != null) {
try {
diff --git a/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java b/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java
index 67febe5e6e..5447bab83d 100644
--- a/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java
+++ b/java/sca/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java
@@ -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 @@ public class InstallerServlet extends HttpServlet {
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");
diff --git a/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/installer.jsp b/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/installer.jsp
index 4c619b4a69..a6c3e4acaa 100644
--- a/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/installer.jsp
+++ b/java/sca/distribution/tomcat/tomcat-war/src/main/webapp/installer.jsp
@@ -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>