You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by lh...@apache.org on 2015/04/12 05:02:39 UTC

svn commit: r1672972 - /shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java

Author: lhazlewood
Date: Sun Apr 12 03:02:39 2015
New Revision: 1672972

URL: http://svn.apache.org/r1672972
Log:
SHIRO-498: applied patch

Modified:
    shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java

Modified: shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java
URL: http://svn.apache.org/viewvc/shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java?rev=1672972&r1=1672971&r2=1672972&view=diff
==============================================================================
--- shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java (original)
+++ shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java Sun Apr 12 03:02:39 2015
@@ -23,6 +23,7 @@ import org.apache.shiro.subject.Subject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -67,7 +68,11 @@ public abstract class ThreadContext {
      * @return the map of bound resources
      */
     public static Map<Object, Object> getResources() {
-        return resources != null ? new HashMap<Object, Object>(resources.get()) : null;
+        if (resources.get() == null){
+            return Collections.emptyMap();
+        } else {
+            return new HashMap<Object, Object>(resources.get());
+        }
     }
 
     /**
@@ -82,6 +87,7 @@ public abstract class ThreadContext {
         if (CollectionUtils.isEmpty(newResources)) {
             return;
         }
+        ensureResourcesInitialized();
         resources.get().clear();
         resources.get().putAll(newResources);
     }
@@ -96,7 +102,14 @@ public abstract class ThreadContext {
      * @since 1.0
      */
     private static Object getValue(Object key) {
-        return resources.get().get(key);
+        Map<Object, Object> perThreadResources = resources.get();
+        return perThreadResources != null ? perThreadResources.get(key) : null;
+    }
+
+    private static void ensureResourcesInitialized(){
+        if (resources.get() == null){
+           resources.set(new HashMap<Object, Object>());
+        }
     }
 
     /**
@@ -147,6 +160,7 @@ public abstract class ThreadContext {
             return;
         }
 
+        ensureResourcesInitialized();
         resources.get().put(key, value);
 
         if (log.isTraceEnabled()) {
@@ -165,7 +179,8 @@ public abstract class ThreadContext {
      *         under the specified <tt>key</tt> name.
      */
     public static Object remove(Object key) {
-        Object value = resources.get().remove(key);
+        Map<Object, Object> perThreadResources = resources.get();
+        Object value = perThreadResources != null ? perThreadResources.remove(key) : null;
 
         if ((value != null) && log.isTraceEnabled()) {
             String msg = "Removed value of type [" + value.getClass().getName() + "] for key [" +
@@ -307,9 +322,6 @@ public abstract class ThreadContext {
     }
     
     private static final class InheritableThreadLocalMap<T extends Map<Object, Object>> extends InheritableThreadLocal<Map<Object, Object>> {
-        protected Map<Object, Object> initialValue() {
-            return new HashMap<Object, Object>();
-        }
 
         /**
          * This implementation was added to address a