相关阅读
简介
Subject
关联的Principal
集合;
核心方法
/**
* 获取首要principal
*/
Object getPrimaryPrincipal();
/**
* 获取满足指定类型的首个Principal
*/
<T> T oneByType(Class<T> type);
/**
* 获取满足指定类型的所有principal
*/
<T> Collection<T> byType(Class<T> type);
/**
* 将principal集合以List形式返回
*/
List asList();
/**
* 将principal集合以Set形式返回
*/
Set asSet();
/**
* 获取来自指定Realm的principal集合
*/
Collection fromRealm(String realmName);
/**
* 获取Realm的集合
*/
Set<String> getRealmNames();
/**
* principal集合是否为空
*/
boolean isEmpty();
实现子类
public interface PrincipalCollection extends Iterable, Serializable
public interface MutablePrincipalCollection extends PrincipalCollection
public class SimplePrincipalCollection implements MutablePrincipalCollection
public interface PrincipalMap extends PrincipalCollection, Map<String,Object>
public class SimplePrincipalCollection implements MutablePrincipalCollection
MutablePrincipalCollection
简介
扩展PrincipalCollection
接口,支持修改;
核心方法
/**
* 增加principal
*/
void add(Object principal, String realmName);
/**
* 增加principal集合
*/
void addAll(Collection principals, String realmName);
/**
* 增加principalCollection
*/
void addAll(PrincipalCollection principals);
/**
* 清空所有principal
*/
void clear();
SimplePrincipalCollection
简介
内部使用LinkedHashMap
存储Principal
的MutablePrincipalCollection
的简单实现;
核心方法
// 存储来自各个Realm的principal
private Map<String, Set> realmPrincipals;
// 缓存toString()的结果,日志中使用
private transient String cachedToString;
/**
* 构造方法
*/
public SimplePrincipalCollection(Object principal, String realmName) {
if (principal instanceof Collection) {
// 添加principal集合
addAll((Collection) principal, realmName);
} else {
// 添加单个principal
add(principal, realmName);
}
}
/**
* 获取指定Realm的principal集合
*/
protected Collection getPrincipalsLazy(String realmName) {
if (realmPrincipals == null) {
// 如果还未初始化,则先初始化
realmPrincipals = new LinkedHashMap<String, Set>();
}
Set principals = realmPrincipals.get(realmName);
if (principals == null) {
// 如果还不存在该Realm的principal集合,则先创建空principal集合
principals = new LinkedHashSet();
realmPrincipals.put(realmName, principals);
}
return principals;
}
/**
* 获取首要principal
*/
public Object getPrimaryPrincipal() {
if (isEmpty()) {
return null;
}
// 返回首个principal
// Realm的配置顺序很重要
return iterator().next();
}
/**
* 增加principal
*/
public void add(Object principal, String realmName) {
// 校验Realm名称
if (realmName == null) {
throw new NullPointerException("realmName argument cannot be null.");
}
// 校验principal
if (principal == null) {
throw new NullPointerException("principal argument cannot be null.");
}
// 清除缓存结果
this.cachedToString = null;
// 添加该Realm的principal
getPrincipalsLazy(realmName).add(principal);
}
/**
* 增加principal集合
*/
public void addAll(Collection principals, String realmName) {
// 校验Realm名称
if (realmName == null) {
throw new NullPointerException("realmName argument cannot be null.");
}
// 校验principals
if (principals == null) {
throw new NullPointerException("principals argument cannot be null.");
}
// 校验principals
if (principals.isEmpty()) {
throw new IllegalArgumentException("principals argument cannot be an empty collection.");
}
// 清除缓存结果
this.cachedToString = null;
// 添加该Realm的principal集合
getPrincipalsLazy(realmName).addAll(principals);
}
/**
* 增加principalCollection
*/
public void addAll(PrincipalCollection principals) {
// 当principalCollection存在Realms
if (principals.getRealmNames() != null) {
for (String realmName : principals.getRealmNames()) {
for (Object principal : principals.fromRealm(realmName)) {
// 依次增加Realm的principal
// 如果使用addAll(principals.fromRealm(realmName), realmName),当存在空principal集合会抛出异常
add(principal, realmName);
}
}
}
}
/**
* 获取满足指定类型的首个Principal
*/
public <T> T oneByType(Class<T> type) {
// 校验principal集合
if (realmPrincipals == null || realmPrincipals.isEmpty()) {
return null;
}
Collection<Set> values = realmPrincipals.values();
// 遍历各Realm下的所有principal
for (Set set : values) {
for (Object o : set) {
// 类型判断
if (type.isAssignableFrom(o.getClass())) {
return (T) o;
}
}
}
return null;
}
/**
* 获取满足指定类型的所有principal
*/
public <T> Collection<T> byType(Class<T> type) {
// 校验principal集合
if (realmPrincipals == null || realmPrincipals.isEmpty()) {
return Collections.EMPTY_SET;
}
Set<T> typed = new LinkedHashSet<T>();
Collection<Set> values = realmPrincipals.values();
// 遍历各Realm下的所有principal
for (Set set : values) {
for (Object o : set) {
// 类型判断
if (type.isAssignableFrom(o.getClass())) {
typed.add((T) o);
}
}
}
if (typed.isEmpty()) {
return Collections.EMPTY_SET;
}
return Collections.unmodifiableSet(typed);
}
/**
* 将principal集合以List形式返回
*/
public List asList() {
// 先转换为Set形式
Set all = asSet();
if (all.isEmpty()) {
return Collections.EMPTY_LIST;
}
// 转换成List形式
return Collections.unmodifiableList(new ArrayList(all));
}
/**
* 将principal集合以Set形式返回
*/
public Set asSet() {
// 校验principal集合
if (realmPrincipals == null || realmPrincipals.isEmpty()) {
return Collections.EMPTY_SET;
}
// 使用LinkedHashSet保证原顺序
Set aggregated = new LinkedHashSet();
Collection<Set> values = realmPrincipals.values();
for (Set set : values) {
aggregated.addAll(set);
}
if (aggregated.isEmpty()) {
return Collections.EMPTY_SET;
}
return Collections.unmodifiableSet(aggregated);
}
/**
* 获取来自指定Realm的principal集合
*/
public Collection fromRealm(String realmName) {
// 校验principal集合
if (realmPrincipals == null || realmPrincipals.isEmpty()) {
return Collections.EMPTY_SET;
}
Set principals = realmPrincipals.get(realmName);
if (principals == null || principals.isEmpty()) {
principals = Collections.EMPTY_SET;
}
return Collections.unmodifiableSet(principals);
}
/**
* 获取Realm的集合
*/
public Set<String> getRealmNames() {
// 校验principal集合
if (realmPrincipals == null) {
return null;
} else {
return realmPrincipals.keySet();
}
}
/**
* principal集合是否为空
*/
public boolean isEmpty() {
return realmPrincipals == null || realmPrincipals.isEmpty();
}
/**
* 清空所有principal
*/
public void clear() {
// 清除缓存结果
this.cachedToString = null;
if (realmPrincipals != null) {
realmPrincipals.clear();
// 设置为null,便于垃圾回收
realmPrincipals = null;
}
}
/**
* 返回principal集合的迭代器
*/
public Iterator iterator() {
// 借助含有所有principal的不可变Set的迭代器
return asSet().iterator();
}
PrincipalMap
简介
扩展PrincipalCollection
接口,支持根据realmName
和principalName
查找Principal
,内部使用HashMap
存储来自不同Realm
的所有Principal
;
还处于实验中,暂时不能使用;
核心方法
/**
* 获取指定Realm的principal集合
*/
Map<String,Object> getRealmPrincipals(String realmName);
/**
* 设置指定Realm的principal集合
*/
Map<String,Object> setRealmPrincipals(String realmName, Map<String,Object> principals);
/**
* 设置指定Realm的指定名称的principal
*/
Object setRealmPrincipal(String realmName, String principalName, Object principal);
/**
* 获取指定Realm的指定名称的principal
*/
Object getRealmPrincipal(String realmName, String realmPrincipal);
/**
* 移除指定Realm的指定principal
*/
Object removeRealmPrincipal(String realmName, String principalName);
SimplePrincipalMap
简介
PrincipalMap
的默认实现;
还处于实验中,暂时不能使用;
核心方法
// 按照Realm分级存储所有principal
private Map<String, Map<String, Object>> realmPrincipals;
// principal集合
private Map<String, Object> combinedPrincipals;
/**
* 构造方法
*/
public SimplePrincipalMap(Map<String, Map<String, Object>> backingMap) {
if (!CollectionUtils.isEmpty(backingMap)) {
this.realmPrincipals = backingMap;
for (Map<String, Object> principals : this.realmPrincipals.values()) {
if (!CollectionUtils.isEmpty(principals) ) {
ensureCombinedPrincipals().putAll(principals);
}
}
}
}
/**
* 获取当前principal数量
*/
public int size() {
return CollectionUtils.size(this.combinedPrincipals);
}
/**
* 确保存储principal的Map有效
*/
protected Map<String, Object> ensureCombinedPrincipals() {
if (this.combinedPrincipals == null) {
this.combinedPrincipals = new HashMap<String, Object>();
}
return this.combinedPrincipals;
}
/**
* 是否存在指定的principalName
*/
public boolean containsKey(Object o) {
return this.combinedPrincipals != null && this.combinedPrincipals.containsKey(o);
}
/**
* 是否存在指定的principal
*/
public boolean containsValue(Object o) {
return this.combinedPrincipals != null && this.combinedPrincipals.containsKey(o);
}
/**
* 获取指定的principal
*/
public Object get(Object o) {
return this.combinedPrincipals != null && this.combinedPrincipals.containsKey(o);
}
/**
* 设置指定的principal
*/
public Object put(String s, Object o) {
return ensureCombinedPrincipals().put(s, o);
}
/**
* 移除指定的principal
*/
public Object remove(Object o) {
return this.combinedPrincipals != null ? this.combinedPrincipals.remove(o) : null;
}
/**
* 添加principal集合
*/
public void putAll(Map<? extends String, ?> map) {
if (!CollectionUtils.isEmpty(map)) {
ensureCombinedPrincipals().putAll(map);
}
}
/**
* 获取所有的principalName
*/
public Set<String> keySet() {
return CollectionUtils.isEmpty(this.combinedPrincipals) ?
Collections.<String>emptySet() :
Collections.unmodifiableSet(this.combinedPrincipals.keySet());
}
/**
* 获取所有的principal
*/
public Collection<Object> values() {
return CollectionUtils.isEmpty(this.combinedPrincipals) ?
Collections.emptySet() :
Collections.unmodifiableCollection(this.combinedPrincipals.values());
}
/**
* 获取所有的principalName/principal健值对
*/
public Set<Entry<String, Object>> entrySet() {
return CollectionUtils.isEmpty(this.combinedPrincipals) ?
Collections.<Entry<String,Object>>emptySet() :
Collections.unmodifiableSet(this.combinedPrincipals.entrySet());
}
/**
* 清除数据
*/
public void clear() {
this.realmPrincipals = null;
this.combinedPrincipals = null;
}
/**
* 获取首要principal
*/
public Object getPrimaryPrincipal() {
//heuristic - just use the first one we come across:
return !CollectionUtils.isEmpty(this.combinedPrincipals) ?
this.combinedPrincipals.values().iterator().next() :
null;
}
/**
* 获取满足指定类型的首个Principal
*/
public <T> T oneByType(Class<T> type) {
if (CollectionUtils.isEmpty(this.combinedPrincipals)) {
return null;
}
for( Object value : this.combinedPrincipals.values()) {
if (type.isInstance(value) ) {
return type.cast(value);
}
}
return null;
}
/**
* 获取满足指定类型的所有principal
*/
public <T> Collection<T> byType(Class<T> type) {
if (CollectionUtils.isEmpty(this.combinedPrincipals)) {
return Collections.emptySet();
}
Collection<T> instances = null;
for( Object value : this.combinedPrincipals.values()) {
if (type.isInstance(value) ) {
if (instances == null) {
instances = new ArrayList<T>();
}
instances.add(type.cast(value));
}
}
return instances != null ? instances : Collections.<T>emptyList();
}
/**
* 将principal集合以List形式返回
*/
public List asList() {
if (CollectionUtils.isEmpty(this.combinedPrincipals)) {
return Collections.emptyList();
}
List<Object> list = new ArrayList<Object>(this.combinedPrincipals.size());
list.addAll(this.combinedPrincipals.values());
return list;
}
/**
* 将principal集合以Set形式返回
*/
public Set asSet() {
if (CollectionUtils.isEmpty(this.combinedPrincipals)) {
return Collections.emptySet();
}
Set<Object> set = new HashSet<Object>(this.combinedPrincipals.size());
set.addAll(this.combinedPrincipals.values());
return set;
}
/**
* 获取来自指定Realm的principal集合
*/
public Collection fromRealm(String realmName) {
if (CollectionUtils.isEmpty(this.realmPrincipals)) {
return Collections.emptySet();
}
Map<String,Object> principals = this.realmPrincipals.get(realmName);
if (CollectionUtils.isEmpty(principals)) {
return Collections.emptySet();
}
return Collections.unmodifiableCollection(principals.values());
}
/**
* 获取Realm的集合
*/
public Set<String> getRealmNames() {
if (CollectionUtils.isEmpty(this.realmPrincipals)) {
return Collections.emptySet();
}
return Collections.unmodifiableSet(this.realmPrincipals.keySet());
}
/**
* principal集合是否为空
*/
public boolean isEmpty() {
return CollectionUtils.isEmpty(this.combinedPrincipals);
}
/**
* 获取principal集合的迭代器
*/
public Iterator iterator() {
return asList().iterator();
}
/**
* 获取指定Realm的principal集合
*/
public Map<String, Object> getRealmPrincipals(String name) {
if (this.realmPrincipals == null) {
return null;
}
Map<String,Object> principals = this.realmPrincipals.get(name);
if (principals == null) {
return null;
}
return Collections.unmodifiableMap(principals);
}
/**
* 设置指定Realm的principal集合
*/
public Map<String,Object> setRealmPrincipals(String realmName, Map<String, Object> principals) {
if (realmName == null) {
throw new NullPointerException("realmName argument cannot be null.");
}
if (this.realmPrincipals == null) {
if (!CollectionUtils.isEmpty(principals)) {
this.realmPrincipals = new HashMap<String,Map<String,Object>>();
return this.realmPrincipals.put(realmName, new HashMap<String,Object>(principals));
} else {
return null;
}
} else {
Map<String,Object> existingPrincipals = this.realmPrincipals.remove(realmName);
if (!CollectionUtils.isEmpty(principals)) {
this.realmPrincipals.put(realmName, new HashMap<String,Object>(principals));
}
return existingPrincipals;
}
}
/**
* 设置指定Realm的指定名称的principal
*/
public Object setRealmPrincipal(String realmName, String principalName, Object principal) {
if (realmName == null) {
throw new NullPointerException("realmName argument cannot be null.");
}
if (principalName == null) {
throw new NullPointerException(("principalName argument cannot be null."));
}
if (principal == null) {
return removeRealmPrincipal(realmName, principalName);
}
if (this.realmPrincipals == null) {
this.realmPrincipals = new HashMap<String,Map<String,Object>>();
}
Map<String,Object> principals = this.realmPrincipals.get(realmName);
if (principals == null) {
principals = new HashMap<String,Object>();
this.realmPrincipals.put(realmName, principals);
}
return principals.put(principalName, principal);
}
/**
* 获取指定Realm的指定名称的principal
*/
public Object getRealmPrincipal(String realmName, String principalName) {
if (realmName == null) {
throw new NullPointerException("realmName argument cannot be null.");
}
if (principalName == null) {
throw new NullPointerException(("principalName argument cannot be null."));
}
if (this.realmPrincipals == null) {
return null;
}
Map<String,Object> principals = this.realmPrincipals.get(realmName);
if (principals != null) {
return principals.get(principalName);
}
return null;
}
/**
* 移除指定Realm的指定principal
*/
public Object removeRealmPrincipal(String realmName, String principalName) {
if (realmName == null) {
throw new NullPointerException("realmName argument cannot be null.");
}
if (principalName == null) {
throw new NullPointerException(("principalName argument cannot be null."));
}
if (this.realmPrincipals == null) {
return null;
}
Map<String,Object> principals = this.realmPrincipals.get(realmName);
if (principals != null) {
return principals.remove(principalName);
}
return null;
}