home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 7.5 KB | 254 lines |
- /*
- * @(#)UnresolvedPermission.java 1.6 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.security;
-
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
- import java.lang.reflect.*;
-
- /**
- * The UnresolvedPermission class is used to hold Permissions that
- * were "unresolved" when the Policy was initialized.
- * An unresolved permission is one whose actual Permission class
- * does not yet exist at the time the Policy is initialized (see below).
- *
- * <p>The policy for a Java runtime (specifying
- * which permissions are available for code from various principals)
- * is represented by a Policy object.
- * Whenever a Policy is initialized or refreshed, Permission objects of
- * appropriate classes are created for all permissions
- * allowed by the Policy.
- *
- * <p>Many permission class types
- * referenced by the policy configuration are ones that exist
- * locally (i.e., ones that can be found on CLASSPATH).
- * Objects for such permissions can be instantiated during
- * Policy initialization. For example, it is always possible
- * to instantiate a java.io.FilePermission, since the
- * FilePermission class is found on the CLASSPATH.
- *
- * <p>Other permission classes may not yet exist during Policy
- * initialization. For example, a referenced permission class may
- * be in a JAR file that will later be loaded.
- * For each such class, an UnresolvedPermission is instantiated.
- * Thus, an UnresolvedPermission is essentially a "placeholder"
- * containing information about the permission.
- *
- * <p>Later, when code calls AccessController.checkPermission
- * on a permission of a type that was previously unresolved,
- * but whose class has since been loaded, previously-unresolved
- * permissions of that type are "resolved". That is,
- * for each such UnresolvedPermission, a new object of
- * the appropriate class type is instantiated, based on the
- * information in the UnresolvedPermission. This new object
- * replaces the UnresolvedPermission, which is removed.
- *
- * @see java.security.Permission
- * @see java.security.Permissions
- * @see java.security.PermissionCollection
- * @see java.security.Policy
- *
- * @version 1.6 98/03/18
- *
- * @author Roland Schemers
- */
-
- public final class UnresolvedPermission extends Permission
- implements java.io.Serializable
- {
- private String type;
- private String name;
- private String actions;
- private PublicKey keys[];
-
- /**
- * Creates a new UnresolvedPermission containing the permission
- * information needed later to actually create a Permission of the
- * specified class, when the permission is resolved.
- *
- * @param type the class name of the Permission class that will be
- * created when this unresolved permission is resolved.
- * @param name the name of the permission.
- * @param actions the actions of the permission.
- * @param keys the public keys the permission's class was signed with.
- */
-
- UnresolvedPermission(String type,
- String name,
- String actions,
- PublicKey keys[])
- {
- super(type);
- this.type = type;
- this.name = name;
- this.actions = actions;
- this.keys = keys;
- }
-
-
- private static final Class[] PARAMS = { String.class, String.class};
-
- /**
- * try and resolve this permission using the class loader of the permission
- * that was passed in.
- */
- Permission resolve(Permission p, PublicKey keys[]) {
- if (this.keys != null) {
- // if p wasn't signed, we don't have a match
- if (keys == null) {
- return null;
- }
-
- // all keys in this.keys must be present in keys
- boolean match;
- for (int i = 0; i < this.keys.length; i++) {
- match = false;
- for (int j = 0; j < keys.length; j++) {
- if (this.keys[i].equals(keys[j])) {
- match = true;
- break;
- }
- }
- if (!match) return null;
- }
- }
- try {
- Class pc = p.getClass();
- Constructor c = pc.getConstructor(PARAMS);
- return (Permission) c.newInstance(new Object[] { name, actions });
- } catch (Exception e) {
- return null;
- }
- }
-
- /**
- * This method always returns false for unresolved permissions.
- * That is, an UnresolvedPermission is never considered to
- * imply another permission.
- *
- * @param p the permission to check against.
- *
- * @return false.
- */
- public boolean implies(Permission p) {
- return false;
- }
-
- /**
- * Checks two UnresolvedPermission objects for equality.
- * Checks that <i>obj</i> is an UnresolvedPermission, and has
- * the same type (class) name, permission name, actions, and
- * public keys as this object.
- *
- * @param obj the object we are testing for equality with this object.
- *
- * @return true if obj is an UnresolvedPermission, and has the same
- * type (class) name, permission name, actions, and
- * public keys as this object.
- */
- public boolean equals(Object obj) {
- if (obj == this)
- return true;
-
- if (! (obj instanceof UnresolvedPermission))
- return false;
- UnresolvedPermission that = (UnresolvedPermission) obj;
-
- if (!(this.type.equals(that.type) &&
- this.name.equals(that.name) &&
- this.actions.equals(that.actions)))
- return false;
-
- if (this.keys.length != that.keys.length)
- return false;
-
- int i,j;
- boolean match;
-
- for (i = 0; i < this.keys.length; i++) {
- match = false;
- for (j = 0; j < that.keys.length; j++) {
- if (this.keys[i].equals(that.keys[j])) {
- match = true;
- break;
- }
- }
- if (!match) return false;
- }
-
- for (i = 0; i < that.keys.length; i++) {
- match = false;
- for (j = 0; j < this.keys.length; j++) {
- if (that.keys[i].equals(this.keys[j])) {
- match = true;
- break;
- }
- }
- if (!match) return false;
- }
- return true;
- }
-
- /**
- * Returns the hash code value for this object.
- *
- * @return a hash code value for this object.
- */
-
- public int hashCode() {
- return type.hashCode()^name.hashCode()^actions.hashCode();
- }
-
- /**
- * Returns the canonical string representation of the actions,
- * which currently is the empty string "", since there are no actions for
- * an UnresolvedPermission. That is, the actions for the
- * permission that will be created when this UnresolvedPermission
- * is resolved may be non-null, but an UnresolvedPermission
- * itself is never considered to have any actions.
- *
- * @return the empty string "".
- */
- public String getActions()
- {
- return "";
- }
-
- /**
- * Returns a string describing this UnresolvedPermission. The convention
- * is to specify the class name, the permission name, and the actions, in
- * the following format: '(unresolved "ClassName" "name" "actions")'.
- *
- * @return information about this UnresolvedPermission.
- */
- public String toString() {
- return "(unresolved " + type + " " + name + " " + actions + ")";
- }
-
- /**
- * Returns a new PermissionCollection object for storing
- * UnresolvedPermission objects.
- * <p>
- * @return a new PermissionCollection object suitable for
- * storing UnresolvedPermissions.
- */
-
- public PermissionCollection newPermissionCollection() {
- return new UnresolvedPermissionCollection();
- }
-
- }
-