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