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

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