From df79fb183b89bbf7fcff3d343e7205ead4f19a3e Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 8 Nov 2010 02:35:58 +0000 Subject: [PATCH] JETTY-1297 Make ServletContext.getContext(String) virtual host aware git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2479 7e9141cc-0065-0410-87d8-b60c137991c4 --- VERSION.txt | 1 + example-jetty-embedded/prodDb.properties | 17 +++++++ example-jetty-embedded/prodDb.script | 4 ++ .../eclipse/jetty/embedded/LikeJettyXml.java | 4 +- .../jetty/server/handler/ContextHandler.java | 51 +++++++++++++++++-- .../jetty/webapp/WebAppClassLoader.java | 2 - 6 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 example-jetty-embedded/prodDb.properties create mode 100644 example-jetty-embedded/prodDb.script diff --git a/VERSION.txt b/VERSION.txt index 2d9372ab1d7..7349109dcca 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -18,6 +18,7 @@ jetty-7.2.1-SNAPSHOT + JETTY-748 Prevent race close of socket by old acceptor threads + JETTY-1291 Extract query parameters even if POST content consumed + JETTY-1295 Contexts mixed up when hot-deploying on virtual hosts + + JETTY-1297 Make ServletContext.getContext(String) virtual host aware jetty-7.2.0.v20101020 20 October 2010 + 289540 added javadoc into distribution diff --git a/example-jetty-embedded/prodDb.properties b/example-jetty-embedded/prodDb.properties new file mode 100644 index 00000000000..5130d856783 --- /dev/null +++ b/example-jetty-embedded/prodDb.properties @@ -0,0 +1,17 @@ +#HSQL Database Engine 1.8.0.10 +#Mon Nov 08 13:35:35 EST 2010 +hsqldb.script_format=0 +runtime.gc_interval=0 +sql.enforce_strict_size=false +hsqldb.cache_size_scale=8 +readonly=false +hsqldb.nio_data_file=true +hsqldb.cache_scale=14 +version=1.8.0 +hsqldb.default_table_type=memory +hsqldb.cache_file_scale=1 +hsqldb.log_size=200 +modified=no +hsqldb.cache_version=1.7.0 +hsqldb.original_version=1.8.0 +hsqldb.compatible_version=1.8.0 diff --git a/example-jetty-embedded/prodDb.script b/example-jetty-embedded/prodDb.script new file mode 100644 index 00000000000..382d243636c --- /dev/null +++ b/example-jetty-embedded/prodDb.script @@ -0,0 +1,4 @@ +CREATE SCHEMA PUBLIC AUTHORIZATION DBA +CREATE USER SA PASSWORD "" +GRANT DBA TO SA +SET WRITE_DELAY 10 diff --git a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java index 27f9d30bac5..21e9f07e7e4 100644 --- a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java +++ b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java @@ -107,6 +107,7 @@ public class LikeJettyXml server.addBean(context_provider); deployer.addAppProvider(context_provider); + /* WebAppProvider webapp_provider = new WebAppProvider(); webapp_provider.setMonitoredDir(jetty_home + "/webapps"); webapp_provider.setParentLoaderPriority(false); @@ -115,7 +116,8 @@ public class LikeJettyXml webapp_provider.setDefaultsDescriptor(jetty_home + "/etc/webdefault.xml"); webapp_provider.setContextXmlDir(jetty_home + "/contexts"); deployer.addAppProvider(webapp_provider); - +*/ + HashLoginService login = new HashLoginService(); login.setName("Test Realm"); login.setConfig(jetty_home + "/etc/realm.properties"); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index 9be1e8cc8f5..0ac46dbb123 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -19,12 +19,14 @@ import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; import java.util.EventListener; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; @@ -1443,8 +1445,10 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server. */ public ServletContext getContext(String uripath) { - ContextHandler context=null; + List contexts=new ArrayList(); Handler[] handlers = getServer().getChildHandlersByClass(ContextHandler.class); + String matched_path=null; + for (int i=0;icontext.getContextPath().length()) - context=ch; + if (matched_path==null || context_path.length()>matched_path.length()) + { + contexts.clear(); + matched_path=context_path; + } + + if (matched_path.equals(context_path)) + contexts.add(ch); } } - if (context!=null) - return context._scontext; + switch (contexts.size()) + { + case 0: return null; + + case 1: + return contexts.get(0)._scontext; + + default: + // Multiple contexts + // Does this context match? + if (contexts.contains(ContextHandler.this)) + { + return _scontext; + } + + // Are there matching virtual hosts? + if (getVirtualHosts()!=null && getVirtualHosts().length>0) + { + for (ContextHandler ch : contexts) + { + if (ch.getVirtualHosts()!=null && ch.getVirtualHosts().length>0) + { + for (String h1 : getVirtualHosts()) + for (String h2 : ch.getVirtualHosts()) + if (h1.equals(h2)) + { + return ch._scontext; + } + } + } + } + } + return null; } diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppClassLoader.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppClassLoader.java index ded248c8235..a6f2acae03c 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppClassLoader.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppClassLoader.java @@ -431,8 +431,6 @@ public class WebAppClassLoader extends URLClassLoader /* ------------------------------------------------------------ */ public String toString() { - if (Log.isDebugEnabled()) - return "WebAppClassLoader@" + _name + "(" + LazyList.array2List(getURLs()) + ") / " + _parent; return "WebAppClassLoader@" + _name; } }