001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.core.security;
019:
020: import java.io.Serializable;
021: import java.security.acl.Permission;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.TreeSet;
025:
026: /**
027: * This class represents a set of Permissions. It makes it easy to build a UI
028: * that would allow someone to add a group of Permissions to a Role. It wraps a
029: * TreeSet object to enforce that only Permission objects are allowed in the set
030: * and only relevant methods are available. TreeSet's contain only unique
031: * Objects (no duplicates).
032: */
033: public class PermissionSet implements Serializable {
034: /** Set to hold the Permission Set */
035: private TreeSet set;
036:
037: /**
038: * Constructs an empty PermissionSet
039: */
040: public PermissionSet() {
041: set = new TreeSet();
042: }
043:
044: /**
045: * Constructs a new PermissionSet with specifed contents.
046: *
047: * If the given collection contains multiple objects that are identical WRT
048: * equals() method, some objects will be overwriten.
049: *
050: * @param permissions
051: * A collection of permissions to be contained in the set.
052: */
053: public PermissionSet(Collection permissions) {
054: this ();
055: add(permissions);
056: }
057:
058: /**
059: * Adds a Permission to this PermissionSet.
060: *
061: * @param permission
062: * A Permission.
063: * @return True if Permission was added; false if PermissionSet already
064: * contained the Permission.
065: */
066: public boolean add(Permission permission) {
067: return set.add((Object) permission);
068: }
069:
070: /**
071: * Adds the Permissions in a Collection to this PermissionSet.
072: *
073: * @param permissionSet
074: * A Collection of Permissions.
075: * @return True if this PermissionSet changed as a result; false if no
076: * change to this PermissionSet occurred (this PermissionSet already
077: * contained all members of the added PermissionSet).
078: */
079: public boolean add(Collection permissions) {
080: return set.addAll(permissions);
081: }
082:
083: /**
084: * Adds the Permissions in another PermissionSet to this PermissionSet.
085: *
086: * @param permissionSet
087: * A PermissionSet.
088: * @return True if this PermissionSet changed as a result; false if no
089: * change to this PermissionSet occurred (this PermissionSet already
090: * contained all members of the added PermissionSet).
091: */
092: public boolean add(PermissionSet permissionSet) {
093: return set.addAll((Collection) permissionSet.set);
094: }
095:
096: /**
097: * Removes a Permission from this PermissionSet.
098: *
099: * @param permission
100: * A Permission.
101: * @return True if this PermissionSet contained the Permission before it was
102: * removed.
103: */
104: public boolean remove(Permission permission) {
105: return set.remove((Object) permission);
106: }
107:
108: /**
109: * Removes all Permissions from this PermissionSet.
110: */
111: public void clear() {
112: set.clear();
113: }
114:
115: /**
116: * Checks whether this PermissionSet contains a Permission.
117: *
118: * @param permission
119: * A Permission.
120: * @return True if this PermissionSet contains the Permission, false
121: * otherwise.
122: */
123: public boolean contains(Permission permission) {
124: return set.contains((Object) permission);
125: }
126:
127: /**
128: * Returns a Permission with the specified string representation, if it is
129: * contained in this PermissionSet.
130: *
131: * @param permissionString
132: * string representation of the Permission to find.
133: * @return Permission if argument matched a Permission in this
134: * PermissionSet; null if no match.
135: */
136: public Permission getPermission(String permissionString) {
137: if (permissionString != null) {
138: Iterator iter = set.iterator();
139: while (iter.hasNext()) {
140: Permission permission = (Permission) iter.next();
141: if (permissionString.equals(permission.toString())) {
142: return permission;
143: }
144: }
145: }
146: return null;
147: }
148:
149: /**
150: * Returns an Permissions[] of Permissions in this PermissionSet.
151: *
152: * @return A Permission[].
153: */
154: public Permission[] getPermissionsArray() {
155: return (Permission[]) set.toArray(new Permission[0]);
156: }
157:
158: /**
159: * Returns an Iterator for Permissions in this PermissionSet.
160: */
161: public Iterator elements() {
162: return set.iterator();
163: }
164:
165: /**
166: * Returns size (cardinality) of this set.
167: *
168: * @return The cardinality of this PermissionSet.
169: */
170: public int size() {
171: return set.size();
172: }
173: }
|