2021SC@SDUSC
CachingRealm的类继承关系图
CachingRealm的变量
Logger:使用指定类初始化日志对象,在日志输出的时候,可以打印出日志信息所在类。
INSTANCE_COUNT:原子数,用来标识当前实例,仅在默认构造函数中为当前实例命名是参与命名标识。
CachingRealm的实例变量
name:该实例Realm的标识
cachingEnabled:此Realm是否使用缓存
caheManager: 缓存管理器,使用缓存可以避免需要授权信息时频繁的调用数据库查询的问题。
CachingRealm的函数方法
- CachingRealm()
//默认的无参数的构造函数,默认cachingEnabled (一般高速缓存)为true
//并根据类名设置name
public CachingRealm() {
this.cachingEnabled = true;
this.name = getClass().getName() + "_" + INSTANCE_COUNT.getAndIncrement();
}
- getCacheManager()
//返回用于数据缓存以减少 EIS 往返的CacheManager ,如果缓存被禁用,则返回null
public CacheManager getCacheManager() {
return this.cacheManager;
}
- setCacheManager(CacheManager cacheManager)
//设置用于数据缓存的CacheManager以减少 EIS 往返。
//该属性默认为空,表示关闭缓存。
public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
afterCacheManagerSet();
}
- isCachingEnabled()
//如果已经配置了CacheManager,应使用缓存,则返回true否则返回false 。
public boolean isCachingEnabled() {
return cachingEnabled;
}
- setCachingEnabled(boolean cachingEnabled)
//如果已配置CacheManager则设置是否应使用缓存
public void setCachingEnabled(boolean cachingEnabled) {
this.cachingEnabled = cachingEnabled;
}
- getName()
public String getName() {
return name;
}
- setName(String name)
public void setName(String name) {
this.name = name;
}
- afterCacheManagerSet()
//如果子类希望通过setCacheManager(CacheManager) mutator 对在领域实例上设置的CacheManager
//实例做出反应,则可以s实现此模板方法。
protected void afterCacheManagerSet() {
}
- onLogout(PrincipalCollection principals)
//如果启用缓存,这将清除与指定帐户身份关联的所有缓存数据。 子类可以随意覆盖该行为,但一定
//要调用super.onLogout 。
public void onLogout(PrincipalCollection principals) {
clearCache(principals);
}
- isEmpty(PrincipalCollection pc)
private static boolean isEmpty(PrincipalCollection pc) {
return pc == null || pc.isEmpty();
}
- clearCache(PrincipalCollection principals)
//清除与指定帐户身份相关联的任何缓存数据。如果 principals 参数为 null 或为空,不发生动作
// 否则它委托给doClearCache(PrincipalCollection) 。
protected void clearCache(PrincipalCollection principals) {
if (!isEmpty(principals)) {
doClearCache(principals);
log.trace("Cleared cache entries for account with principals [{}]", principals);
}
}
- doClearCache(PrincipalCollection principals)
//这是一个模板函数,子类需要按照自身需求覆盖。
protected void doClearCache(PrincipalCollection principals) {
}
- getAvailablePrincipal(PrincipalCollection principals)
//此方法用于获取认证主体
//如果principles中存在此realm的principles集合,就返回此集合的第一个principle
//如果在调用之前principles里不存在此realm的principles集合,就返回principles中的第一个,即primaryprinciple
protected Object getAvailablePrincipal(PrincipalCollection principals) {
Object primary = null;
if (!isEmpty(principals)) {
Collection thisPrincipals = principals.fromRealm(getName());
if (!CollectionUtils.isEmpty(thisPrincipals)) {
primary = thisPrincipals.iterator().next();
} else {
//no principals attributed to this particular realm. Fall back to the 'master' primary:
primary = principals.getPrimaryPrincipal();
}
}
return primary;
}