source: trunk/src/gcc/libjava/java/security/Permissions.java@ 192

Last change on this file since 192 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.8 KB
Line 
1/* Permissions.java -- A collection of permission collections
2 Copyright (C) 1998, 2001 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 * @version 0.0
56 *
57 * @author Aaron M. Renn ([email protected])
58 */
59public final class Permissions
60 extends PermissionCollection
61 implements Serializable
62{
63 /**
64 * Holds instances of <code>AllPermission</code>.
65 */
66 private PermissionCollection allPermission;
67
68 /**
69 * This is the <code>Hashtable</code> that contains our collections.
70 */
71 Hashtable perms = new Hashtable();
72
73 /**
74 * This method initializes a new instance of <code>Permissions</code>.
75 */
76 public Permissions()
77 {
78 }
79
80 /**
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
87 * be used.
88 *
89 * @param perm The <code>Permission</code> object to be added to this collection.
90 *
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
93 */
94 public void add(Permission perm)
95 throws SecurityException, IllegalArgumentException
96 {
97 if (isReadOnly())
98 throw new SecurityException("PermissionCollection is read only");
99
100 if (perm instanceof AllPermission)
101 {
102 if (allPermission == null)
103 {
104 allPermission = new
105 DefaultPermissionCollection("java.security.AllPermission");
106
107 perms.put("java.security.AllPermission", allPermission);
108 }
109 }
110 else
111 {
112 Object obj = perms.get(perm.getClass().getName());
113 if (obj != null)
114 {
115 if (!(obj instanceof PermissionCollection))
116 throw new RuntimeException("Internal error in Permissions");
117
118 ((PermissionCollection) obj).add(perm);
119 }
120 else
121 {
122 PermissionCollection pc = perm.newPermissionCollection();
123 if (pc == null)
124 pc = new DefaultPermissionCollection(perm.getClass().getName());
125
126 pc.add(perm);
127
128 perms.put(perm.getClass().getName(), pc);
129 }
130 }
131 }
132
133 /**
134 * This method tests whether or not the specified <code>Permission</code>
135 * is implied by this <code>PermissionCollection</code>.
136 *
137 * @param perm The <code>Permission</code> to test.
138 *
139 * @return <code>true</code> if the specified permission is implied by this <code>PermissionCollection</code>, or <code>false</code> otherwise.
140 */
141 public boolean implies(Permission perm)
142 {
143 if (allPermission != null)
144 return (true);
145
146 Object obj = perms.get(perm.getClass().getName());
147 if (obj == null)
148 return (false);
149
150 if (!(obj instanceof PermissionCollection))
151 return (false);
152
153 return (((PermissionCollection) obj).implies(perm));
154 }
155
156 /**
157 * This method returns an <code>Enumeration</code> which contains a
158 * list of all <code>Permission</code> objects contained in this
159 * collection.
160 *
161 * @return An <code>Enumeration</code> of this collection's elements.
162 */
163 public Enumeration elements()
164 {
165 return new Enumeration()
166 {
167 Enumeration main_enum = perms.elements();
168 Enumeration sub_enum;
169
170 public boolean hasMoreElements()
171 {
172 if (sub_enum == null)
173 if (main_enum == null)
174 return (false);
175 else
176 {
177 if (!main_enum.hasMoreElements())
178 return (false);
179 else
180 {
181 try
182 {
183 PermissionCollection pc =
184 (PermissionCollection) main_enum.nextElement();
185 sub_enum = pc.elements();
186 }
187 catch (NoSuchElementException e)
188 {
189 return (false);
190 }
191 }
192 }
193 else if (!sub_enum.hasMoreElements())
194 {
195 sub_enum = null;
196 return (hasMoreElements());
197 }
198
199 return (true);
200 }
201
202 public Object nextElement() throws NoSuchElementException
203 {
204 if (!hasMoreElements())
205 throw new NoSuchElementException();
206
207 return (sub_enum.nextElement());
208 }
209 };
210 }
211
212 static class DefaultPermissionCollection extends PermissionCollection
213 implements Serializable
214 {
215
216 // Type of Permission we can store
217 private Class permcls;
218
219 // Hashtable where we store permissions.
220 private Hashtable perms = new Hashtable();
221
222 DefaultPermissionCollection(String permtype) throws IllegalArgumentException
223 {
224 try
225 {
226 permcls = Class.forName(permtype);
227 }
228 catch(ClassNotFoundException e)
229 {
230 throw new IllegalArgumentException(e.getMessage());
231 }
232 }
233
234 public void add(Permission perm)
235 throws SecurityException, IllegalArgumentException
236 {
237 if (isReadOnly())
238 throw new SecurityException("PermissionCollection is read only");
239
240 if (!permcls.isInstance(perm))
241 throw new IllegalArgumentException("Wrong permission type: " +
242 perm.getClass().getName());
243
244 if (perms.get(perm.getName()) != null)
245 throw new IllegalArgumentException("Duplicate permission: " +
246 perm.getName());
247
248 perms.put(perm.getName(), perm);
249 }
250
251 public boolean implies(Permission perm)
252 {
253 Object obj = perms.get(perm.getName());
254 if (obj == null)
255 return(false);
256
257 if (!(obj instanceof Permission))
258 return(false);
259
260 Permission p = (Permission)obj;
261
262 return(p.implies(perm));
263 }
264
265 public Enumeration elements()
266 {
267 return(perms.elements());
268 }
269 }
270}
Note: See TracBrowser for help on using the repository browser.