Package org.apache.shiro.env
Class DefaultEnvironment
- java.lang.Object
-
- org.apache.shiro.env.DefaultEnvironment
-
- All Implemented Interfaces:
Environment
,NamedObjectEnvironment
,Destroyable
- Direct Known Subclasses:
BasicIniEnvironment
,DefaultWebEnvironment
public class DefaultEnvironment extends Object implements NamedObjectEnvironment, Destroyable
Simple/defaultEnvironment
implementation that stores Shiro objects as key-value pairs in aMap
instance. The key is the object name, the value is the object itself.- Since:
- 1.2
-
-
Constructor Summary
Constructors Constructor Description DefaultEnvironment()
Creates a new instance with a thread-safeConcurrentHashMap
backing map.DefaultEnvironment(Map<String,?> seed)
Creates a new instance with the specified backing map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
destroy()
Called when this object is being destroyed, allowing any necessary cleanup of internal resources.<T> T
getObject(String name, Class<T> requiredType)
Returns the object in Shiro's environment with the specified name and type ornull
if no object with that name was found.Map<String,Object>
getObjects()
Returns the live (modifiable) internal objects collection.SecurityManager
getSecurityManager()
Returns the application'sSecurityManager
instance accessible in the backing map using thesecurityManagerName
property as the lookup key.String
getSecurityManagerName()
Returns the name of theSecurityManager
instance in the backing map.protected SecurityManager
lookupSecurityManager()
Looks up theSecurityManager
instance in the backing map without performing any non-null guarantees.void
setObject(String name, Object instance)
void
setSecurityManager(SecurityManager securityManager)
void
setSecurityManagerName(String securityManagerName)
Sets the name of theSecurityManager
instance in the backing map.
-
-
-
Field Detail
-
DEFAULT_SECURITY_MANAGER_KEY
public static final String DEFAULT_SECURITY_MANAGER_KEY
The default name under which the application'sSecurityManager
instance may be acquired, equal tosecurityManager
.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DefaultEnvironment
public DefaultEnvironment()
Creates a new instance with a thread-safeConcurrentHashMap
backing map.
-
DefaultEnvironment
public DefaultEnvironment(Map<String,?> seed)
Creates a new instance with the specified backing map.- Parameters:
seed
- backing map to use to maintain Shiro objects.
-
-
Method Detail
-
getSecurityManager
public SecurityManager getSecurityManager() throws IllegalStateException
Returns the application'sSecurityManager
instance accessible in the backing map using thesecurityManagerName
property as the lookup key. This implementation guarantees that a non-null instance is always returned, as this is expected for Environment API end-users. If subclasses have the need to perform the map lookup without this guarantee (for example, during initialization when the instance may not have been added to the map yet), thelookupSecurityManager()
method is provided as an alternative.- Specified by:
getSecurityManager
in interfaceEnvironment
- Returns:
- the application's
SecurityManager
instance accessible in the backing map using thesecurityManagerName
property as the lookup key. - Throws:
IllegalStateException
-
setSecurityManager
public void setSecurityManager(SecurityManager securityManager)
-
lookupSecurityManager
protected SecurityManager lookupSecurityManager()
Looks up theSecurityManager
instance in the backing map without performing any non-null guarantees.- Returns:
- the
SecurityManager
in the backing map, ornull
if it has not yet been populated.
-
getSecurityManagerName
public String getSecurityManagerName()
Returns the name of theSecurityManager
instance in the backing map. Used as a key to lookup the instance. Unless set otherwise, the default issecurityManager
.- Returns:
- the name of the
SecurityManager
instance in the backing map. Used as a key to lookup the instance.
-
setSecurityManagerName
public void setSecurityManagerName(String securityManagerName)
Sets the name of theSecurityManager
instance in the backing map. Used as a key to lookup the instance. Unless set otherwise, the default issecurityManager
.- Parameters:
securityManagerName
- the name of theSecurityManager
instance in the backing map. Used as a key to lookup the instance.
-
getObjects
public Map<String,Object> getObjects()
Returns the live (modifiable) internal objects collection.- Returns:
- the live (modifiable) internal objects collection.
-
getObject
public <T> T getObject(String name, Class<T> requiredType) throws RequiredTypeException
Description copied from interface:NamedObjectEnvironment
Returns the object in Shiro's environment with the specified name and type ornull
if no object with that name was found.- Specified by:
getObject
in interfaceNamedObjectEnvironment
- Type Parameters:
T
- the type of the class- Parameters:
name
- the assigned name of the object.requiredType
- the class to which the discovered object must be assignable.- Returns:
- the object in Shiro's environment with the specified name (of the specified type) or
null
if no object with that name was found. - Throws:
RequiredTypeException
- if the discovered object does not equal, extend, or implement the specified class.
-
destroy
public void destroy() throws Exception
Description copied from interface:Destroyable
Called when this object is being destroyed, allowing any necessary cleanup of internal resources.- Specified by:
destroy
in interfaceDestroyable
- Throws:
Exception
- if an exception occurs during object destruction.
-
-