| 1 21 22 package com.rift.coad.lib.security.role; 24 25 import java.util.Map ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Set ; 30 import java.util.StringTokenizer ; 31 32 import com.rift.coad.lib.configuration.ConfigurationFactory; 34 import com.rift.coad.lib.configuration.Configuration; 35 import com.rift.coad.lib.security.RoleManager; 36 import com.rift.coad.lib.security.Role; 37 import com.rift.coad.lib.security.RoleHandler; 38 import com.rift.coad.lib.security.SecurityException; 39 40 41 47 public class ConfigurationRoleHandler implements RoleHandler { 48 49 private static final String PRINCIPALS = "principals"; 51 private static final String ROLES = "roles"; 52 53 private Set principals = new HashSet (); 55 56 59 public ConfigurationRoleHandler() { 60 } 61 62 63 70 public Map getRoles() throws SecurityException { 71 try { 72 Map roles = new HashMap (); 73 Configuration config = ConfigurationFactory.getInstance().getConfig( 74 RoleManager.class); 75 StringTokenizer principalList = new StringTokenizer ( 76 config.getString(PRINCIPALS),","); 77 while(principalList.hasMoreTokens()) { 78 principals.add(principalList.nextToken().trim()); 79 } 80 StringTokenizer roleList = new StringTokenizer ( 81 config.getString(ROLES),","); 82 while(roleList.hasMoreTokens()) { 83 String role = (String )roleList.nextToken().trim(); 84 roles.put(role,loadRole(config,role)); 85 } 86 return roles; 87 } catch (Exception ex) { 88 throw new SecurityException ("Failed to load the role information : " 89 + ex.getMessage(),ex); 90 } 91 } 92 93 94 102 private Role loadRole(Configuration config,String role) 103 throws SecurityException { 104 try { 105 StringTokenizer stringTokenizer = new StringTokenizer ( 106 config.getString(role),","); 107 Set roleSet = new HashSet (); 108 while(stringTokenizer.hasMoreTokens()) { 109 String principal = (String )stringTokenizer.nextToken(); 110 if (principals.contains(principal) == false) { 111 throw new SecurityException ("Invalid principal : " + 112 principal); 113 } 114 roleSet.add(principal); 115 } 116 return new Role(role,roleSet); 117 } catch (Exception ex) { 118 throw new SecurityException ("The list of roles : " + ex.getMessage(), 119 ex); 120 } 121 } 122 } 123 | Popular Tags |