1 package org.apache.turbine.util.security;
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;
58 import java.util.Collection;
59 import java.util.Iterator;
60 import java.util.TreeSet;
61 import org.apache.turbine.om.security.Permission;
62 import org.apache.turbine.om.security.SecurityObject;
63
64 /***
65 * This class represents a set of Permissions. It makes it easy to
66 * build a UI that would allow someone to add a group of Permissions
67 * to a Role. It wraps a TreeSet object to enforce that only
68 * Permission objects are allowed in the set and only relevant methods
69 * are available. TreeSet's contain only unique Objects (no
70 * duplicates).
71 *
72 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
73 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
74 * @version $Id: PermissionSet.java,v 1.3 2002/07/16 12:15:30 henning Exp $
75 */
76 public class PermissionSet implements Serializable
77 {
78 /*** Set to hold the Permission Set */
79 private TreeSet set;
80
81 /***
82 * Constructs an empty PermissionSet
83 */
84 public PermissionSet()
85 {
86 set = new TreeSet();
87 }
88
89 /***
90 * Constructs a new PermissionSet with specifed contents.
91 *
92 * If the given collection contains multiple objects that are
93 * identical WRT equals() method, some objects will be overwriten.
94 *
95 * @param permissions A collection of permissions to be contained in the set.
96 */
97 public PermissionSet(Collection permissions)
98 {
99 this();
100 add(permissions);
101 }
102
103 /***
104 * Adds a Permission to this PermissionSet.
105 *
106 * @param permission A Permission.
107 * @return True if Permission was added; false if PermissionSet
108 * already contained the Permission.
109 */
110 public boolean add(Permission permission)
111 {
112 return set.add((Object) permission);
113 }
114
115 /***
116 * Adds the Permissions in a Collection to this PermissionSet.
117 *
118 * @param permissions A Permission.
119 * @return True if this PermissionSet changed as a result; false
120 * if no change to this PermissionSet occurred (this PermissionSet
121 * already contained all members of the added PermissionSet).
122 */
123 public boolean add(Collection permissions)
124 {
125 return set.addAll(permissions);
126 }
127
128 /***
129 * Adds the Permissions in another PermissionSet to this
130 * PermissionSet.
131 *
132 * @param permissionSet A PermissionSet.
133 * @return True if this PermissionSet changed as a result; false
134 * if no change to this PermissionSet occurred (this PermissionSet
135 * already contained all members of the added PermissionSet).
136 */
137 public boolean add(PermissionSet permissionSet)
138 {
139 return set.addAll(permissionSet.set);
140 }
141
142 /***
143 * Removes a Permission from this PermissionSet.
144 *
145 * @param permission A Permission.
146 * @return True if this PermissionSet contained the Permission
147 * before it was removed.
148 */
149 public boolean remove(Permission permission)
150 {
151 return set.remove((Object) permission);
152 }
153
154 /***
155 * Removes all Permissions from this PermissionSet.
156 */
157 public void clear()
158 {
159 set.clear();
160 }
161
162 /***
163 * Checks whether this PermissionSet contains a Permission.
164 *
165 * @param permission A Permission.
166 * @return True if this PermissionSet contains the Permission,
167 * false otherwise.
168 */
169 public boolean contains(Permission permission)
170 {
171 return set.contains((Object) permission);
172 }
173
174 /***
175 * Compares by name a Permission with the Permissions contained in
176 * this PermissionSet.
177 *
178 * @param permissionName Name of Permission.
179 * @return True if argument matched a Permission in this
180 * PermissionSet; false if no match.
181 */
182 public boolean contains(String permissionName)
183 {
184 Iterator iter = set.iterator();
185 while (iter.hasNext())
186 {
187 Permission permission = (Permission) iter.next();
188 if (permissionName != null &&
189 permissionName.equals(((SecurityObject) permission).getName()))
190 {
191 return true;
192 }
193 }
194 return false;
195 }
196
197 /***
198 * Returns a Permission with the given name, if it is contained in
199 * this PermissionSet.
200 *
201 * @param permissionName Name of Permission.
202 * @return Permission if argument matched a Permission in this
203 * PermissionSet; null if no match.
204 */
205 public Permission getPermission(String permissionName)
206 {
207 Iterator iter = set.iterator();
208 while (iter.hasNext())
209 {
210 Permission permission = (Permission) iter.next();
211 if (permissionName != null &&
212 permissionName.equals(((SecurityObject) permission).getName()))
213 {
214 return permission;
215 }
216 }
217 return null;
218 }
219
220 /***
221 * Returns an Permissions[] of Permissions in this PermissionSet.
222 *
223 * @return A Permission[].
224 */
225 public Permission[] getPermissionsArray()
226 {
227 return (Permission[]) set.toArray(new Permission[0]);
228 }
229
230 /***
231 * Returns an Iterator for Permissions in this PermissionSet.
232 */
233 public Iterator elements()
234 {
235 return set.iterator();
236 }
237
238 /***
239 * Returns size (cardinality) of this set.
240 *
241 * @return The cardinality of this PermissionSet.
242 */
243 public int size()
244 {
245 return set.size();
246 }
247 }
This page was automatically generated by Maven