Skip to content

Commit

Permalink
413034 Multiple webapps redeploy returns NamingException with AppDyna…
Browse files Browse the repository at this point in the history
…mics javaagent
  • Loading branch information
janbartel committed Jul 19, 2013
1 parent d65b511 commit ecd687d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
55 changes: 43 additions & 12 deletions jetty-jndi/src/main/java/org/eclipse/jetty/jndi/ContextFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context> __threadContext = new ThreadLocal<Context>();

/**
* Threadlocal for setting a classloader which must be used
* when finding the comp context.
*/
private static final ThreadLocal<ClassLoader> __threadClassLoader = new ThreadLocal<ClassLoader>();



/**
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -170,6 +172,7 @@ public void deconfigure (WebAppContext context) throws Exception
}
finally
{
ContextFactory.disassociateClassLoader();
Thread.currentThread().setContextClassLoader(oldLoader);
}
}
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down

0 comments on commit ecd687d

Please sign in to comment.