public SecurityClassRealm( CodeBase codeBase, String className, ApplicationContext applicationContext) throws InstantiationException, IllegalAccessException, IOException, ClassNotFoundException { this.codeBase = codeBase; this.className = className; this.applicationContext = applicationContext; doEnsureDelegate(); }
@Override public String encryptPassword(String password) { return ensureDelegate().encryptPassword(password); }
private PortofinoRealm doEnsureDelegate() throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException { Class<?> scriptClass = codeBase.loadClass(className); if(scriptClass.isInstance(security)) { //Class did not change return security; } else { logger.info("Refreshing Portofino Realm Delegate instance (Security.groovy)"); if(security != null) { logger.debug("Script class changed: from " + security.getClass() + " to " + scriptClass); } Object securityTemp = scriptClass.newInstance(); if(securityTemp instanceof PortofinoRealm) { PortofinoRealm realm = (PortofinoRealm) securityTemp; configureDelegate(realm); PortofinoRealm oldSecurity = security; security = realm; LifecycleUtils.destroy(oldSecurity); return realm; } else { throw new ClassCastException( "Security object is not an instance of " + PortofinoRealm.class + ": " + securityTemp + " (" + securityTemp.getClass().getSuperclass() + " " + Arrays.asList(securityTemp.getClass().getInterfaces()) + ")"); } } }
SecurityClassRealm realm = new SecurityClassRealm(codeBase, "Security", applicationContext); LifecycleUtils.init(realm); rsm.setRealm(realm);
@Override public void checkPermission(PrincipalCollection subjectPrincipal, Permission permission) throws AuthorizationException { ensureDelegate().checkPermission(subjectPrincipal, permission); }
private synchronized PortofinoRealm ensureDelegate() { if(destroyed) { throw new IllegalStateException("This realm has been destroyed."); } try { return doEnsureDelegate(); } catch (Exception e) { throw new Error("Security.groovy not found or not loadable", e); } }
@Override public boolean[] hasRoles(PrincipalCollection subjectPrincipal, List<String> roleIdentifiers) { return ensureDelegate().hasRoles(subjectPrincipal, roleIdentifiers); }
@Override public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { return ensureDelegate().getAuthenticationInfo(token); }
@Override public void verifyUser(Serializable user) { ensureDelegate().verifyUser(user); }
@Override public String generateOneTimeToken(Serializable user) { return ensureDelegate().generateOneTimeToken(user); }
@Override public Map<Serializable, String> getUsers() { return ensureDelegate().getUsers(); }
@Override public Set<String> getGroups() { return ensureDelegate().getGroups(); }
@Override public Set<String> getGroups(Object principal) { return ensureDelegate().getGroups(principal); }
@Override public String generateWebToken(Object principal) { return ensureDelegate().generateWebToken(principal); }
@Override public boolean isPermittedAll(PrincipalCollection subjectPrincipal, String... permissions) { return ensureDelegate().isPermittedAll(subjectPrincipal, permissions); }
@Override public void checkRoles(PrincipalCollection subjectPrincipal, String... roleIdentifiers) throws AuthorizationException { ensureDelegate().checkRoles(subjectPrincipal, roleIdentifiers); }
@Override public Serializable getUserById(String encodedUserId) { return ensureDelegate().getUserById(encodedUserId); }
@Override public ClassAccessor getSelfRegisteredUserClassAccessor() { return ensureDelegate().getSelfRegisteredUserClassAccessor(); }
@Override public Serializable getUserId(Serializable user) { return ensureDelegate().getUserId(user); }
@Override public boolean isPermitted(PrincipalCollection principals, String permission) { return ensureDelegate().isPermitted(principals, permission); }