KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > security > util > PermissionSet


1 package org.apache.fulcrum.security.util;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.Serializable JavaDoc;
58
59 import java.util.Collection JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.TreeSet JavaDoc;
62
63 import org.apache.fulcrum.security.entity.Permission;
64 import org.apache.fulcrum.security.entity.SecurityEntity;
65
66 /**
67  * This class represents a set of Permissions. It makes it easy to
68  * build a UI that would allow someone to add a group of Permissions
69  * to a Role. It wraps a TreeSet object to enforce that only
70  * Permission objects are allowed in the set and only relevant methods
71  * are available. TreeSet's contain only unique Objects (no
72  * duplicates).
73  *
74  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
75  * @author <a HREF="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
76  * @version $Id: PermissionSet.java,v 1.1 2004/11/12 10:25:42 epugh Exp $
77  */

78 public class PermissionSet implements Serializable JavaDoc
79 {
80     /** Set to hold the Permission Set */
81     private TreeSet JavaDoc set;
82
83     /**
84      * Constructs an empty PermissionSet
85      */

86     public PermissionSet()
87     {
88         set = new TreeSet JavaDoc();
89     }
90
91     /**
92      * Constructs a new PermissionSet with specifed contents.
93      *
94      * If the given collection contains multiple objects that are
95      * identical WRT equals() method, some objects will be overwriten.
96      *
97      * @param permissions A collection of permissions to be contained in the set.
98      */

99     public PermissionSet(Collection JavaDoc permissions)
100     {
101         this();
102         add(permissions);
103     }
104
105     /**
106      * Adds a Permission to this PermissionSet.
107      *
108      * @param permission A Permission.
109      * @return True if Permission was added; false if PermissionSet
110      * already contained the Permission.
111      */

112     public boolean add(Permission permission)
113     {
114         return set.add((Object JavaDoc) permission);
115     }
116
117     /**
118      * Adds the Permissions in a Collection to this PermissionSet.
119      *
120      * @param permissions A Collection of Permissions.
121      *
122      * @return True if this PermissionSet changed as a result; false
123      * if no change to this PermissionSet occurred (this PermissionSet
124      * already contained all members of the added PermissionSet).
125      */

126     public boolean add(Collection JavaDoc permissions)
127     {
128         return set.addAll(permissions);
129     }
130
131     /**
132      * Adds the Permissions in another PermissionSet to this
133      * PermissionSet.
134      *
135      * @param permissionSet A PermissionSet.
136      *
137      * @return True if this PermissionSet changed as a result; false
138      * if no change to this PermissionSet occurred (this PermissionSet
139      * already contained all members of the added PermissionSet).
140      */

141     public boolean add(PermissionSet permissionSet)
142     {
143         return set.addAll(permissionSet.set);
144     }
145
146     /**
147      * Removes a Permission from this PermissionSet.
148      *
149      * @param permission A Permission.
150      * @return True if this PermissionSet contained the Permission
151      * before it was removed.
152      */

153     public boolean remove(Permission permission)
154     {
155         return set.remove((Object JavaDoc) permission);
156     }
157
158     /**
159      * Removes all Permissions from this PermissionSet.
160      */

161     public void clear()
162     {
163         set.clear();
164     }
165
166     /**
167      * Checks whether this PermissionSet contains a Permission.
168      *
169      * @param permission A Permission.
170      * @return True if this PermissionSet contains the Permission,
171      * false otherwise.
172      */

173     public boolean contains(Permission permission)
174     {
175         return set.contains((Object JavaDoc) permission);
176     }
177
178     /**
179      * Compares by name a Permission with the Permissions contained in
180      * this PermissionSet.
181      *
182      * @param permissionName Name of Permission.
183      *
184      * @return True if argument matched a Permission in this
185      * PermissionSet; false if no match.
186      */

187     public boolean contains(String JavaDoc permissionName)
188     {
189         Iterator JavaDoc iter = set.iterator();
190         while (iter.hasNext())
191         {
192             Permission permission = (Permission) iter.next();
193             if (permissionName != null &&
194                  permissionName.equals(((SecurityEntity) permission).getName()))
195             {
196                 return true;
197             }
198         }
199         return false;
200     }
201
202     /**
203      * Returns a Permission with the given name, if it is contained in
204      * this PermissionSet.
205      *
206      * @param permissionName Name of Permission.
207      * @return Permission if argument matched a Permission in this
208      * PermissionSet; null if no match.
209      */

210     public Permission getPermission(String JavaDoc permissionName)
211     {
212         Iterator JavaDoc iter = set.iterator();
213         while (iter.hasNext())
214         {
215             Permission permission = (Permission) iter.next();
216             if (permissionName != null &&
217                  permissionName.equals(((SecurityEntity) permission).getName()))
218             {
219                 return permission;
220             }
221         }
222         return null;
223     }
224
225     /**
226      * Returns an Permissions [] of Permissions in this PermissionSet.
227      *
228      * @return A Permission [].
229      */

230     public Permission [] getPermissionsArray()
231     {
232         return (Permission []) set.toArray(new Permission[0]);
233     }
234
235     /**
236      * Returns an Iterator for Permissions in this PermissionSet.
237      *
238      * @return An Iterator for this Permission Set.
239      */

240     public Iterator JavaDoc elements()
241     {
242         return set.iterator();
243     }
244
245     /**
246      * Returns size (cardinality) of this set.
247      *
248      * @return The cardinality of this PermissionSet.
249      */

250     public int size()
251     {
252         return set.size();
253     }
254
255     /**
256      * list of permission names in this set
257      *
258      * @return A String representation of this Permission Set.
259      */

260     public String JavaDoc toString()
261     {
262         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(12 * size());
263         Iterator JavaDoc i = set.iterator();
264         while (i.hasNext())
265         {
266             sbuf.append(((Permission) i.next()).getName())
267                 .append(", ");
268         }
269         return sbuf.toString();
270     }
271 }
272
Popular Tags