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

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