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  
31  import java.io.Serializable;
32  import java.security.Permission;
33  import java.security.PermissionCollection;
34  import java.util.HashSet;
35  import java.util.Iterator;
36  import java.util.Set;
37  
38  
39  /**
40   * regroups Permissions (in a 'functional' way) as a JGPositivePermissionCollection
41   *  (additive mechanism).it differs from JGPermissionCollection 
42   *  which regroups Permission in a technical way, by owning a name which
43   *  refers to functional resources.
44   * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
45   *
46   */
47  public class Domain extends JGPermissionCollection implements Comparable,Cloneable,Serializable{
48  
49  	private static final long serialVersionUID = 178066544850786962L;
50  
51      private String name;
52  
53  
54      public Domain(String domainName){
55      	super();
56          name = domainName;
57      }
58      public Domain(String domainName,PermissionCollection pcoll){
59          name = domainName;
60          permissions = new HashSet();
61          super.addAll(pcoll);
62      }
63      /**
64       * @return Returns the domainName.
65       */
66      public String getName() {
67          return name;
68      }
69      /**
70       * @param domainName The domainName to set.
71       */
72      public void setName(String domainName) {
73  
74          this.name = domainName;
75      }
76  
77      /**
78       * override the <i>equals</i> method inherited from Object.
79       * @param obj an Domain
80       * @return true if equals, false otherwise
81       */
82      public boolean equals(Object obj){
83          Domain domain = (Domain)obj;
84          if(this.name.equals(domain.getName())){
85              return true;
86          }
87          return false;
88      }
89  
90      /**
91       * methode used to accelerate the comparation process:
92       *  useful when hashcode return different int.
93       *  it uses hashcode of the name.
94       * @return hashcode
95       */
96      public int hashCode() {
97          return name.toString().hashCode();
98      }
99  
100 
101     /**
102      * override the java.lang.Object 's <i>clone</i> method.
103      * @return new Domain
104      */
105     public Object clone()throws CloneNotSupportedException{
106        JGPermissionCollection dom = new Domain(new String(this.name));
107        Set perms = new HashSet();
108        Iterator itPermissions = permissions.iterator();
109        while(itPermissions.hasNext()){
110            Permission perm = (Permission)itPermissions.next();
111            String permName = perm.getName();
112            String permActions = perm.getActions();
113            Class permClass = perm.getClass();
114            Permission newPerm;
115 		try {
116 			newPerm = PermissionUtils.getPermission(permClass.getName(),permName,permActions);
117 		} catch (ClassNotFoundException e) {
118 			throw new CloneNotSupportedException(e.getMessage());
119 		}
120            perms.add(newPerm);
121        }
122        dom.setPermissions(perms);
123        return dom;
124     }
125 
126     /**
127      * compare name with the name of the specified object for order.
128      * @param o the Domain to compare
129      * @see java.lang.Comparable#compareTo(java.lang.Object)
130      */
131     public int compareTo(Object o) {
132        Domain domain = (Domain)o;
133        return this.name.compareTo(domain.getName());
134     }
135 
136 
137     /**
138      * convert object into a string representation.
139      * @return string representation
140      */
141     public String toString(){
142         StringBuffer sb = new StringBuffer();
143         sb.append("\n");
144         sb.append(" name="+name);
145         sb.append("\n");
146         sb.append(" permissions=\n"+super.toString());
147         sb.append("\n");
148         return sb.toString();
149     }
150     
151     public boolean implies(Permission permission){
152 	    Iterator it = permissions.iterator();
153 		Permission p;
154 	
155 	    while(it.hasNext()){
156 			p = (Permission) it.next();
157 			if (p.implies(permission)) {
158 				return true;
159 			}
160 		}
161 		return false;
162 	}
163 
164 }