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

  1. /*
  2.  * @(#)PermissionCollection.java    1.19 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.util.*;
  18.  
  19. /**
  20.  * Abstract class representing a collection of Permission objects. 
  21.  * 
  22.  * <p>With a PermissionCollection, you can:
  23.  * <UL>
  24.  * <LI> add a permission to the collection using the <code>add</code> method.
  25.  * <LI> check to see if a particular permission is implied in the 
  26.  *      collection, using the <code>implies</code> method.
  27.  * <LI> enumerate all the permissions, using the <code>elements</code> method.
  28.  * </UL>
  29.  * <P>
  30.  * 
  31.  * <p>When it is desirable to group together a number of Permission objects of the
  32.  * same type, the <code>newPermissionCollection</code> method on that particular 
  33.  * type of Permission object should first be called. The default behavior (from the 
  34.  * Permission class) is to simply return null. Subclasses of class Permission 
  35.  * override the method if they need to store their permissions in a particular
  36.  * PermissionCollection object in order to provide the correct semantics
  37.  * when the <code>PermissionCollection.implies</code> method is called. 
  38.  * If a non-null value is returned, that PermissionCollection must be used.
  39.  * If null is returned, then the caller of <code>newPermissionCollection</code> 
  40.  * is free to store permissions of the
  41.  * given type in any PermissionCollection they choose (one that uses a Hashtable,
  42.  * one that uses a Vector, etc).
  43.  * 
  44.  * <p>The PermissionCollection returned by the 
  45.  * <code>Permission.newPermissionCollection</code> 
  46.  * method is a homogeneous collection, which stores only Permission objects
  47.  * for a given Permission type.  A PermissionCollection may also be heterogenous.
  48.  * For example, Permissions is a PermissionCollection subclass that represents a 
  49.  * collection of PermissionCollections. That is, its members are each a homogeneous
  50.  * PermissionCollection. For example, a Permissions object might have a 
  51.  * FilePermissionCollection
  52.  * for all the FilePermission objects, a SocketPermissionCollection for all the
  53.  * SocketPermission objects, and so on. Its <code>add</code> method adds a permission
  54.  * to the appropriate collection.
  55.  * 
  56.  * <p>Whenever a permission is added to a heterogeneous PermissionCollection such 
  57.  * as Permissions, and the PermissionCollection doesn't yet contain a 
  58.  * PermissionCollection of the specified permission's type, the 
  59.  * PermissionCollection should call 
  60.  * the <code>newPermissionCollection</code> method on the permission's class 
  61.  * to see if it requires a special PermissionCollection. If 
  62.  * <code>newPermissionCollection</code>
  63.  * returns null, the PermissionCollection
  64.  * is free to store the permission in any type of PermissionCollection it desires
  65.  * (one using a Hastable, one using a Vector, etc.). For example,
  66.  * the Permissions object uses a default PermissionCollection implementation
  67.  * that stores the permission objects in a Hashtable.
  68.  *
  69.  * @see Permission
  70.  * @see Permissions
  71.  *
  72.  * @version 1.19 98/03/18
  73.  *
  74.  * @author Roland Schemers 
  75.  */
  76.  
  77. public abstract class PermissionCollection implements java.io.Serializable {
  78.  
  79.     /** use serialVersionUID from JDK 1.2 for interoperability */
  80.     private static final long serialVersionUID = -3258014079993964575L;
  81.  
  82.     /**
  83.      * Adds a permission object to the current collection of permission objects.
  84.      *
  85.      * @param permission the Permission object to add.
  86.      */
  87.  
  88.     public abstract void add(Permission permission);
  89.  
  90.     /**
  91.      * Checks to see if the specified permission is implied by
  92.      * the collection of Permission objects held in this PermissionCollection. 
  93.      * 
  94.      * @param permission the Permission object to compare.
  95.      *
  96.      * @return true if "permission" is implied by the  permissions in 
  97.      * the collection, false if not.  
  98.      */
  99.     public abstract boolean implies(Permission permission);
  100.  
  101.     /**
  102.      * Returns an enumeration of all the Permission objects in the collection.
  103.      *
  104.      * @return an enumeration of all the Permissions.
  105.      */
  106.     public abstract Enumeration elements();
  107.  
  108.     /**
  109.      * Returns a string describing this PermissionCollection object, 
  110.      * providing information about all the permissions it contains.
  111.      * The format is:
  112.      * <pre>
  113.      * super.toString() (
  114.      *   // enumerate all the Permission
  115.      *   // objects and call toString() on them,
  116.      *   // one per line..
  117.      * )</pre>
  118.      * 
  119.      * <code>super.toString</code> is a call to the <code>toString</code> 
  120.      * method of this
  121.      * object's superclass, which is Object. The result is
  122.      * this PermissionCollection's type name followed by this object's
  123.      * hashcode, thus enabling clients to differentiate different
  124.      * PermissionCollections object, even if they contain the same permissions.
  125.      * 
  126.      * @return information about this PermissionCollection object, 
  127.      *         as described above.
  128.      *
  129.      */
  130.     public String toString() {
  131.     Enumeration enum = elements();
  132.     StringBuffer sb = new StringBuffer();
  133.     sb.append(super.toString()+" (\n");
  134.     while (enum.hasMoreElements()) {
  135.         try {
  136.         sb.append(" ");
  137.         sb.append(enum.nextElement().toString());
  138.         sb.append("\n");
  139.         } catch (NoSuchElementException e){
  140.         // ignore
  141.         }
  142.     }
  143.     sb.append(")\n");
  144.     return sb.toString();
  145.     }
  146. }
  147.