KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > AllPermission


1 /*
2  * @(#)AllPermission.java 1.20 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7  
8 package java.security;
9
10 import java.security.*;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.StringTokenizer JavaDoc;
14 import sun.security.util.SecurityConstants;
15
16 /**
17  * The AllPermission is a permission that implies all other permissions.
18  * <p>
19  * <b>Note:</b> Granting AllPermission should be done with extreme care,
20  * as it implies all other permissions. Thus, it grants code the ability
21  * to run with security
22  * disabled. Extreme caution should be taken before granting such
23  * a permission to code. This permission should be used only during testing,
24  * or in extremely rare cases where an application or applet is
25  * completely trusted and adding the necessary permissions to the policy
26  * is prohibitively cumbersome.
27  *
28  * @see java.security.Permission
29  * @see java.security.AccessController
30  * @see java.security.Permissions
31  * @see java.security.PermissionCollection
32  * @see java.lang.SecurityManager
33  *
34  * @version 1.20 03/12/19
35  *
36  * @author Roland Schemers
37  *
38  * @serial exclude
39  */

40
41 public final class AllPermission extends Permission JavaDoc {
42
43     private static final long serialVersionUID = -2916474571451318075L;
44
45     /**
46      * Creates a new AllPermission object.
47      */

48
49     public AllPermission()
50     {
51     super("<all permissions>");
52     }
53
54
55     /**
56      * Creates a new AllPermission object. This
57      * constructor exists for use by the <code>Policy</code> object
58      * to instantiate new Permission objects.
59      *
60      * @param name ignored
61      * @param actions ignored.
62      */

63     public AllPermission(String JavaDoc name, String JavaDoc actions)
64     {
65     this();
66     }
67
68     /**
69      * Checks if the specified permission is "implied" by
70      * this object. This method always returns true.
71      *
72      * @param p the permission to check against.
73      *
74      * @return return
75      */

76     public boolean implies(Permission JavaDoc p) {
77      return true;
78     }
79
80     /**
81      * Checks two AllPermission objects for equality. Two AllPermission
82      * objects are always equal.
83      *
84      * @param obj the object we are testing for equality with this object.
85      * @return true if <i>obj</i> is an AllPermission, false otherwise.
86      */

87     public boolean equals(Object JavaDoc obj) {
88     return (obj instanceof AllPermission JavaDoc);
89     }
90
91     /**
92      * Returns the hash code value for this object.
93      *
94      * @return a hash code value for this object.
95      */

96
97     public int hashCode() {
98     return 1;
99     }
100
101     /**
102      * Returns the canonical string representation of the actions.
103      *
104      * @return the actions.
105      */

106     public String JavaDoc getActions()
107     {
108     return "<all actions>";
109     }
110
111     /**
112      * Returns a new PermissionCollection object for storing AllPermission
113      * objects.
114      * <p>
115      *
116      * @return a new PermissionCollection object suitable for
117      * storing AllPermissions.
118      */

119
120     public PermissionCollection JavaDoc newPermissionCollection() {
121     return new AllPermissionCollection();
122     }
123
124 }
125
126 /**
127  * A AllPermissionCollection stores a collection
128  * of AllPermission permissions. AllPermission objects
129  * must be stored in a manner that allows them to be inserted in any
130  * order, but enable the implies function to evaluate the implies
131  * method in an efficient (and consistent) manner.
132  *
133  * @see java.security.Permission
134  * @see java.security.Permissions
135  *
136  * @version 1.20 12/19/03
137  *
138  * @author Roland Schemers
139  *
140  * @serial include
141  */

142
143 final class AllPermissionCollection
144 extends PermissionCollection JavaDoc
145 implements java.io.Serializable JavaDoc
146 {
147
148     // use serialVersionUID from JDK 1.2.2 for interoperability
149
private static final long serialVersionUID = -4023755556366636806L;
150
151     private boolean all_allowed; // true if any all permissions have been added
152

153     /**
154      * Create an empty AllPermissions object.
155      *
156      */

157
158     public AllPermissionCollection() {
159     all_allowed = false;
160     }
161
162     /**
163      * Adds a permission to the AllPermissions. The key for the hash is
164      * permission.path.
165      *
166      * @param permission the Permission object to add.
167      *
168      * @exception IllegalArgumentException - if the permission is not a
169      * AllPermission
170      *
171      * @exception SecurityException - if this AllPermissionCollection object
172      * has been marked readonly
173      */

174
175     public void add(Permission JavaDoc permission)
176     {
177     if (! (permission instanceof AllPermission JavaDoc))
178         throw new IllegalArgumentException JavaDoc("invalid permission: "+
179                            permission);
180     if (isReadOnly())
181         throw new SecurityException JavaDoc("attempt to add a Permission to a readonly PermissionCollection");
182
183     all_allowed = true; // No sync; staleness OK
184
}
185
186     /**
187      * Check and see if this set of permissions implies the permissions
188      * expressed in "permission".
189      *
190      * @param p the Permission object to compare
191      *
192      * @return always returns true.
193      */

194
195     public boolean implies(Permission JavaDoc permission)
196     {
197     return all_allowed; // No sync; staleness OK
198
}
199
200     /**
201      * Returns an enumeration of all the AllPermission objects in the
202      * container.
203      *
204      * @return an enumeration of all the AllPermission objects.
205      */

206     public Enumeration JavaDoc elements()
207     {
208     return new Enumeration JavaDoc() {
209         private boolean hasMore = all_allowed;
210
211         public boolean hasMoreElements() {
212         return hasMore;
213         }
214
215         public Object JavaDoc nextElement() {
216         hasMore = false;
217         return SecurityConstants.ALL_PERMISSION;
218         }
219     };
220     }
221 }
222
223
Popular Tags