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 / BasicPermission.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  10.2 KB  |  378 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)BasicPermission.java    1.16 98/09/15
  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.security.*;
  18. import java.util.Enumeration;
  19. import java.util.Hashtable;
  20. import java.util.StringTokenizer;
  21. import java.io.IOException;
  22.  
  23. /**
  24.  * The BasicPermission class extends the Permission class, and
  25.  * can be used as the base class for permissions that want to
  26.  * follow the same naming convention as BasicPermission.
  27.  * <P>
  28.  * The name for a BasicPermission is the name of the given permission
  29.  * (for example, "exit",
  30.  * "setFactory", "print.queueJob", etc). The naming
  31.  * convention follows the  hierarchical property naming convention.
  32.  * An asterisk
  33.  * may appear at the end of the name, following a ".", or by itself, to
  34.  * signify a wildcard match. For example: "java.*" or "*" is valid,
  35.  * "*java" or "a*b" is not valid.
  36.  * <P>
  37.  * The action string (inherited from Permission) is unused.
  38.  * Thus, BasicPermission is commonly used as the base class for
  39.  * "named" permissions
  40.  * (ones that contain a name but no actions list; you either have the
  41.  * named permission or you don't.)
  42.  * Subclasses may implement actions on top of BasicPermission,
  43.  * if desired.
  44.  * <p>
  45.  * <P>
  46.  * @see java.security.Permission
  47.  * @see java.security.Permissions
  48.  * @see java.security.PermissionCollection
  49.  * @see java.lang.RuntimePermission
  50.  * @see java.security.SecurityPermission
  51.  * @see java.util.PropertyPermission
  52.  * @see java.awt.AWTPermission
  53.  * @see java.net.NetPermission
  54.  * @see java.lang.SecurityManager
  55.  *
  56.  * @version 1.16 99/03/26
  57.  *
  58.  * @author Marianne Mueller
  59.  * @author Roland Schemers
  60.  */
  61.  
  62. public abstract class BasicPermission extends Permission
  63. implements java.io.Serializable
  64. {
  65.  
  66.     // does this permission have a wildcard at the end?
  67.     private transient boolean wildcard;
  68.  
  69.     // the name without the wildcard on the end
  70.     private transient String path;
  71.  
  72.     /**
  73.      * initialize a BasicPermission object. Common to all constructors.
  74.      *
  75.      */
  76.  
  77.     private void init(String name)
  78.     {
  79.  
  80.     if (name == null)
  81.         throw new IllegalArgumentException("name can't be null");
  82.  
  83.     if (name.endsWith(".*") || name.equals("*")) {
  84.         wildcard = true;
  85.         if (name.length() == 1) {
  86.         path = "";
  87.         } else {
  88.         path = name.substring(0, name.length()-1);
  89.         }
  90.     } else {
  91.         path = name;
  92.     }
  93.     }
  94.  
  95.     /**
  96.      * Creates a new BasicPermission with the specified name.
  97.      * Name is the symbolic name of the permission, such as
  98.      * "setFactory",
  99.      * "print.queueJob", or "topLevelWindow", etc. An asterisk
  100.      * may appear at the end of the name, following a ".", or by itself, to
  101.      * signify a wildcard match.
  102.      *
  103.      * @param name the name of the BasicPermission.
  104.      */
  105.  
  106.     public BasicPermission(String name)
  107.     {
  108.     super(name);
  109.     init(name);
  110.     }
  111.  
  112.  
  113.     /**
  114.      * Creates a new BasicPermission object with the specified name.
  115.      * The name is the symbolic name of the BasicPermission, and the
  116.      * actions String is currently unused. This
  117.      * constructor exists for use by the <code>Policy</code> object
  118.      * to instantiate new Permission objects.
  119.      *
  120.      * @param name the name of the BasicPermission.
  121.      * @param actions ignored.
  122.      */
  123.     public BasicPermission(String name, String actions)
  124.     {
  125.     super(name);
  126.     init(name);
  127.     }
  128.  
  129.     /**
  130.      * Checks if the specified permission is "implied" by
  131.      * this object.
  132.      * <P>
  133.      * More specifically, this method returns true if:<p>
  134.      * <ul>
  135.      * <li> <i>p</i>'s class is the same as this object's class, and<p>
  136.      * <li> <i>p</i>'s name equals or (in the case of wildcards)
  137.      *      is implied by this object's
  138.      *      name. For example, "a.b.*" implies "a.b.c".
  139.      * </ul>
  140.      *
  141.      * @param p the permission to check against.
  142.      *
  143.      * @return true if the passed permission is equal to or
  144.      * implied by this permission, false otherwise.
  145.      */
  146.     public boolean implies(Permission p) {
  147.     if ((p == null) || (p.getClass() != getClass()))
  148.         return false;
  149.  
  150.     BasicPermission that = (BasicPermission) p;
  151.  
  152.     if (this.wildcard) {
  153.         if (that.wildcard)
  154.         // one wildcard can imply another
  155.         return that.path.startsWith(path);
  156.         else
  157.         // make sure ap.path is longer so a.b.* doesn't imply a.b
  158.         return (that.path.length() > this.path.length()) &&
  159.             that.path.startsWith(this.path);
  160.     } else {
  161.         if (that.wildcard) {
  162.         // a non-wildcard can't imply a wildcard
  163.         return false;
  164.         }
  165.         else {
  166.         return this.path.equals(that.path);
  167.         }
  168.     }
  169.     }
  170.  
  171.     /**
  172.      * Checks two BasicPermission objects for equality.
  173.      * Checks that <i>obj</i>'s class is the same as this object's class
  174.      * and has the same name as this object.
  175.      * <P>
  176.      * @param obj the object we are testing for equality with this object.
  177.      * @return true if <i>obj</i> is a BasicPermission, and has the same name
  178.      *  as this BasicPermission object, false otherwise.
  179.      */
  180.     public boolean equals(Object obj) {
  181.     if (obj == this)
  182.         return true;
  183.  
  184.     if ((obj == null) || (obj.getClass() != getClass()))
  185.         return false;
  186.  
  187.     BasicPermission bp = (BasicPermission) obj;
  188.  
  189.     return getName().equals(bp.getName());
  190.     }
  191.  
  192.  
  193.     /**
  194.      * Returns the hash code value for this object.
  195.      * The hash code used is the hash code of the name, that is,
  196.      * <code>getName().hashCode()</code>, where <code>getName</code> is
  197.      * from the Permission superclass.
  198.      *
  199.      * @return a hash code value for this object.
  200.      */
  201.  
  202.     public int hashCode() {
  203.     return this.getName().hashCode();
  204.     }
  205.  
  206.     /**
  207.      * Returns the canonical string representation of the actions,
  208.      * which currently is the empty string "", since there are no actions for
  209.      * a BasicPermission.
  210.      *
  211.      * @return the empty string "".
  212.      */
  213.     public String getActions()
  214.     {
  215.     return "";
  216.     }
  217.  
  218.     /**
  219.      * Returns a new PermissionCollection object for storing BasicPermission
  220.      * objects.
  221.      * <p>
  222.      * A BasicPermissionCollection stores a collection of
  223.      * BasicPermission permissions.
  224.      *
  225.      * <p>BasicPermission objects must be stored in a manner that allows them
  226.      * to be inserted in any order, but that also enables the
  227.      * PermissionCollection <code>implies</code> method
  228.      * to be implemented in an efficient (and consistent) manner.
  229.      *
  230.      * @return a new PermissionCollection object suitable for
  231.      * storing BasicPermissions.
  232.      */
  233.  
  234.     public PermissionCollection newPermissionCollection() {
  235.     return new BasicPermissionCollection();
  236.     }
  237.  
  238.     /**
  239.      * readObject is called to restore the state of the BasicPermission from
  240.      * a stream. 
  241.      */
  242.     private synchronized void readObject(java.io.ObjectInputStream s)
  243.          throws IOException, ClassNotFoundException
  244.     {
  245.     s.defaultReadObject();
  246.     // init is called to initialize the rest of the values.
  247.     init(getName());
  248.     }
  249. }
  250.  
  251. /**
  252.  * A BasicPermissionCollection stores a collection
  253.  * of BasicPermission permissions. BasicPermission objects
  254.  * must be stored in a manner that allows them to be inserted in any
  255.  * order, but enable the implies function to evaluate the implies
  256.  * method in an efficient (and consistent) manner.
  257.  *
  258.  * A BasicPermissionCollection handles comparing a permission like "a.b.c.d.e"
  259.  * with a Permission such as "a.b.*", or "*".
  260.  *
  261.  * @see java.security.Permission
  262.  * @see java.security.Permissions
  263.  * @see java.security.PermissionsImpl
  264.  *
  265.  * @version 1.16 99/03/26
  266.  *
  267.  * @author Roland Schemers
  268.  */
  269.  
  270. final class BasicPermissionCollection
  271. extends PermissionCollection
  272. implements java.io.Serializable
  273. {
  274.  
  275.     private Hashtable permissions;
  276.     private boolean all_allowed; // true if "*" is in the collection
  277.  
  278.     /**
  279.      * Create an empty BasicPermissions object.
  280.      *
  281.      */
  282.  
  283.     public BasicPermissionCollection() {
  284.     permissions = new Hashtable(11);
  285.     all_allowed = false;
  286.     }
  287.  
  288.     /**
  289.      * Adds a permission to the BasicPermissions. The key for the hash is
  290.      * permission.path.
  291.      *
  292.      * @param permission the Permission object to add.
  293.      */
  294.  
  295.     public void add(Permission permission)
  296.     {
  297.     if (! (permission instanceof BasicPermission))
  298.         throw new IllegalArgumentException("invalid permission: "+
  299.                            permission);
  300.     BasicPermission bp = (BasicPermission) permission;
  301.  
  302.     permissions.put(bp.getName(), permission);
  303.         if (!all_allowed) {
  304.         if (bp.getName().equals("*"))
  305.         all_allowed = true;
  306.     }
  307.     }
  308.  
  309.     /**
  310.      * Check and see if this set of permissions implies the permissions
  311.      * expressed in "permission".
  312.      *
  313.      * @param p the Permission object to compare
  314.      *
  315.      * @return true if "permission" is a proper subset of a permission in
  316.      * the set, false if not.
  317.      */
  318.  
  319.     public boolean implies(Permission permission)
  320.     {
  321.     if (! (permission instanceof BasicPermission))
  322.            return false;
  323.  
  324.     BasicPermission bp = (BasicPermission) permission;
  325.  
  326.     // short circuit if the "*" Permission was added
  327.     if (all_allowed)
  328.         return true;
  329.  
  330.     // strategy:
  331.     // Check for full match first. Then work our way up the
  332.     // path looking for matches on a.b..*
  333.  
  334.     String path = bp.getName();
  335.     //System.out.println("check "+path);
  336.  
  337.     Permission x = (Permission) permissions.get(path);
  338.  
  339.     if (x != null) {
  340.         // we have a direct hit!
  341.         return x.implies(permission);
  342.     }
  343.  
  344.     // work our way up the tree...
  345.     int last, offset;
  346.  
  347.     offset = path.length()-1;
  348.  
  349.     while ((last = path.lastIndexOf(".", offset)) != -1) {
  350.  
  351.         path = path.substring(0, last+1) + "*";
  352.         //System.out.println("check "+path);
  353.         x = (Permission) permissions.get(path);
  354.  
  355.         if (x != null) {
  356.         return x.implies(permission);
  357.         }
  358.         offset = last -1;
  359.     }
  360.  
  361.     // we don't have to check for "*" as it was already checked
  362.     // at the top (all_allowed), so we just return false
  363.     return false;
  364.     }
  365.  
  366.     /**
  367.      * Returns an enumeration of all the BasicPermission objects in the
  368.      * container.
  369.      *
  370.      * @return an enumeration of all the BasicPermission objects.
  371.      */
  372.  
  373.     public Enumeration elements()
  374.     {
  375.     return permissions.elements();
  376.     }
  377. }
  378.