001: /*
002: * @(#)AllPermission.java 1.21 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.security;
029:
030: import java.security.*;
031: import java.util.Enumeration;
032: import java.util.Hashtable;
033: import java.util.StringTokenizer;
034: import sun.security.util.SecurityConstants;
035:
036: /**
037: * The AllPermission is a permission that implies all other permissions.
038: * <p>
039: * <b>Note:</b> Granting AllPermission should be done with extreme care,
040: * as it implies all other permissions. Thus, it grants code the ability
041: * to run with security
042: * disabled. Extreme caution should be taken before granting such
043: * a permission to code. This permission should be used only during testing,
044: * or in extremely rare cases where an application or applet is
045: * completely trusted and adding the necessary permissions to the policy
046: * is prohibitively cumbersome.
047: *
048: * @see java.security.Permission
049: * @see java.security.AccessController
050: * @see java.security.Permissions
051: * @see java.security.PermissionCollection
052: * @see java.lang.SecurityManager
053: *
054: * @version 1.14 00/02/17
055: *
056: * @author Roland Schemers
057: *
058: * @serial exclude
059: */
060:
061: public final class AllPermission extends Permission {
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.14 02/17/00
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;
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;
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 elements() {
218: return new Enumeration() {
219: private boolean hasMore = all_allowed;
220:
221: public boolean hasMoreElements() {
222: return hasMore;
223: }
224:
225: public Object nextElement() {
226: hasMore = false;
227: return SecurityConstants.ALL_PERMISSION;
228: }
229: };
230: }
231: }
|