1 22 package org.jboss.test.xml.mbeanserver; 23 24 import java.util.HashMap ; 25 import java.util.ArrayList ; 26 import java.util.Enumeration ; 27 import java.util.Iterator ; 28 import java.security.acl.Group ; 29 import java.security.Principal ; 30 31 import org.jboss.security.SimpleGroup; 32 import org.jboss.security.SimplePrincipal; 33 34 40 public class Users 41 { 42 private HashMap users = new HashMap (); 43 44 public static class User implements Comparable 45 { 46 private String name; 47 private String password; 48 private String encoding; 49 private HashMap roleGroups = new HashMap (); 50 51 public User() 52 { 53 } 54 public User(String name) 55 { 56 this.name = name; 57 } 58 public String getName() 59 { 60 return name; 61 } 62 public String getPassword() 63 { 64 return password; 65 } 66 public void setPassword(String password) 67 { 68 this.password = password; 69 } 70 71 public String getEncoding() 72 { 73 return encoding; 74 } 75 public void setEncoding(String encoding) 76 { 77 this.encoding = encoding; 78 } 79 80 public Group [] getRoleSets() 81 { 82 Group [] roleSets = new Group [roleGroups.size()]; 83 roleGroups.values().toArray(roleSets); 84 return roleSets; 85 } 86 public String [] getRoleNames() 87 { 88 return getRoleNames("Roles"); 89 } 90 public String [] getRoleNames(String roleGroup) 91 { 92 Group group = (Group ) roleGroups.get(roleGroup); 93 String [] names = {}; 94 if( group != null ) 95 { 96 ArrayList tmp = new ArrayList (); 97 Enumeration iter = group.members(); 98 while( iter.hasMoreElements() ) 99 { 100 Principal p = (Principal ) iter.nextElement(); 101 tmp.add(p.getName()); 102 } 103 names = new String [tmp.size()]; 104 tmp.toArray(names); 105 } 106 return names; 107 } 108 public void addRole(String roleName, String roleGroup) 109 { 110 Group group = (Group ) roleGroups.get(roleGroup); 111 if( group == null ) 112 { 113 group = new SimpleGroup(roleGroup); 114 roleGroups.put(roleGroup, group); 115 } 116 SimplePrincipal role = new SimplePrincipal(roleName); 117 group.addMember(role); 118 } 119 public int compareTo(Object obj) 120 { 121 Users.User u = (Users.User) obj; 122 return name.compareTo(u.name); 123 } 124 125 public String toString() 126 { 127 return "User{" + 128 "name='" + name + "'" + 129 ", password=*" + 130 ", encoding='" + encoding + "'" + 131 ", roleGroups=" + roleGroups + 132 "}"; 133 } 134 } 135 136 public void addUser(Users.User user) 137 { 138 users.put(user.getName(), user); 139 } 140 public Iterator getUsers() 141 { 142 return users.values().iterator(); 143 } 144 public Users.User getUser(String name) 145 { 146 Users.User find = (Users.User) users.get(name); 147 return find; 148 } 149 150 public String toString() 151 { 152 return "Users("+System.identityHashCode(this)+"){" + 153 "users=" + users + 154 "}"; 155 } 156 } 157 | Popular Tags |