|
31 | 31 | import java.security.SecurityPermission; |
32 | 32 | import java.security.cert.Certificate; |
33 | 33 | import java.util.ArrayList; |
| 34 | +import java.util.Collections; |
34 | 35 | import java.util.Enumeration; |
35 | 36 | import java.util.List; |
| 37 | +import java.util.Map; |
36 | 38 | import java.util.Optional; |
37 | 39 | import java.util.PropertyPermission; |
38 | 40 | import java.util.Set; |
| 41 | +import java.util.concurrent.ConcurrentHashMap; |
| 42 | +import java.util.function.Supplier; |
39 | 43 |
|
40 | 44 | @SuppressWarnings("removal") |
41 | 45 | public class PolicyFile extends java.security.Policy { |
@@ -180,7 +184,11 @@ public void refresh() { |
180 | 184 |
|
181 | 185 | @Override |
182 | 186 | public boolean implies(ProtectionDomain pd, Permission p) { |
183 | | - PermissionCollection pc = getPermissions(pd); |
| 187 | + if (pd == null || p == null) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + PermissionCollection pc = policyInfo.getOrCompute(pd, () -> getPermissions(pd)); |
184 | 192 | return pc != null && pc.implies(p); |
185 | 193 | } |
186 | 194 |
|
@@ -308,10 +316,17 @@ public String toString() { |
308 | 316 |
|
309 | 317 | private static class PolicyInfo { |
310 | 318 | final List<PolicyEntry> policyEntries; |
| 319 | + private final Map<ProtectionDomain, PermissionCollection> pdMapping; |
311 | 320 |
|
312 | 321 | PolicyInfo() { |
313 | | - policyEntries = new ArrayList<>(); |
| 322 | + policyEntries = Collections.synchronizedList(new ArrayList<PolicyEntry>()); |
| 323 | + pdMapping = new ConcurrentHashMap<>(); |
314 | 324 | } |
| 325 | + |
| 326 | + public PermissionCollection getOrCompute(ProtectionDomain pd, Supplier<PermissionCollection> computeFn) { |
| 327 | + return pdMapping.computeIfAbsent(pd, k -> computeFn.get()); |
| 328 | + } |
| 329 | + |
315 | 330 | } |
316 | 331 |
|
317 | 332 | private static URL newURL(String spec) throws MalformedURLException, URISyntaxException { |
|
0 commit comments