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