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.Permission;
028:
029: /**
030: * This class represents a set of Permissions. It makes it easy to
031: * build a UI that would allow someone to add a group of Permissions
032: * to a Role. It enforces that only
033: * Permission objects are allowed in the set and only relevant methods
034: * are available.
035: *
036: * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
037: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
038: * @author <a href="mailto:marco@intermeta.de">Marco Knüttel</a>
039: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
040: * @version $Id: PermissionSet.java 534527 2007-05-02 16:10:59Z tv $
041: */
042: public class PermissionSet extends SecuritySet {
043: /** Serial Version UID */
044: private static final long serialVersionUID = 8276935936763076884L;
045:
046: /**
047: * Constructs an empty PermissionSet
048: */
049: public PermissionSet() {
050: super ();
051: }
052:
053: /**
054: * Constructs a new PermissionSet with specified contents.
055: *
056: * If the given collection contains multiple objects that are
057: * identical WRT equals() method, some objects will be overwritten.
058: *
059: * @param permissions A collection of permissions to be contained in the set.
060: */
061: public PermissionSet(Collection permissions) {
062: super ();
063: add(permissions);
064: }
065:
066: /**
067: * Adds a Permission to this PermissionSet.
068: *
069: * @param permission A Permission.
070: * @return True if Permission was added; false if PermissionSet
071: * already contained the Permission.
072: */
073: public boolean add(Permission permission) {
074: boolean res = contains(permission);
075: nameMap.put(permission.getName(), permission);
076: idMap.put(permission.getIdAsObj(), permission);
077: return res;
078: }
079:
080: /**
081: * Adds the Permissions in a Collection to this PermissionSet.
082: *
083: * @param permissions A Collection of Permissions.
084: * @return True if this PermissionSet changed as a result; false
085: * if no change to this PermissionSet occurred (this PermissionSet
086: * already contained all members of the added PermissionSet).
087: */
088: public boolean add(Collection permissions) {
089: boolean res = false;
090: for (Iterator it = permissions.iterator(); it.hasNext();) {
091: Permission p = (Permission) it.next();
092: res |= add(p);
093: }
094: return res;
095: }
096:
097: /**
098: * Adds the Permissions in another PermissionSet to this
099: * PermissionSet.
100: *
101: * @param permissionSet A PermissionSet.
102: * @return True if this PermissionSet changed as a result; false
103: * if no change to this PermissionSet occurred (this PermissionSet
104: * already contained all members of the added PermissionSet).
105: */
106: public boolean add(PermissionSet permissionSet) {
107: boolean res = false;
108: for (Iterator it = permissionSet.iterator(); it.hasNext();) {
109: Permission p = (Permission) it.next();
110: res |= add(p);
111: }
112: return res;
113: }
114:
115: /**
116: * Removes a Permission from this PermissionSet.
117: *
118: * @param permission A Permission.
119: * @return True if this PermissionSet contained the Permission
120: * before it was removed.
121: */
122: public boolean remove(Permission permission) {
123: boolean res = contains(permission);
124: nameMap.remove(permission.getName());
125: idMap.remove(permission.getIdAsObj());
126: return res;
127: }
128:
129: /**
130: * Checks whether this PermissionSet contains a Permission.
131: *
132: * @param permission A Permission.
133: * @return True if this PermissionSet contains the Permission,
134: * false otherwise.
135: */
136: public boolean contains(Permission permission) {
137: return nameMap.containsValue((Object) permission);
138: }
139:
140: /**
141: * Returns a Permission with the given name, if it is contained in
142: * this PermissionSet.
143: *
144: * @param permissionName Name of Permission.
145: * @return Permission if argument matched a Permission in this
146: * PermissionSet; null if no match.
147: * @deprecated Use <a href="#getPermissionByName">getPermissionByName</a> instead.
148: */
149: public Permission getPermission(String permissionName) {
150: return getPermissionByName(permissionName);
151: }
152:
153: /**
154: * Returns a Permission with the given name, if it is contained in
155: * this PermissionSet.
156: *
157: * @param permissionName Name of Permission.
158: * @return Permission if argument matched a Permission in this
159: * PermissionSet; null if no match.
160: */
161: public Permission getPermissionByName(String permissionName) {
162: return (StringUtils.isNotEmpty(permissionName)) ? (Permission) nameMap
163: .get(permissionName)
164: : null;
165: }
166:
167: /**
168: * Returns a Permission with the given id, if it is contained in
169: * this PermissionSet.
170: *
171: * @param permissionId Id of the Permission.
172: * @return Permission if argument matched a Permission in this
173: * PermissionSet; null if no match.
174: */
175: public Permission getPermissionById(int permissionId) {
176: return (permissionId != 0) ? (Permission) idMap
177: .get(new Integer(permissionId)) : null;
178: }
179:
180: /**
181: * Returns an Array of Permissions in this PermissionSet.
182: *
183: * @return An Array of Permission Objects.
184: */
185: public Permission[] getPermissionsArray() {
186: return (Permission[]) getSet().toArray(new Permission[0]);
187: }
188:
189: /**
190: * Print out a PermissionSet as a String
191: *
192: * @returns The Permission Set as String
193: *
194: */
195: public String toString() {
196: StringBuffer sb = new StringBuffer();
197: sb.append("PermissionSet: ");
198:
199: for (Iterator it = iterator(); it.hasNext();) {
200: Permission p = (Permission) it.next();
201: sb.append('[');
202: sb.append(p.getName());
203: sb.append(" -> ");
204: sb.append(p.getIdAsObj());
205: sb.append(']');
206: if (it.hasNext()) {
207: sb.append(", ");
208: }
209: }
210:
211: return sb.toString();
212: }
213: }
|