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:08:16 UTC
svn commit: r1672975 -
/shiro/branches/1.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java
Author: lhazlewood
Date: Sun Apr 12 03:08:16 2015
New Revision: 1672975
URL: http://svn.apache.org/r1672975
Log:
SHIRO-498: applied patch
Modified:
shiro/branches/1.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java
Modified: shiro/branches/1.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java
URL: http://svn.apache.org/viewvc/shiro/branches/1.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java?rev=1672975&r1=1672974&r2=1672975&view=diff
==============================================================================
--- shiro/branches/1.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java (original)
+++ shiro/branches/1.x/core/src/main/java/org/apache/shiro/util/ThreadContext.java Sun Apr 12 03:08:16 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