diff --git a/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/ContextFactory.java b/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/ContextFactory.java index b3c69074f889..c56b694a5bdb 100644 --- a/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/ContextFactory.java +++ b/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/ContextFactory.java @@ -75,7 +75,14 @@ public class ContextFactory implements ObjectFactory * Threadlocal for injecting a context to use * instead of looking up the map. */ - private static final ThreadLocal __threadContext = new ThreadLocal(); + private static final ThreadLocal __threadContext = new ThreadLocal(); + + /** + * Threadlocal for setting a classloader which must be used + * when finding the comp context. + */ + private static final ThreadLocal __threadClassLoader = new ThreadLocal(); + /** @@ -107,10 +114,25 @@ public Object getObjectInstance (Object obj, return ctx; } + //See if there is a classloader to use for finding the comp context + //Don't use its parent hierarchy if set. + ClassLoader loader = (ClassLoader)__threadClassLoader.get(); + if (loader != null) + { + if (__log.isDebugEnabled() && loader != null) __log.debug("Using threadlocal classloader"); + ctx = getContextForClassLoader(loader); + if (ctx == null) + { + ctx = newNamingContext(obj, loader, env, name, nameCtx); + __contextMap.put (loader, ctx); + if(__log.isDebugEnabled())__log.debug("Made context "+name.get(0)+" for classloader: "+loader); + } + return ctx; + } - ClassLoader tccl = Thread.currentThread().getContextClassLoader(); - ClassLoader loader = tccl; //If the thread context classloader is set, then try its hierarchy to find a matching context + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + loader = tccl; if (loader != null) { if (__log.isDebugEnabled() && loader != null) __log.debug("Trying thread context classloader"); @@ -194,25 +216,34 @@ public Context getContextForClassLoader(ClassLoader loader) /** * Associate the given Context with the current thread. - * resetComponentContext method should be called to reset the context. + * disassociate method should be called to reset the context. * @param ctx the context to associate to the current thread. * @return the previous context associated on the thread (can be null) */ - public static Context setComponentContext(final Context ctx) + public static Context associateContext(final Context ctx) { Context previous = (Context)__threadContext.get(); __threadContext.set(ctx); return previous; } - /** - * Set back the context with the given value. - * Don't return the previous context, use setComponentContext() method for this. - * @param ctx the context to associate to the current thread. - */ - public static void resetComponentContext(final Context ctx) + public static void disassociateContext(final Context ctx) { - __threadContext.set(ctx); + __threadContext.remove(); + } + + + public static ClassLoader associateClassLoader(final ClassLoader loader) + { + ClassLoader prev = (ClassLoader)__threadClassLoader.get(); + __threadClassLoader.set(loader); + return prev; + } + + + public static void disassociateClassLoader () + { + __threadClassLoader.remove(); } public static void dump(Appendable out, String indent) throws IOException diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/webapp/EnvConfiguration.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/webapp/EnvConfiguration.java index 660231d9f259..26227f155593 100644 --- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/webapp/EnvConfiguration.java +++ b/jetty-plus/src/main/java/org/eclipse/jetty/plus/webapp/EnvConfiguration.java @@ -31,6 +31,7 @@ import javax.naming.NameNotFoundException; import javax.naming.NamingException; +import org.eclipse.jetty.jndi.ContextFactory; import org.eclipse.jetty.jndi.NamingContext; import org.eclipse.jetty.jndi.NamingUtil; import org.eclipse.jetty.jndi.local.localContextRoot; @@ -147,6 +148,7 @@ public void deconfigure (WebAppContext context) throws Exception //get rid of any bindings for comp/env for webapp ClassLoader oldLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(context.getClassLoader()); + ContextFactory.associateClassLoader(context.getClassLoader()); try { Context ic = new InitialContext(); @@ -170,6 +172,7 @@ public void deconfigure (WebAppContext context) throws Exception } finally { + ContextFactory.disassociateClassLoader(); Thread.currentThread().setContextClassLoader(oldLoader); } } @@ -251,6 +254,7 @@ protected void createEnvContext (WebAppContext wac) { ClassLoader old_loader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(wac.getClassLoader()); + ContextFactory.associateClassLoader(wac.getClassLoader()); try { Context context = new InitialContext(); @@ -259,8 +263,9 @@ protected void createEnvContext (WebAppContext wac) } finally { - Thread.currentThread().setContextClassLoader(old_loader); - } + ContextFactory.disassociateClassLoader(); + Thread.currentThread().setContextClassLoader(old_loader); + } } private static class Bound