home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / security / UnresolvedPermission.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  7.5 KB  |  254 lines

  1. /*
  2.  * @(#)UnresolvedPermission.java    1.6 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.util.Enumeration;
  18. import java.util.Hashtable;
  19. import java.util.Vector;
  20. import java.lang.reflect.*;
  21.  
  22. /**
  23.  * The UnresolvedPermission class is used to hold Permissions that
  24.  * were "unresolved" when the Policy was initialized. 
  25.  * An unresolved permission is one whose actual Permission class
  26.  * does not yet exist at the time the Policy is initialized (see below).
  27.  * 
  28.  * <p>The policy for a Java runtime (specifying 
  29.  * which permissions are available for code from various principals)
  30.  * is represented by a Policy object.
  31.  * Whenever a Policy is initialized or refreshed, Permission objects of
  32.  * appropriate classes are created for all permissions
  33.  * allowed by the Policy. 
  34.  * 
  35.  * <p>Many permission class types 
  36.  * referenced by the policy configuration are ones that exist
  37.  * locally (i.e., ones that can be found on CLASSPATH).
  38.  * Objects for such permissions can be instantiated during
  39.  * Policy initialization. For example, it is always possible
  40.  * to instantiate a java.io.FilePermission, since the
  41.  * FilePermission class is found on the CLASSPATH.
  42.  * 
  43.  * <p>Other permission classes may not yet exist during Policy
  44.  * initialization. For example, a referenced permission class may
  45.  * be in a JAR file that will later be loaded.
  46.  * For each such class, an UnresolvedPermission is instantiated.
  47.  * Thus, an UnresolvedPermission is essentially a "placeholder"
  48.  * containing information about the permission.
  49.  * 
  50.  * <p>Later, when code calls AccessController.checkPermission 
  51.  * on a permission of a type that was previously unresolved,
  52.  * but whose class has since been loaded, previously-unresolved
  53.  * permissions of that type are "resolved". That is,
  54.  * for each such UnresolvedPermission, a new object of
  55.  * the appropriate class type is instantiated, based on the
  56.  * information in the UnresolvedPermission. This new object
  57.  * replaces the UnresolvedPermission, which is removed.
  58.  *
  59.  * @see java.security.Permission
  60.  * @see java.security.Permissions
  61.  * @see java.security.PermissionCollection
  62.  * @see java.security.Policy
  63.  *
  64.  * @version 1.6 98/03/18
  65.  *
  66.  * @author Roland Schemers
  67.  */
  68.  
  69. public final class UnresolvedPermission extends Permission 
  70. implements java.io.Serializable
  71. {
  72.     private String type;
  73.     private String name;
  74.     private String actions;
  75.     private PublicKey keys[];
  76.  
  77.     /**
  78.      * Creates a new UnresolvedPermission containing the permission
  79.      * information needed later to actually create a Permission of the
  80.      * specified class, when the permission is resolved.
  81.      * 
  82.      * @param type the class name of the Permission class that will be
  83.      * created when this unresolved permission is resolved.
  84.      * @param name the name of the permission.
  85.      * @param actions the actions of the permission.
  86.      * @param keys the public keys the permission's class was signed with.
  87.      */
  88.  
  89.     UnresolvedPermission(String type,
  90.              String name,
  91.              String actions,
  92.              PublicKey keys[])
  93.     {
  94.     super(type);
  95.     this.type = type;
  96.     this.name = name;
  97.     this.actions = actions;
  98.     this.keys = keys;
  99.     }
  100.  
  101.  
  102.     private static final Class[] PARAMS = { String.class, String.class};
  103.  
  104.     /**
  105.      * try and resolve this permission using the class loader of the permission
  106.      * that was passed in.
  107.      */
  108.     Permission resolve(Permission p, PublicKey keys[]) {
  109.     if (this.keys != null) {
  110.         // if p wasn't signed, we don't have a match
  111.         if (keys == null) {
  112.         return null;
  113.         }
  114.  
  115.         // all keys in this.keys must be present in keys
  116.         boolean match;
  117.         for (int i = 0; i < this.keys.length; i++) {
  118.         match = false;
  119.         for (int j = 0; j < keys.length; j++) {
  120.             if (this.keys[i].equals(keys[j])) {
  121.             match = true;
  122.             break;
  123.             }
  124.         }
  125.         if (!match) return null;
  126.         }
  127.     }
  128.     try {
  129.         Class pc = p.getClass();
  130.         Constructor c = pc.getConstructor(PARAMS);
  131.         return (Permission) c.newInstance(new Object[] { name, actions });
  132.     } catch (Exception e) {
  133.         return null;
  134.     }
  135.     }
  136.  
  137.     /**
  138.      * This method always returns false for unresolved permissions.
  139.      * That is, an UnresolvedPermission is never considered to
  140.      * imply another permission.
  141.      *
  142.      * @param p the permission to check against.
  143.      * 
  144.      * @return false.
  145.      */
  146.     public boolean implies(Permission p) {
  147.     return false;
  148.     }
  149.  
  150.     /**
  151.      * Checks two UnresolvedPermission objects for equality. 
  152.      * Checks that <i>obj</i> is an UnresolvedPermission, and has 
  153.      * the same type (class) name, permission name, actions, and
  154.      * public keys as this object.
  155.      * 
  156.      * @param obj the object we are testing for equality with this object.
  157.      * 
  158.      * @return true if obj is an UnresolvedPermission, and has the same 
  159.      * type (class) name, permission name, actions, and
  160.      * public keys as this object.
  161.      */
  162.     public boolean equals(Object obj) {
  163.     if (obj == this)
  164.         return true;
  165.  
  166.     if (! (obj instanceof UnresolvedPermission))
  167.         return false;
  168.     UnresolvedPermission that = (UnresolvedPermission) obj;
  169.  
  170.     if (!(this.type.equals(that.type) &&
  171.         this.name.equals(that.name) &&
  172.         this.actions.equals(that.actions)))
  173.         return false;
  174.  
  175.     if (this.keys.length != that.keys.length)
  176.         return false;
  177.         
  178.     int i,j;
  179.     boolean match;
  180.  
  181.     for (i = 0; i < this.keys.length; i++) {
  182.         match = false;
  183.         for (j = 0; j < that.keys.length; j++) {
  184.         if (this.keys[i].equals(that.keys[j])) {
  185.             match = true;
  186.             break;
  187.         }
  188.         }
  189.         if (!match) return false;
  190.     }
  191.  
  192.     for (i = 0; i < that.keys.length; i++) {
  193.         match = false;
  194.         for (j = 0; j < this.keys.length; j++) {
  195.         if (that.keys[i].equals(this.keys[j])) {
  196.             match = true;
  197.             break;
  198.         }
  199.         }
  200.         if (!match) return false;
  201.     }
  202.     return true;
  203.     }
  204.  
  205.     /**
  206.      * Returns the hash code value for this object.
  207.      *
  208.      * @return a hash code value for this object.
  209.      */
  210.  
  211.     public int hashCode() {
  212.     return type.hashCode()^name.hashCode()^actions.hashCode();
  213.     }
  214.  
  215.     /**
  216.      * Returns the canonical string representation of the actions,
  217.      * which currently is the empty string "", since there are no actions for 
  218.      * an UnresolvedPermission. That is, the actions for the
  219.      * permission that will be created when this UnresolvedPermission
  220.      * is resolved may be non-null, but an UnresolvedPermission
  221.      * itself is never considered to have any actions.
  222.      *
  223.      * @return the empty string "".
  224.      */
  225.     public String getActions()
  226.     {
  227.     return "";
  228.     }
  229.  
  230.     /**
  231.      * Returns a string describing this UnresolvedPermission.  The convention 
  232.      * is to specify the class name, the permission name, and the actions, in
  233.      * the following format: '(unresolved "ClassName" "name" "actions")'.
  234.      * 
  235.      * @return information about this UnresolvedPermission.
  236.      */
  237.     public String toString() {
  238.     return "(unresolved " + type + " " + name + " " + actions + ")";
  239.     }
  240.  
  241.     /**
  242.      * Returns a new PermissionCollection object for storing 
  243.      * UnresolvedPermission  objects.
  244.      * <p>
  245.      * @return a new PermissionCollection object suitable for 
  246.      * storing UnresolvedPermissions.
  247.      */
  248.  
  249.     public PermissionCollection newPermissionCollection() {
  250.     return new UnresolvedPermissionCollection();
  251.     }
  252.  
  253. }
  254.