1 24 25 package com.rift.coad.lib.security; 27 28 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Set ; 35 import java.util.StringTokenizer ; 36 import java.util.Iterator ; 37 38 import org.apache.log4j.Logger; 40 41 import com.rift.coad.lib.configuration.ConfigurationFactory; 43 import com.rift.coad.lib.configuration.Configuration; 44 import com.rift.coad.lib.deployment.DeploymentMonitor; 45 import com.rift.coad.lib.thread.ThreadStateMonitor; 46 47 48 54 public class RoleManager { 55 56 61 public class RoleLoaderThread extends Thread { 62 63 private ThreadStateMonitor stateMonitor = new ThreadStateMonitor(60000); 65 66 69 public RoleLoaderThread() { 70 71 } 72 73 74 77 public void run() { 78 while (!stateMonitor.isTerminated()) { 79 stateMonitor.monitor(); 80 if (!stateMonitor.isTerminated()) { 81 try { 82 loadRoles(); 83 } catch (Throwable ex) { 84 log.error("Failed to load the roles : " + 85 ex.getMessage(),ex); 86 } 87 } 88 } 89 } 90 91 92 95 public void terminate() { 96 if (DeploymentMonitor.getInstance().isTerminated()) { 97 stateMonitor.terminate(true); 98 } 99 } 100 } 101 102 103 private static final String ROLE_HANDLERS = "role_handlers"; 105 106 protected static Logger log = 108 Logger.getLogger(RoleManager.class.getName()); 109 110 111 private static RoleManager singleton = null; 113 private List roleHandlers = null; 114 private Map roles = null; 115 private RoleLoaderThread roleLoaderThread = null; 116 117 122 private RoleManager() throws SecurityException { 123 roleHandlers = new ArrayList (); 124 roles = new HashMap (); 125 126 try { 127 Configuration config = ConfigurationFactory.getInstance().getConfig( 128 RoleManager.class); 129 StringTokenizer roleHandlerList = new StringTokenizer ( 130 config.getString(ROLE_HANDLERS),","); 131 while(roleHandlerList.hasMoreTokens()) { 132 roleHandlers.add( 133 Class.forName(roleHandlerList.nextToken().trim()). 134 newInstance()); 135 } 136 } catch (Exception ex) { 137 throw new SecurityException ( 138 "Failed to load the configuration role handler list : " + 139 ex.getMessage(),ex); 140 } 141 loadRoles(); 142 } 143 144 145 150 private void loadRoles() throws SecurityException { 151 try { 152 Map roles = new HashMap (); 153 for (Iterator iter = roleHandlers.iterator(); iter.hasNext();) { 154 RoleHandler handler = (RoleHandler)iter.next(); 155 Map updatedRoles = handler.getRoles(); 156 for (Iterator roleIter = updatedRoles.keySet().iterator(); 157 roleIter.hasNext();) { 158 String role = (String )roleIter.next(); 159 if (roles.containsKey(role)) { 160 log.info("Duplicate role [" + role + "] from source : " 161 + handler.getClass().getName()); 162 } else { 163 roles.put(role,updatedRoles.get(role)); 164 } 165 } 166 } 167 synchronized (this) { 168 this.roles = roles; 169 } 170 } catch (Exception ex) { 171 throw new SecurityException ("Failed to load the role information : " 172 + ex.getMessage(),ex); 173 } 174 } 175 176 177 183 public static synchronized RoleManager getInstance() 184 throws SecurityException { 185 if (singleton != null) { 186 return singleton; 187 } 188 singleton = new RoleManager(); 189 return singleton; 190 } 191 192 193 196 public void startBackgroundThread() { 197 if (roleLoaderThread == null) { 198 roleLoaderThread = new RoleLoaderThread(); 199 roleLoaderThread.start(); 200 } 201 } 202 203 204 208 public void terminateBackgroundThread() { 209 roleLoaderThread.terminate(); 210 try { 211 roleLoaderThread.join(); 212 } catch (Exception ex) { 213 log.error("Failed to terminate the background thread : " + 214 ex.getMessage(),ex); 215 } 216 } 217 218 219 224 public synchronized Set getRoles() { 225 return roles.keySet(); 226 } 227 228 229 235 public synchronized Role getRole(String role) throws SecurityException { 236 Role roleRef = (Role)roles.get(role); 237 if (roleRef == null) { 238 throw new SecurityException ("The role [" + role 239 + "] could not be found"); 240 } 241 return roleRef; 242 } 243 } 244 | Popular Tags |