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