001: /*
002: jGuard is a security framework based on top of jaas (java authentication and authorization security).
003: it is written for web applications, to resolve simply, access control problems.
004: version $Name$
005: http://sourceforge.net/projects/jguard/
006:
007: Copyright (C) 2004 Charles GAY
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023:
024: jGuard project home page:
025: http://sourceforge.net/projects/jguard/
026:
027: */
028: package net.sf.jguard.core.authorization.permissions;
029:
030: import java.security.Permission;
031: import java.security.PermissionCollection;
032: import java.util.Collection;
033: import java.util.Collections;
034: import java.util.Enumeration;
035: import java.util.HashSet;
036: import java.util.Iterator;
037: import java.util.Set;
038: import java.util.logging.Logger;
039:
040: /**
041: * contains similar permissions.
042: * this class contains similar <strong>java.security.Permission</strong> instances,
043: * with the same type.
044: * it is a "technical" container in opposite to Domain,which is a "functional" container.
045: * Classes extending this abstract class must implements implies method from PermissionCollection.
046: * @author <a href="mailto:diabolo512@users.sourceforge.net ">Charles Gay</a>
047: *
048: */
049: public abstract class JGPermissionCollection extends
050: PermissionCollection {
051:
052: /**
053: * serial version id.
054: */
055: private static final long serialVersionUID = 3834030277143377201L;
056: /** Logger for this class */
057: private static final Logger logger = Logger
058: .getLogger(JGPermissionCollection.class.getName());
059:
060: protected Set permissions;
061:
062: /**
063: * default constructor.
064: *
065: */
066: public JGPermissionCollection() {
067:
068: permissions = new HashSet();
069: }
070:
071: /**
072: * constructor.
073: * @param coll
074: */
075: public JGPermissionCollection(Collection coll) {
076:
077: permissions = new HashSet(coll);
078: }
079:
080: /**
081: * add a permission to the set.
082: * @see java.security.PermissionCollection#add(java.security.Permission)
083: */
084: public void add(Permission permission) {
085: permissions.add(permission);
086:
087: }
088:
089: /**
090: * add permissions to the set.
091: * @param permissionSet
092: * @see java.security.PermissionCollection#add(java.security.Permission)
093: */
094: public void addAll(Set permissionSet) {
095: permissions.addAll(permissionSet);
096:
097: }
098:
099: public void addAll(PermissionCollection pcColl) {
100: Enumeration en = pcColl.elements();
101: while (en.hasMoreElements()) {
102: permissions.add((Permission) en.nextElement());
103: }
104: }
105:
106: /**
107: * return all the permissions.
108: * @see java.security.PermissionCollection#elements()
109: */
110: public Enumeration elements() {
111: return Collections.enumeration(permissions);
112:
113: }
114:
115: /**
116: * return the corresponding permission.
117: * @param permissionName
118: * @return permission
119: * @throws NoSuchPermissionException
120: */
121: public Permission getPermission(String permissionName)
122: throws NoSuchPermissionException {
123: Permission permission;
124: Iterator it = permissions.iterator();
125: while (it.hasNext()) {
126: permission = (Permission) it.next();
127: if (permission.getName().equals(permissionName)) {
128: return permission;
129: }
130:
131: }
132: logger
133: .warning("permission "
134: + permissionName
135: + " not found in JGPermissionCollection#getPermission!!!");
136: logger.warning("permissions=" + permissions);
137: throw new NoSuchPermissionException("permission "
138: + permissionName
139: + " not found in JGPermissionCollection#getPermission");
140:
141: }
142:
143: /**
144: * remove permission from Permission's collection.
145: * @param permission
146: */
147: public void removePermission(Permission permission) {
148: permissions.remove(permission);
149: }
150:
151: /**
152: * remove permission from Permission's collection.
153: * @param permission
154: */
155: public void removePermissions(PermissionCollection permColl) {
156: Enumeration permissionsEnum = permColl.elements();
157: while (permissionsEnum.hasMoreElements()) {
158: permissions.remove((Permission) permissionsEnum
159: .nextElement());
160: }
161: }
162:
163: /**
164: * remove permission from Permission's collection.
165: * @param permission
166: */
167: public void clear() {
168: permissions.clear();
169: }
170:
171: public String toString() {
172: StringBuffer sb = new StringBuffer();
173: Iterator permissionsIterator = this .permissions.iterator();
174: while (permissionsIterator.hasNext()) {
175: Permission permission = (Permission) permissionsIterator
176: .next();
177: sb.append(permission.toString());
178: sb.append("\n");
179: }
180: return sb.toString();
181: }
182:
183: /**
184: *
185: * @return permissions number owned by this JgPermissionCollection.
186: */
187: public int size() {
188: return permissions.size();
189: }
190:
191: /**
192: * @return Returns the permissions.
193: */
194: public Set getPermissions() {
195: return permissions;
196: }
197:
198: /**
199: *
200: * @param permission
201: * @return
202: */
203: public boolean containsPermission(Permission permission) {
204: return permissions.contains(permission);
205: }
206:
207: /**
208: * @param perms The permissions to set.
209: */
210: public void setPermissions(Set perms) {
211:
212: this.permissions = perms;
213: }
214:
215: }
|