source: trunk/src/gcc/libjava/java/security/BasicPermission.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: 8.4 KB
Line 
1/* BasicPermission.java -- Implements a simple named permission.
2 Copyright (C) 1998, 1999 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 * <p>
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.
56 * <p>
57 * This class ignores the action list. Subclasses can choose to implement
58 * actions on top of this class if desired.
59 *
60 * @version 0.1
61 *
62 * @author Aaron M. Renn ([email protected])
63 */
64public abstract class BasicPermission extends java.security.Permission
65 implements Serializable
66 // FIXME extends with fully qualified classname as workaround for gcj 3.0.4
67{
68 /**
69 * This method initializes a new instance of <code>BasicPermission</code>
70 * with the specified name. If the name contains an illegal wildcard
71 * character, an exception is thrown.
72 *
73 * @param name The name of this permission.
74 *
75 * @exception IllegalArgumentException If the name contains an invalid wildcard character
76 * @exception NullPointerException If the name is null
77 */
78 public BasicPermission(String name)
79 throws IllegalArgumentException, NullPointerException
80 {
81 super(name);
82
83 if (name.indexOf("*") != -1)
84 {
85 if (!name.endsWith(".*") && !name.equals("*"))
86 throw new IllegalArgumentException("Bad wildcard: " + name);
87
88 if (name.indexOf("*") != name.lastIndexOf("*"))
89 throw new IllegalArgumentException("Bad wildcard: " + name);
90 }
91 }
92
93 /**
94 * This method initializes a new instance of <code>BasicPermission</code>
95 * with the specified name. If the name contains an illegal wildcard
96 * character, an exception is thrown. The action list passed to this
97 * form of the constructor is ignored.
98 *
99 * @param name The name of this permission.
100 * @param actions The list of actions for this permission - ignored in this class.
101 *
102 * @exception IllegalArgumentException If the name contains an invalid wildcard character
103 * @exception NullPointerException If the name is null
104 */
105 public BasicPermission(String name, String actions)
106 throws IllegalArgumentException, NullPointerException
107 {
108 // ignore actions
109 this(name);
110 }
111
112 /**
113 * This method tests to see if the specified permission is implied by
114 * this permission. This will be true if the following conditions are met:
115 * <p>
116 * <ul>
117 * <li>The specified object is an instance of <code>BasicPermission</code>,
118 * or a subclass.
119 * <li>The name of the specified permission is identical to this permission's
120 * name or the name of the specified permission satisfies a wildcard match
121 * on this permission.
122 * </ul>
123 *
124 * @param perm The <code>Permission</code> object to test against.
125 *
126 * @return <code>true</code> if the specified permission is implied by this one or <code>false</code> otherwise.
127 */
128 public boolean implies(Permission perm)
129 {
130 if (!(perm instanceof BasicPermission))
131 return false;
132
133 String otherName = perm.getName();
134 String name = getName();
135
136 if (name.equals(otherName))
137 return true;
138
139 int last = name.length() - 1;
140 if (name.charAt(last) == '*'
141 && otherName.startsWith(name.substring(0, last)))
142 return true;
143
144 return false;
145 }
146
147 /**
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:
151 * <p>
152 * <ul>
153 * <li>It is an instance of <code>BasicPermission</code>, or a subclass.
154 * <li>It has the same name as this permission.
155 * </ul>
156 *
157 * @param obj The <code>Object</code> to test for equality against this object
158 *
159 * @return <code>true</code> if the specified <code>Object</code> is equal to this object or <code>false</code> otherwise.
160 */
161 public boolean equals(Object obj)
162 {
163 if (!(obj instanceof BasicPermission))
164 return (false);
165
166 if (!getName().equals(((BasicPermission) obj).getName()))
167 return (false);
168
169 return (true);
170 }
171
172 /**
173 * This method returns a hash code for this permission object. The hash
174 * code returned is the value returned by calling the <code>hashCode</code>
175 * method on the <code>String</code> that is the name of this permission.
176 *
177 * @return A hash value for this object
178 */
179 public int hashCode()
180 {
181 return (getName().hashCode());
182 }
183
184 /**
185 * This method returns a list of the actions associated with this
186 * permission. This method always returns the empty string ("") since
187 * this class ignores actions.
188 *
189 * @return The action list.
190 */
191 public String getActions()
192 {
193 return ("");
194 }
195
196 /**
197 * This method returns an instance of <code>PermissionCollection</code>
198 * suitable for storing <code>BasicPermission</code> objects. This returns
199 * be a sub class of <code>PermissionCollection</code>
200 * that allows for an efficient and consistent implementation of
201 * the <code>implies</code> method. The collection doesn't handle subclasses
202 * of BasicPermission correctly; they must override this method.
203 *
204 * @return A new empty <code>PermissionCollection</code> object.
205 */
206 public PermissionCollection newPermissionCollection()
207 {
208 return new PermissionCollection()
209 {
210 Hashtable permissions = new Hashtable();
211 boolean allAllowed = false;
212
213 public void add(Permission permission)
214 {
215 if (isReadOnly())
216 throw new IllegalStateException("readonly");
217
218 BasicPermission bp = (BasicPermission) permission;
219 String name = bp.getName();
220 if (name.equals("*"))
221 allAllowed = true;
222 permissions.put(name, bp);
223 }
224
225 public boolean implies(Permission permission)
226 {
227 if (!(permission instanceof BasicPermission))
228 return false;
229
230 if (allAllowed)
231 return true;
232
233 BasicPermission toImply = (BasicPermission) permission;
234 String name = toImply.getName();
235 if (name.equals("*"))
236 return false;
237
238 int prefixLength = name.length();
239 if (name.endsWith("*"))
240 prefixLength -= 2;
241
242 while (true)
243 {
244 if (permissions.get(name) != null)
245 return true;
246
247 prefixLength = name.lastIndexOf('.', prefixLength);
248 if (prefixLength < 0)
249 return false;
250 name = name.substring(0, prefixLength + 1) + '*';
251 }
252 }
253
254 public Enumeration elements()
255 {
256 return permissions.elements();
257 }
258 };
259 }
260}
Note: See TracBrowser for help on using the repository browser.