From f7020755be458196ee6d9b3733cfb5f0553fdbcd Mon Sep 17 00:00:00 2001
From: Ben Alex MODE_
settings defined as static final
fields, or a fully qualified classname
* to a concrete implementation of {@link org.acegisecurity.context.SecurityContextHolderStrategy} that provides a
* public no-argument constructor.
There are two ways to specify the desired mode String
. The first is to specify it via the
- * system property keyed on {@link #SYSTEM_PROPERTY}. The second is to call {@link #setStrategyName(String)} before
- * using the class. If neither approach is used, the class will default to using {@link #MODE_THREADLOCAL}, which is
- * backwards compatible, has fewer JVM incompatibilities and is appropriate on servers (whereas {@link #MODE_GLOBAL}
- * is not).
There are two ways to specify the desired strategy mode String
. The first is to specify it via
+ * the system property keyed on {@link #SYSTEM_PROPERTY}. The second is to call {@link #setStrategyName(String)}
+ * before using the class. If neither approach is used, the class will default to using {@link #MODE_THREADLOCAL},
+ * which is backwards compatible, has fewer JVM incompatibilities and is appropriate on servers (whereas {@link
+ * #MODE_GLOBAL} is definitely inappropriate for server use).
null
)
*/
public static SecurityContext getContext() {
- initialize();
-
return strategy.getContext();
}
+ /**
+ * Primarily for troubleshooting purposes, this method shows how many times the class has reinitialized its
+ * SecurityContextHolderStrategy
.
+ *
+ * @return the count (should be one unless you've called {@link #setStrategyName(String)} to switch to an alternate
+ * strategy.
+ */
+ public static int getInitializeCount() {
+ return initializeCount;
+ }
+
private static void initialize() {
if ((strategyName == null) || "".equals(strategyName)) {
// Set default
@@ -88,16 +100,15 @@ public class SecurityContextHolder {
} else {
// Try to load a custom strategy
try {
- if (customStrategy == null) {
- Class clazz = Class.forName(strategyName);
- customStrategy = clazz.getConstructor(new Class[] {});
- }
-
+ Class clazz = Class.forName(strategyName);
+ Constructor customStrategy = clazz.getConstructor(new Class[] {});
strategy = (SecurityContextHolderStrategy) customStrategy.newInstance(new Object[] {});
} catch (Exception ex) {
ReflectionUtils.handleReflectionException(ex);
}
}
+
+ initializeCount++;
}
/**
@@ -106,7 +117,6 @@ public class SecurityContextHolder {
* @param context the new SecurityContext
(may not be null
)
*/
public static void setContext(SecurityContext context) {
- initialize();
strategy.setContext(context);
}
@@ -122,6 +132,6 @@ public class SecurityContextHolder {
}
public String toString() {
- return "SecurityContextHolder[strategy='" + strategyName + "']";
+ return "SecurityContextHolder[strategy='" + strategyName + "'; initializeCount=" + initializeCount + "]";
}
}
diff --git a/core/src/test/java/org/acegisecurity/context/SecurityContextHolderTests.java b/core/src/test/java/org/acegisecurity/context/SecurityContextHolderTests.java
index a086ef0eb4..8178d998c5 100644
--- a/core/src/test/java/org/acegisecurity/context/SecurityContextHolderTests.java
+++ b/core/src/test/java/org/acegisecurity/context/SecurityContextHolderTests.java
@@ -240,8 +240,8 @@ public class SecurityContextHolderTests extends TestCase {
public void testSynchronizationCustomStrategyLoading() {
SecurityContextHolder.setStrategyName(InheritableThreadLocalSecurityContextHolderStrategy.class.getName());
- assertEquals("SecurityContextHolder[strategy='org.acegisecurity.context.InheritableThreadLocalSecurityContextHolderStrategy']",
- new SecurityContextHolder().toString());
+ assertTrue(new SecurityContextHolder().toString()
+ .lastIndexOf("SecurityContextHolder[strategy='org.acegisecurity.context.InheritableThreadLocalSecurityContextHolderStrategy'") != -1);
loadStartAndWaitForThreads(true, "Main_", 10, false, true);
assertEquals("Thread errors detected; review log output for details", 0, errors);
}