View Javadoc

1   /*
2   jGuard is a security framework based on top of jaas (java authentication and authorization security).
3   it is written for web applications, to resolve simply, access control problems.
4   version $Name$
5   http://sourceforge.net/projects/jguard/
6   
7   Copyright (C) 2004  Charles GAY
8   
9   This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13  
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  Lesser General Public License for more details.
18  
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23  
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26  
27  */
28  package net.sf.jguard.core.authorization.permissions;
29  
30  import java.security.Permission;
31  import java.security.PermissionCollection;
32  import java.util.Collection;
33  import java.util.Collections;
34  import java.util.Enumeration;
35  import java.util.HashSet;
36  import java.util.Iterator;
37  import java.util.Set;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  
42  
43  /**
44   * contains similar permissions.
45   * this class contains similar <strong>java.security.Permission</strong> instances,
46   *  with the same type.
47   * it is a "technical" container in opposite to Domain,which is a "functional" container.
48   * Classes extending this abstract class must implements implies method from PermissionCollection.
49   * @author <a href="mailto:diabolo512@users.sourceforge.net ">Charles Gay</a>
50   *
51   */
52  public abstract class JGPermissionCollection extends PermissionCollection {
53  
54  	/**
55  	 * serial version id.
56  	 */
57  	private static final long serialVersionUID = 3834030277143377201L;
58  	/** Logger for this class */
59  	private static final Logger logger = LoggerFactory.getLogger(JGPermissionCollection.class.getName());
60  
61  	protected Set permissions;
62  
63  	/**
64       * default constructor.
65       *
66       */
67      public JGPermissionCollection() {
68  
69          permissions = new HashSet();
70      }
71  
72      /**
73       * constructor.
74       * @param coll
75       */
76      public JGPermissionCollection(Collection coll) {
77  
78          permissions = new HashSet(coll);
79      }
80  	/**
81  	 * add a permission to the set.
82  	 * @see java.security.PermissionCollection#add(java.security.Permission)
83  	 */
84  	public void add(Permission permission) {
85              if(permission!=null){
86  		permissions.add(permission);
87              }
88  
89  	}
90  
91      /**
92       * add permissions to the set.
93       * @param permissionSet
94       * @see java.security.PermissionCollection#add(java.security.Permission)
95       */
96      public void addAll(Set permissionSet) {
97          if(permissionSet!=null){
98              permissions.addAll(permissionSet);
99          }
100 
101     }
102     
103     public void addAll(PermissionCollection pcColl){
104     	Enumeration en = pcColl.elements();
105         while(en.hasMoreElements()){
106         	permissions.add((Permission)en.nextElement());
107         }
108     }
109 
110 	/**
111 	 * return all the permissions.
112 	 * @see java.security.PermissionCollection#elements()
113 	 */
114 	public Enumeration elements() {
115         return Collections.enumeration(permissions);
116 
117 	}
118 
119    
120 
121 
122 
123    /**
124     * return the corresponding permission.
125     * @param permissionName
126     * @return permission
127  * @throws NoSuchPermissionException
128     */
129     public Permission getPermission (String permissionName) throws NoSuchPermissionException{
130         Permission permission;
131         Iterator it = permissions.iterator();
132         while(it.hasNext()){
133             permission = (Permission)it.next();
134             if(permission.getName().equals(permissionName)){
135                 return permission;
136             }
137 
138         }
139         logger.warn("permission "+permissionName+" not found in JGPermissionCollection#getPermission!!!");
140         logger.warn("permissions="+permissions);
141         throw new NoSuchPermissionException("permission "+permissionName+" not found in JGPermissionCollection#getPermission");
142 
143     }
144 
145     /**
146      * remove permission from Permission's collection.
147      * @param permission
148      */
149     public void removePermission(Permission permission){
150         if(permission!= null){
151             permissions.remove(permission);
152         }
153     }
154 
155     /**
156      * remove permission from Permission's collection.
157      * @param permission
158      */
159     public void removePermissions(PermissionCollection permColl){
160     	Enumeration permissionsEnum = permColl.elements();
161     	while(permissionsEnum.hasMoreElements()){
162     		permissions.remove((Permission)permissionsEnum.nextElement());
163     	}
164     }
165     
166     /**
167      * remove permission from Permission's collection.
168      * @param permission
169      */
170     public void clear(){
171     	permissions.clear();
172     }
173     
174     @Override
175     public String toString(){
176     	StringBuffer sb = new StringBuffer();
177     	Iterator permissionsIterator = this.permissions.iterator();
178     	while(permissionsIterator.hasNext()){
179     		Permission permission = (Permission)permissionsIterator.next();
180     		sb.append(permission.toString());
181     		sb.append("\n");
182     	}
183     	return sb.toString();
184     }
185 
186     /**
187      *
188      * @return permissions number owned by this JgPermissionCollection.
189      */
190     public int size(){
191     	return permissions.size();
192     }
193 
194 	/**
195 	 * @return Returns the permissions.
196 	 */
197 	public Set getPermissions() {
198 	    return permissions;
199 	}
200 
201 	/**
202 	 *
203 	 * @param permission
204 	 * @return
205 	 */
206 	public boolean containsPermission(Permission permission) {
207 	     return permissions.contains(permission);
208 	}
209 
210 	/**
211 	 * @param perms The permissions to set.
212 	 */
213 	public void setPermissions(Set perms) {
214 	    this.permissions = perms;
215 	}
216 	
217 }