%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.util.security.PermissionSet |
|
|
1 | package org.apache.turbine.util.security; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.util.Collection; |
|
20 | import java.util.Iterator; |
|
21 | ||
22 | import org.apache.commons.lang.StringUtils; |
|
23 | ||
24 | import org.apache.turbine.om.security.Permission; |
|
25 | ||
26 | /** |
|
27 | * This class represents a set of Permissions. It makes it easy to |
|
28 | * build a UI that would allow someone to add a group of Permissions |
|
29 | * to a Role. It enforces that only |
|
30 | * Permission objects are allowed in the set and only relevant methods |
|
31 | * are available. |
|
32 | * |
|
33 | * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> |
|
34 | * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> |
|
35 | * @author <a href="mailto:marco@intermeta.de">Marco Knüttel</a> |
|
36 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
|
37 | * @version $Id: PermissionSet.java 278822 2005-09-05 19:53:05Z henning $ |
|
38 | */ |
|
39 | public class PermissionSet |
|
40 | extends SecuritySet |
|
41 | { |
|
42 | /** Serial Version UID */ |
|
43 | private static final long serialVersionUID = 8276935936763076884L; |
|
44 | ||
45 | /** |
|
46 | * Constructs an empty PermissionSet |
|
47 | */ |
|
48 | public PermissionSet() |
|
49 | { |
|
50 | 106 | super(); |
51 | 106 | } |
52 | ||
53 | /** |
|
54 | * Constructs a new PermissionSet with specified contents. |
|
55 | * |
|
56 | * If the given collection contains multiple objects that are |
|
57 | * identical WRT equals() method, some objects will be overwritten. |
|
58 | * |
|
59 | * @param permissions A collection of permissions to be contained in the set. |
|
60 | */ |
|
61 | public PermissionSet(Collection permissions) |
|
62 | { |
|
63 | 60 | super(); |
64 | 60 | add(permissions); |
65 | 60 | } |
66 | ||
67 | /** |
|
68 | * Adds a Permission to this PermissionSet. |
|
69 | * |
|
70 | * @param permission A Permission. |
|
71 | * @return True if Permission was added; false if PermissionSet |
|
72 | * already contained the Permission. |
|
73 | */ |
|
74 | public boolean add(Permission permission) |
|
75 | { |
|
76 | 326 | boolean res = contains(permission); |
77 | 326 | nameMap.put(permission.getName(), permission); |
78 | 326 | idMap.put(permission.getIdAsObj(), permission); |
79 | 326 | return res; |
80 | } |
|
81 | ||
82 | /** |
|
83 | * Adds the Permissions in a Collection to this PermissionSet. |
|
84 | * |
|
85 | * @param permissions A Collection of Permissions. |
|
86 | * @return True if this PermissionSet changed as a result; false |
|
87 | * if no change to this PermissionSet occurred (this PermissionSet |
|
88 | * already contained all members of the added PermissionSet). |
|
89 | */ |
|
90 | public boolean add(Collection permissions) |
|
91 | { |
|
92 | 60 | boolean res = false; |
93 | 189 | for (Iterator it = permissions.iterator(); it.hasNext();) |
94 | { |
|
95 | 198 | Permission p = (Permission) it.next(); |
96 | 198 | res |= add(p); |
97 | } |
|
98 | 60 | return res; |
99 | } |
|
100 | ||
101 | /** |
|
102 | * Adds the Permissions in another PermissionSet to this |
|
103 | * PermissionSet. |
|
104 | * |
|
105 | * @param permissionSet A PermissionSet. |
|
106 | * @return True if this PermissionSet changed as a result; false |
|
107 | * if no change to this PermissionSet occurred (this PermissionSet |
|
108 | * already contained all members of the added PermissionSet). |
|
109 | */ |
|
110 | public boolean add(PermissionSet permissionSet) |
|
111 | { |
|
112 | 20 | boolean res = false; |
113 | 44 | for( Iterator it = permissionSet.iterator(); it.hasNext();) |
114 | { |
|
115 | 28 | Permission p = (Permission) it.next(); |
116 | 28 | res |= add(p); |
117 | } |
|
118 | 20 | return res; |
119 | } |
|
120 | ||
121 | /** |
|
122 | * Removes a Permission from this PermissionSet. |
|
123 | * |
|
124 | * @param permission A Permission. |
|
125 | * @return True if this PermissionSet contained the Permission |
|
126 | * before it was removed. |
|
127 | */ |
|
128 | public boolean remove(Permission permission) |
|
129 | { |
|
130 | 2 | boolean res = contains(permission); |
131 | 2 | nameMap.remove(permission.getName()); |
132 | 2 | idMap.remove(permission.getIdAsObj()); |
133 | 2 | return res; |
134 | } |
|
135 | ||
136 | /** |
|
137 | * Checks whether this PermissionSet contains a Permission. |
|
138 | * |
|
139 | * @param permission A Permission. |
|
140 | * @return True if this PermissionSet contains the Permission, |
|
141 | * false otherwise. |
|
142 | */ |
|
143 | public boolean contains(Permission permission) |
|
144 | { |
|
145 | 364 | return nameMap.containsValue((Object) permission); |
146 | } |
|
147 | ||
148 | /** |
|
149 | * Returns a Permission with the given name, if it is contained in |
|
150 | * this PermissionSet. |
|
151 | * |
|
152 | * @param permissionName Name of Permission. |
|
153 | * @return Permission if argument matched a Permission in this |
|
154 | * PermissionSet; null if no match. |
|
155 | * @deprecated Use <a href="#getPermissionByName">getPermissionByName</a> instead. |
|
156 | */ |
|
157 | public Permission getPermission(String permissionName) |
|
158 | { |
|
159 | 0 | return getPermissionByName(permissionName); |
160 | } |
|
161 | ||
162 | /** |
|
163 | * Returns a Permission with the given name, if it is contained in |
|
164 | * this PermissionSet. |
|
165 | * |
|
166 | * @param permissionName Name of Permission. |
|
167 | * @return Permission if argument matched a Permission in this |
|
168 | * PermissionSet; null if no match. |
|
169 | */ |
|
170 | public Permission getPermissionByName(String permissionName) |
|
171 | { |
|
172 | 40 | return (StringUtils.isNotEmpty(permissionName)) |
173 | ? (Permission) nameMap.get(permissionName) : null; |
|
174 | } |
|
175 | ||
176 | /** |
|
177 | * Returns a Permission with the given id, if it is contained in |
|
178 | * this PermissionSet. |
|
179 | * |
|
180 | * @param permissionId Id of the Permission. |
|
181 | * @return Permission if argument matched a Permission in this |
|
182 | * PermissionSet; null if no match. |
|
183 | */ |
|
184 | public Permission getPermissionById(int permissionId) |
|
185 | { |
|
186 | 2 | return (permissionId != 0) |
187 | ? (Permission) idMap.get(new Integer(permissionId)) : null; |
|
188 | } |
|
189 | ||
190 | /** |
|
191 | * Returns an Array of Permissions in this PermissionSet. |
|
192 | * |
|
193 | * @return An Array of Permission Objects. |
|
194 | */ |
|
195 | public Permission[] getPermissionsArray() |
|
196 | { |
|
197 | 0 | return (Permission[]) getSet().toArray(new Permission[0]); |
198 | } |
|
199 | ||
200 | /** |
|
201 | * Print out a PermissionSet as a String |
|
202 | * |
|
203 | * @returns The Permission Set as String |
|
204 | * |
|
205 | */ |
|
206 | public String toString() |
|
207 | { |
|
208 | 0 | StringBuffer sb = new StringBuffer(); |
209 | 0 | sb.append("PermissionSet: "); |
210 | ||
211 | 0 | for(Iterator it = iterator(); it.hasNext();) |
212 | { |
|
213 | 0 | Permission p = (Permission) it.next(); |
214 | 0 | sb.append('['); |
215 | 0 | sb.append(p.getName()); |
216 | 0 | sb.append(" -> "); |
217 | 0 | sb.append(p.getIdAsObj()); |
218 | 0 | sb.append(']'); |
219 | 0 | if (it.hasNext()) |
220 | { |
|
221 | 0 | sb.append(", "); |
222 | } |
|
223 | } |
|
224 | ||
225 | 0 | return sb.toString(); |
226 | } |
|
227 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |