From: Scott M S. <st...@us...> - 2004-05-17 01:28:51
|
User: starksm Date: 04/05/16 18:28:43 Modified: src/main/org/jboss/util TCLStack.java Log: Perform the getContextClassLoader()/setContextClassLoader() methods in PrivilegedAction blocks Revision Changes Path 1.5 +44 -8 jboss-common/src/main/org/jboss/util/TCLStack.java Index: TCLStack.java =================================================================== RCS file: /cvsroot/jboss/jboss-common/src/main/org/jboss/util/TCLStack.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- TCLStack.java 5 Jan 2004 20:44:58 -0000 1.4 +++ TCLStack.java 17 May 2004 01:28:43 -0000 1.5 @@ -12,6 +12,8 @@ import java.util.Stack; import java.util.EmptyStackException; import java.util.Iterator; +import java.security.PrivilegedAction; +import java.security.AccessController; import org.jboss.logging.Logger; @@ -24,7 +26,7 @@ * Also provides TRACE level logging for a better view of TCL usage and * provides an immutable view of the stack for inspection. * - * @version <tt>$Revision: 1.4 $</tt> + * @version <tt>$Revision: 1.5 $</tt> * @author <a href="mailto:ja...@pl...">Jason Dillon</a> * @author Sco...@jb... */ @@ -52,17 +54,18 @@ * argument is null then the current TCL is not updated and pop will leave * the current TCL the same as entry into push. * - * @param cl The class loader to set as the TCL. + * @param cl The class loader to set as the TCL. */ public static void push(final ClassLoader cl) { boolean trace = log.isTraceEnabled(); // push the old cl and set the new cl - Thread currentThread = Thread.currentThread(); - ClassLoader oldCL = currentThread.getContextClassLoader(); + ClassLoader oldCL = GetTCLAction.getContextClassLoader(); if( cl != null ) - currentThread.setContextClassLoader(cl); + { + SetTCLAction.setContextClassLoader(cl); + } getStack().push(oldCL); if (trace) @@ -84,11 +87,10 @@ // get the last cl in the stack & make it the current try { - Thread currentThread = Thread.currentThread(); ClassLoader cl = (ClassLoader) getStack().pop(); - ClassLoader oldCL = currentThread.getContextClassLoader(); + ClassLoader oldCL = GetTCLAction.getContextClassLoader(); - currentThread.setContextClassLoader(cl); + SetTCLAction.setContextClassLoader(cl); if (log.isTraceEnabled()) { @@ -128,4 +130,38 @@ { return (ClassLoader) getStack().get(index); } + + private static class GetTCLAction implements PrivilegedAction + { + static PrivilegedAction ACTION = new GetTCLAction(); + public Object run() + { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + return loader; + } + static ClassLoader getContextClassLoader() + { + ClassLoader loader = (ClassLoader) AccessController.doPrivileged(ACTION); + return loader; + } + } + private static class SetTCLAction implements PrivilegedAction + { + ClassLoader loader; + SetTCLAction(ClassLoader loader) + { + this.loader = loader; + } + public Object run() + { + Thread.currentThread().setContextClassLoader(loader); + loader = null; + return null; + } + static void setContextClassLoader(ClassLoader loader) + { + PrivilegedAction action = new SetTCLAction(loader); + AccessController.doPrivileged(action); + } + } } |