org.apache.shiro.subject
Class SimplePrincipalMap

java.lang.Object
  extended by org.apache.shiro.subject.SimplePrincipalMap
All Implemented Interfaces:
Serializable, Iterable, Map<String,Object>, PrincipalCollection, PrincipalMap

public class SimplePrincipalMap
extends Object
implements PrincipalMap

Default implementation of the PrincipalMap interface. *EXPERIMENTAL for Shiro 1.2 - DO NOT USE YET*

Since:
1.2
Author:
Les Hazlewood
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
SimplePrincipalMap()
           
SimplePrincipalMap(Map<String,Map<String,Object>> backingMap)
           
 
Method Summary
 List asList()
          Returns a single Subject's principals retrieved from all configured Realms as a List, or an empty List if there are not any principals.
 Set asSet()
          Returns a single Subject's principals retrieved from all configured Realms as a Set, or an empty Set if there are not any principals.
<T> Collection<T>
byType(Class<T> type)
          Returns all principals assignable from the specified type, or an empty Collection if no principals of that type are contained.
 void clear()
           
 boolean containsKey(Object o)
           
 boolean containsValue(Object o)
           
protected  Map<String,Object> ensureCombinedPrincipals()
           
 Set<Map.Entry<String,Object>> entrySet()
           
 Collection fromRealm(String realmName)
          Returns a single Subject's principals retrieved from the specified Realm only as a Collection, or an empty Collection if there are not any principals from that realm.
 Object get(Object o)
           
 Object getPrimaryPrincipal()
          Returns the primary principal used application-wide to uniquely identify the owning account/Subject.
 Set<String> getRealmNames()
          Returns the realm names that this collection has principals for.
 Object getRealmPrincipal(String realmName, String principalName)
           
 Map<String,Object> getRealmPrincipals(String name)
           
 boolean isEmpty()
          Returns true if this collection is empty, false otherwise.
 Iterator iterator()
           
 Set<String> keySet()
           
<T> T
oneByType(Class<T> type)
          Returns the first discovered principal assignable from the specified type, or null if there are none of the specified type.
 Object put(String s, Object o)
           
 void putAll(Map<? extends String,?> map)
           
 Object remove(Object o)
           
 Object removeRealmPrincipal(String realmName, String principalName)
           
 Object setRealmPrincipal(String realmName, String principalName, Object principal)
           
 Map<String,Object> setRealmPrincipals(String realmName, Map<String,Object> principals)
           
 int size()
           
 Collection<Object> values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

SimplePrincipalMap

public SimplePrincipalMap()

SimplePrincipalMap

public SimplePrincipalMap(Map<String,Map<String,Object>> backingMap)
Method Detail

size

public int size()
Specified by:
size in interface Map<String,Object>

ensureCombinedPrincipals

protected Map<String,Object> ensureCombinedPrincipals()

containsKey

public boolean containsKey(Object o)
Specified by:
containsKey in interface Map<String,Object>

containsValue

public boolean containsValue(Object o)
Specified by:
containsValue in interface Map<String,Object>

get

public Object get(Object o)
Specified by:
get in interface Map<String,Object>

put

public Object put(String s,
                  Object o)
Specified by:
put in interface Map<String,Object>

remove

public Object remove(Object o)
Specified by:
remove in interface Map<String,Object>

putAll

public void putAll(Map<? extends String,?> map)
Specified by:
putAll in interface Map<String,Object>

keySet

public Set<String> keySet()
Specified by:
keySet in interface Map<String,Object>

values

public Collection<Object> values()
Specified by:
values in interface Map<String,Object>

entrySet

public Set<Map.Entry<String,Object>> entrySet()
Specified by:
entrySet in interface Map<String,Object>

clear

public void clear()
Specified by:
clear in interface Map<String,Object>

getPrimaryPrincipal

public Object getPrimaryPrincipal()
Description copied from interface: PrincipalCollection
Returns the primary principal used application-wide to uniquely identify the owning account/Subject.

The value is usually always a uniquely identifying attribute specific to the data source that retrieved the account data. Some examples:

Multi-Realm Applications

In a single-Realm application, typically there is only ever one unique principal to retain and that is the value returned from this method. However, in a multi-Realm application, where the PrincipalCollection might retain principals across more than one realm, the value returned from this method should be the single principal that uniquely identifies the subject for the entire application.

That value is of course application specific, but most applications will typically choose one of the primary principals from one of the Realms.

Shiro's default implementations of this interface make this assumption by usually simply returning Iterable.iterator().next(), which just returns the first returned principal obtained from the first consulted/configured Realm during the authentication attempt. This means in a multi-Realm application, Realm configuraiton order matters if you want to retain this default heuristic.

If this heuristic is not sufficient, most Shiro end-users will need to implement a custom AuthenticationStrategy. An AuthenticationStrategy has exact control over the PrincipalCollection returned at the end of an authentication attempt via the AuthenticationStrategy#afterAllAttempts implementation.

Specified by:
getPrimaryPrincipal in interface PrincipalCollection
Returns:
the primary principal used to uniquely identify the owning account/Subject

oneByType

public <T> T oneByType(Class<T> type)
Description copied from interface: PrincipalCollection
Returns the first discovered principal assignable from the specified type, or null if there are none of the specified type.

Note that this will return null if the 'owning' subject has not yet logged in.

Specified by:
oneByType in interface PrincipalCollection
Parameters:
type - the type of the principal that should be returned.
Returns:
a principal of the specified type or null if there isn't one of the specified type.

byType

public <T> Collection<T> byType(Class<T> type)
Description copied from interface: PrincipalCollection
Returns all principals assignable from the specified type, or an empty Collection if no principals of that type are contained.

Note that this will return an empty Collection if the 'owning' subject has not yet logged in.

Specified by:
byType in interface PrincipalCollection
Parameters:
type - the type of the principals that should be returned.
Returns:
a Collection of principals that are assignable from the specified type, or an empty Collection if no principals of this type are associated.

asList

public List asList()
Description copied from interface: PrincipalCollection
Returns a single Subject's principals retrieved from all configured Realms as a List, or an empty List if there are not any principals.

Note that this will return an empty List if the 'owning' subject has not yet logged in.

Specified by:
asList in interface PrincipalCollection
Returns:
a single Subject's principals retrieved from all configured Realms as a List.

asSet

public Set asSet()
Description copied from interface: PrincipalCollection
Returns a single Subject's principals retrieved from all configured Realms as a Set, or an empty Set if there are not any principals.

Note that this will return an empty Set if the 'owning' subject has not yet logged in.

Specified by:
asSet in interface PrincipalCollection
Returns:
a single Subject's principals retrieved from all configured Realms as a Set.

fromRealm

public Collection fromRealm(String realmName)
Description copied from interface: PrincipalCollection
Returns a single Subject's principals retrieved from the specified Realm only as a Collection, or an empty Collection if there are not any principals from that realm.

Note that this will return an empty Collection if the 'owning' subject has not yet logged in.

Specified by:
fromRealm in interface PrincipalCollection
Parameters:
realmName - the name of the Realm from which the principals were retrieved.
Returns:
the Subject's principals from the specified Realm only as a Collection or an empty Collection if there are not any principals from that realm.

getRealmNames

public Set<String> getRealmNames()
Description copied from interface: PrincipalCollection
Returns the realm names that this collection has principals for.

Specified by:
getRealmNames in interface PrincipalCollection
Returns:
the names of realms that this collection has one or more principals for.

isEmpty

public boolean isEmpty()
Description copied from interface: PrincipalCollection
Returns true if this collection is empty, false otherwise.

Specified by:
isEmpty in interface Map<String,Object>
Specified by:
isEmpty in interface PrincipalCollection
Returns:
true if this collection is empty, false otherwise.

iterator

public Iterator iterator()
Specified by:
iterator in interface Iterable

getRealmPrincipals

public Map<String,Object> getRealmPrincipals(String name)
Specified by:
getRealmPrincipals in interface PrincipalMap

setRealmPrincipals

public Map<String,Object> setRealmPrincipals(String realmName,
                                             Map<String,Object> principals)
Specified by:
setRealmPrincipals in interface PrincipalMap

setRealmPrincipal

public Object setRealmPrincipal(String realmName,
                                String principalName,
                                Object principal)
Specified by:
setRealmPrincipal in interface PrincipalMap

getRealmPrincipal

public Object getRealmPrincipal(String realmName,
                                String principalName)
Specified by:
getRealmPrincipal in interface PrincipalMap

removeRealmPrincipal

public Object removeRealmPrincipal(String realmName,
                                   String principalName)
Specified by:
removeRealmPrincipal in interface PrincipalMap


Copyright © 2004-2012 The Apache Software Foundation. All Rights Reserved.