donaldp 2002/07/18 20:50:02 Modified: threadcontext/src/java/org/apache/excalibur/threadcontext ThreadContext.java Log: Duplicate() retains old state Converted javadoc <code/> refs to {@link}s Made c_context ThreadLocal rather than InheritableThreadLocal as it had been causing nightmares in apps that started their own thread and then tried to set a new threadContext (thereby whiping out oldState in original thread). Revision Changes Path 1.8 +29 -13 jakarta-avalon-excalibur/threadcontext/src/java/org/apache/excalibur/threadcontext/ThreadContext.java Index: ThreadContext.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/threadcontext/src/java/org/apache/excalibur/threadcontext/ThreadContext.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- ThreadContext.java 13 May 2002 12:17:38 -0000 1.7 +++ ThreadContext.java 19 Jul 2002 03:50:02 -0000 1.8 @@ -18,16 +18,16 @@ * As such the type of data contained in <code>ThreadContext</code> * is usually data such as ContextClassLoader, Transaction ID, * User ID/Subject, etc. Note that these ThreadLocal variables may - * not actually be implemented using the <code>ThreadLocal</code> + * not actually be implemented using the {@link ThreadLocal} * class but need to model such behaviour. * * <p>These variables are managed by the ThreadContext. However as it * is not possible to inject code to be executed at the start of a * thread, new threads may not be activated and the variables may not * be set appropriately. In such cases it is recomended that the developer - * use <code>InheritableThreadLocal</code> as the underlying representation - * of the variable. By doing this the <code>InheritableThreadLocal</code> - * will maintain the appropriate state in newly created Thread.</p> + * use {@link InheritableThreadLocal} as the underlying representation + * of the variable. By doing this the {@link InheritableThreadLocal} + * will maintain the appropriate state in newly created {@link Thread}.</p> * * <p>The policy chosend to manage such state is pluggable by the user. * It is expected developers will provide a policy object that will manage @@ -46,7 +46,7 @@ private static final RuntimePermission c_setThreadContext = new RuntimePermission( "ThreadContext.setThreadContext" ); - private static final InheritableThreadLocal c_context = new InheritableThreadLocal(); + private static final ThreadLocal c_context = new ThreadLocal(); ///Accessor object used to provide policy with access to variables in context private final ThreadContextAccessor m_accessor = new InnerThreadContextAccessor(); @@ -61,7 +61,7 @@ * Map that contains any state that needs to be restored * by ThreadContextPolicy when the ThreadContext is deactivated. */ - private final HashMap m_oldState = new HashMap(); + private final HashMap m_oldState; /** * This variable is set to the thread that the ThreadContext @@ -82,8 +82,8 @@ /** * Set the ThreadContext associated with the current thread. - * This code will also call <code>deactivate()</code> on the old - * <code>ThreadContext</code> if present and <code>activate()</code> + * This code will also call {@link #deactivate()} on the old + * <code>ThreadContext</code> if present and {@link #activate()} * on new <code>ThreadContext</code> (if not null). * * @param threadContext the new ThreadContext @@ -119,12 +119,27 @@ } /** - * Constructor that places values specified in <code>Map</code> + * Construct a ThreadContext with specified policy, map and + * oldState. + * + * @see #ThreadContext(ThreadContextPolicy, Map) + */ + private ThreadContext( final ThreadContextPolicy policy, + final Map map, + final Map oldState ) + { + this( policy, map ); + m_oldState.putAll( oldState ); + } + + /** + * Constructor that places values specified in {@link Map} * into <code>ThreadContext</code>. * * @param map the Map */ - public ThreadContext( final ThreadContextPolicy policy, final Map map ) + public ThreadContext( final ThreadContextPolicy policy, + final Map map ) { if( null == policy ) { @@ -138,6 +153,7 @@ m_policy = policy; m_map = new HashMap(); + m_oldState = new HashMap(); final Iterator keys = map.keySet().iterator(); while( keys.hasNext() ) @@ -158,7 +174,7 @@ */ public ThreadContext duplicate() { - return new ThreadContext( m_policy, m_map ); + return new ThreadContext( m_policy, m_map, m_oldState ); } /** @@ -166,7 +182,7 @@ */ private boolean isActive() { - return ( null != m_activeThread ); + return (null != m_activeThread); } /**
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>