home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / security / AllPermission.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.0 KB  |  212 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)AllPermission.java    1.4 98/04/30
  3.  *
  4.  * Copyright 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.security.*;
  18. import java.util.Enumeration;
  19. import java.util.Hashtable;
  20. import java.util.StringTokenizer;
  21.  
  22. /**
  23.  * The AllPermission is a permission that implies all other permissions.
  24.  * <p>
  25.  * <b>Note:</b> Granting AllPermission should be done with extreme care,
  26.  * as it implies all other permissions. Thus, it grants code the ability 
  27.  * to run with security
  28.  * disabled.  Extreme caution should be taken before granting such
  29.  * a permission to code.  This permission should be used only during testing,
  30.  * or in extremely rare cases where an application or applet is
  31.  * completely trusted and adding the necessary permissions to the policy 
  32.  * is prohibitively cumbersome.
  33.  * 
  34.  * @see java.security.Permission
  35.  * @see java.security.AccessController
  36.  * @see java.security.Permissions
  37.  * @see java.security.PermissionCollection
  38.  * @see java.lang.SecurityManager
  39.  *
  40.  * @version 1.4 99/03/26
  41.  *
  42.  * @author Roland Schemers
  43.  */
  44.  
  45. public final class AllPermission extends Permission {
  46.  
  47.     /**
  48.      * Creates a new AllPermission object.
  49.      */
  50.  
  51.     public AllPermission()
  52.     {
  53.     super("<all permissions>");
  54.     }
  55.  
  56.  
  57.     /**
  58.      * Creates a new AllPermission object. This
  59.      * constructor exists for use by the <code>Policy</code> object
  60.      * to instantiate new Permission objects.
  61.      *
  62.      * @param name ignored
  63.      * @param actions ignored.
  64.      */
  65.     public AllPermission(String name, String actions) 
  66.     {
  67.     this();
  68.     }
  69.  
  70.     /**
  71.      * Checks if the specified permission is "implied" by 
  72.      * this object. This method always returns true.
  73.      *
  74.      * @param p the permission to check against.
  75.      *
  76.      * @return return
  77.      */
  78.     public boolean implies(Permission p) {
  79.      return true;
  80.     }
  81.  
  82.     /**
  83.      * Checks two AllPermission objects for equality. Two AllPermission
  84.      * objects are always equal.
  85.      *
  86.      * @param obj the object we are testing for equality with this object.
  87.      * @return true if <i>obj</i> is an AllPermission, false otherwise.
  88.      */
  89.     public boolean equals(Object obj) {
  90.     return (obj instanceof AllPermission);
  91.     }
  92.  
  93.     /**
  94.      * Returns the hash code value for this object.
  95.      * 
  96.      * @return a hash code value for this object.
  97.      */
  98.  
  99.     public int hashCode() {
  100.     return 1;
  101.     }
  102.  
  103.     /**
  104.      * Returns the canonical string representation of the actions.
  105.      *
  106.      * @return the actions.
  107.      */
  108.     public String getActions()
  109.     {
  110.     return "<all actions>";
  111.     }
  112.  
  113.     /**
  114.      * Returns a new PermissionCollection object for storing AllPermission 
  115.      * objects.
  116.      * <p>
  117.      * 
  118.      * @return a new PermissionCollection object suitable for 
  119.      * storing AllPermissions.
  120.      */
  121.  
  122.     public PermissionCollection newPermissionCollection() {
  123.     return new AllPermissionCollection();
  124.     }
  125.  
  126. }
  127.  
  128. /**
  129.  * A AllPermissionCollection stores a collection
  130.  * of AllPermission permissions. AllPermission objects
  131.  * must be stored in a manner that allows them to be inserted in any
  132.  * order, but enable the implies function to evaluate the implies
  133.  * method in an efficient (and consistent) manner. 
  134.  *
  135.  * @see java.security.Permission
  136.  * @see java.security.Permissions
  137.  *
  138.  * @version 1.4 99/03/26
  139.  *
  140.  * @author Roland Schemers
  141.  */
  142.  
  143. final class AllPermissionCollection
  144. extends PermissionCollection 
  145. implements java.io.Serializable 
  146. {
  147.  
  148.     private boolean all_allowed; // true if any all permissions have been added
  149.  
  150.     /**
  151.      * Create an empty AllPermissions object.
  152.      *
  153.      */
  154.  
  155.     public AllPermissionCollection() {
  156.     all_allowed = false;
  157.     }
  158.  
  159.     /**
  160.      * Adds a permission to the AllPermissions. The key for the hash is
  161.      * permission.path.
  162.      *
  163.      * @param permission the Permission object to add.
  164.      */
  165.  
  166.     public void add(Permission permission)
  167.     {
  168.     if (! (permission instanceof AllPermission))
  169.         throw new IllegalArgumentException("invalid permission: "+
  170.                            permission);
  171.     all_allowed = true;
  172.     }
  173.  
  174.     /**
  175.      * Check and see if this set of permissions implies the permissions 
  176.      * expressed in "permission".
  177.      *
  178.      * @param p the Permission object to compare
  179.      *
  180.      * @return always returns true.
  181.      */
  182.  
  183.     public boolean implies(Permission permission) 
  184.     {
  185.     return all_allowed;
  186.     }
  187.  
  188.     /**
  189.      * Returns an enumeration of all the AllPermission objects in the 
  190.      * container.
  191.      *
  192.      * @return an enumeration of all the AllPermission objects.
  193.      */
  194.  
  195.     public Enumeration elements()
  196.     {
  197.     return new Enumeration() {
  198.         private boolean done = false;
  199.  
  200.         public boolean hasMoreElements() {
  201.         return !done;
  202.         }
  203.  
  204.         public Object nextElement() {
  205.         done = true;
  206.         return new AllPermission();
  207.         }
  208.     };
  209.     }
  210. }
  211.  
  212.