001: package org.apache.turbine.util.security;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: import org.apache.commons.lang.StringUtils;
023:
024: import org.apache.turbine.om.security.Role;
025:
026: /**
027: * This class represents a set of Roles. It makes it easy to build a
028: * UI that would allow someone to add a group of Roles to a User.
029: * It enforces that only Role objects are
030: * allowed in the set and only relevant methods are available.
031: *
032: * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
033: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
034: * @author <a href="mailto:marco@intermeta.de">Marco Knüttel</a>
035: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
036: * @version $Id: RoleSet.java 278822 2005-09-05 19:53:05Z henning $
037: */
038: public class RoleSet extends SecuritySet {
039: /** Serial Version UID */
040: private static final long serialVersionUID = -5521518890129125912L;
041:
042: /**
043: * Constructs an empty RoleSet
044: */
045: public RoleSet() {
046: super ();
047: }
048:
049: /**
050: * Constructs a new RoleSet with specified contents.
051: *
052: * If the given collection contains multiple objects that are
053: * identical WRT equals() method, some objects will be overwritten.
054: *
055: * @param roles A collection of roles to be contained in the set.
056: */
057: public RoleSet(Collection roles) {
058: super ();
059: add(roles);
060: }
061:
062: /**
063: * Adds a Role to this RoleSet.
064: *
065: * @param role A Role.
066: * @return True if Role was added; false if RoleSet already
067: * contained the Role.
068: */
069: public boolean add(Role role) {
070: boolean res = contains(role);
071: nameMap.put(role.getName(), role);
072: idMap.put(role.getIdAsObj(), role);
073: return res;
074: }
075:
076: /**
077: * Adds the Roles in a Collection to this RoleSet.
078: *
079: * @param roles A Collection of Roles.
080: * @return True if this RoleSet changed as a result; false
081: * if no change to this RoleSet occurred (this RoleSet
082: * already contained all members of the added RoleSet).
083: */
084: public boolean add(Collection roles) {
085: boolean res = false;
086: for (Iterator it = roles.iterator(); it.hasNext();) {
087: Role r = (Role) it.next();
088: res |= add(r);
089: }
090: return res;
091: }
092:
093: /**
094: * Adds the Roles in another RoleSet to this RoleSet.
095: *
096: * @param roleSet A RoleSet.
097: * @return True if this RoleSet changed as a result; false
098: * if no change to this RoleSet occurred (this RoleSet
099: * already contained all members of the added RoleSet).
100: */
101: public boolean add(RoleSet roleSet) {
102: boolean res = false;
103: for (Iterator it = roleSet.iterator(); it.hasNext();) {
104: Role r = (Role) it.next();
105: res |= add(r);
106: }
107: return res;
108: }
109:
110: /**
111: * Removes a Role from this RoleSet.
112: *
113: * @param role A Role.
114: * @return True if this RoleSet contained the Role
115: * before it was removed.
116: */
117: public boolean remove(Role role) {
118: boolean res = contains(role);
119: nameMap.remove(role.getName());
120: idMap.remove(role.getIdAsObj());
121: return res;
122: }
123:
124: /**
125: * Checks whether this RoleSet contains a Role.
126: *
127: * @param role A Role.
128: * @return True if this RoleSet contains the Role,
129: * false otherwise.
130: */
131: public boolean contains(Role role) {
132: return nameMap.containsValue((Object) role);
133: }
134:
135: /**
136: * Returns a Role with the given name, if it is contained in
137: * this RoleSet.
138: *
139: * @param roleName Name of Role.
140: * @return Role if argument matched a Role in this
141: * RoleSet; null if no match.
142: * @deprecated Use <a href="#getRoleByName">getRoleByName</a> instead.
143: */
144: public Role getRole(String roleName) {
145: return getRoleByName(roleName);
146: }
147:
148: /**
149: * Returns a Role with the given name, if it is contained in
150: * this RoleSet.
151: *
152: * @param roleName Name of Role.
153: * @return Role if argument matched a Role in this
154: * RoleSet; null if no match.
155: */
156: public Role getRoleByName(String roleName) {
157: return (StringUtils.isNotEmpty(roleName)) ? (Role) nameMap
158: .get(roleName) : null;
159: }
160:
161: /**
162: * Returns a Role with the given id, if it is contained in this
163: * RoleSet.
164: *
165: * @param roleId id of the Role.
166: * @return Role if argument matched a Role in this RoleSet; null
167: * if no match.
168: */
169: public Role getRoleById(int roleId) {
170: return (roleId != 0) ? (Role) idMap.get(new Integer(roleId))
171: : null;
172: }
173:
174: /**
175: * Returns an Array of Roles in this RoleSet.
176: *
177: * @return An Array of Role objects.
178: */
179: public Role[] getRolesArray() {
180: return (Role[]) getSet().toArray(new Role[0]);
181: }
182:
183: /**
184: * Print out a RoleSet as a String
185: *
186: * @returns The Role Set as String
187: *
188: */
189: public String toString() {
190: StringBuffer sb = new StringBuffer();
191: sb.append("RoleSet: ");
192:
193: for (Iterator it = iterator(); it.hasNext();) {
194: Role r = (Role) it.next();
195: sb.append('[');
196: sb.append(r.getName());
197: sb.append(" -> ");
198: sb.append(r.getIdAsObj());
199: sb.append(']');
200: if (it.hasNext()) {
201: sb.append(", ");
202: }
203: }
204:
205: return sb.toString();
206: }
207: }
|