author | mchung |
Fri, 22 May 2015 16:43:39 -0700 | |
changeset 30789 | 9eca83469588 |
parent 26219 | 1a19360ff122 |
child 31080 | 00a25f4c4d44 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21334
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util; |
|
27 |
||
28 |
import java.io.Serializable; |
|
29 |
import java.io.IOException; |
|
30 |
import java.security.*; |
|
31 |
import java.util.Map; |
|
32 |
import java.util.HashMap; |
|
33 |
import java.util.Enumeration; |
|
34 |
import java.util.Hashtable; |
|
35 |
import java.util.Collections; |
|
36 |
import java.io.ObjectStreamField; |
|
37 |
import java.io.ObjectOutputStream; |
|
38 |
import java.io.ObjectInputStream; |
|
39 |
import java.io.IOException; |
|
40 |
import sun.security.util.SecurityConstants; |
|
41 |
||
42 |
/** |
|
43 |
* This class is for property permissions. |
|
44 |
* |
|
45 |
* <P> |
|
46 |
* The name is the name of the property ("java.home", |
|
47 |
* "os.name", etc). The naming |
|
48 |
* convention follows the hierarchical property naming convention. |
|
49 |
* Also, an asterisk |
|
50 |
* may appear at the end of the name, following a ".", or by itself, to |
|
14667
6862285acf46
7167056: Clarify that BasicPermission names that contain non-wildcard asterisks are not invalid
mullan
parents:
14014
diff
changeset
|
51 |
* signify a wildcard match. For example: "java.*" and "*" signify a wildcard |
6862285acf46
7167056: Clarify that BasicPermission names that contain non-wildcard asterisks are not invalid
mullan
parents:
14014
diff
changeset
|
52 |
* match, while "*java" and "a*b" do not. |
2 | 53 |
* <P> |
54 |
* The actions to be granted are passed to the constructor in a string containing |
|
55 |
* a list of one or more comma-separated keywords. The possible keywords are |
|
56 |
* "read" and "write". Their meaning is defined as follows: |
|
21334 | 57 |
* |
2 | 58 |
* <DL> |
59 |
* <DT> read |
|
60 |
* <DD> read permission. Allows <code>System.getProperty</code> to |
|
61 |
* be called. |
|
62 |
* <DT> write |
|
63 |
* <DD> write permission. Allows <code>System.setProperty</code> to |
|
64 |
* be called. |
|
65 |
* </DL> |
|
66 |
* <P> |
|
67 |
* The actions string is converted to lowercase before processing. |
|
68 |
* <P> |
|
69 |
* Care should be taken before granting code permission to access |
|
70 |
* certain system properties. For example, granting permission to |
|
71 |
* access the "java.home" system property gives potentially malevolent |
|
72 |
* code sensitive information about the system environment (the Java |
|
73 |
* installation directory). Also, granting permission to access |
|
74 |
* the "user.name" and "user.home" system properties gives potentially |
|
75 |
* malevolent code sensitive information about the user environment |
|
76 |
* (the user's account name and home directory). |
|
77 |
* |
|
78 |
* @see java.security.BasicPermission |
|
79 |
* @see java.security.Permission |
|
80 |
* @see java.security.Permissions |
|
81 |
* @see java.security.PermissionCollection |
|
82 |
* @see java.lang.SecurityManager |
|
83 |
* |
|
84 |
* |
|
85 |
* @author Roland Schemers |
|
86 |
* @since 1.2 |
|
87 |
* |
|
88 |
* @serial exclude |
|
89 |
*/ |
|
90 |
||
91 |
public final class PropertyPermission extends BasicPermission { |
|
92 |
||
93 |
/** |
|
94 |
* Read action. |
|
95 |
*/ |
|
96 |
private final static int READ = 0x1; |
|
97 |
||
98 |
/** |
|
99 |
* Write action. |
|
100 |
*/ |
|
101 |
private final static int WRITE = 0x2; |
|
102 |
/** |
|
103 |
* All actions (read,write); |
|
104 |
*/ |
|
105 |
private final static int ALL = READ|WRITE; |
|
106 |
/** |
|
107 |
* No actions. |
|
108 |
*/ |
|
109 |
private final static int NONE = 0x0; |
|
110 |
||
111 |
/** |
|
112 |
* The actions mask. |
|
113 |
* |
|
114 |
*/ |
|
115 |
private transient int mask; |
|
116 |
||
117 |
/** |
|
118 |
* The actions string. |
|
119 |
* |
|
120 |
* @serial |
|
121 |
*/ |
|
122 |
private String actions; // Left null as long as possible, then |
|
123 |
// created and re-used in the getAction function. |
|
124 |
||
125 |
/** |
|
126 |
* initialize a PropertyPermission object. Common to all constructors. |
|
127 |
* Also called during de-serialization. |
|
128 |
* |
|
129 |
* @param mask the actions mask to use. |
|
130 |
* |
|
131 |
*/ |
|
14014 | 132 |
private void init(int mask) { |
2 | 133 |
if ((mask & ALL) != mask) |
14014 | 134 |
throw new IllegalArgumentException("invalid actions mask"); |
2 | 135 |
|
136 |
if (mask == NONE) |
|
14014 | 137 |
throw new IllegalArgumentException("invalid actions mask"); |
2 | 138 |
|
139 |
if (getName() == null) |
|
14014 | 140 |
throw new NullPointerException("name can't be null"); |
2 | 141 |
|
142 |
this.mask = mask; |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Creates a new PropertyPermission object with the specified name. |
|
147 |
* The name is the name of the system property, and |
|
148 |
* <i>actions</i> contains a comma-separated list of the |
|
149 |
* desired actions granted on the property. Possible actions are |
|
150 |
* "read" and "write". |
|
151 |
* |
|
152 |
* @param name the name of the PropertyPermission. |
|
153 |
* @param actions the actions string. |
|
154 |
* |
|
155 |
* @throws NullPointerException if <code>name</code> is <code>null</code>. |
|
156 |
* @throws IllegalArgumentException if <code>name</code> is empty or if |
|
157 |
* <code>actions</code> is invalid. |
|
158 |
*/ |
|
14014 | 159 |
public PropertyPermission(String name, String actions) { |
2 | 160 |
super(name,actions); |
161 |
init(getMask(actions)); |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Checks if this PropertyPermission object "implies" the specified |
|
166 |
* permission. |
|
167 |
* <P> |
|
21334 | 168 |
* More specifically, this method returns true if: |
2 | 169 |
* <ul> |
21334 | 170 |
* <li> <i>p</i> is an instanceof PropertyPermission, |
2 | 171 |
* <li> <i>p</i>'s actions are a subset of this |
21334 | 172 |
* object's actions, and |
2 | 173 |
* <li> <i>p</i>'s name is implied by this object's |
174 |
* name. For example, "java.*" implies "java.home". |
|
175 |
* </ul> |
|
176 |
* @param p the permission to check against. |
|
177 |
* |
|
178 |
* @return true if the specified permission is implied by this object, |
|
179 |
* false if not. |
|
180 |
*/ |
|
181 |
public boolean implies(Permission p) { |
|
182 |
if (!(p instanceof PropertyPermission)) |
|
183 |
return false; |
|
184 |
||
185 |
PropertyPermission that = (PropertyPermission) p; |
|
186 |
||
187 |
// we get the effective mask. i.e., the "and" of this and that. |
|
188 |
// They must be equal to that.mask for implies to return true. |
|
189 |
||
190 |
return ((this.mask & that.mask) == that.mask) && super.implies(that); |
|
191 |
} |
|
192 |
||
193 |
/** |
|
194 |
* Checks two PropertyPermission objects for equality. Checks that <i>obj</i> is |
|
195 |
* a PropertyPermission, and has the same name and actions as this object. |
|
24196
61c9885d76e2
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents:
23010
diff
changeset
|
196 |
* |
2 | 197 |
* @param obj the object we are testing for equality with this object. |
198 |
* @return true if obj is a PropertyPermission, and has the same name and |
|
199 |
* actions as this PropertyPermission object. |
|
200 |
*/ |
|
201 |
public boolean equals(Object obj) { |
|
202 |
if (obj == this) |
|
203 |
return true; |
|
204 |
||
205 |
if (! (obj instanceof PropertyPermission)) |
|
206 |
return false; |
|
207 |
||
208 |
PropertyPermission that = (PropertyPermission) obj; |
|
209 |
||
210 |
return (this.mask == that.mask) && |
|
211 |
(this.getName().equals(that.getName())); |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Returns the hash code value for this object. |
|
216 |
* The hash code used is the hash code of this permissions name, that is, |
|
217 |
* <code>getName().hashCode()</code>, where <code>getName</code> is |
|
218 |
* from the Permission superclass. |
|
219 |
* |
|
220 |
* @return a hash code value for this object. |
|
221 |
*/ |
|
222 |
public int hashCode() { |
|
223 |
return this.getName().hashCode(); |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Converts an actions String to an actions mask. |
|
228 |
* |
|
14014 | 229 |
* @param actions the action string. |
2 | 230 |
* @return the actions mask. |
231 |
*/ |
|
232 |
private static int getMask(String actions) { |
|
233 |
||
234 |
int mask = NONE; |
|
235 |
||
236 |
if (actions == null) { |
|
237 |
return mask; |
|
238 |
} |
|
239 |
||
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
240 |
// Use object identity comparison against known-interned strings for |
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
241 |
// performance benefit (these values are used heavily within the JDK). |
2 | 242 |
if (actions == SecurityConstants.PROPERTY_READ_ACTION) { |
243 |
return READ; |
|
244 |
} if (actions == SecurityConstants.PROPERTY_WRITE_ACTION) { |
|
245 |
return WRITE; |
|
246 |
} else if (actions == SecurityConstants.PROPERTY_RW_ACTION) { |
|
247 |
return READ|WRITE; |
|
248 |
} |
|
249 |
||
250 |
char[] a = actions.toCharArray(); |
|
251 |
||
252 |
int i = a.length - 1; |
|
253 |
if (i < 0) |
|
254 |
return mask; |
|
255 |
||
256 |
while (i != -1) { |
|
257 |
char c; |
|
258 |
||
259 |
// skip whitespace |
|
260 |
while ((i!=-1) && ((c = a[i]) == ' ' || |
|
261 |
c == '\r' || |
|
262 |
c == '\n' || |
|
263 |
c == '\f' || |
|
264 |
c == '\t')) |
|
265 |
i--; |
|
266 |
||
267 |
// check for the known strings |
|
268 |
int matchlen; |
|
269 |
||
270 |
if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') && |
|
271 |
(a[i-2] == 'e' || a[i-2] == 'E') && |
|
272 |
(a[i-1] == 'a' || a[i-1] == 'A') && |
|
273 |
(a[i] == 'd' || a[i] == 'D')) |
|
274 |
{ |
|
275 |
matchlen = 4; |
|
276 |
mask |= READ; |
|
277 |
||
278 |
} else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') && |
|
279 |
(a[i-3] == 'r' || a[i-3] == 'R') && |
|
280 |
(a[i-2] == 'i' || a[i-2] == 'I') && |
|
281 |
(a[i-1] == 't' || a[i-1] == 'T') && |
|
282 |
(a[i] == 'e' || a[i] == 'E')) |
|
283 |
{ |
|
284 |
matchlen = 5; |
|
285 |
mask |= WRITE; |
|
286 |
||
287 |
} else { |
|
288 |
// parse error |
|
289 |
throw new IllegalArgumentException( |
|
290 |
"invalid permission: " + actions); |
|
291 |
} |
|
292 |
||
293 |
// make sure we didn't just match the tail of a word |
|
294 |
// like "ackbarfaccept". Also, skip to the comma. |
|
295 |
boolean seencomma = false; |
|
296 |
while (i >= matchlen && !seencomma) { |
|
297 |
switch(a[i-matchlen]) { |
|
298 |
case ',': |
|
299 |
seencomma = true; |
|
11139
db0c2ff5e1ea
7116997: fix warnings in java.util.PropertyPermission
smarks
parents:
5506
diff
changeset
|
300 |
break; |
2 | 301 |
case ' ': case '\r': case '\n': |
302 |
case '\f': case '\t': |
|
303 |
break; |
|
304 |
default: |
|
305 |
throw new IllegalArgumentException( |
|
306 |
"invalid permission: " + actions); |
|
307 |
} |
|
308 |
i--; |
|
309 |
} |
|
310 |
||
311 |
// point i at the location of the comma minus one (or -1). |
|
312 |
i -= matchlen; |
|
313 |
} |
|
314 |
||
315 |
return mask; |
|
316 |
} |
|
317 |
||
318 |
||
319 |
/** |
|
320 |
* Return the canonical string representation of the actions. |
|
321 |
* Always returns present actions in the following order: |
|
322 |
* read, write. |
|
323 |
* |
|
324 |
* @return the canonical string representation of the actions. |
|
325 |
*/ |
|
14014 | 326 |
static String getActions(int mask) { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
327 |
switch (mask & (READ|WRITE)) { |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
328 |
case READ: |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
329 |
return SecurityConstants.PROPERTY_READ_ACTION; |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
330 |
case WRITE: |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
331 |
return SecurityConstants.PROPERTY_WRITE_ACTION; |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
332 |
case READ|WRITE: |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
333 |
return SecurityConstants.PROPERTY_RW_ACTION; |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
334 |
default: |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
335 |
return ""; |
2 | 336 |
} |
337 |
} |
|
338 |
||
339 |
/** |
|
340 |
* Returns the "canonical string representation" of the actions. |
|
341 |
* That is, this method always returns present actions in the following order: |
|
342 |
* read, write. For example, if this PropertyPermission object |
|
343 |
* allows both write and read actions, a call to <code>getActions</code> |
|
344 |
* will return the string "read,write". |
|
345 |
* |
|
346 |
* @return the canonical string representation of the actions. |
|
347 |
*/ |
|
14014 | 348 |
public String getActions() { |
2 | 349 |
if (actions == null) |
350 |
actions = getActions(this.mask); |
|
351 |
||
352 |
return actions; |
|
353 |
} |
|
354 |
||
355 |
/** |
|
356 |
* Return the current action mask. |
|
357 |
* Used by the PropertyPermissionCollection |
|
358 |
* |
|
359 |
* @return the actions mask. |
|
360 |
*/ |
|
361 |
int getMask() { |
|
362 |
return mask; |
|
363 |
} |
|
364 |
||
365 |
/** |
|
366 |
* Returns a new PermissionCollection object for storing |
|
367 |
* PropertyPermission objects. |
|
368 |
* |
|
369 |
* @return a new PermissionCollection object suitable for storing |
|
370 |
* PropertyPermissions. |
|
371 |
*/ |
|
372 |
public PermissionCollection newPermissionCollection() { |
|
373 |
return new PropertyPermissionCollection(); |
|
374 |
} |
|
375 |
||
376 |
||
377 |
private static final long serialVersionUID = 885438825399942851L; |
|
378 |
||
379 |
/** |
|
380 |
* WriteObject is called to save the state of the PropertyPermission |
|
381 |
* to a stream. The actions are serialized, and the superclass |
|
382 |
* takes care of the name. |
|
383 |
*/ |
|
384 |
private synchronized void writeObject(java.io.ObjectOutputStream s) |
|
385 |
throws IOException |
|
386 |
{ |
|
387 |
// Write out the actions. The superclass takes care of the name |
|
388 |
// call getActions to make sure actions field is initialized |
|
389 |
if (actions == null) |
|
390 |
getActions(); |
|
391 |
s.defaultWriteObject(); |
|
392 |
} |
|
393 |
||
394 |
/** |
|
395 |
* readObject is called to restore the state of the PropertyPermission from |
|
396 |
* a stream. |
|
397 |
*/ |
|
398 |
private synchronized void readObject(java.io.ObjectInputStream s) |
|
399 |
throws IOException, ClassNotFoundException |
|
400 |
{ |
|
401 |
// Read in the action, then initialize the rest |
|
402 |
s.defaultReadObject(); |
|
403 |
init(getMask(actions)); |
|
404 |
} |
|
405 |
} |
|
406 |
||
407 |
/** |
|
408 |
* A PropertyPermissionCollection stores a set of PropertyPermission |
|
409 |
* permissions. |
|
410 |
* |
|
411 |
* @see java.security.Permission |
|
412 |
* @see java.security.Permissions |
|
413 |
* @see java.security.PermissionCollection |
|
414 |
* |
|
415 |
* |
|
416 |
* @author Roland Schemers |
|
417 |
* |
|
418 |
* @serial include |
|
419 |
*/ |
|
420 |
final class PropertyPermissionCollection extends PermissionCollection |
|
14014 | 421 |
implements Serializable |
2 | 422 |
{ |
423 |
||
424 |
/** |
|
425 |
* Key is property name; value is PropertyPermission. |
|
426 |
* Not serialized; see serialization section at end of class. |
|
427 |
*/ |
|
12448 | 428 |
private transient Map<String, PropertyPermission> perms; |
2 | 429 |
|
430 |
/** |
|
431 |
* Boolean saying if "*" is in the collection. |
|
432 |
* |
|
433 |
* @see #serialPersistentFields |
|
434 |
*/ |
|
435 |
// No sync access; OK for this to be stale. |
|
436 |
private boolean all_allowed; |
|
437 |
||
438 |
/** |
|
14014 | 439 |
* Create an empty PropertyPermissionCollection object. |
2 | 440 |
*/ |
441 |
public PropertyPermissionCollection() { |
|
11139
db0c2ff5e1ea
7116997: fix warnings in java.util.PropertyPermission
smarks
parents:
5506
diff
changeset
|
442 |
perms = new HashMap<>(32); // Capacity for default policy |
2 | 443 |
all_allowed = false; |
444 |
} |
|
445 |
||
446 |
/** |
|
447 |
* Adds a permission to the PropertyPermissions. The key for the hash is |
|
448 |
* the name. |
|
449 |
* |
|
450 |
* @param permission the Permission object to add. |
|
451 |
* |
|
452 |
* @exception IllegalArgumentException - if the permission is not a |
|
453 |
* PropertyPermission |
|
454 |
* |
|
455 |
* @exception SecurityException - if this PropertyPermissionCollection |
|
456 |
* object has been marked readonly |
|
457 |
*/ |
|
14014 | 458 |
public void add(Permission permission) { |
2 | 459 |
if (! (permission instanceof PropertyPermission)) |
460 |
throw new IllegalArgumentException("invalid permission: "+ |
|
461 |
permission); |
|
462 |
if (isReadOnly()) |
|
463 |
throw new SecurityException( |
|
464 |
"attempt to add a Permission to a readonly PermissionCollection"); |
|
465 |
||
466 |
PropertyPermission pp = (PropertyPermission) permission; |
|
467 |
String propName = pp.getName(); |
|
468 |
||
469 |
synchronized (this) { |
|
12448 | 470 |
PropertyPermission existing = perms.get(propName); |
2 | 471 |
|
472 |
if (existing != null) { |
|
473 |
int oldMask = existing.getMask(); |
|
474 |
int newMask = pp.getMask(); |
|
475 |
if (oldMask != newMask) { |
|
476 |
int effective = oldMask | newMask; |
|
477 |
String actions = PropertyPermission.getActions(effective); |
|
478 |
perms.put(propName, new PropertyPermission(propName, actions)); |
|
479 |
} |
|
480 |
} else { |
|
12448 | 481 |
perms.put(propName, pp); |
2 | 482 |
} |
483 |
} |
|
484 |
||
485 |
if (!all_allowed) { |
|
486 |
if (propName.equals("*")) |
|
487 |
all_allowed = true; |
|
488 |
} |
|
489 |
} |
|
490 |
||
491 |
/** |
|
492 |
* Check and see if this set of permissions implies the permissions |
|
493 |
* expressed in "permission". |
|
494 |
* |
|
14014 | 495 |
* @param permission the Permission object to compare |
2 | 496 |
* |
497 |
* @return true if "permission" is a proper subset of a permission in |
|
498 |
* the set, false if not. |
|
499 |
*/ |
|
14014 | 500 |
public boolean implies(Permission permission) { |
2 | 501 |
if (! (permission instanceof PropertyPermission)) |
502 |
return false; |
|
503 |
||
504 |
PropertyPermission pp = (PropertyPermission) permission; |
|
505 |
PropertyPermission x; |
|
506 |
||
507 |
int desired = pp.getMask(); |
|
508 |
int effective = 0; |
|
509 |
||
510 |
// short circuit if the "*" Permission was added |
|
511 |
if (all_allowed) { |
|
512 |
synchronized (this) { |
|
12448 | 513 |
x = perms.get("*"); |
2 | 514 |
} |
515 |
if (x != null) { |
|
516 |
effective |= x.getMask(); |
|
517 |
if ((effective & desired) == desired) |
|
518 |
return true; |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
// strategy: |
|
523 |
// Check for full match first. Then work our way up the |
|
524 |
// name looking for matches on a.b.* |
|
525 |
||
526 |
String name = pp.getName(); |
|
527 |
//System.out.println("check "+name); |
|
528 |
||
529 |
synchronized (this) { |
|
12448 | 530 |
x = perms.get(name); |
2 | 531 |
} |
532 |
||
533 |
if (x != null) { |
|
534 |
// we have a direct hit! |
|
535 |
effective |= x.getMask(); |
|
536 |
if ((effective & desired) == desired) |
|
537 |
return true; |
|
538 |
} |
|
539 |
||
540 |
// work our way up the tree... |
|
541 |
int last, offset; |
|
542 |
||
543 |
offset = name.length()-1; |
|
544 |
||
24685
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
24196
diff
changeset
|
545 |
while ((last = name.lastIndexOf('.', offset)) != -1) { |
2 | 546 |
|
547 |
name = name.substring(0, last+1) + "*"; |
|
548 |
//System.out.println("check "+name); |
|
549 |
synchronized (this) { |
|
12448 | 550 |
x = perms.get(name); |
2 | 551 |
} |
552 |
||
553 |
if (x != null) { |
|
554 |
effective |= x.getMask(); |
|
555 |
if ((effective & desired) == desired) |
|
556 |
return true; |
|
557 |
} |
|
558 |
offset = last -1; |
|
559 |
} |
|
560 |
||
561 |
// we don't have to check for "*" as it was already checked |
|
562 |
// at the top (all_allowed), so we just return false |
|
563 |
return false; |
|
564 |
} |
|
565 |
||
566 |
/** |
|
567 |
* Returns an enumeration of all the PropertyPermission objects in the |
|
568 |
* container. |
|
569 |
* |
|
570 |
* @return an enumeration of all the PropertyPermission objects. |
|
571 |
*/ |
|
12448 | 572 |
@SuppressWarnings("unchecked") |
11139
db0c2ff5e1ea
7116997: fix warnings in java.util.PropertyPermission
smarks
parents:
5506
diff
changeset
|
573 |
public Enumeration<Permission> elements() { |
2 | 574 |
// Convert Iterator of Map values into an Enumeration |
575 |
synchronized (this) { |
|
12448 | 576 |
/** |
577 |
* Casting to rawtype since Enumeration<PropertyPermission> |
|
578 |
* cannot be directly cast to Enumeration<Permission> |
|
579 |
*/ |
|
580 |
return (Enumeration)Collections.enumeration(perms.values()); |
|
2 | 581 |
} |
582 |
} |
|
583 |
||
584 |
private static final long serialVersionUID = 7015263904581634791L; |
|
585 |
||
586 |
// Need to maintain serialization interoperability with earlier releases, |
|
587 |
// which had the serializable field: |
|
588 |
// |
|
589 |
// Table of permissions. |
|
590 |
// |
|
591 |
// @serial |
|
592 |
// |
|
593 |
// private Hashtable permissions; |
|
594 |
/** |
|
595 |
* @serialField permissions java.util.Hashtable |
|
596 |
* A table of the PropertyPermissions. |
|
597 |
* @serialField all_allowed boolean |
|
598 |
* boolean saying if "*" is in the collection. |
|
599 |
*/ |
|
600 |
private static final ObjectStreamField[] serialPersistentFields = { |
|
601 |
new ObjectStreamField("permissions", Hashtable.class), |
|
602 |
new ObjectStreamField("all_allowed", Boolean.TYPE), |
|
603 |
}; |
|
604 |
||
605 |
/** |
|
606 |
* @serialData Default fields. |
|
607 |
*/ |
|
608 |
/* |
|
609 |
* Writes the contents of the perms field out as a Hashtable for |
|
610 |
* serialization compatibility with earlier releases. all_allowed |
|
611 |
* unchanged. |
|
612 |
*/ |
|
613 |
private void writeObject(ObjectOutputStream out) throws IOException { |
|
614 |
// Don't call out.defaultWriteObject() |
|
615 |
||
616 |
// Copy perms into a Hashtable |
|
12448 | 617 |
Hashtable<String, Permission> permissions = |
618 |
new Hashtable<>(perms.size()*2); |
|
2 | 619 |
synchronized (this) { |
620 |
permissions.putAll(perms); |
|
621 |
} |
|
622 |
||
623 |
// Write out serializable fields |
|
624 |
ObjectOutputStream.PutField pfields = out.putFields(); |
|
625 |
pfields.put("all_allowed", all_allowed); |
|
626 |
pfields.put("permissions", permissions); |
|
627 |
out.writeFields(); |
|
628 |
} |
|
629 |
||
630 |
/* |
|
631 |
* Reads in a Hashtable of PropertyPermissions and saves them in the |
|
632 |
* perms field. Reads in all_allowed. |
|
633 |
*/ |
|
14014 | 634 |
private void readObject(ObjectInputStream in) |
635 |
throws IOException, ClassNotFoundException |
|
636 |
{ |
|
2 | 637 |
// Don't call defaultReadObject() |
638 |
||
639 |
// Read in serialized fields |
|
640 |
ObjectInputStream.GetField gfields = in.readFields(); |
|
641 |
||
642 |
// Get all_allowed |
|
643 |
all_allowed = gfields.get("all_allowed", false); |
|
644 |
||
645 |
// Get permissions |
|
11139
db0c2ff5e1ea
7116997: fix warnings in java.util.PropertyPermission
smarks
parents:
5506
diff
changeset
|
646 |
@SuppressWarnings("unchecked") |
12448 | 647 |
Hashtable<String, PropertyPermission> permissions = |
648 |
(Hashtable<String, PropertyPermission>)gfields.get("permissions", null); |
|
11139
db0c2ff5e1ea
7116997: fix warnings in java.util.PropertyPermission
smarks
parents:
5506
diff
changeset
|
649 |
perms = new HashMap<>(permissions.size()*2); |
2 | 650 |
perms.putAll(permissions); |
651 |
} |
|
652 |
} |