shiro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lhazlew...@apache.org
Subject svn commit: r1672973 - /shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/util/ThreadContext.java
Date Sun, 12 Apr 2015 03:05:10 GMT
Author: lhazlewood
Date: Sun Apr 12 03:05:10 2015
New Revision: 1672973

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

Modified:
    shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/util/ThreadContext.java

Modified: shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/util/ThreadContext.java
URL: http://svn.apache.org/viewvc/shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/util/ThreadContext.java?rev=1672973&r1=1672972&r2=1672973&view=diff
==============================================================================
--- shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/util/ThreadContext.java
(original)
+++ shiro/branches/2.0-api-design-changes/core/src/main/java/org/apache/shiro/util/ThreadContext.java
Sun Apr 12 03:05:10 2015
@@ -24,6 +24,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.MDC;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -74,7 +75,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());
+        }
     }
 
     /**
@@ -89,6 +94,7 @@ public abstract class ThreadContext {
         if (CollectionUtils.isEmpty(newResources)) {
             return;
         }
+        ensureResourcesInitialized();
         resources.get().clear();
         resources.get().putAll(newResources);
     }
@@ -103,7 +109,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>());
+        }
     }
 
     /**
@@ -154,6 +167,7 @@ public abstract class ThreadContext {
             return;
         }
 
+        ensureResourcesInitialized();
         resources.get().put(key, value);
 
         if (log.isTraceEnabled()) {
@@ -172,7 +186,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 [" +
@@ -316,9 +331,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



Mime
View raw message