Author: rfeng
Date: Wed Jan 16 15:35:32 2008
New Revision: 612626
URL: http://svn.apache.org/viewvc?rev=612626&view=rev
Log:
Change the classloader for embedded Tomcat
Modified:
incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/ExtensibleServletHost.java
incubator/tuscany/java/sca/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
Modified:
incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/ExtensibleServletHost.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/ExtensibleServletHost.java?rev=612626&r1=612625&r2=612626&view=diff
==============================================================================
---
incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/ExtensibleServletHost.java
(original)
+++
incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/ExtensibleServletHost.java
Wed Jan 16 15:35:32 2008
@@ -47,12 +47,16 @@
}
}
+ private ServletHost getDefaultServletHost() {
+ return servletHosts.getServletHosts().get(0);
+ }
+
public int getDefaultPort() {
if (servletHosts.getServletHosts().isEmpty()) {
throw new ServletMappingException("No servlet host available");
}
- return servletHosts.getServletHosts().get(0).getDefaultPort();
+ return getDefaultServletHost().getDefaultPort();
}
public void addServletMapping(String uri, Servlet servlet) throws
ServletMappingException {
@@ -62,7 +66,7 @@
// TODO implement selection of the correct servlet host based on the
mapping
// For now just select the first one
- servletHosts.getServletHosts().get(0).addServletMapping(uri, servlet);
+ getDefaultServletHost().addServletMapping(uri, servlet);
}
public Servlet getServletMapping(String uri) throws
ServletMappingException {
@@ -72,7 +76,7 @@
// TODO implement selection of the correct servlet host based on the
mapping
// For now just select the first one
- return servletHosts.getServletHosts().get(0).getServletMapping(uri);
+ return getDefaultServletHost().getServletMapping(uri);
}
public Servlet removeServletMapping(String uri) throws
ServletMappingException {
@@ -82,7 +86,7 @@
// TODO implement selection of the correct servlet host based on the
mapping
// For now just select the first one
- return servletHosts.getServletHosts().get(0).removeServletMapping(uri);
+ return getDefaultServletHost().removeServletMapping(uri);
}
public RequestDispatcher getRequestDispatcher(String uri) throws
ServletMappingException {
@@ -92,7 +96,7 @@
// TODO implement selection of the correct servlet host based on the
mapping
// For now just select the first one
- return servletHosts.getServletHosts().get(0).getRequestDispatcher(uri);
+ return getDefaultServletHost().getRequestDispatcher(uri);
}
public String getContextPath() {
@@ -102,7 +106,7 @@
// TODO implement selection of the correct servlet host based on the
mapping
// For now just select the first one
- return servletHosts.getServletHosts().get(0).getContextPath();
+ return getDefaultServletHost().getContextPath();
}
public URL getURLMapping(String uri) {
@@ -110,10 +114,10 @@
throw new ServletMappingException("No servlet host available");
}
- return servletHosts.getServletHosts().get(0).getURLMapping(uri);
+ return getDefaultServletHost().getURLMapping(uri);
}
public void setContextPath(String path) {
- servletHosts.getServletHosts().get(0).setContextPath(path);
+ getDefaultServletHost().setContextPath(path);
}
}
Modified:
incubator/tuscany/java/sca/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java?rev=612626&r1=612625&r2=612626&view=diff
==============================================================================
---
incubator/tuscany/java/sca/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
(original)
+++
incubator/tuscany/java/sca/modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
Wed Jan 16 15:35:32 2008
@@ -18,13 +18,16 @@
*/
package org.apache.tuscany.sca.http.tomcat;
+import java.beans.PropertyChangeListener;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.UnknownHostException;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
@@ -35,8 +38,10 @@
import javax.servlet.Servlet;
import javax.servlet.ServletException;
+import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Lifecycle;
+import org.apache.catalina.Loader;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
@@ -59,9 +64,77 @@
@SuppressWarnings("deprecation")
public class TomcatServer implements ServletHost {
private final static Logger logger =
Logger.getLogger(TomcatServer.class.getName());
-
+
private int defaultPortNumber = 8080;
-
+
+ private final class TuscanyLoader implements Loader {
+ private final ClassLoader tccl;
+ private boolean delegate;
+ private boolean reloadable;
+ private Container container;
+ private List<String> repos = new ArrayList<String>();
+ private List<PropertyChangeListener> listeners = new
ArrayList<PropertyChangeListener>();
+
+ private TuscanyLoader(ClassLoader tccl) {
+ this.tccl = tccl;
+ }
+
+ public void addPropertyChangeListener(PropertyChangeListener listener)
{
+ listeners.add(listener);
+ }
+
+ public void addRepository(String repository) {
+ repos.add(repository);
+ }
+
+ public void backgroundProcess() {
+ }
+
+ public String[] findRepositories() {
+ return repos.toArray(new String[repos.size()]);
+ }
+
+ public Container getContainer() {
+ return container;
+ }
+
+ public boolean getDelegate() {
+ return delegate;
+ }
+
+ public String getInfo() {
+ return "Tuscany Loader for Embedded Tomcat";
+ }
+
+ public boolean getReloadable() {
+ return reloadable;
+ }
+
+ public boolean modified() {
+ return false;
+ }
+
+ public void removePropertyChangeListener(PropertyChangeListener
listener) {
+ listeners.remove(listener);
+ }
+
+ public void setContainer(Container container) {
+ this.container = container;
+ }
+
+ public void setDelegate(boolean delegate) {
+ this.delegate = delegate;
+ }
+
+ public void setReloadable(boolean reloadable) {
+ this.reloadable = reloadable;
+ }
+
+ public ClassLoader getClassLoader() {
+ return tccl;
+ }
+ }
+
/**
* Represents a port and the server that serves it.
*/
@@ -69,7 +142,7 @@
private StandardEngine engine;
private StandardHost host;
private Connector connector;
-
+
private Port(StandardEngine engine, StandardHost host, Connector
connector) {
this.engine = engine;
this.host = host;
@@ -79,16 +152,16 @@
public StandardEngine getEngine() {
return engine;
}
-
+
public StandardHost getHost() {
return host;
}
-
+
public Connector getConnector() {
return connector;
}
}
-
+
private Map<Integer, Port> ports = new HashMap<Integer, Port>();
private WorkScheduler workScheduler;
@@ -107,7 +180,7 @@
public void setDefaultPort(int port) {
defaultPortNumber = port;
}
-
+
public int getDefaultPort() {
return defaultPortNumber;
}
@@ -119,7 +192,7 @@
if (!ports.isEmpty()) {
try {
Set<Entry<Integer, Port>> entries = new HashSet<Entry<Integer,
Port>>(ports.entrySet());
- for (Entry<Integer, Port> entry: entries) {
+ for (Entry<Integer, Port> entry : entries) {
entry.getValue().getConnector().stop();
entry.getValue().getEngine().stop();
ports.remove(entry.getKey());
@@ -132,7 +205,7 @@
public void addServletMapping(String suri, Servlet servlet) {
URI uri = URI.create(suri);
-
+
// Get the URI scheme and port
String scheme = uri.getScheme();
if (scheme == null) {
@@ -161,13 +234,15 @@
// Create the root context
StandardContext context = new StandardContext();
-
context.setParentClassLoader(Thread.currentThread().getContextClassLoader());
+ final ClassLoader tccl =
Thread.currentThread().getContextClassLoader();
+ context.setLoader(new TuscanyLoader(tccl));
+ // context.setParentClassLoader(tccl.getParent());
context.setDocBase("");
context.setPath("");
ContextConfig config = new ContextConfig();
((Lifecycle)context).addLifecycleListener(config);
host.addChild(context);
-
+
// Install an HTTP connector
Connector connector;
try {
@@ -180,7 +255,7 @@
} catch (Exception e) {
throw new ServletMappingException(e);
}
-
+
// Keep track of the running server
port = new Port(engine, host, connector);
ports.put(portNumber, port);
@@ -188,31 +263,32 @@
// Register the servlet mapping
String path = uri.getPath();
-
+
if (!path.startsWith("/")) {
path = '/' + path;
}
-
+
if (!path.startsWith(contextPath)) {
path = contextPath + path;
}
-
+
ServletWrapper wrapper;
if (servlet instanceof DefaultResourceServlet) {
String defaultServletPath = path;
-
+
// Optimize the handling of resource requests, use the Tomcat
default servlet
// instead of our default resource servlet
if (defaultServletPath.endsWith("*")) {
- defaultServletPath = defaultServletPath.substring(0,
defaultServletPath.length()-1);
+ defaultServletPath = defaultServletPath.substring(0,
defaultServletPath.length() - 1);
}
if (defaultServletPath.endsWith("/")) {
- defaultServletPath = defaultServletPath.substring(0,
defaultServletPath.length()-1);
+ defaultServletPath = defaultServletPath.substring(0,
defaultServletPath.length() - 1);
}
DefaultResourceServlet resourceServlet =
(DefaultResourceServlet)servlet;
- TomcatDefaultServlet defaultServlet = new
TomcatDefaultServlet(defaultServletPath, resourceServlet.getDocumentRoot());
+ TomcatDefaultServlet defaultServlet =
+ new TomcatDefaultServlet(defaultServletPath,
resourceServlet.getDocumentRoot());
wrapper = new ServletWrapper(defaultServlet);
-
+
} else {
wrapper = new ServletWrapper(servlet);
}
@@ -245,7 +321,7 @@
}
logger.info("Added Servlet mapping: " + addedURL);
}
-
+
public URL getURLMapping(String suri) throws ServletMappingException {
URI uri = URI.create(suri);
@@ -258,7 +334,7 @@
if (portNumber == -1) {
portNumber = defaultPortNumber;
}
-
+
// Get the host
String host;
try {
@@ -266,14 +342,14 @@
} catch (UnknownHostException e) {
host = "localhost";
}
-
+
// Construct the URL
String path = uri.getPath();
if (!path.startsWith("/")) {
path = '/' + path;
}
-
+
if (!path.startsWith(contextPath)) {
path = contextPath + path;
}
@@ -285,15 +361,15 @@
}
return url;
}
-
+
public Servlet getServletMapping(String suri) throws
ServletMappingException {
-
- if (suri == null){
- return null;
+
+ if (suri == null) {
+ return null;
}
-
+
URI uri = URI.create(suri);
-
+
// Get the URI port
int portNumber = uri.getPort();
if (portNumber == -1) {
@@ -305,17 +381,17 @@
if (port == null) {
return null;
}
-
+
String mapping = uri.getPath();
-
+
if (!mapping.startsWith("/")) {
mapping = '/' + mapping;
}
-
+
if (!mapping.startsWith(contextPath)) {
mapping = contextPath + mapping;
}
-
+
Context context = port.getHost().map(mapping);
MappingData md = new MappingData();
MessageBytes mb = MessageBytes.newInstance();
@@ -335,7 +411,7 @@
public Servlet removeServletMapping(String suri) {
URI uri = URI.create(suri);
-
+
// Get the URI port
int portNumber = uri.getPort();
if (portNumber == -1) {
@@ -347,17 +423,17 @@
if (port == null) {
throw new IllegalStateException("No servlet registered at this
URI: " + suri);
}
-
+
String mapping = uri.getPath();
-
+
if (!mapping.startsWith("/")) {
mapping = '/' + mapping;
}
-
+
if (!mapping.startsWith(contextPath)) {
mapping = contextPath + mapping;
}
-
+
Context context = port.getHost().map(mapping);
MappingData md = new MappingData();
MessageBytes mb = MessageBytes.newInstance();
@@ -369,11 +445,11 @@
}
if (md.wrapper instanceof ServletWrapper) {
ServletWrapper servletWrapper = (ServletWrapper)md.wrapper;
-
+
port.getConnector().getMapper().removeWrapper("localhost", "",
mapping);
try {
- context.removeServletMapping(mapping);
+ context.removeServletMapping(mapping);
} catch (NegativeArraySizeException e) {
// JIRA TUSCANY-1599
//FIXME Looks like a bug in Tomcat when removing the last
@@ -383,7 +459,7 @@
context.removeChild(servletWrapper);
try {
servletWrapper.destroyServlet();
- } catch (Exception ex){
+ } catch (Exception ex) {
// Temporary hack to stop destruction of servlets without
servlet
// context
}
@@ -399,7 +475,7 @@
//FIXME implement this later
return null;
}
-
+
public String getContextPath() {
return contextPath;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]