1 /* Permissions.java -- A collection of permission collections
2 Copyright (C) 1998, 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.security;
40 import java.io.Serializable;
41 import java.util.Hashtable;
42 import java.util.Enumeration;
43 import java.util.NoSuchElementException;
46 * This class is a heterogeneous collection of permissions. It is
47 * organized as a collection of <code>PermissionCollection</code>'s stored
48 * in a hashtable. Each individual <code>PermissionCollection</code>
49 * contains permissions of a single type. If a specific type of
50 * <code>Permission</code> does not provide a collection type to use
51 * via its <code>newPermissionCollection</code> method, then a default
52 * collection type which stores its permissions in a hash table will be
57 * @author Aaron M. Renn (arenn@urbanophile.com)
59 public final class Permissions
60 extends PermissionCollection
61 implements Serializable
64 * Holds instances of <code>AllPermission</code>.
66 private PermissionCollection allPermission;
69 * This is the <code>Hashtable</code> that contains our collections.
71 Hashtable perms = new Hashtable();
74 * This method initializes a new instance of <code>Permissions</code>.
81 * This method adds a new <code>Permission</code> to this collection. It
82 * will be stored in a <code>PermissionCollection</code> of the appropriate
83 * type, as determined by calling <code>newPermissionCollection</code> on
84 * the specified permission (if an appropriate collection does not already
85 * exist). If this object does not specify a particular type of collection,
86 * a default collection which stores in permissions in a hash table will
89 * @param perm The <code>Permission</code> object to be added to this collection.
91 * @exception SecurityException If this collection is marked as read only.
92 * @exception IllegalArgumentException If the specified <code>Permission</code> cannot be added to this collection
94 public void add(Permission perm)
95 throws SecurityException, IllegalArgumentException
98 throw new SecurityException("PermissionCollection is read only");
100 if (perm instanceof AllPermission)
102 if (allPermission == null)
105 DefaultPermissionCollection("java.security.AllPermission");
107 perms.put("java.security.AllPermission", allPermission);
112 Object obj = perms.get(perm.getClass().getName());
115 if (!(obj instanceof PermissionCollection))
116 throw new RuntimeException("Internal error in Permissions");
118 ((PermissionCollection) obj).add(perm);
122 PermissionCollection pc = perm.newPermissionCollection();
124 pc = new DefaultPermissionCollection(perm.getClass().getName());
128 perms.put(perm.getClass().getName(), pc);
134 * This method tests whether or not the specified <code>Permission</code>
135 * is implied by this <code>PermissionCollection</code>.
137 * @param perm The <code>Permission</code> to test.
139 * @return <code>true</code> if the specified permission is implied by this <code>PermissionCollection</code>, or <code>false</code> otherwise.
141 public boolean implies(Permission perm)
143 if (allPermission != null)
146 Object obj = perms.get(perm.getClass().getName());
150 if (!(obj instanceof PermissionCollection))
153 return (((PermissionCollection) obj).implies(perm));
157 * This method returns an <code>Enumeration</code> which contains a
158 * list of all <code>Permission</code> objects contained in this
161 * @return An <code>Enumeration</code> of this collection's elements.
163 public Enumeration elements()
165 return new Enumeration()
167 Enumeration main_enum = perms.elements();
168 Enumeration sub_enum;
170 public boolean hasMoreElements()
172 if (sub_enum == null)
173 if (main_enum == null)
177 if (!main_enum.hasMoreElements())
183 PermissionCollection pc =
184 (PermissionCollection) main_enum.nextElement();
185 sub_enum = pc.elements();
187 catch (NoSuchElementException e)
193 else if (!sub_enum.hasMoreElements())
196 return (hasMoreElements());
202 public Object nextElement() throws NoSuchElementException
204 if (!hasMoreElements())
205 throw new NoSuchElementException();
207 return (sub_enum.nextElement());
212 static class DefaultPermissionCollection extends PermissionCollection
213 implements Serializable
216 // Type of Permission we can store
217 private Class permcls;
219 // Hashtable where we store permissions.
220 private Hashtable perms = new Hashtable();
222 DefaultPermissionCollection(String permtype) throws IllegalArgumentException
226 permcls = Class.forName(permtype);
228 catch(ClassNotFoundException e)
230 throw new IllegalArgumentException(e.getMessage());
234 public void add(Permission perm)
235 throws SecurityException, IllegalArgumentException
238 throw new SecurityException("PermissionCollection is read only");
240 if (!permcls.isInstance(perm))
241 throw new IllegalArgumentException("Wrong permission type: " +
242 perm.getClass().getName());
244 if (perms.get(perm.getName()) != null)
245 throw new IllegalArgumentException("Duplicate permission: " +
248 perms.put(perm.getName(), perm);
251 public boolean implies(Permission perm)
253 Object obj = perms.get(perm.getName());
257 if (!(obj instanceof Permission))
260 Permission p = (Permission)obj;
262 return(p.implies(perm));
265 public Enumeration elements()
267 return(perms.elements());