Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/com/sun/rmi/rmid/ExecOptionPermission.java
+++ new/src/share/classes/com/sun/rmi/rmid/ExecOptionPermission.java
1 1 /*
2 2 * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 package com.sun.rmi.rmid;
27 27
28 28 import java.security.*;
29 29 import java.io.*;
30 30 import java.util.*;
31 31
32 32 /**
33 33 * The ExecOptionPermission class represents permission for rmid to use
34 34 * a specific command-line option when launching an activation group.
35 35 * <P>
36 36 *
37 37 * @author Ann Wollrath
38 38 *
39 39 * @serial exclude
40 40 */
41 41 public final class ExecOptionPermission extends Permission
42 42 {
43 43 /**
44 44 * does this permission have a wildcard at the end?
45 45 */
46 46 private transient boolean wildcard;
47 47
48 48 /**
49 49 * the name without the wildcard on the end
50 50 */
51 51 private transient String name;
52 52
53 53 /**
54 54 * UID for serialization
55 55 */
56 56 private static final long serialVersionUID = 5842294756823092756L;
57 57
58 58 public ExecOptionPermission(String name) {
59 59 super(name);
60 60 init(name);
61 61 }
62 62
63 63 public ExecOptionPermission(String name, String actions) {
64 64 this(name);
65 65 }
66 66
67 67 /**
68 68 * Checks if the specified permission is "implied" by
69 69 * this object.
70 70 * <P>
71 71 * More specifically, this method returns true if:<p>
72 72 * <ul>
73 73 * <li> <i>p</i>'s class is the same as this object's class, and<p>
74 74 * <li> <i>p</i>'s name equals or (in the case of wildcards)
75 75 * is implied by this object's
76 76 * name. For example, "a.b.*" implies "a.b.c", and
77 77 * "a.b=*" implies "a.b=c"
78 78 * </ul>
79 79 *
80 80 * @param p the permission to check against.
81 81 *
82 82 * @return true if the passed permission is equal to or
83 83 * implied by this permission, false otherwise.
84 84 */
85 85 public boolean implies(Permission p) {
86 86 if (!(p instanceof ExecOptionPermission))
87 87 return false;
88 88
89 89 ExecOptionPermission that = (ExecOptionPermission) p;
90 90
91 91 if (this.wildcard) {
92 92 if (that.wildcard) {
93 93 // one wildcard can imply another
94 94 return that.name.startsWith(name);
95 95 } else {
96 96 // make sure p.name is longer so a.b.* doesn't imply a.b
97 97 return (that.name.length() > this.name.length()) &&
98 98 that.name.startsWith(this.name);
99 99 }
100 100 } else {
101 101 if (that.wildcard) {
102 102 // a non-wildcard can't imply a wildcard
103 103 return false;
104 104 } else {
105 105 return this.name.equals(that.name);
106 106 }
107 107 }
108 108 }
109 109
110 110 /**
111 111 * Checks two ExecOptionPermission objects for equality.
112 112 * Checks that <i>obj</i>'s class is the same as this object's class
113 113 * and has the same name as this object.
114 114 * <P>
115 115 * @param obj the object we are testing for equality with this object.
116 116 * @return true if <i>obj</i> is an ExecOptionPermission, and has the same
117 117 * name as this ExecOptionPermission object, false otherwise.
118 118 */
119 119 public boolean equals(Object obj) {
120 120 if (obj == this)
121 121 return true;
122 122
123 123 if ((obj == null) || (obj.getClass() != getClass()))
124 124 return false;
125 125
126 126 ExecOptionPermission that = (ExecOptionPermission) obj;
127 127
128 128 return this.getName().equals(that.getName());
129 129 }
130 130
131 131
132 132 /**
133 133 * Returns the hash code value for this object.
134 134 * The hash code used is the hash code of the name, that is,
135 135 * <code>getName().hashCode()</code>, where <code>getName</code> is
136 136 * from the Permission superclass.
137 137 *
138 138 * @return a hash code value for this object.
139 139 */
140 140 public int hashCode() {
141 141 return this.getName().hashCode();
142 142 }
143 143
144 144 /**
145 145 * Returns the canonical string representation of the actions.
146 146 *
147 147 * @return the canonical string representation of the actions.
148 148 */
149 149 public String getActions() {
150 150 return "";
151 151 }
152 152
153 153 /**
154 154 * Returns a new PermissionCollection object for storing
155 155 * ExecOptionPermission objects.
156 156 * <p>
157 157 * A ExecOptionPermissionCollection stores a collection of
158 158 * ExecOptionPermission permissions.
159 159 *
160 160 * <p>ExecOptionPermission objects must be stored in a manner that allows
161 161 * them to be inserted in any order, but that also enables the
162 162 * PermissionCollection <code>implies</code> method
163 163 * to be implemented in an efficient (and consistent) manner.
164 164 *
165 165 * @return a new PermissionCollection object suitable for
166 166 * storing ExecOptionPermissions.
167 167 */
168 168 public PermissionCollection newPermissionCollection() {
169 169 return new ExecOptionPermissionCollection();
170 170 }
171 171
172 172 /**
173 173 * readObject is called to restore the state of the ExecOptionPermission
174 174 * from a stream.
175 175 */
176 176 private synchronized void readObject(java.io.ObjectInputStream s)
177 177 throws IOException, ClassNotFoundException
178 178 {
179 179 s.defaultReadObject();
180 180 // init is called to initialize the rest of the values.
181 181 init(getName());
182 182 }
183 183
184 184 /**
185 185 * Initialize a ExecOptionPermission object. Common to all constructors.
186 186 * Also called during de-serialization.
187 187 */
188 188 private void init(String name)
189 189 {
190 190 if (name == null)
191 191 throw new NullPointerException("name can't be null");
192 192
193 193 if (name.equals("")) {
194 194 throw new IllegalArgumentException("name can't be empty");
195 195 }
196 196
197 197 if (name.endsWith(".*") || name.endsWith("=*") || name.equals("*")) {
198 198 wildcard = true;
199 199 if (name.length() == 1) {
200 200 this.name = "";
201 201 } else {
202 202 this.name = name.substring(0, name.length()-1);
203 203 }
204 204 } else {
205 205 this.name = name;
206 206 }
207 207 }
208 208
209 209 /**
210 210 * A ExecOptionPermissionCollection stores a collection
211 211 * of ExecOptionPermission permissions. ExecOptionPermission objects
212 212 * must be stored in a manner that allows them to be inserted in any
213 213 * order, but enable the implies function to evaluate the implies
214 214 * method in an efficient (and consistent) manner.
215 215 *
↓ open down ↓ |
215 lines elided |
↑ open up ↑ |
216 216 * A ExecOptionPermissionCollection handles comparing a permission like
217 217 * "a.b.c.d.e" * with a Permission such as "a.b.*", or "*".
218 218 *
219 219 * @serial include
220 220 */
221 221 private static class ExecOptionPermissionCollection
222 222 extends PermissionCollection
223 223 implements java.io.Serializable
224 224 {
225 225
226 - private Hashtable permissions;
226 + private Hashtable<String, Permission> permissions;
227 227 private boolean all_allowed; // true if "*" is in the collection
228 228 private static final long serialVersionUID = -1242475729790124375L;
229 229
230 230 /**
231 231 * Create an empty ExecOptionPermissionCollection.
232 232 */
233 233 public ExecOptionPermissionCollection() {
234 - permissions = new Hashtable(11);
234 + permissions = new Hashtable<String, Permission>(11);
235 235 all_allowed = false;
236 236 }
237 237
238 238 /**
239 239 * Adds a permission to the collection. The key for the hash is
240 240 * permission.name.
241 241 *
242 242 * @param permission the Permission object to add.
243 243 *
244 244 * @exception IllegalArgumentException - if the permission is not a
245 245 * ExecOptionPermission
246 246 *
247 247 * @exception SecurityException - if this ExecOptionPermissionCollection
248 248 * object has been marked readonly
249 249 */
250 250
251 251 public void add(Permission permission)
252 252 {
253 253 if (! (permission instanceof ExecOptionPermission))
254 254 throw new IllegalArgumentException("invalid permission: "+
255 255 permission);
256 256 if (isReadOnly())
257 257 throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
258 258
259 259 ExecOptionPermission p = (ExecOptionPermission) permission;
260 260
261 261 permissions.put(p.getName(), permission);
262 262 if (!all_allowed) {
263 263 if (p.getName().equals("*"))
264 264 all_allowed = true;
265 265 }
266 266 }
267 267
268 268 /**
269 269 * Check and see if this set of permissions implies the permissions
270 270 * expressed in "permission".
271 271 *
272 272 * @param p the Permission object to compare
273 273 *
274 274 * @return true if "permission" is a proper subset of a permission in
275 275 * the set, false if not.
276 276 */
277 277 public boolean implies(Permission permission)
278 278 {
279 279 if (! (permission instanceof ExecOptionPermission))
280 280 return false;
281 281
282 282 ExecOptionPermission p = (ExecOptionPermission) permission;
283 283
↓ open down ↓ |
39 lines elided |
↑ open up ↑ |
284 284 // short circuit if the "*" Permission was added
285 285 if (all_allowed)
286 286 return true;
287 287
288 288 // strategy:
289 289 // Check for full match first. Then work our way up the
290 290 // name looking for matches on a.b.*
291 291
292 292 String pname = p.getName();
293 293
294 - Permission x = (Permission) permissions.get(pname);
294 + Permission x = permissions.get(pname);
295 295
296 296 if (x != null)
297 297 // we have a direct hit!
298 298 return x.implies(permission);
299 299
300 300
301 301 // work our way up the tree...
302 302 int last, offset;
303 303
304 304 offset = pname.length() - 1;
305 305
306 306 while ((last = pname.lastIndexOf(".", offset)) != -1) {
307 307
308 308 pname = pname.substring(0, last+1) + "*";
309 - x = (Permission) permissions.get(pname);
309 + x = permissions.get(pname);
310 310
311 311 if (x != null) {
312 312 return x.implies(permission);
313 313 }
314 314 offset = last - 1;
315 315 }
316 316
317 317 // check for "=*" wildcard match
318 318 pname = p.getName();
319 319 offset = pname.length() - 1;
320 320
321 321 while ((last = pname.lastIndexOf("=", offset)) != -1) {
322 322
323 323 pname = pname.substring(0, last+1) + "*";
324 - x = (Permission) permissions.get(pname);
324 + x = permissions.get(pname);
325 325
326 326 if (x != null) {
327 327 return x.implies(permission);
328 328 }
329 329 offset = last - 1;
330 330 }
331 331
332 332 // we don't have to check for "*" as it was already checked
333 333 // at the top (all_allowed), so we just return false
334 334 return false;
335 335 }
336 336
337 337 /**
338 338 * Returns an enumeration of all the ExecOptionPermission objects in the
339 339 * container.
340 340 *
341 341 * @return an enumeration of all the ExecOptionPermission objects.
342 342 */
343 343
344 - public Enumeration elements()
344 + public Enumeration<Permission> elements()
345 345 {
346 346 return permissions.elements();
347 347 }
348 348 }
349 349 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX