author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 18830 | jdk/src/share/classes/javax/security/auth/SubjectDomainCombiner.java@90956ead732f |
child 28059 | e576535359cc |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18830 | 2 |
* Copyright (c) 1999, 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 javax.security.auth; |
|
27 |
||
28 |
import java.security.AccessController; |
|
29 |
import java.security.Permission; |
|
30 |
import java.security.Permissions; |
|
31 |
import java.security.PermissionCollection; |
|
32 |
import java.security.Policy; |
|
33 |
import java.security.Principal; |
|
34 |
import java.security.PrivilegedAction; |
|
35 |
import java.security.ProtectionDomain; |
|
36 |
import java.security.Security; |
|
37 |
import java.util.Set; |
|
38 |
import java.util.WeakHashMap; |
|
39 |
import java.lang.ref.WeakReference; |
|
40 |
||
41 |
/** |
|
18830 | 42 |
* A {@code SubjectDomainCombiner} updates ProtectionDomains |
43 |
* with Principals from the {@code Subject} associated with this |
|
44 |
* {@code SubjectDomainCombiner}. |
|
2 | 45 |
* |
46 |
*/ |
|
47 |
public class SubjectDomainCombiner implements java.security.DomainCombiner { |
|
48 |
||
49 |
private Subject subject; |
|
50 |
private WeakKeyValueMap<ProtectionDomain, ProtectionDomain> cachedPDs = |
|
7970
af1579474d16
7008728: diamond conversion of basic security, permissions, authentication
smarks
parents:
7291
diff
changeset
|
51 |
new WeakKeyValueMap<>(); |
2 | 52 |
private Set<Principal> principalSet; |
53 |
private Principal[] principals; |
|
54 |
||
55 |
private static final sun.security.util.Debug debug = |
|
56 |
sun.security.util.Debug.getInstance("combiner", |
|
57 |
"\t[SubjectDomainCombiner]"); |
|
58 |
||
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
9035
diff
changeset
|
59 |
@SuppressWarnings("deprecation") |
2 | 60 |
// Note: check only at classloading time, not dynamically during combine() |
7291
9fefa2786251
6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents:
5506
diff
changeset
|
61 |
private static final boolean useJavaxPolicy = |
9fefa2786251
6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents:
5506
diff
changeset
|
62 |
javax.security.auth.Policy.isCustomPolicySet(debug); |
2 | 63 |
|
64 |
// Relevant only when useJavaxPolicy is true |
|
65 |
private static final boolean allowCaching = |
|
66 |
(useJavaxPolicy && cachePolicy()); |
|
67 |
||
68 |
/** |
|
18830 | 69 |
* Associate the provided {@code Subject} with this |
70 |
* {@code SubjectDomainCombiner}. |
|
2 | 71 |
* |
72 |
* <p> |
|
73 |
* |
|
18830 | 74 |
* @param subject the {@code Subject} to be associated with |
75 |
* with this {@code SubjectDomainCombiner}. |
|
2 | 76 |
*/ |
77 |
public SubjectDomainCombiner(Subject subject) { |
|
78 |
this.subject = subject; |
|
79 |
||
80 |
if (subject.isReadOnly()) { |
|
81 |
principalSet = subject.getPrincipals(); |
|
82 |
principals = principalSet.toArray |
|
83 |
(new Principal[principalSet.size()]); |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
/** |
|
18830 | 88 |
* Get the {@code Subject} associated with this |
89 |
* {@code SubjectDomainCombiner}. |
|
2 | 90 |
* |
91 |
* <p> |
|
92 |
* |
|
18830 | 93 |
* @return the {@code Subject} associated with this |
94 |
* {@code SubjectDomainCombiner}, or {@code null} |
|
95 |
* if no {@code Subject} is associated with this |
|
96 |
* {@code SubjectDomainCombiner}. |
|
2 | 97 |
* |
98 |
* @exception SecurityException if the caller does not have permission |
|
18830 | 99 |
* to get the {@code Subject} associated with this |
100 |
* {@code SubjectDomainCombiner}. |
|
2 | 101 |
*/ |
102 |
public Subject getSubject() { |
|
103 |
java.lang.SecurityManager sm = System.getSecurityManager(); |
|
104 |
if (sm != null) { |
|
105 |
sm.checkPermission(new AuthPermission |
|
106 |
("getSubjectFromDomainCombiner")); |
|
107 |
} |
|
108 |
return subject; |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Update the relevant ProtectionDomains with the Principals |
|
18830 | 113 |
* from the {@code Subject} associated with this |
114 |
* {@code SubjectDomainCombiner}. |
|
2 | 115 |
* |
18830 | 116 |
* <p> A new {@code ProtectionDomain} instance is created |
117 |
* for each {@code ProtectionDomain} in the |
|
118 |
* <i>currentDomains</i> array. Each new {@code ProtectionDomain} |
|
119 |
* instance is created using the {@code CodeSource}, |
|
120 |
* {@code Permission}s and {@code ClassLoader} |
|
121 |
* from the corresponding {@code ProtectionDomain} in |
|
2 | 122 |
* <i>currentDomains</i>, as well as with the Principals from |
18830 | 123 |
* the {@code Subject} associated with this |
124 |
* {@code SubjectDomainCombiner}. |
|
2 | 125 |
* |
126 |
* <p> All of the newly instantiated ProtectionDomains are |
|
127 |
* combined into a new array. The ProtectionDomains from the |
|
128 |
* <i>assignedDomains</i> array are appended to this new array, |
|
129 |
* and the result is returned. |
|
130 |
* |
|
131 |
* <p> Note that optimizations such as the removal of duplicate |
|
132 |
* ProtectionDomains may have occurred. |
|
133 |
* In addition, caching of ProtectionDomains may be permitted. |
|
134 |
* |
|
135 |
* <p> |
|
136 |
* |
|
137 |
* @param currentDomains the ProtectionDomains associated with the |
|
138 |
* current execution Thread, up to the most recent |
|
18830 | 139 |
* privileged {@code ProtectionDomain}. |
2 | 140 |
* The ProtectionDomains are are listed in order of execution, |
18830 | 141 |
* with the most recently executing {@code ProtectionDomain} |
2 | 142 |
* residing at the beginning of the array. This parameter may |
18830 | 143 |
* be {@code null} if the current execution Thread |
2 | 144 |
* has no associated ProtectionDomains.<p> |
145 |
* |
|
146 |
* @param assignedDomains the ProtectionDomains inherited from the |
|
147 |
* parent Thread, or the ProtectionDomains from the |
|
148 |
* privileged <i>context</i>, if a call to |
|
149 |
* AccessController.doPrivileged(..., <i>context</i>) |
|
18830 | 150 |
* had occurred This parameter may be {@code null} |
2 | 151 |
* if there were no ProtectionDomains inherited from the |
152 |
* parent Thread, or from the privileged <i>context</i>. |
|
153 |
* |
|
154 |
* @return a new array consisting of the updated ProtectionDomains, |
|
18830 | 155 |
* or {@code null}. |
2 | 156 |
*/ |
157 |
public ProtectionDomain[] combine(ProtectionDomain[] currentDomains, |
|
158 |
ProtectionDomain[] assignedDomains) { |
|
159 |
if (debug != null) { |
|
160 |
if (subject == null) { |
|
161 |
debug.println("null subject"); |
|
162 |
} else { |
|
163 |
final Subject s = subject; |
|
164 |
AccessController.doPrivileged |
|
165 |
(new java.security.PrivilegedAction<Void>() { |
|
166 |
public Void run() { |
|
167 |
debug.println(s.toString()); |
|
168 |
return null; |
|
169 |
} |
|
170 |
}); |
|
171 |
} |
|
172 |
printInputDomains(currentDomains, assignedDomains); |
|
173 |
} |
|
174 |
||
175 |
if (currentDomains == null || currentDomains.length == 0) { |
|
176 |
// No need to optimize assignedDomains because it should |
|
177 |
// have been previously optimized (when it was set). |
|
178 |
||
179 |
// Note that we are returning a direct reference |
|
180 |
// to the input array - since ACC does not clone |
|
181 |
// the arrays when it calls combiner.combine, |
|
182 |
// multiple ACC instances may share the same |
|
183 |
// array instance in this case |
|
184 |
||
185 |
return assignedDomains; |
|
186 |
} |
|
187 |
||
188 |
// optimize currentDomains |
|
189 |
// |
|
190 |
// No need to optimize assignedDomains because it should |
|
191 |
// have been previously optimized (when it was set). |
|
192 |
||
193 |
currentDomains = optimize(currentDomains); |
|
194 |
if (debug != null) { |
|
195 |
debug.println("after optimize"); |
|
196 |
printInputDomains(currentDomains, assignedDomains); |
|
197 |
} |
|
198 |
||
199 |
if (currentDomains == null && assignedDomains == null) { |
|
200 |
return null; |
|
201 |
} |
|
202 |
||
7291
9fefa2786251
6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents:
5506
diff
changeset
|
203 |
// maintain backwards compatibility for developers who provide |
9fefa2786251
6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents:
5506
diff
changeset
|
204 |
// their own custom javax.security.auth.Policy implementations |
2 | 205 |
if (useJavaxPolicy) { |
206 |
return combineJavaxPolicy(currentDomains, assignedDomains); |
|
207 |
} |
|
208 |
||
209 |
int cLen = (currentDomains == null ? 0 : currentDomains.length); |
|
210 |
int aLen = (assignedDomains == null ? 0 : assignedDomains.length); |
|
211 |
||
212 |
// the ProtectionDomains for the new AccessControlContext |
|
213 |
// that we will return |
|
214 |
ProtectionDomain[] newDomains = new ProtectionDomain[cLen + aLen]; |
|
215 |
||
216 |
boolean allNew = true; |
|
217 |
synchronized(cachedPDs) { |
|
218 |
if (!subject.isReadOnly() && |
|
219 |
!subject.getPrincipals().equals(principalSet)) { |
|
220 |
||
221 |
// if the Subject was mutated, clear the PD cache |
|
222 |
Set<Principal> newSet = subject.getPrincipals(); |
|
223 |
synchronized(newSet) { |
|
224 |
principalSet = new java.util.HashSet<Principal>(newSet); |
|
225 |
} |
|
226 |
principals = principalSet.toArray |
|
227 |
(new Principal[principalSet.size()]); |
|
228 |
cachedPDs.clear(); |
|
229 |
||
230 |
if (debug != null) { |
|
231 |
debug.println("Subject mutated - clearing cache"); |
|
232 |
} |
|
233 |
} |
|
234 |
||
235 |
ProtectionDomain subjectPd; |
|
236 |
for (int i = 0; i < cLen; i++) { |
|
237 |
ProtectionDomain pd = currentDomains[i]; |
|
238 |
||
239 |
subjectPd = cachedPDs.getValue(pd); |
|
240 |
||
241 |
if (subjectPd == null) { |
|
242 |
subjectPd = new ProtectionDomain(pd.getCodeSource(), |
|
243 |
pd.getPermissions(), |
|
244 |
pd.getClassLoader(), |
|
245 |
principals); |
|
246 |
cachedPDs.putValue(pd, subjectPd); |
|
247 |
} else { |
|
248 |
allNew = false; |
|
249 |
} |
|
250 |
newDomains[i] = subjectPd; |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
if (debug != null) { |
|
255 |
debug.println("updated current: "); |
|
256 |
for (int i = 0; i < cLen; i++) { |
|
257 |
debug.println("\tupdated[" + i + "] = " + |
|
258 |
printDomain(newDomains[i])); |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
// now add on the assigned domains |
|
263 |
if (aLen > 0) { |
|
264 |
System.arraycopy(assignedDomains, 0, newDomains, cLen, aLen); |
|
265 |
||
266 |
// optimize the result (cached PDs might exist in assignedDomains) |
|
267 |
if (!allNew) { |
|
268 |
newDomains = optimize(newDomains); |
|
269 |
} |
|
270 |
} |
|
271 |
||
272 |
// if aLen == 0 || allNew, no need to further optimize newDomains |
|
273 |
||
274 |
if (debug != null) { |
|
275 |
if (newDomains == null || newDomains.length == 0) { |
|
276 |
debug.println("returning null"); |
|
277 |
} else { |
|
278 |
debug.println("combinedDomains: "); |
|
279 |
for (int i = 0; i < newDomains.length; i++) { |
|
280 |
debug.println("newDomain " + i + ": " + |
|
281 |
printDomain(newDomains[i])); |
|
282 |
} |
|
283 |
} |
|
284 |
} |
|
285 |
||
286 |
// return the new ProtectionDomains |
|
287 |
if (newDomains == null || newDomains.length == 0) { |
|
288 |
return null; |
|
289 |
} else { |
|
290 |
return newDomains; |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
/** |
|
295 |
* Use the javax.security.auth.Policy implementation |
|
296 |
*/ |
|
297 |
private ProtectionDomain[] combineJavaxPolicy( |
|
298 |
ProtectionDomain[] currentDomains, |
|
299 |
ProtectionDomain[] assignedDomains) { |
|
300 |
||
301 |
if (!allowCaching) { |
|
302 |
java.security.AccessController.doPrivileged |
|
303 |
(new PrivilegedAction<Void>() { |
|
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
9035
diff
changeset
|
304 |
@SuppressWarnings("deprecation") |
2 | 305 |
public Void run() { |
306 |
// Call refresh only caching is disallowed |
|
307 |
javax.security.auth.Policy.getPolicy().refresh(); |
|
308 |
return null; |
|
309 |
} |
|
310 |
}); |
|
311 |
} |
|
312 |
||
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
9035
diff
changeset
|
313 |
|
2 | 314 |
int cLen = (currentDomains == null ? 0 : currentDomains.length); |
315 |
int aLen = (assignedDomains == null ? 0 : assignedDomains.length); |
|
316 |
||
317 |
// the ProtectionDomains for the new AccessControlContext |
|
318 |
// that we will return |
|
319 |
ProtectionDomain[] newDomains = new ProtectionDomain[cLen + aLen]; |
|
320 |
||
321 |
synchronized(cachedPDs) { |
|
322 |
if (!subject.isReadOnly() && |
|
323 |
!subject.getPrincipals().equals(principalSet)) { |
|
324 |
||
325 |
// if the Subject was mutated, clear the PD cache |
|
326 |
Set<Principal> newSet = subject.getPrincipals(); |
|
327 |
synchronized(newSet) { |
|
328 |
principalSet = new java.util.HashSet<Principal>(newSet); |
|
329 |
} |
|
330 |
principals = principalSet.toArray |
|
331 |
(new Principal[principalSet.size()]); |
|
332 |
cachedPDs.clear(); |
|
333 |
||
334 |
if (debug != null) { |
|
335 |
debug.println("Subject mutated - clearing cache"); |
|
336 |
} |
|
337 |
} |
|
338 |
||
339 |
for (int i = 0; i < cLen; i++) { |
|
340 |
ProtectionDomain pd = currentDomains[i]; |
|
341 |
ProtectionDomain subjectPd = cachedPDs.getValue(pd); |
|
342 |
||
343 |
if (subjectPd == null) { |
|
344 |
||
345 |
// XXX |
|
346 |
// we must first add the original permissions. |
|
347 |
// that way when we later add the new JAAS permissions, |
|
348 |
// any unresolved JAAS-related permissions will |
|
349 |
// automatically get resolved. |
|
350 |
||
351 |
// get the original perms |
|
352 |
Permissions perms = new Permissions(); |
|
353 |
PermissionCollection coll = pd.getPermissions(); |
|
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
9035
diff
changeset
|
354 |
java.util.Enumeration<Permission> e; |
2 | 355 |
if (coll != null) { |
356 |
synchronized (coll) { |
|
357 |
e = coll.elements(); |
|
358 |
while (e.hasMoreElements()) { |
|
359 |
Permission newPerm = |
|
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
9035
diff
changeset
|
360 |
e.nextElement(); |
2 | 361 |
perms.add(newPerm); |
362 |
} |
|
363 |
} |
|
364 |
} |
|
365 |
||
366 |
// get perms from the policy |
|
367 |
||
368 |
final java.security.CodeSource finalCs = pd.getCodeSource(); |
|
369 |
final Subject finalS = subject; |
|
370 |
PermissionCollection newPerms = |
|
371 |
java.security.AccessController.doPrivileged |
|
372 |
(new PrivilegedAction<PermissionCollection>() { |
|
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
9035
diff
changeset
|
373 |
@SuppressWarnings("deprecation") |
2 | 374 |
public PermissionCollection run() { |
375 |
return |
|
376 |
javax.security.auth.Policy.getPolicy().getPermissions |
|
377 |
(finalS, finalCs); |
|
378 |
} |
|
379 |
}); |
|
380 |
||
381 |
// add the newly granted perms, |
|
382 |
// avoiding duplicates |
|
383 |
synchronized (newPerms) { |
|
384 |
e = newPerms.elements(); |
|
385 |
while (e.hasMoreElements()) { |
|
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
9035
diff
changeset
|
386 |
Permission newPerm = e.nextElement(); |
2 | 387 |
if (!perms.implies(newPerm)) { |
388 |
perms.add(newPerm); |
|
389 |
if (debug != null) |
|
390 |
debug.println ( |
|
391 |
"Adding perm " + newPerm + "\n"); |
|
392 |
} |
|
393 |
} |
|
394 |
} |
|
395 |
subjectPd = new ProtectionDomain |
|
396 |
(finalCs, perms, pd.getClassLoader(), principals); |
|
397 |
||
398 |
if (allowCaching) |
|
399 |
cachedPDs.putValue(pd, subjectPd); |
|
400 |
} |
|
401 |
newDomains[i] = subjectPd; |
|
402 |
} |
|
403 |
} |
|
404 |
||
405 |
if (debug != null) { |
|
406 |
debug.println("updated current: "); |
|
407 |
for (int i = 0; i < cLen; i++) { |
|
408 |
debug.println("\tupdated[" + i + "] = " + newDomains[i]); |
|
409 |
} |
|
410 |
} |
|
411 |
||
412 |
// now add on the assigned domains |
|
413 |
if (aLen > 0) { |
|
414 |
System.arraycopy(assignedDomains, 0, newDomains, cLen, aLen); |
|
415 |
} |
|
416 |
||
417 |
if (debug != null) { |
|
418 |
if (newDomains == null || newDomains.length == 0) { |
|
419 |
debug.println("returning null"); |
|
420 |
} else { |
|
421 |
debug.println("combinedDomains: "); |
|
422 |
for (int i = 0; i < newDomains.length; i++) { |
|
423 |
debug.println("newDomain " + i + ": " + |
|
424 |
newDomains[i].toString()); |
|
425 |
} |
|
426 |
} |
|
427 |
} |
|
428 |
||
429 |
// return the new ProtectionDomains |
|
430 |
if (newDomains == null || newDomains.length == 0) { |
|
431 |
return null; |
|
432 |
} else { |
|
433 |
return newDomains; |
|
434 |
} |
|
435 |
} |
|
436 |
||
437 |
private static ProtectionDomain[] optimize(ProtectionDomain[] domains) { |
|
438 |
if (domains == null || domains.length == 0) |
|
439 |
return null; |
|
440 |
||
441 |
ProtectionDomain[] optimized = new ProtectionDomain[domains.length]; |
|
442 |
ProtectionDomain pd; |
|
443 |
int num = 0; |
|
444 |
for (int i = 0; i < domains.length; i++) { |
|
445 |
||
446 |
// skip domains with AllPermission |
|
447 |
// XXX |
|
448 |
// |
|
449 |
// if (domains[i].implies(ALL_PERMISSION)) |
|
450 |
// continue; |
|
451 |
||
452 |
// skip System Domains |
|
453 |
if ((pd = domains[i]) != null) { |
|
454 |
||
455 |
// remove duplicates |
|
456 |
boolean found = false; |
|
457 |
for (int j = 0; j < num && !found; j++) { |
|
458 |
found = (optimized[j] == pd); |
|
459 |
} |
|
460 |
if (!found) { |
|
461 |
optimized[num++] = pd; |
|
462 |
} |
|
463 |
} |
|
464 |
} |
|
465 |
||
466 |
// resize the array if necessary |
|
467 |
if (num > 0 && num < domains.length) { |
|
468 |
ProtectionDomain[] downSize = new ProtectionDomain[num]; |
|
469 |
System.arraycopy(optimized, 0, downSize, 0, downSize.length); |
|
470 |
optimized = downSize; |
|
471 |
} |
|
472 |
||
473 |
return ((num == 0 || optimized.length == 0) ? null : optimized); |
|
474 |
} |
|
475 |
||
476 |
private static boolean cachePolicy() { |
|
477 |
String s = AccessController.doPrivileged |
|
478 |
(new PrivilegedAction<String>() { |
|
479 |
public String run() { |
|
7291
9fefa2786251
6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents:
5506
diff
changeset
|
480 |
return Security.getProperty("cache.auth.policy"); |
2 | 481 |
} |
482 |
}); |
|
483 |
if (s != null) { |
|
484 |
return Boolean.parseBoolean(s); |
|
485 |
} |
|
486 |
||
487 |
// cache by default |
|
488 |
return true; |
|
489 |
} |
|
490 |
||
491 |
private static void printInputDomains(ProtectionDomain[] currentDomains, |
|
492 |
ProtectionDomain[] assignedDomains) { |
|
493 |
if (currentDomains == null || currentDomains.length == 0) { |
|
494 |
debug.println("currentDomains null or 0 length"); |
|
495 |
} else { |
|
496 |
for (int i = 0; currentDomains != null && |
|
497 |
i < currentDomains.length; i++) { |
|
498 |
if (currentDomains[i] == null) { |
|
499 |
debug.println("currentDomain " + i + ": SystemDomain"); |
|
500 |
} else { |
|
501 |
debug.println("currentDomain " + i + ": " + |
|
502 |
printDomain(currentDomains[i])); |
|
503 |
} |
|
504 |
} |
|
505 |
} |
|
506 |
||
507 |
if (assignedDomains == null || assignedDomains.length == 0) { |
|
508 |
debug.println("assignedDomains null or 0 length"); |
|
509 |
} else { |
|
510 |
debug.println("assignedDomains = "); |
|
511 |
for (int i = 0; assignedDomains != null && |
|
512 |
i < assignedDomains.length; i++) { |
|
513 |
if (assignedDomains[i] == null) { |
|
514 |
debug.println("assignedDomain " + i + ": SystemDomain"); |
|
515 |
} else { |
|
516 |
debug.println("assignedDomain " + i + ": " + |
|
517 |
printDomain(assignedDomains[i])); |
|
518 |
} |
|
519 |
} |
|
520 |
} |
|
521 |
} |
|
522 |
||
523 |
private static String printDomain(final ProtectionDomain pd) { |
|
524 |
if (pd == null) { |
|
525 |
return "null"; |
|
526 |
} |
|
527 |
return AccessController.doPrivileged(new PrivilegedAction<String>() { |
|
528 |
public String run() { |
|
529 |
return pd.toString(); |
|
530 |
} |
|
531 |
}); |
|
532 |
} |
|
533 |
||
534 |
/** |
|
535 |
* A HashMap that has weak keys and values. |
|
536 |
* |
|
537 |
* Key objects in this map are the "current" ProtectionDomain instances |
|
538 |
* received via the combine method. Each "current" PD is mapped to a |
|
539 |
* new PD instance that holds both the contents of the "current" PD, |
|
540 |
* as well as the principals from the Subject associated with this combiner. |
|
541 |
* |
|
542 |
* The newly created "principal-based" PD values must be stored as |
|
543 |
* WeakReferences since they contain strong references to the |
|
544 |
* corresponding key object (the "current" non-principal-based PD), |
|
545 |
* which will prevent the key from being GC'd. Specifically, |
|
546 |
* a "principal-based" PD contains strong references to the CodeSource, |
|
547 |
* signer certs, PermissionCollection and ClassLoader objects |
|
548 |
* in the "current PD". |
|
549 |
*/ |
|
550 |
private static class WeakKeyValueMap<K,V> extends |
|
551 |
WeakHashMap<K,WeakReference<V>> { |
|
552 |
||
553 |
public V getValue(K key) { |
|
554 |
WeakReference<V> wr = super.get(key); |
|
555 |
if (wr != null) { |
|
556 |
return wr.get(); |
|
557 |
} |
|
558 |
return null; |
|
559 |
} |
|
560 |
||
561 |
public V putValue(K key, V value) { |
|
562 |
WeakReference<V> wr = super.put(key, new WeakReference<V>(value)); |
|
563 |
if (wr != null) { |
|
564 |
return wr.get(); |
|
565 |
} |
|
566 |
return null; |
|
567 |
} |
|
568 |
} |
|
569 |
} |