1 /* BasicPermission.java -- Implements a simple named permission.
2 Copyright (C) 1998, 1999 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;
45 * This class implements a simple model for named permissions without an
46 * associated action list. That is, either the named permission is granted
49 * It also supports trailing wildcards to allow the
50 * easy granting of permissions in a hierarchical fashion. (For example,
51 * the name "org.gnu.*" might grant all permissions under the "org.gnu"
52 * permissions hierarchy). The only valid wildcard character is a '*'
53 * which matches anything. It must be the rightmost element in the
54 * permission name and must follow a '.' or else the Permission name must
55 * consist of only a '*'. Any other occurrence of a '*' is not valid.
57 * This class ignores the action list. Subclasses can choose to implement
58 * actions on top of this class if desired.
62 * @author Aaron M. Renn (arenn@urbanophile.com)
64 public abstract class BasicPermission extends Permission implements
68 * This method initializes a new instance of <code>BasicPermission</code>
69 * with the specified name. If the name contains an illegal wildcard
70 * character, an exception is thrown.
72 * @param name The name of this permission.
74 * @exception IllegalArgumentException If the name contains an invalid wildcard character
75 * @exception NullPointerException If the name is null
77 public BasicPermission(String name)
78 throws IllegalArgumentException, NullPointerException
82 if (name.indexOf("*") != -1)
84 if (!name.endsWith(".*") && !name.equals("*"))
85 throw new IllegalArgumentException("Bad wildcard: " + name);
87 if (name.indexOf("*") != name.lastIndexOf("*"))
88 throw new IllegalArgumentException("Bad wildcard: " + name);
93 * This method initializes a new instance of <code>BasicPermission</code>
94 * with the specified name. If the name contains an illegal wildcard
95 * character, an exception is thrown. The action list passed to this
96 * form of the constructor is ignored.
98 * @param name The name of this permission.
99 * @param actions The list of actions for this permission - ignored in this class.
101 * @exception IllegalArgumentException If the name contains an invalid wildcard character
102 * @exception NullPointerException If the name is null
104 public BasicPermission(String name, String actions)
105 throws IllegalArgumentException, NullPointerException
112 * This method tests to see if the specified permission is implied by
113 * this permission. This will be true if the following conditions are met:
116 * <li>The specified object is an instance of <code>BasicPermission</code>,
118 * <li>The name of the specified permission is identical to this permission's
119 * name or the name of the specified permission satisfies a wildcard match
120 * on this permission.
123 * @param perm The <code>Permission</code> object to test against.
125 * @return <code>true</code> if the specified permission is implied by this one or <code>false</code> otherwise.
127 public boolean implies(Permission perm)
129 if (!(perm instanceof BasicPermission))
132 String otherName = perm.getName();
133 String name = getName();
135 if (name.equals(otherName))
138 int last = name.length() - 1;
139 if (name.charAt(last) == '*'
140 && otherName.startsWith(name.substring(0, last)))
147 * This method tests to see if this object is equal to the specified
148 * <code>Object</code>. This will be true if and only if the specified
149 * object meets the following conditions:
152 * <li>It is an instance of <code>BasicPermission</code>, or a subclass.
153 * <li>It has the same name as this permission.
156 * @param obj The <code>Object</code> to test for equality against this object
158 * @return <code>true</code> if the specified <code>Object</code> is equal to this object or <code>false</code> otherwise.
160 public boolean equals(Object obj)
162 if (!(obj instanceof BasicPermission))
165 if (!getName().equals(((BasicPermission) obj).getName()))
172 * This method returns a hash code for this permission object. The hash
173 * code returned is the value returned by calling the <code>hashCode</code>
174 * method on the <code>String</code> that is the name of this permission.
176 * @return A hash value for this object
178 public int hashCode()
180 return (getName().hashCode());
184 * This method returns a list of the actions associated with this
185 * permission. This method always returns the empty string ("") since
186 * this class ignores actions.
188 * @return The action list.
190 public String getActions()
196 * This method returns an instance of <code>PermissionCollection</code>
197 * suitable for storing <code>BasicPermission</code> objects. This returns
198 * be a sub class of <code>PermissionCollection</code>
199 * that allows for an efficient and consistent implementation of
200 * the <code>implies</code> method. The collection doesn't handle subclasses
201 * of BasicPermission correctly; they must override this method.
203 * @return A new empty <code>PermissionCollection</code> object.
205 public PermissionCollection newPermissionCollection()
207 return new PermissionCollection()
209 Hashtable permissions = new Hashtable();
210 boolean allAllowed = false;
212 public void add(Permission permission)
215 throw new IllegalStateException("readonly");
217 BasicPermission bp = (BasicPermission) permission;
218 String name = bp.getName();
219 if (name.equals("*"))
221 permissions.put(name, bp);
224 public boolean implies(Permission permission)
226 if (!(permission instanceof BasicPermission))
232 BasicPermission toImply = (BasicPermission) permission;
233 String name = toImply.getName();
234 if (name.equals("*"))
237 int prefixLength = name.length();
238 if (name.endsWith("*"))
243 if (permissions.get(name) != null)
246 prefixLength = name.lastIndexOf('.', prefixLength);
247 if (prefixLength < 0)
249 name = name.substring(0, prefixLength + 1) + '*';
253 public Enumeration elements()
255 return permissions.elements();