Updated Branches:
  refs/heads/wicket-1.5.x f8bc999ed -> bd071e2c0

WICKET-4574 ThreadContext does not properly remove the ThreadLocal from the 
current thread


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/bd071e2c
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/bd071e2c
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/bd071e2c

Branch: refs/heads/wicket-1.5.x
Commit: bd071e2c07ad6ce70b2fda2bdc7db20d8aa60e74
Parents: f8bc999
Author: Martin Tzvetanov Grigorov <[email protected]>
Authored: Mon May 28 15:16:06 2012 +0300
Committer: Martin Tzvetanov Grigorov <[email protected]>
Committed: Sun Jun 3 07:36:54 2012 +0300

----------------------------------------------------------------------
 .../main/java/org/apache/wicket/ThreadContext.java |   28 ++++++++++++---
 1 files changed, 23 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/bd071e2c/wicket-core/src/main/java/org/apache/wicket/ThreadContext.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ThreadContext.java 
b/wicket-core/src/main/java/org/apache/wicket/ThreadContext.java
index a01fd51..a4f3f7f 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ThreadContext.java
+++ b/wicket-core/src/main/java/org/apache/wicket/ThreadContext.java
@@ -31,7 +31,7 @@ public class ThreadContext
 
        private Session session;
 
-       private static ThreadLocal<ThreadContext> threadLocal = new 
ThreadLocal<ThreadContext>();
+       private static final ThreadLocal<ThreadContext> threadLocal = new 
ThreadLocal<ThreadContext>();
 
        /**
         * INTERNAL METHOD
@@ -42,10 +42,21 @@ public class ThreadContext
        public static ThreadContext get(boolean createIfDoesNotExist)
        {
                ThreadContext context = threadLocal.get();
-               if (createIfDoesNotExist && context == null)
+               if (context == null)
                {
-                       context = new ThreadContext();
-                       threadLocal.set(context);
+                       if (createIfDoesNotExist)
+                       {
+                               context = new ThreadContext();
+                               threadLocal.set(context);
+                       }
+                       else
+                       {
+                               /*
+                                * There is no ThreadContext set, but the 
threadLocal.get() operation has registered
+                                * registered the threadLocal in this Thread's 
ThreadLocal map. We must now remove it.
+                                */
+                               threadLocal.remove();
+                       }
                }
                return context;
        }
@@ -140,7 +151,14 @@ public class ThreadContext
         */
        public static void restore(ThreadContext threadContext)
        {
-               threadLocal.set(threadContext);
+               if (threadContext == null)
+               {
+                       threadLocal.remove();
+               }
+               else
+               {
+                       threadLocal.set(threadContext);
+               }
        }
 
        /**

Reply via email to