Index: /home/benoitf/workspace/Jetty/modules/naming/src/main/java/org/mortbay/naming/ContextFactory.java
===================================================================
--- /home/benoitf/workspace/Jetty/modules/naming/src/main/java/org/mortbay/naming/ContextFactory.java	(revision 1308)
+++ /home/benoitf/workspace/Jetty/modules/naming/src/main/java/org/mortbay/naming/ContextFactory.java	(working copy)
@@ -4,7 +4,7 @@
 // ------------------------------------------------------------------------
 // Licensed 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 
+// 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,
@@ -42,15 +42,20 @@
 public class ContextFactory implements ObjectFactory
 {
     //map of classloaders to contexts
-    private static WeakHashMap _contextMap;
+    private static WeakHashMap<ClassLoader, Context> _contextMap;
 
+    /**
+     * Allows to associate a context (java:comp) to a thread.
+     */
+    private static ThreadLocal<Context> threadContext;
 
     static
     {
-        _contextMap = new WeakHashMap();
+        _contextMap = new WeakHashMap<ClassLoader, Context>();
+        threadContext = new ThreadLocal<Context>();
     }
-    
-  
+
+
 
     public Object getObjectInstance (Object obj,
                                      Name name,
@@ -58,10 +63,19 @@
                                      Hashtable env)
         throws Exception
     {
+        // First, look if there is a context in the current thread.
+        Context ctx = threadContext.get();
+        if (ctx != null) {
+            if(Log.isDebugEnabled()) Log.debug("Using the Context that is bound on the thread");
+            return ctx;
+        }
+
+        // Else, use the context that is associated on the current classloader
+
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
         if(Log.isDebugEnabled()) Log.debug("looking for context for "+loader);
-        Context ctx = (Context)_contextMap.get(loader);
-        
+        ctx = (Context)_contextMap.get(loader);
+
         //the map does not contain an entry for this classloader
         if (ctx == null)
         {
@@ -74,7 +88,7 @@
                 StringRefAddr parserAddr = (StringRefAddr)ref.get("parser");
                 String parserClassName = (parserAddr==null?null:(String)parserAddr.getContent());
                 NameParser parser = (NameParser)(parserClassName==null?null:loader.loadClass(parserClassName).newInstance());
-                
+
                 ctx = new NamingContext (env,
                                          name.get(0),
                                          nameCtx,
@@ -98,4 +112,25 @@
 
         return ctx;
     }
-} 
+
+    /**
+     * Associate the given Context with the current thread.
+     * resetComponentContext 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) {
+        Context previous = 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) {
+        threadContext.set(ctx);
+    }
+}

