org.wamblee.glassfish.auth.cache.impl
Class SimpleExpiryCache

java.lang.Object
  extended by org.wamblee.glassfish.auth.cache.impl.SimpleExpiryCache
All Implemented Interfaces:
AuthenticationCache

public class SimpleExpiryCache
extends java.lang.Object
implements AuthenticationCache

Simple cache that has entries that expire after a certain amount of time. Also, the cache has a JMX interface SimpleExpiryCacheManagementMBean by which the entries for one or all users can be expired. The cache does not do an automatic cleanup of expired items although it is possible to trigger cleanup through JMX.

The cache exposes an MBean for remote management and for managing it from an application. This access is unsecured so anyone can in principle clean the cache. The main intention of the JMX MBean is to use it for invalidating the cache for a specific user in case his authentication information has changed. The MBean is exposed at the domain specified by JMX_DOMAIN with a property specified by JMX_REALM_PROPERTY with the value of the realm.

To clear a specific user, proceed as follows:

 ObjectName objectName = new ObjectName(
     "org.wamblee.glassfish.auth.FlexibleJdbcRealm", "realm", REALM_NAME);
 ManagementFactory.getPlatformMBeanServer().invoke(objectName, "clearUser",
     new Object[] { "username" }, new String[] { String.class.getName() });
 

Author:
Erik Brakkee

Nested Class Summary
static interface SimpleExpiryCache.Clock
           
static class SimpleExpiryCache.SystemClock
           
static class SimpleExpiryCache.UserEntry
          Class representing cached information about a user.
 
Field Summary
static long DEFAULT_EXPIRY_TIME_SECONDS
          Default expiry time in seconds.
static java.lang.String JMX_DOMAIN
           
static java.lang.String PROP_EXPIRY_TIME_SECONDS
          Name of the property that defines the timeout in seconds for caching.
static java.lang.String PROP_REALM_NAME
           
 
Constructor Summary
SimpleExpiryCache(java.util.Properties aProperties)
          Constructs the cache.
SimpleExpiryCache(java.util.Properties aProperties, SimpleExpiryCache.Clock aClock)
          Constructor for unit test.
 
Method Summary
protected  void clearExpired()
           
 SimpleExpiryCache.UserEntry evict(java.lang.String aUsername)
           
 int getExpiryTimeSeconds()
           
 java.util.List<java.lang.String> getGroups(java.lang.String aUsername)
          Gets the groups a user belongs to from the cache.
 java.lang.String getPassword(java.lang.String aUsername)
          Gets the cached password for a given user.
 java.lang.String getSeed(java.lang.String aUsername)
          Gets a cached seed for a given username based on the configured sql seed query.
 void setGroups(java.lang.String aUsername, java.util.List<java.lang.String> aGroups)
          Sets the groups for a given user.
 void setPassword(java.lang.String aUserName, java.lang.String aPassword)
          Sets the cached password for a user.
 void setSeed(java.lang.String aUsername, java.lang.String aSeed)
          Sets the seed.
 int size()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PROP_EXPIRY_TIME_SECONDS

public static final java.lang.String PROP_EXPIRY_TIME_SECONDS
Name of the property that defines the timeout in seconds for caching. In case the value is < 0, then entries in the cache will never expire.

See Also:
Constant Field Values

JMX_DOMAIN

public static final java.lang.String JMX_DOMAIN
See Also:
Constant Field Values

PROP_REALM_NAME

public static final java.lang.String PROP_REALM_NAME
See Also:
Constant Field Values

DEFAULT_EXPIRY_TIME_SECONDS

public static final long DEFAULT_EXPIRY_TIME_SECONDS
Default expiry time in seconds.

See Also:
Constant Field Values
Constructor Detail

SimpleExpiryCache

public SimpleExpiryCache(java.util.Properties aProperties)
Constructs the cache.

Parameters:
aProperties - Properties to configure the cache with.

SimpleExpiryCache

public SimpleExpiryCache(java.util.Properties aProperties,
                         SimpleExpiryCache.Clock aClock)
Constructor for unit test.

Parameters:
aProperties - Properties.
aClock - Clock.
Method Detail

clearExpired

protected void clearExpired()

getExpiryTimeSeconds

public int getExpiryTimeSeconds()

size

public int size()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getGroups

public java.util.List<java.lang.String> getGroups(java.lang.String aUsername)
Description copied from interface: AuthenticationCache
Gets the groups a user belongs to from the cache.

Specified by:
getGroups in interface AuthenticationCache
Parameters:
aUsername - User.
Returns:
Groups. Returns a (possibly) empty list of cached groups or null of no cached groups are found.

getPassword

public java.lang.String getPassword(java.lang.String aUsername)
Description copied from interface: AuthenticationCache
Gets the cached password for a given user.

Specified by:
getPassword in interface AuthenticationCache
Parameters:
aUsername - Username.
Returns:
Password or null if no cached value is available.

getSeed

public java.lang.String getSeed(java.lang.String aUsername)
Description copied from interface: AuthenticationCache
Gets a cached seed for a given username based on the configured sql seed query.

Specified by:
getSeed in interface AuthenticationCache
Parameters:
aUsername - Username.
Returns:
The seed found or the empty string if no seeds or multiple seeds were found. Returns null in case no cached seed is available.

setGroups

public void setGroups(java.lang.String aUsername,
                      java.util.List<java.lang.String> aGroups)
Description copied from interface: AuthenticationCache
Sets the groups for a given user.

Specified by:
setGroups in interface AuthenticationCache
Parameters:
aUsername - User name.
aGroups - Groups (must be non-null).

setPassword

public void setPassword(java.lang.String aUserName,
                        java.lang.String aPassword)
Description copied from interface: AuthenticationCache
Sets the cached password for a user.

Specified by:
setPassword in interface AuthenticationCache
Parameters:
aUserName - Username.
aPassword - Password.

setSeed

public void setSeed(java.lang.String aUsername,
                    java.lang.String aSeed)
Description copied from interface: AuthenticationCache
Sets the seed.

Specified by:
setSeed in interface AuthenticationCache
Parameters:
aUsername - Username.
aSeed - Seed (may not be null).

evict

public SimpleExpiryCache.UserEntry evict(java.lang.String aUsername)


Copyright © 2019. All Rights Reserved.