From: Tom Pantelis Date: Sat, 17 Mar 2018 01:24:05 +0000 (-0400) Subject: Remove static AuthenticationManager instance X-Git-Tag: release/fluorine~66 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=d97016b1e22fea17160802cfe1ebe0715f6405b2;p=aaa.git Remove static AuthenticationManager instance It's only used by UT's. Change-Id: I25271cd06d578942b7cf9cd35a38a338c5527f29 Signed-off-by: Tom Pantelis --- diff --git a/aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/tokenauthrealm/auth/AuthenticationManager.java b/aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/tokenauthrealm/auth/AuthenticationManager.java index 728e63c5d..5ba9573c0 100644 --- a/aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/tokenauthrealm/auth/AuthenticationManager.java +++ b/aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/tokenauthrealm/auth/AuthenticationManager.java @@ -8,7 +8,6 @@ package org.opendaylight.aaa.shiro.tokenauthrealm.auth; import java.util.Dictionary; -import java.util.Hashtable; import org.opendaylight.aaa.api.Authentication; import org.opendaylight.aaa.api.AuthenticationService; import org.osgi.service.cm.ConfigurationException; @@ -23,25 +22,15 @@ public class AuthenticationManager implements AuthenticationService, ManagedServ private static final String AUTH_ENABLED_ERR = "Error setting authEnabled"; protected static final String AUTH_ENABLED = "authEnabled"; - protected static final Dictionary DEFAULTS = new Hashtable<>(); - - static { - DEFAULTS.put(AUTH_ENABLED, Boolean.FALSE.toString()); - } // In non-Karaf environments, authEnabled is set to false by default - private static volatile boolean authEnabled = false; + private volatile boolean authEnabled = false; - private static final AuthenticationManager AUTHENTICATION_MANAGER = new AuthenticationManager(); private final ThreadLocal auth = new InheritableThreadLocal<>(); public AuthenticationManager() { } - static AuthenticationManager instance() { - return AUTHENTICATION_MANAGER; - } - @Override public Authentication get() { return auth.get(); @@ -59,7 +48,7 @@ public class AuthenticationManager implements AuthenticationService, ManagedServ @Override public boolean isAuthEnabled() { - return this.authEnabled; + return authEnabled; } @Override diff --git a/aaa-shiro/impl/src/test/java/org/opendaylight/aaa/shiro/tokenauthrealm/auth/AuthenticationManagerTest.java b/aaa-shiro/impl/src/test/java/org/opendaylight/aaa/shiro/tokenauthrealm/auth/AuthenticationManagerTest.java index fae42b697..c202c17e5 100644 --- a/aaa-shiro/impl/src/test/java/org/opendaylight/aaa/shiro/tokenauthrealm/auth/AuthenticationManagerTest.java +++ b/aaa-shiro/impl/src/test/java/org/opendaylight/aaa/shiro/tokenauthrealm/auth/AuthenticationManagerTest.java @@ -24,37 +24,36 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import org.junit.Test; import org.opendaylight.aaa.api.Authentication; -import org.opendaylight.aaa.api.AuthenticationService; import org.osgi.service.cm.ConfigurationException; public class AuthenticationManagerTest { + private final AuthenticationManager authManager = new AuthenticationManager(); + @Test public void testAuthenticationCrudSameThread() { Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob") .setUserId("1234").addRole("admin").addRole("guest").build()).build(); - AuthenticationService as = AuthenticationManager.instance(); - assertNotNull(as); + assertNotNull(authManager); - as.set(auth); - assertEquals(auth, as.get()); + authManager.set(auth); + assertEquals(auth, authManager.get()); - as.clear(); - assertNull(as.get()); + authManager.clear(); + assertNull(authManager.get()); } @Test public void testAuthenticationCrudSpawnedThread() throws InterruptedException, ExecutionException { - AuthenticationService as = AuthenticationManager.instance(); Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob") .setUserId("1234").addRole("admin").addRole("guest").build()).build(); - as.set(auth); + authManager.set(auth); Future future = Executors.newSingleThreadExecutor().submit(new Worker()); assertEquals(auth, future.get()); - as.clear(); + authManager.clear(); future = Executors.newSingleThreadExecutor().submit(new Worker()); assertNull(future.get()); } @@ -62,18 +61,17 @@ public class AuthenticationManagerTest { @Test public void testAuthenticationCrudSpawnedThreadPool() throws InterruptedException, ExecutionException { - AuthenticationService as = AuthenticationManager.instance(); Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob") .setUserId("1234").addRole("admin").addRole("guest").build()).build(); - as.set(auth); + authManager.set(auth); List> fs = Executors.newFixedThreadPool(2).invokeAll( Arrays.asList(new Worker(), new Worker())); for (Future f : fs) { assertEquals(auth, f.get()); } - as.clear(); + authManager.clear(); fs = Executors.newFixedThreadPool(2).invokeAll(Arrays.asList(new Worker(), new Worker())); for (Future f : fs) { assertNull(f.get()); @@ -83,51 +81,46 @@ public class AuthenticationManagerTest { @Test public void testUpdatedValid() throws ConfigurationException { Dictionary props = new Hashtable<>(); - AuthenticationManager as = AuthenticationManager.instance(); - assertFalse(as.isAuthEnabled()); + assertFalse(authManager.isAuthEnabled()); props.put(AuthenticationManager.AUTH_ENABLED, "TrUe"); - as.updated(props); - assertTrue(as.isAuthEnabled()); + authManager.updated(props); + assertTrue(authManager.isAuthEnabled()); props.put(AuthenticationManager.AUTH_ENABLED, "FaLsE"); - as.updated(props); - assertFalse(as.isAuthEnabled()); + authManager.updated(props); + assertFalse(authManager.isAuthEnabled()); } @Test public void testUpdatedNullProperty() throws ConfigurationException { - AuthenticationManager as = AuthenticationManager.instance(); - assertFalse(as.isAuthEnabled()); - as.updated(null); - assertFalse(as.isAuthEnabled()); + assertFalse(authManager.isAuthEnabled()); + authManager.updated(null); + assertFalse(authManager.isAuthEnabled()); } @Test(expected = ConfigurationException.class) public void testUpdatedInvalidValue() throws ConfigurationException { - AuthenticationManager as = AuthenticationManager.instance(); Dictionary props = new Hashtable<>(); props.put(AuthenticationManager.AUTH_ENABLED, "yes"); - as.updated(props); + authManager.updated(props); } @Test(expected = ConfigurationException.class) public void testUpdatedInvalidKey() throws ConfigurationException { - AuthenticationManager as = AuthenticationManager.instance(); Dictionary props = new Hashtable<>(); props.put("Invalid Key", "true"); - as.updated(props); + authManager.updated(props); } private class Worker implements Callable { @Override public Authentication call() throws Exception { - AuthenticationService as = AuthenticationManager.instance(); - return as.get(); + return authManager.get(); } } }