source: trunk/gcc/libjava/java/security/BasicPermission.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: 9.7 KB
Line 
1/* BasicPermission.java -- implements a simple named permission
2 Copyright (C) 1998, 1999, 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;
43
44/**
45 * This class implements a simple model for named permissions without an
46 * associated action list. That is, either the named permission is granted
47 * or it is not.
48 *
49 * <p>It also supports trailing wildcards to allow the easy granting of
50 * permissions in a hierarchical fashion. (For example, the name "org.gnu.*"
51 * might grant all permissions under the "org.gnu" permissions hierarchy).
52 * The only valid wildcard character is a '*' which matches anything. It
53 * must be the rightmost element in the permission name and must follow a
54 * '.' or else the Permission name must consist of only a '*'. Any other
55 * occurrence of a '*' is not valid.
56 *
57 * <p>This class ignores the action list. Subclasses can choose to implement
58 * actions on top of this class if desired.
59 *
60 * @author Aaron M. Renn <arenn@urbanophile.com>
61 * @author Eric Blake <ebb9@email.byu.edu>
62 * @see Permission
63 * @see Permissions
64 * @see PermissionCollection
65 * @see RuntimePermission
66 * @see SecurityPermission
67 * @see PropertyPermission
68 * @see AWTPermission
69 * @see NetPermission
70 * @see SecurityManager
71 * @since 1.1
72 * @status updated to 1.4
73 */
74public abstract class BasicPermission extends java.security.Permission
75 implements Serializable
76 // FIXME extends with fully qualified classname is workaround for gcj 3.0.4.
77{
78 /**
79 * Compatible with JDK 1.1+.
80 */
81 private static final long serialVersionUID = 6279438298436773498L;
82
83 /**
84 * Create a new instance with the specified permission name. If the name
85 * is empty, or contains an illegal wildcard character, an exception is
86 * thrown.
87 *
88 * @param name the name of this permission
89 * @throws NullPointerException if name is null
90 * @throws IllegalArgumentException if name is invalid
91 */
92 public BasicPermission(String name)
93 {
94 super(name);
95 if (name.indexOf("*") != -1)
96 {
97 if ((! name.endsWith(".*") && ! name.equals("*"))
98 || name.indexOf("*") != name.lastIndexOf("*"))
99 throw new IllegalArgumentException("Bad wildcard: " + name);
100 }
101 if ("".equals(name))
102 throw new IllegalArgumentException("Empty name");
103 }
104
105 /**
106 * Create a new instance with the specified permission name. If the name
107 * is empty, or contains an illegal wildcard character, an exception is
108 * thrown. The actions parameter is ignored.
109 *
110 * @param name the name of this permission
111 * @param actions ignored
112 * @throws NullPointerException if name is null
113 * @throws IllegalArgumentException if name is invalid
114 */
115 public BasicPermission(String name, String actions)
116 {
117 this(name);
118 }
119
120 /**
121 * This method tests to see if the specified permission is implied by this
122 * permission. This will be true if the following conditions are met:<ul>
123 * <li>The specified object is an instance of the same class as this
124 * object.</li>
125 * <li>The name of the specified permission is implied by this permission's
126 * name based on wildcard matching. For example, "a.*" implies "a.b".</li>
127 * </ul>
128 *
129 * @param perm the <code>Permission</code> object to test against
130 * @return true if the specified permission is implied
131 */
132 public boolean implies(Permission perm)
133 {
134 if (! getClass().isInstance(perm))
135 return false;
136
137 String otherName = perm.getName();
138 String name = getName();
139
140 if (name.equals(otherName))
141 return true;
142
143 int last = name.length() - 1;
144 return name.charAt(last) == '*'
145 && otherName.startsWith(name.substring(0, last));
146 }
147
148 /**
149 * This method tests to see if this object is equal to the specified
150 * <code>Object</code>. This will be true if and only if the specified
151 * object meets the following conditions:<ul>
152 * <li>It is an instance of the same class as this.</li>
153 * <li>It has the same name as this permission.</li>
154 * </ul>
155 *
156 * @param obj the <code>Object</code> to test for equality
157 * @return true if obj is semantically equal to this
158 */
159 public boolean equals(Object obj)
160 {
161 return getClass().isInstance(obj)
162 && getName().equals(((BasicPermission) obj).getName());
163 }
164
165 /**
166 * This method returns a hash code for this permission object. The hash
167 * code returned is the value returned by calling the <code>hashCode</code>
168 * method on the <code>String</code> that is the name of this permission.
169 *
170 * @return a hash value for this object
171 */
172 public int hashCode()
173 {
174 return getName().hashCode();
175 }
176
177 /**
178 * This method returns a list of the actions associated with this
179 * permission. This method always returns the empty string ("") since
180 * this class ignores actions.
181 *
182 * @return the action list
183 */
184 public String getActions()
185 {
186 return "";
187 }
188
189 /**
190 * This method returns an instance of <code>PermissionCollection</code>
191 * suitable for storing <code>BasicPermission</code> objects. The
192 * collection returned can only store objects of the same type as this.
193 * Subclasses which use actions must override this method; but a class with
194 * no actions will work fine with this.
195 *
196 * @return a new empty <code>PermissionCollection</code> object
197 */
198 public PermissionCollection newPermissionCollection()
199 {
200 return new BasicPermissionCollection(getClass());
201 }
202} // class BasicPermission
203
204/**
205 * Implements AllPermission.newPermissionCollection, and obeys serialization
206 * of JDK.
207 *
208 * @author Eric Blake <ebb9@email.byu.edu>
209 */
210final class BasicPermissionCollection extends PermissionCollection
211{
212 /**
213 * Compatible with JDK 1.1+.
214 */
215 private static final long serialVersionUID = 739301742472979399L;
216
217 /**
218 * The permissions in the collection.
219 *
220 * @serial a hash mapping name to permissions, all of type permClass
221 */
222 private final Hashtable permissions = new Hashtable();
223
224 /**
225 * If "*" is in the collection.
226 *
227 * @serial true if a permission named "*" is in the collection
228 */
229 private boolean all_allowed;
230
231 /**
232 * The runtime class which all entries in the table must belong to.
233 *
234 * @serial the limiting subclass of this collection
235 */
236 private final Class permClass;
237
238 /**
239 * Construct a collection over the given runtime class.
240 *
241 * @param c the class
242 */
243 BasicPermissionCollection(Class c)
244 {
245 permClass = c;
246 }
247
248 /**
249 * Add a Permission. It must be of the same type as the permission which
250 * created this collection.
251 *
252 * @param perm the permission to add
253 * @throws IllegalArgumentException if perm is not the correct type
254 * @throws SecurityException if the collection is read-only
255 */
256 public void add(Permission perm)
257 {
258 if (isReadOnly())
259 throw new SecurityException("readonly");
260 if (! permClass.isInstance(perm))
261 throw new IllegalArgumentException("Expecting instance of " + permClass);
262 BasicPermission bp = (BasicPermission) perm;
263 String name = bp.getName();
264 if (name.equals("*"))
265 all_allowed = true;
266 permissions.put(name, bp);
267 }
268
269 /**
270 * Returns true if this collection implies the given permission.
271 *
272 * @param permission the permission to check
273 * @return true if it is implied by this
274 */
275 public boolean implies(Permission permission)
276 {
277 if (! permClass.isInstance(permission))
278 return false;
279 if (all_allowed)
280 return true;
281 BasicPermission toImply = (BasicPermission) permission;
282 String name = toImply.getName();
283 if (name.equals("*"))
284 return false;
285 int prefixLength = name.length();
286 if (name.endsWith("*"))
287 prefixLength -= 2;
288
289 while (true)
290 {
291 if (permissions.get(name) != null)
292 return true;
293 prefixLength = name.lastIndexOf('.', prefixLength);
294 if (prefixLength < 0)
295 return false;
296 name = name.substring(0, prefixLength + 1) + '*';
297 }
298 }
299
300 /**
301 * Enumerate over the collection.
302 *
303 * @return an enumeration of the collection contents
304 */
305 public Enumeration elements()
306 {
307 return permissions.elements();
308 }
309} // class BasicPermissionCollection
Note: See TracBrowser for help on using the repository browser.