001 /*
002 * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.security;
027
028 import java.security.*;
029 import java.util.Enumeration;
030 import java.util.Hashtable;
031 import java.util.StringTokenizer;
032 import sun.security.util.SecurityConstants;
033
034 /**
035 * The AllPermission is a permission that implies all other permissions.
036 * <p>
037 * <b>Note:</b> Granting AllPermission should be done with extreme care,
038 * as it implies all other permissions. Thus, it grants code the ability
039 * to run with security
040 * disabled. Extreme caution should be taken before granting such
041 * a permission to code. This permission should be used only during testing,
042 * or in extremely rare cases where an application or applet is
043 * completely trusted and adding the necessary permissions to the policy
044 * is prohibitively cumbersome.
045 *
046 * @see java.security.Permission
047 * @see java.security.AccessController
048 * @see java.security.Permissions
049 * @see java.security.PermissionCollection
050 * @see java.lang.SecurityManager
051 *
052 * @version 1.28 07/05/05
053 *
054 * @author Roland Schemers
055 *
056 * @serial exclude
057 */
058
059 public final class AllPermission extends Permission {
060
061 private static final long serialVersionUID = -2916474571451318075L;
062
063 /**
064 * Creates a new AllPermission object.
065 */
066
067 public AllPermission() {
068 super ("<all permissions>");
069 }
070
071 /**
072 * Creates a new AllPermission object. This
073 * constructor exists for use by the <code>Policy</code> object
074 * to instantiate new Permission objects.
075 *
076 * @param name ignored
077 * @param actions ignored.
078 */
079 public AllPermission(String name, String actions) {
080 this ();
081 }
082
083 /**
084 * Checks if the specified permission is "implied" by
085 * this object. This method always returns true.
086 *
087 * @param p the permission to check against.
088 *
089 * @return return
090 */
091 public boolean implies(Permission p) {
092 return true;
093 }
094
095 /**
096 * Checks two AllPermission objects for equality. Two AllPermission
097 * objects are always equal.
098 *
099 * @param obj the object we are testing for equality with this object.
100 * @return true if <i>obj</i> is an AllPermission, false otherwise.
101 */
102 public boolean equals(Object obj) {
103 return (obj instanceof AllPermission);
104 }
105
106 /**
107 * Returns the hash code value for this object.
108 *
109 * @return a hash code value for this object.
110 */
111
112 public int hashCode() {
113 return 1;
114 }
115
116 /**
117 * Returns the canonical string representation of the actions.
118 *
119 * @return the actions.
120 */
121 public String getActions() {
122 return "<all actions>";
123 }
124
125 /**
126 * Returns a new PermissionCollection object for storing AllPermission
127 * objects.
128 * <p>
129 *
130 * @return a new PermissionCollection object suitable for
131 * storing AllPermissions.
132 */
133
134 public PermissionCollection newPermissionCollection() {
135 return new AllPermissionCollection();
136 }
137
138 }
139
140 /**
141 * A AllPermissionCollection stores a collection
142 * of AllPermission permissions. AllPermission objects
143 * must be stored in a manner that allows them to be inserted in any
144 * order, but enable the implies function to evaluate the implies
145 * method in an efficient (and consistent) manner.
146 *
147 * @see java.security.Permission
148 * @see java.security.Permissions
149 *
150 * @version 1.28 05/05/07
151 *
152 * @author Roland Schemers
153 *
154 * @serial include
155 */
156
157 final class AllPermissionCollection extends PermissionCollection
158 implements java.io.Serializable {
159
160 // use serialVersionUID from JDK 1.2.2 for interoperability
161 private static final long serialVersionUID = -4023755556366636806L;
162
163 private boolean all_allowed; // true if any all permissions have been added
164
165 /**
166 * Create an empty AllPermissions object.
167 *
168 */
169
170 public AllPermissionCollection() {
171 all_allowed = false;
172 }
173
174 /**
175 * Adds a permission to the AllPermissions. The key for the hash is
176 * permission.path.
177 *
178 * @param permission the Permission object to add.
179 *
180 * @exception IllegalArgumentException - if the permission is not a
181 * AllPermission
182 *
183 * @exception SecurityException - if this AllPermissionCollection object
184 * has been marked readonly
185 */
186
187 public void add(Permission permission) {
188 if (!(permission instanceof AllPermission))
189 throw new IllegalArgumentException("invalid permission: "
190 + permission);
191 if (isReadOnly())
192 throw new SecurityException(
193 "attempt to add a Permission to a readonly PermissionCollection");
194
195 all_allowed = true; // No sync; staleness OK
196 }
197
198 /**
199 * Check and see if this set of permissions implies the permissions
200 * expressed in "permission".
201 *
202 * @param p the Permission object to compare
203 *
204 * @return always returns true.
205 */
206
207 public boolean implies(Permission permission) {
208 return all_allowed; // No sync; staleness OK
209 }
210
211 /**
212 * Returns an enumeration of all the AllPermission objects in the
213 * container.
214 *
215 * @return an enumeration of all the AllPermission objects.
216 */
217 public Enumeration<Permission> elements() {
218 return new Enumeration<Permission>() {
219 private boolean hasMore = all_allowed;
220
221 public boolean hasMoreElements() {
222 return hasMore;
223 }
224
225 public Permission nextElement() {
226 hasMore = false;
227 return SecurityConstants.ALL_PERMISSION;
228 }
229 };
230 }
231 }
|