1 package org.apache.turbine.util.security;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Collection;
20 import java.util.Iterator;
21
22 import org.apache.commons.lang.StringUtils;
23
24 import org.apache.turbine.om.security.Role;
25
26 /***
27 * This class represents a set of Roles. It makes it easy to build a
28 * UI that would allow someone to add a group of Roles to a User.
29 * It enforces that only Role objects are
30 * allowed in the set and only relevant methods are available.
31 *
32 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
33 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
34 * @author <a href="mailto:marco@intermeta.de">Marco Knüttel</a>
35 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
36 * @version $Id: RoleSet.java,v 1.9.2.2 2004/05/20 03:27:24 seade Exp $
37 */
38 public class RoleSet
39 extends SecuritySet
40 {
41 /***
42 * Constructs an empty RoleSet
43 */
44 public RoleSet()
45 {
46 super();
47 }
48
49 /***
50 * Constructs a new RoleSet with specified contents.
51 *
52 * If the given collection contains multiple objects that are
53 * identical WRT equals() method, some objects will be overwritten.
54 *
55 * @param roles A collection of roles to be contained in the set.
56 */
57 public RoleSet(Collection roles)
58 {
59 super();
60 add(roles);
61 }
62
63 /***
64 * Adds a Role to this RoleSet.
65 *
66 * @param role A Role.
67 * @return True if Role was added; false if RoleSet already
68 * contained the Role.
69 */
70 public boolean add(Role role)
71 {
72 boolean res = contains(role);
73 nameMap.put(role.getName(), role);
74 idMap.put(role.getIdAsObj(), role);
75 return res;
76 }
77
78 /***
79 * Adds the Roles in a Collection to this RoleSet.
80 *
81 * @param roles A Collection of Roles.
82 * @return True if this RoleSet changed as a result; false
83 * if no change to this RoleSet occurred (this RoleSet
84 * already contained all members of the added RoleSet).
85 */
86 public boolean add(Collection roles)
87 {
88 boolean res = false;
89 for (Iterator it = roles.iterator(); it.hasNext();)
90 {
91 Role r = (Role) it.next();
92 res |= add(r);
93 }
94 return res;
95 }
96
97 /***
98 * Adds the Roles in another RoleSet to this RoleSet.
99 *
100 * @param roleSet A RoleSet.
101 * @return True if this RoleSet changed as a result; false
102 * if no change to this RoleSet occurred (this RoleSet
103 * already contained all members of the added RoleSet).
104 */
105 public boolean add(RoleSet roleSet)
106 {
107 boolean res = false;
108 for( Iterator it = roleSet.iterator(); it.hasNext();)
109 {
110 Role r = (Role) it.next();
111 res |= add(r);
112 }
113 return res;
114 }
115
116 /***
117 * Removes a Role from this RoleSet.
118 *
119 * @param role A Role.
120 * @return True if this RoleSet contained the Role
121 * before it was removed.
122 */
123 public boolean remove(Role role)
124 {
125 boolean res = contains(role);
126 nameMap.remove(role.getName());
127 idMap.remove(role.getIdAsObj());
128 return res;
129 }
130
131 /***
132 * Checks whether this RoleSet contains a Role.
133 *
134 * @param role A Role.
135 * @return True if this RoleSet contains the Role,
136 * false otherwise.
137 */
138 public boolean contains(Role role)
139 {
140 return nameMap.containsValue((Object) role);
141 }
142
143 /***
144 * Returns a Role with the given name, if it is contained in
145 * this RoleSet.
146 *
147 * @param roleName Name of Role.
148 * @return Role if argument matched a Role in this
149 * RoleSet; null if no match.
150 * @deprecated Use <a href="#getRoleByName">getRoleByName</a> instead.
151 */
152 public Role getRole(String roleName)
153 {
154 return getRoleByName(roleName);
155 }
156
157 /***
158 * Returns a Role with the given name, if it is contained in
159 * this RoleSet.
160 *
161 * @param roleName Name of Role.
162 * @return Role if argument matched a Role in this
163 * RoleSet; null if no match.
164 */
165 public Role getRoleByName(String roleName)
166 {
167 return (StringUtils.isNotEmpty(roleName))
168 ? (Role) nameMap.get(roleName) : null;
169 }
170
171 /***
172 * Returns a Role with the given id, if it is contained in this
173 * RoleSet.
174 *
175 * @param roleId id of the Role.
176 * @return Role if argument matched a Role in this RoleSet; null
177 * if no match.
178 */
179 public Role getRoleById(int roleId)
180 {
181 return (roleId != 0)
182 ? (Role) idMap.get(new Integer(roleId)) : null;
183 }
184
185 /***
186 * Returns an Array of Roles in this RoleSet.
187 *
188 * @return An Array of Role objects.
189 */
190 public Role[] getRolesArray()
191 {
192 return (Role[]) getSet().toArray(new Role[0]);
193 }
194
195 /***
196 * Print out a RoleSet as a String
197 *
198 * @returns The Role Set as String
199 *
200 */
201 public String toString()
202 {
203 StringBuffer sb = new StringBuffer();
204 sb.append("RoleSet: ");
205
206 for(Iterator it = iterator(); it.hasNext();)
207 {
208 Role r = (Role) it.next();
209 sb.append('[');
210 sb.append(r.getName());
211 sb.append(" -> ");
212 sb.append(r.getIdAsObj());
213 sb.append(']');
214 if (it.hasNext())
215 {
216 sb.append(", ");
217 }
218 }
219
220 return sb.toString();
221 }
222 }