View Javadoc

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