2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / security / BasicPermission.java
blob0945f2d7faf549f065a06c7693ca2bd80e6e3cf5
1 /* BasicPermission.java -- implements a simple named permission
2 Copyright (C) 1998, 1999, 2002, 2003 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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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;
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.
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.
57 * <p>This class ignores the action list. Subclasses can choose to implement
58 * actions on top of this class if desired.
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
74 public abstract class BasicPermission extends Permission
75 implements Serializable
77 /**
78 * Compatible with JDK 1.1+.
80 private static final long serialVersionUID = 6279438298436773498L;
82 /**
83 * Create a new instance with the specified permission name. If the name
84 * is empty, or contains an illegal wildcard character, an exception is
85 * thrown.
87 * @param name the name of this permission
88 * @throws NullPointerException if name is null
89 * @throws IllegalArgumentException if name is invalid
91 public BasicPermission(String name)
93 super(name);
94 if (name.indexOf("*") != -1)
96 if ((! name.endsWith(".*") && ! name.equals("*"))
97 || name.indexOf("*") != name.lastIndexOf("*"))
98 throw new IllegalArgumentException("Bad wildcard: " + name);
100 if ("".equals(name))
101 throw new IllegalArgumentException("Empty name");
105 * Create a new instance with the specified permission name. If the name
106 * is empty, or contains an illegal wildcard character, an exception is
107 * thrown. The actions parameter is ignored.
109 * @param name the name of this permission
110 * @param actions ignored
111 * @throws NullPointerException if name is null
112 * @throws IllegalArgumentException if name is invalid
114 public BasicPermission(String name, String actions)
116 this(name);
120 * This method tests to see if the specified permission is implied by this
121 * permission. This will be true if the following conditions are met:<ul>
122 * <li>The specified object is an instance of the same class as this
123 * object.</li>
124 * <li>The name of the specified permission is implied by this permission's
125 * name based on wildcard matching. For example, "a.*" implies "a.b".</li>
126 * </ul>
128 * @param perm the <code>Permission</code> object to test against
129 * @return true if the specified permission is implied
131 public boolean implies(Permission perm)
133 if (! getClass().isInstance(perm))
134 return false;
136 String otherName = perm.getName();
137 String name = getName();
139 if (name.equals(otherName))
140 return true;
142 int last = name.length() - 1;
143 return name.charAt(last) == '*'
144 && otherName.startsWith(name.substring(0, last));
148 * This method tests to see if this object is equal to the specified
149 * <code>Object</code>. This will be true if and only if the specified
150 * object meets the following conditions:<ul>
151 * <li>It is an instance of the same class as this.</li>
152 * <li>It has the same name as this permission.</li>
153 * </ul>
155 * @param obj the <code>Object</code> to test for equality
156 * @return true if obj is semantically equal to this
158 public boolean equals(Object obj)
160 return getClass().isInstance(obj)
161 && getName().equals(((BasicPermission) obj).getName());
165 * This method returns a hash code for this permission object. The hash
166 * code returned is the value returned by calling the <code>hashCode</code>
167 * method on the <code>String</code> that is the name of this permission.
169 * @return a hash value for this object
171 public int hashCode()
173 return getName().hashCode();
177 * This method returns a list of the actions associated with this
178 * permission. This method always returns the empty string ("") since
179 * this class ignores actions.
181 * @return the action list
183 public String getActions()
185 return "";
189 * This method returns an instance of <code>PermissionCollection</code>
190 * suitable for storing <code>BasicPermission</code> objects. The
191 * collection returned can only store objects of the same type as this.
192 * Subclasses which use actions must override this method; but a class with
193 * no actions will work fine with this.
195 * @return a new empty <code>PermissionCollection</code> object
197 public PermissionCollection newPermissionCollection()
199 return new BasicPermissionCollection(getClass());
201 } // class BasicPermission
204 * Implements AllPermission.newPermissionCollection, and obeys serialization
205 * of JDK.
207 * @author Eric Blake <ebb9@email.byu.edu>
209 final class BasicPermissionCollection extends PermissionCollection
212 * Compatible with JDK 1.1+.
214 private static final long serialVersionUID = 739301742472979399L;
217 * The permissions in the collection.
219 * @serial a hash mapping name to permissions, all of type permClass
221 private final Hashtable permissions = new Hashtable();
224 * If "*" is in the collection.
226 * @serial true if a permission named "*" is in the collection
228 private boolean all_allowed;
231 * The runtime class which all entries in the table must belong to.
233 * @serial the limiting subclass of this collection
235 private final Class permClass;
238 * Construct a collection over the given runtime class.
240 * @param c the class
242 BasicPermissionCollection(Class c)
244 permClass = c;
248 * Add a Permission. It must be of the same type as the permission which
249 * created this collection.
251 * @param perm the permission to add
252 * @throws IllegalArgumentException if perm is not the correct type
253 * @throws SecurityException if the collection is read-only
255 public void add(Permission perm)
257 if (isReadOnly())
258 throw new SecurityException("readonly");
259 if (! permClass.isInstance(perm))
260 throw new IllegalArgumentException("Expecting instance of " + permClass);
261 BasicPermission bp = (BasicPermission) perm;
262 String name = bp.getName();
263 if (name.equals("*"))
264 all_allowed = true;
265 permissions.put(name, bp);
269 * Returns true if this collection implies the given permission.
271 * @param permission the permission to check
272 * @return true if it is implied by this
274 public boolean implies(Permission permission)
276 if (! permClass.isInstance(permission))
277 return false;
278 if (all_allowed)
279 return true;
280 BasicPermission toImply = (BasicPermission) permission;
281 String name = toImply.getName();
282 if (name.equals("*"))
283 return false;
284 int prefixLength = name.length();
285 if (name.endsWith("*"))
286 prefixLength -= 2;
288 while (true)
290 if (permissions.get(name) != null)
291 return true;
292 prefixLength = name.lastIndexOf('.', prefixLength);
293 if (prefixLength < 0)
294 return false;
295 name = name.substring(0, prefixLength + 1) + '*';
300 * Enumerate over the collection.
302 * @return an enumeration of the collection contents
304 public Enumeration elements()
306 return permissions.elements();
308 } // class BasicPermissionCollection