1 /* Permissions.java -- a collection of permission collections
2 Copyright (C) 1998, 2001, 2002 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
55 * @author Aaron M. Renn <arenn@urbanophile.com>
56 * @author Eric Blake <ebb9@email.byu.edu>
59 public final class Permissions
extends PermissionCollection
60 implements Serializable
63 * Compatible with JDK 1.1+.
65 private static final long serialVersionUID
= 4858622370623524688L;
68 * Holds instances of <code>AllPermission</code>.
70 * @serial the permission collection for AllPermission
72 private PermissionCollection allPermission
;
75 * This is the <code>Hashtable</code> that contains our collections.
77 * @serial maps Class to PermissionCollection
79 private final Hashtable perms
= new Hashtable();
82 * This method initializes a new instance of <code>Permissions</code>.
89 * This method adds a new <code>Permission</code> to this collection. It
90 * will be stored in a <code>PermissionCollection</code> of the appropriate
91 * type, as determined by calling <code>newPermissionCollection</code> on
92 * the specified permission (if an appropriate collection does not already
93 * exist). If this object does not specify a particular type of collection,
94 * a default collection, which stores in permissions in a hash table, will
97 * @param perm the <code>Permission</code> to add
98 * @throws SecurityException if this collection is marked as read only
100 public void add(Permission perm
)
103 throw new SecurityException("PermissionCollection is read only");
104 if (perm
instanceof AllPermission
)
106 if (allPermission
== null)
108 allPermission
= perm
.newPermissionCollection();
109 allPermission
.add(perm
);
110 perms
.put(perm
.getClass(), allPermission
);
115 PermissionCollection pc
116 = (PermissionCollection
) perms
.get(perm
.getClass());
119 pc
= perm
.newPermissionCollection();
121 pc
= new PermissionsHash();
122 perms
.put(perm
.getClass(), pc
);
129 * This method tests whether or not the specified <code>Permission</code>
130 * is implied by this <code>PermissionCollection</code>.
132 * @param perm the <code>Permission</code> to test
133 * @return true if the specified permission is implied by this
135 public boolean implies(Permission perm
)
137 if (allPermission
!= null)
139 PermissionCollection pc
140 = (PermissionCollection
) perms
.get(perm
.getClass());
141 return pc
== null ?
false : pc
.implies(perm
);
145 * This method returns an <code>Enumeration</code> which contains a
146 * list of all <code>Permission</code> objects contained in this
149 * @return an <code>Enumeration</code> of this collection's elements
151 public Enumeration
elements()
153 return new Enumeration()
155 Enumeration main_enum
= perms
.elements();
156 Enumeration sub_enum
;
158 public boolean hasMoreElements()
160 if (sub_enum
== null)
162 if (main_enum
== null)
164 if (! main_enum
.hasMoreElements())
169 PermissionCollection pc
=
170 (PermissionCollection
) main_enum
.nextElement();
171 sub_enum
= pc
.elements();
173 if (! sub_enum
.hasMoreElements())
176 return hasMoreElements();
181 public Object
nextElement()
183 if (! hasMoreElements())
184 throw new NoSuchElementException();
185 return sub_enum
.nextElement();
189 } // class Permissions
192 * Implements the permission collection for all permissions without one of
193 * their own, and obeys serialization of JDK.
195 * @author Eric Blake <ebb9@email.byu.edu>
197 class PermissionsHash
extends PermissionCollection
200 * Compatible with JDK 1.1+.
202 private static final long serialVersionUID
= -8491988220802933440L;
205 * Hashtable where we store permissions.
207 * @serial the stored permissions, both as key and value
209 private final Hashtable perms
= new Hashtable();
212 * Add a permission. We don't need to check for read-only, as this
213 * collection is never exposed outside of Permissions, which has already
216 * @param perm the permission to add
218 public void add(Permission perm
)
220 perms
.put(perm
, perm
);
224 * Returns true if perm is in the collection.
226 * @param perm the permission to check
227 * @return true if it is implied
229 public boolean implies(Permission perm
)
231 return perms
.get(perm
) != null;
235 * Return the elements.
237 * @return the elements
239 public Enumeration
elements()
241 return perms
.elements();
243 } // class Permissions