RealmSecurityManager抽象类源码解析

RealmSecurityManager抽象类继承了CachingSecurityManager缓存安全管理器,先对其解析如下:

1.CachingSecurityManager缓存安全管理器

这部分的源码解析可以参照CachingSecurityManager抽象类源码解析。

2.RealmSecurityManager抽象类

2.1数据属性(安全数据信息)

private Collection<Realm> realms;

2.2.构造方法

public RealmSecurityManager() {
        super();
}

2.3.设置单个realm

public void setRealm(Realm realm) {
        if (realm == null) {
            throw new IllegalArgumentException("Realm argument cannot be null");
        }
        Collection<Realm> realms = new ArrayList<Realm>(1);
        realms.add(realm);
        setRealms(realms);
    }

2.4.设置realms集合

public void setRealms(Collection<Realm> realms) {
        if (realms == null) {
            throw new IllegalArgumentException("Realms collection argument cannot be null.");
        }
        if (realms.isEmpty()) {
            throw new IllegalArgumentException("Realms collection argument cannot be empty.");
        }
        this.realms = realms;
        afterRealmsSet();
    }

2.5.设置realms后调用的方法(内部方法,暂时性将缓存管理器赋值给每一个realm)

protected void afterRealmsSet() {
        applyCacheManagerToRealms();
}

2.6.将缓存管理器赋值给每一个realm(内部方法)

protected void applyCacheManagerToRealms() {
        CacheManager cacheManager = getCacheManager();
        Collection<Realm> realms = getRealms();
        if (cacheManager != null && realms != null && !realms.isEmpty()) {
            for (Realm realm : realms) {
                if (realm instanceof CacheManagerAware) {
                    ((CacheManagerAware) realm).setCacheManager(cacheManager);
                }
            }
        }
    }

2.7.获取所有的realm列表

public Collection<Realm> getRealms() {
        return realms;
}

2.8.设置完缓存管理器后调用的方法(继承自CachingSecurityManager中的afterCacheManagerSet,在设置完缓存管理器后,将每一个realm的缓存管理器设置为当前缓存管理器)

protected void afterCacheManagerSet() {
        applyCacheManagerToRealms();
    }

2.9.注销方法(声明周期销毁了realms,然后设置realms为空,并且调用CachingSecurityManager中的注销方法,将缓存管理器进行注销处理)

public void destroy() {
        LifecycleUtils.destroy(getRealms());
        this.realms = null;
        super.destroy();
    }

相关推荐