/[classpath]/classpath/java/security/BasicPermission.java
ViewVC logotype

Diff of /classpath/java/security/BasicPermission.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.12 by mkoch, Thu Oct 21 20:22:12 2004 UTC revision 1.13 by robilad, Thu Feb 3 19:20:05 2005 UTC
# Line 198  public abstract class BasicPermission ex Line 198  public abstract class BasicPermission ex
198    {    {
199      return new BasicPermissionCollection(getClass());      return new BasicPermissionCollection(getClass());
200    }    }
 } // class BasicPermission  
   
 /**  
  * Implements AllPermission.newPermissionCollection, and obeys serialization  
  * of JDK.  
  *  
  * @author Eric Blake <ebb9@email.byu.edu>  
  */  
 final class BasicPermissionCollection extends PermissionCollection  
 {  
   /**  
    * Compatible with JDK 1.1+.  
    */  
   private static final long serialVersionUID = 739301742472979399L;  
   
   /**  
    * The permissions in the collection.  
    *  
    * @serial a hash mapping name to permissions, all of type permClass  
    */  
   private final Hashtable permissions = new Hashtable();  
   
   /**  
    * If "*" is in the collection.  
    *  
    * @serial true if a permission named "*" is in the collection  
    */  
   private boolean all_allowed;  
   
   /**  
    * The runtime class which all entries in the table must belong to.  
    *  
    * @serial the limiting subclass of this collection  
    */  
   private final Class permClass;  
   
   /**  
    * Construct a collection over the given runtime class.  
    *  
    * @param c the class  
    */  
   BasicPermissionCollection(Class c)  
   {  
     permClass = c;  
   }  
   
   /**  
    * Add a Permission. It must be of the same type as the permission which  
    * created this collection.  
    *  
    * @param perm the permission to add  
    * @throws IllegalArgumentException if perm is not the correct type  
    * @throws SecurityException if the collection is read-only  
    */  
   public void add(Permission perm)  
   {  
     if (isReadOnly())  
       throw new SecurityException("readonly");  
     if (! permClass.isInstance(perm))  
       throw new IllegalArgumentException("Expecting instance of " + permClass);  
     BasicPermission bp = (BasicPermission) perm;  
     String name = bp.getName();  
     if (name.equals("*"))  
       all_allowed = true;  
     permissions.put(name, bp);  
   }  
201    
202    /**    /**
203     * Returns true if this collection implies the given permission.     * Implements AllPermission.newPermissionCollection, and obeys serialization
204       * of JDK.
205     *     *
206     * @param permission the permission to check     * @author Eric Blake <ebb9@email.byu.edu>
    * @return true if it is implied by this  
207     */     */
208    public boolean implies(Permission permission)    private static final class BasicPermissionCollection extends PermissionCollection
209    {    {
210      if (! permClass.isInstance(permission))      /**
211        return false;       * Compatible with JDK 1.1+.
212      if (all_allowed)       */
213        return true;      private static final long serialVersionUID = 739301742472979399L;
214      BasicPermission toImply = (BasicPermission) permission;  
215      String name = toImply.getName();      /**
216      if (name.equals("*"))       * The permissions in the collection.
217        return false;       *
218      int prefixLength = name.length();       * @serial a hash mapping name to permissions, all of type permClass
219      if (name.endsWith("*"))       */
220        prefixLength -= 2;      private final Hashtable permissions = new Hashtable();
221    
222      while (true)      /**
223        {       * If "*" is in the collection.
224          if (permissions.get(name) != null)       *
225            return true;       * @serial true if a permission named "*" is in the collection
226          prefixLength = name.lastIndexOf('.', prefixLength);       */
227          if (prefixLength < 0)      private boolean all_allowed;
228            return false;  
229          name = name.substring(0, prefixLength + 1) + '*';      /**
230        }       * The runtime class which all entries in the table must belong to.
231    }       *
232         * @serial the limiting subclass of this collection
233    /**       */
234     * Enumerate over the collection.      private final Class permClass;
235     *  
236     * @return an enumeration of the collection contents      /**
237     */       * Construct a collection over the given runtime class.
238    public Enumeration elements()       *
239    {       * @param c the class
240      return permissions.elements();       */
241    }      BasicPermissionCollection(Class c)
242  } // class BasicPermissionCollection      {
243          permClass = c;
244        }
245    
246        /**
247         * Add a Permission. It must be of the same type as the permission which
248         * created this collection.
249         *
250         * @param perm the permission to add
251         * @throws IllegalArgumentException if perm is not the correct type
252         * @throws SecurityException if the collection is read-only
253         */
254        public void add(Permission perm)
255        {
256          if (isReadOnly())
257            throw new SecurityException("readonly");
258          if (! permClass.isInstance(perm))
259            throw new IllegalArgumentException("Expecting instance of " + permClass);
260          BasicPermission bp = (BasicPermission) perm;
261          String name = bp.getName();
262          if (name.equals("*"))
263            all_allowed = true;
264          permissions.put(name, bp);
265        }
266    
267        /**
268         * Returns true if this collection implies the given permission.
269         *
270         * @param permission the permission to check
271         * @return true if it is implied by this
272         */
273        public boolean implies(Permission permission)
274        {
275          if (! permClass.isInstance(permission))
276            return false;
277          if (all_allowed)
278            return true;
279          BasicPermission toImply = (BasicPermission) permission;
280          String name = toImply.getName();
281          if (name.equals("*"))
282            return false;
283          int prefixLength = name.length();
284          if (name.endsWith("*"))
285            prefixLength -= 2;
286    
287          while (true)
288            {
289              if (permissions.get(name) != null)
290                return true;
291              prefixLength = name.lastIndexOf('.', prefixLength);
292              if (prefixLength < 0)
293                return false;
294              name = name.substring(0, prefixLength + 1) + '*';
295            }
296        }
297    
298        /**
299         * Enumerate over the collection.
300         *
301         * @return an enumeration of the collection contents
302         */
303        public Enumeration elements()
304        {
305          return permissions.elements();
306        }
307      } // class BasicPermissionCollection
308    } // class BasicPermission

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.13

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26