source: trunk/gcc/libjava/java/security/Permissions.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.3 KB
Line 
1/* Permissions.java -- a collection of permission collections
2 Copyright (C) 1998, 2001, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package java.security;
39
40import java.io.Serializable;
41import java.util.Hashtable;
42import java.util.Enumeration;
43import java.util.NoSuchElementException;
44
45/**
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
53 * used.
54 *
55 * @author Aaron M. Renn <arenn@urbanophile.com>
56 * @author Eric Blake <ebb9@email.byu.edu>
57 * @since 1.1
58 */
59public final class Permissions extends PermissionCollection
60 implements Serializable
61{
62 /**
63 * Compatible with JDK 1.1+.
64 */
65 private static final long serialVersionUID = 4858622370623524688L;
66
67 /**
68 * Holds instances of <code>AllPermission</code>.
69 *
70 * @serial the permission collection for AllPermission
71 */
72 private PermissionCollection allPermission;
73
74 /**
75 * This is the <code>Hashtable</code> that contains our collections.
76 *
77 * @serial maps Class to PermissionCollection
78 */
79 private final Hashtable perms = new Hashtable();
80
81 /**
82 * This method initializes a new instance of <code>Permissions</code>.
83 */
84 public Permissions()
85 {
86 }
87
88 /**
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
95 * be used.
96 *
97 * @param perm the <code>Permission</code> to add
98 * @throws SecurityException if this collection is marked as read only
99 */
100 public void add(Permission perm)
101 {
102 if (isReadOnly())
103 throw new SecurityException("PermissionCollection is read only");
104 if (perm instanceof AllPermission)
105 {
106 if (allPermission == null)
107 {
108 allPermission = perm.newPermissionCollection();
109 allPermission.add(perm);
110 perms.put(perm.getClass(), allPermission);
111 }
112 }
113 else
114 {
115 PermissionCollection pc
116 = (PermissionCollection) perms.get(perm.getClass());
117 if (pc == null)
118 {
119 pc = perm.newPermissionCollection();
120 if (pc == null)
121 pc = new PermissionsHash();
122 perms.put(perm.getClass(), pc);
123 }
124 pc.add(perm);
125 }
126 }
127
128 /**
129 * This method tests whether or not the specified <code>Permission</code>
130 * is implied by this <code>PermissionCollection</code>.
131 *
132 * @param perm the <code>Permission</code> to test
133 * @return true if the specified permission is implied by this
134 */
135 public boolean implies(Permission perm)
136 {
137 if (allPermission != null)
138 return true;
139 PermissionCollection pc
140 = (PermissionCollection) perms.get(perm.getClass());
141 return pc == null ? false : pc.implies(perm);
142 }
143
144 /**
145 * This method returns an <code>Enumeration</code> which contains a
146 * list of all <code>Permission</code> objects contained in this
147 * collection.
148 *
149 * @return an <code>Enumeration</code> of this collection's elements
150 */
151 public Enumeration elements()
152 {
153 return new Enumeration()
154 {
155 Enumeration main_enum = perms.elements();
156 Enumeration sub_enum;
157
158 public boolean hasMoreElements()
159 {
160 if (sub_enum == null)
161 {
162 if (main_enum == null)
163 return false;
164 if (! main_enum.hasMoreElements())
165 {
166 main_enum = null;
167 return false;
168 }
169 PermissionCollection pc =
170 (PermissionCollection) main_enum.nextElement();
171 sub_enum = pc.elements();
172 }
173 if (! sub_enum.hasMoreElements())
174 {
175 sub_enum = null;
176 return hasMoreElements();
177 }
178 return true;
179 }
180
181 public Object nextElement()
182 {
183 if (! hasMoreElements())
184 throw new NoSuchElementException();
185 return sub_enum.nextElement();
186 }
187 };
188 }
189} // class Permissions
190
191/**
192 * Implements the permission collection for all permissions without one of
193 * their own, and obeys serialization of JDK.
194 *
195 * @author Eric Blake <ebb9@email.byu.edu>
196 */
197class PermissionsHash extends PermissionCollection
198{
199 /**
200 * Compatible with JDK 1.1+.
201 */
202 private static final long serialVersionUID = -8491988220802933440L;
203
204 /**
205 * Hashtable where we store permissions.
206 *
207 * @serial the stored permissions, both as key and value
208 */
209 private final Hashtable perms = new Hashtable();
210
211 /**
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
214 * done that check.
215 *
216 * @param perm the permission to add
217 */
218 public void add(Permission perm)
219 {
220 perms.put(perm, perm);
221 }
222
223 /**
224 * Returns true if perm is in the collection.
225 *
226 * @param perm the permission to check
227 * @return true if it is implied
228 */
229 public boolean implies(Permission perm)
230 {
231 return perms.get(perm) != null;
232 }
233
234 /**
235 * Return the elements.
236 *
237 * @return the elements
238 */
239 public Enumeration elements()
240 {
241 return perms.elements();
242 }
243} // class Permissions
Note: See TracBrowser for help on using the repository browser.