Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/security/Policy.java
41152 views
1
/*
2
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
27
package java.security;
28
29
import java.util.Enumeration;
30
import java.util.WeakHashMap;
31
import java.util.Objects;
32
import sun.security.jca.GetInstance;
33
import sun.security.util.Debug;
34
import sun.security.util.SecurityConstants;
35
36
37
/**
38
* A Policy object is responsible for determining whether code executing
39
* in the Java runtime environment has permission to perform a
40
* security-sensitive operation.
41
*
42
* <p> There is only one Policy object installed in the runtime at any
43
* given time. A Policy object can be installed by calling the
44
* {@code setPolicy} method. The installed Policy object can be
45
* obtained by calling the {@code getPolicy} method.
46
*
47
* <p> If no Policy object has been installed in the runtime, a call to
48
* {@code getPolicy} installs an instance of the default Policy
49
* implementation (a default subclass implementation of this abstract class).
50
* The default Policy implementation can be changed by setting the value
51
* of the {@code policy.provider} security property to the fully qualified
52
* name of the desired Policy subclass implementation. The system class loader
53
* is used to load this class.
54
*
55
* <p> Application code can directly subclass Policy to provide a custom
56
* implementation. In addition, an instance of a Policy object can be
57
* constructed by invoking one of the {@code getInstance} factory methods
58
* with a standard type. The default policy type is "JavaPolicy".
59
*
60
* <p> Once a Policy instance has been installed (either by default, or by
61
* calling {@code setPolicy}), the Java runtime invokes its
62
* {@code implies} method when it needs to
63
* determine whether executing code (encapsulated in a ProtectionDomain)
64
* can perform SecurityManager-protected operations. How a Policy object
65
* retrieves its policy data is up to the Policy implementation itself.
66
* The policy data may be stored, for example, in a flat ASCII file,
67
* in a serialized binary file of the Policy class, or in a database.
68
*
69
* <p> The {@code refresh} method causes the policy object to
70
* refresh/reload its data. This operation is implementation-dependent.
71
* For example, if the policy object stores its data in configuration files,
72
* calling {@code refresh} will cause it to re-read the configuration
73
* policy files. If a refresh operation is not supported, this method does
74
* nothing. Note that refreshed policy may not have an effect on classes
75
* in a particular ProtectionDomain. This is dependent on the Policy
76
* provider's implementation of the {@code implies}
77
* method and its PermissionCollection caching strategy.
78
*
79
* @author Roland Schemers
80
* @author Gary Ellison
81
* @since 1.2
82
* @see java.security.Provider
83
* @see java.security.ProtectionDomain
84
* @see java.security.Permission
85
* @see java.security.Security security properties
86
* @deprecated This class is only useful in conjunction with
87
* {@linkplain SecurityManager the Security Manager}, which is deprecated
88
* and subject to removal in a future release. Consequently, this class
89
* is also deprecated and subject to removal. There is no replacement for
90
* the Security Manager or this class.
91
*/
92
93
@Deprecated(since="17", forRemoval=true)
94
public abstract class Policy {
95
96
/**
97
* Constructor for subclasses to call.
98
*/
99
public Policy() {}
100
101
/**
102
* A read-only empty PermissionCollection instance.
103
* @since 1.6
104
*/
105
public static final PermissionCollection UNSUPPORTED_EMPTY_COLLECTION =
106
new UnsupportedEmptyCollection();
107
108
// Information about the system-wide policy.
109
private static class PolicyInfo {
110
// the system-wide policy
111
final Policy policy;
112
// a flag indicating if the system-wide policy has been initialized
113
final boolean initialized;
114
115
PolicyInfo(Policy policy, boolean initialized) {
116
this.policy = policy;
117
this.initialized = initialized;
118
}
119
}
120
121
// PolicyInfo is volatile since we apply DCL during initialization.
122
// For correctness, care must be taken to read the field only once and only
123
// write to it after any other initialization action has taken place.
124
private static volatile PolicyInfo policyInfo = new PolicyInfo(null, false);
125
126
private static final Debug debug = Debug.getInstance("policy");
127
128
// Default policy provider
129
private static final String DEFAULT_POLICY =
130
"sun.security.provider.PolicyFile";
131
132
// Cache mapping ProtectionDomain.Key to PermissionCollection
133
private WeakHashMap<ProtectionDomain.Key, PermissionCollection> pdMapping;
134
135
/** package private for AccessControlContext and ProtectionDomain */
136
static boolean isSet() {
137
PolicyInfo pi = policyInfo;
138
return pi.policy != null && pi.initialized == true;
139
}
140
141
private static void checkPermission(String type) {
142
@SuppressWarnings("removal")
143
SecurityManager sm = System.getSecurityManager();
144
if (sm != null) {
145
sm.checkPermission(new SecurityPermission("createPolicy." + type));
146
}
147
}
148
149
/**
150
* Returns the installed Policy object. This value should not be cached,
151
* as it may be changed by a call to {@code setPolicy}.
152
* This method first calls
153
* {@code SecurityManager.checkPermission} with a
154
* {@code SecurityPermission("getPolicy")} permission
155
* to ensure it's ok to get the Policy object.
156
*
157
* @return the installed Policy.
158
*
159
* @throws SecurityException
160
* if a security manager exists and its
161
* {@code checkPermission} method doesn't allow
162
* getting the Policy object.
163
*
164
* @see SecurityManager#checkPermission(Permission)
165
* @see #setPolicy(java.security.Policy)
166
*/
167
public static Policy getPolicy()
168
{
169
@SuppressWarnings("removal")
170
SecurityManager sm = System.getSecurityManager();
171
if (sm != null)
172
sm.checkPermission(SecurityConstants.GET_POLICY_PERMISSION);
173
return getPolicyNoCheck();
174
}
175
176
/**
177
* Returns the installed Policy object, skipping the security check.
178
* Used by ProtectionDomain and getPolicy.
179
*
180
* @return the installed Policy.
181
*/
182
static Policy getPolicyNoCheck()
183
{
184
PolicyInfo pi = policyInfo;
185
// Use double-check idiom to avoid locking if system-wide policy is
186
// already initialized
187
if (pi.initialized == false || pi.policy == null) {
188
synchronized (Policy.class) {
189
pi = policyInfo;
190
if (pi.policy == null) {
191
return loadPolicyProvider();
192
}
193
}
194
}
195
return pi.policy;
196
}
197
198
/**
199
* Loads and instantiates a Policy implementation specified by the
200
* policy.provider security property. Note that this method should only
201
* be called by getPolicyNoCheck and from within a synchronized block with
202
* an intrinsic lock on the Policy.class.
203
*/
204
private static Policy loadPolicyProvider() {
205
@SuppressWarnings("removal")
206
String policyProvider =
207
AccessController.doPrivileged(new PrivilegedAction<>() {
208
@Override
209
public String run() {
210
return Security.getProperty("policy.provider");
211
}
212
});
213
214
/*
215
* If policy.provider is not set or is set to the default provider,
216
* simply instantiate it and return.
217
*/
218
if (policyProvider == null || policyProvider.isEmpty() ||
219
policyProvider.equals(DEFAULT_POLICY))
220
{
221
Policy polFile = new sun.security.provider.PolicyFile();
222
policyInfo = new PolicyInfo(polFile, true);
223
return polFile;
224
}
225
226
/*
227
* Locate, load, and instantiate the policy.provider impl using
228
* the system class loader. While doing so, install the bootstrap
229
* provider to avoid potential recursion.
230
*/
231
Policy polFile = new sun.security.provider.PolicyFile();
232
policyInfo = new PolicyInfo(polFile, false);
233
234
@SuppressWarnings("removal")
235
Policy pol = AccessController.doPrivileged(new PrivilegedAction<>() {
236
@Override
237
public Policy run() {
238
try {
239
ClassLoader scl = ClassLoader.getSystemClassLoader();
240
@SuppressWarnings("deprecation")
241
Object o = Class.forName(policyProvider, true, scl).newInstance();
242
return (Policy)o;
243
} catch (Exception e) {
244
if (debug != null) {
245
debug.println("policy provider " + policyProvider +
246
" not available");
247
e.printStackTrace();
248
}
249
return null;
250
}
251
}
252
});
253
254
if (pol == null) {
255
// Fallback and use the system default implementation
256
if (debug != null) {
257
debug.println("using " + DEFAULT_POLICY);
258
}
259
pol = polFile;
260
}
261
policyInfo = new PolicyInfo(pol, true);
262
return pol;
263
}
264
265
/**
266
* Sets the system-wide Policy object. This method first calls
267
* {@code SecurityManager.checkPermission} with a
268
* {@code SecurityPermission("setPolicy")}
269
* permission to ensure it's ok to set the Policy.
270
*
271
* @param p the new system Policy object.
272
*
273
* @throws SecurityException
274
* if a security manager exists and its
275
* {@code checkPermission} method doesn't allow
276
* setting the Policy.
277
*
278
* @see SecurityManager#checkPermission(Permission)
279
* @see #getPolicy()
280
*
281
*/
282
public static void setPolicy(Policy p)
283
{
284
@SuppressWarnings("removal")
285
SecurityManager sm = System.getSecurityManager();
286
if (sm != null) sm.checkPermission(
287
new SecurityPermission("setPolicy"));
288
if (p != null) {
289
initPolicy(p);
290
}
291
synchronized (Policy.class) {
292
policyInfo = new PolicyInfo(p, p != null);
293
}
294
}
295
296
/**
297
* Initialize superclass state such that a legacy provider can
298
* handle queries for itself.
299
*
300
* @since 1.4
301
*/
302
private static void initPolicy (final Policy p) {
303
/*
304
* A policy provider not on the bootclasspath could trigger
305
* security checks fulfilling a call to either Policy.implies
306
* or Policy.getPermissions. If this does occur the provider
307
* must be able to answer for it's own ProtectionDomain
308
* without triggering additional security checks, otherwise
309
* the policy implementation will end up in an infinite
310
* recursion.
311
*
312
* To mitigate this, the provider can collect it's own
313
* ProtectionDomain and associate a PermissionCollection while
314
* it is being installed. The currently installed policy
315
* provider (if there is one) will handle calls to
316
* Policy.implies or Policy.getPermissions during this
317
* process.
318
*
319
* This Policy superclass caches away the ProtectionDomain and
320
* statically binds permissions so that legacy Policy
321
* implementations will continue to function.
322
*/
323
324
@SuppressWarnings("removal")
325
ProtectionDomain policyDomain =
326
AccessController.doPrivileged(new PrivilegedAction<>() {
327
public ProtectionDomain run() {
328
return p.getClass().getProtectionDomain();
329
}
330
});
331
332
/*
333
* Collect the permissions granted to this protection domain
334
* so that the provider can be security checked while processing
335
* calls to Policy.implies or Policy.getPermissions.
336
*/
337
PermissionCollection policyPerms = null;
338
synchronized (p) {
339
if (p.pdMapping == null) {
340
p.pdMapping = new WeakHashMap<>();
341
}
342
}
343
344
if (policyDomain.getCodeSource() != null) {
345
Policy pol = policyInfo.policy;
346
if (pol != null) {
347
policyPerms = pol.getPermissions(policyDomain);
348
}
349
350
if (policyPerms == null) { // assume it has all
351
policyPerms = new Permissions();
352
policyPerms.add(SecurityConstants.ALL_PERMISSION);
353
}
354
355
synchronized (p.pdMapping) {
356
// cache of pd to permissions
357
p.pdMapping.put(policyDomain.key, policyPerms);
358
}
359
}
360
return;
361
}
362
363
364
/**
365
* Returns a Policy object of the specified type.
366
*
367
* <p> This method traverses the list of registered security providers,
368
* starting with the most preferred Provider.
369
* A new Policy object encapsulating the
370
* PolicySpi implementation from the first
371
* Provider that supports the specified type is returned.
372
*
373
* <p> Note that the list of registered providers may be retrieved via
374
* the {@link Security#getProviders() Security.getProviders()} method.
375
*
376
* @implNote
377
* The JDK Reference Implementation additionally uses the
378
* {@code jdk.security.provider.preferred}
379
* {@link Security#getProperty(String) Security} property to determine
380
* the preferred provider order for the specified algorithm. This
381
* may be different than the order of providers returned by
382
* {@link Security#getProviders() Security.getProviders()}.
383
*
384
* @param type the specified Policy type. See the Policy section in the
385
* <a href=
386
* "{@docRoot}/../specs/security/standard-names.html#policy-types">
387
* Java Security Standard Algorithm Names Specification</a>
388
* for a list of standard Policy types.
389
*
390
* @param params parameters for the Policy, which may be null.
391
*
392
* @return the new {@code Policy} object
393
*
394
* @throws IllegalArgumentException if the specified parameters
395
* are not understood by the {@code PolicySpi} implementation
396
* from the selected {@code Provider}
397
*
398
* @throws NoSuchAlgorithmException if no {@code Provider} supports
399
* a {@code PolicySpi} implementation for the specified type
400
*
401
* @throws NullPointerException if {@code type} is {@code null}
402
*
403
* @throws SecurityException if the caller does not have permission
404
* to get a {@code Policy} instance for the specified type.
405
*
406
* @see Provider
407
* @since 1.6
408
*/
409
@SuppressWarnings("removal")
410
public static Policy getInstance(String type, Policy.Parameters params)
411
throws NoSuchAlgorithmException {
412
Objects.requireNonNull(type, "null type name");
413
checkPermission(type);
414
try {
415
GetInstance.Instance instance = GetInstance.getInstance("Policy",
416
PolicySpi.class,
417
type,
418
params);
419
return new PolicyDelegate((PolicySpi)instance.impl,
420
instance.provider,
421
type,
422
params);
423
} catch (NoSuchAlgorithmException nsae) {
424
return handleException(nsae);
425
}
426
}
427
428
/**
429
* Returns a Policy object of the specified type.
430
*
431
* <p> A new Policy object encapsulating the
432
* PolicySpi implementation from the specified provider
433
* is returned. The specified provider must be registered
434
* in the provider list.
435
*
436
* <p> Note that the list of registered providers may be retrieved via
437
* the {@link Security#getProviders() Security.getProviders()} method.
438
*
439
* @param type the specified Policy type. See the Policy section in the
440
* <a href=
441
* "{@docRoot}/../specs/security/standard-names.html#policy-types">
442
* Java Security Standard Algorithm Names Specification</a>
443
* for a list of standard Policy types.
444
*
445
* @param params parameters for the Policy, which may be null.
446
*
447
* @param provider the provider.
448
*
449
* @return the new {@code Policy} object
450
*
451
* @throws IllegalArgumentException if the specified provider
452
* is {@code null} or empty, or if the specified parameters are
453
* not understood by the {@code PolicySpi} implementation from
454
* the specified provider
455
*
456
* @throws NoSuchAlgorithmException if the specified provider does not
457
* support a {@code PolicySpi} implementation for the specified
458
* type
459
*
460
* @throws NoSuchProviderException if the specified provider is not
461
* registered in the security provider list
462
*
463
* @throws NullPointerException if {@code type} is {@code null}
464
*
465
* @throws SecurityException if the caller does not have permission
466
* to get a {@code Policy} instance for the specified type
467
*
468
* @see Provider
469
* @since 1.6
470
*/
471
@SuppressWarnings("removal")
472
public static Policy getInstance(String type,
473
Policy.Parameters params,
474
String provider)
475
throws NoSuchProviderException, NoSuchAlgorithmException {
476
477
Objects.requireNonNull(type, "null type name");
478
if (provider == null || provider.isEmpty()) {
479
throw new IllegalArgumentException("missing provider");
480
}
481
482
checkPermission(type);
483
try {
484
GetInstance.Instance instance = GetInstance.getInstance("Policy",
485
PolicySpi.class,
486
type,
487
params,
488
provider);
489
return new PolicyDelegate((PolicySpi)instance.impl,
490
instance.provider,
491
type,
492
params);
493
} catch (NoSuchAlgorithmException nsae) {
494
return handleException(nsae);
495
}
496
}
497
498
/**
499
* Returns a Policy object of the specified type.
500
*
501
* <p> A new Policy object encapsulating the
502
* PolicySpi implementation from the specified Provider
503
* object is returned. Note that the specified Provider object
504
* does not have to be registered in the provider list.
505
*
506
* @param type the specified Policy type. See the Policy section in the
507
* <a href=
508
* "{@docRoot}/../specs/security/standard-names.html#policy-types">
509
* Java Security Standard Algorithm Names Specification</a>
510
* for a list of standard Policy types.
511
*
512
* @param params parameters for the Policy, which may be null.
513
*
514
* @param provider the Provider.
515
*
516
* @return the new {@code Policy} object
517
*
518
* @throws IllegalArgumentException if the specified {@code Provider}
519
* is {@code null}, or if the specified parameters are not
520
* understood by the {@code PolicySpi} implementation from the
521
* specified {@code Provider}
522
*
523
* @throws NoSuchAlgorithmException if the specified {@code Provider}
524
* does not support a {@code PolicySpi} implementation for
525
* the specified type
526
*
527
* @throws NullPointerException if {@code type} is {@code null}
528
*
529
* @throws SecurityException if the caller does not have permission
530
* to get a {@code Policy} instance for the specified type
531
*
532
* @see Provider
533
* @since 1.6
534
*/
535
@SuppressWarnings("removal")
536
public static Policy getInstance(String type,
537
Policy.Parameters params,
538
Provider provider)
539
throws NoSuchAlgorithmException {
540
541
Objects.requireNonNull(type, "null type name");
542
if (provider == null) {
543
throw new IllegalArgumentException("missing provider");
544
}
545
546
checkPermission(type);
547
try {
548
GetInstance.Instance instance = GetInstance.getInstance("Policy",
549
PolicySpi.class,
550
type,
551
params,
552
provider);
553
return new PolicyDelegate((PolicySpi)instance.impl,
554
instance.provider,
555
type,
556
params);
557
} catch (NoSuchAlgorithmException nsae) {
558
return handleException(nsae);
559
}
560
}
561
562
private static Policy handleException(NoSuchAlgorithmException nsae)
563
throws NoSuchAlgorithmException {
564
Throwable cause = nsae.getCause();
565
if (cause instanceof IllegalArgumentException) {
566
throw (IllegalArgumentException)cause;
567
}
568
throw nsae;
569
}
570
571
/**
572
* Return the Provider of this Policy.
573
*
574
* <p> This Policy instance will only have a Provider if it
575
* was obtained via a call to {@code Policy.getInstance}.
576
* Otherwise this method returns null.
577
*
578
* @return the Provider of this Policy, or null.
579
*
580
* @since 1.6
581
*/
582
public Provider getProvider() {
583
return null;
584
}
585
586
/**
587
* Return the type of this Policy.
588
*
589
* <p> This Policy instance will only have a type if it
590
* was obtained via a call to {@code Policy.getInstance}.
591
* Otherwise this method returns null.
592
*
593
* @return the type of this Policy, or null.
594
*
595
* @since 1.6
596
*/
597
public String getType() {
598
return null;
599
}
600
601
/**
602
* Return Policy parameters.
603
*
604
* <p> This Policy instance will only have parameters if it
605
* was obtained via a call to {@code Policy.getInstance}.
606
* Otherwise this method returns null.
607
*
608
* @return Policy parameters, or null.
609
*
610
* @since 1.6
611
*/
612
public Policy.Parameters getParameters() {
613
return null;
614
}
615
616
/**
617
* Return a PermissionCollection object containing the set of
618
* permissions granted to the specified CodeSource.
619
*
620
* <p> Applications are discouraged from calling this method
621
* since this operation may not be supported by all policy implementations.
622
* Applications should solely rely on the {@code implies} method
623
* to perform policy checks. If an application absolutely must call
624
* a getPermissions method, it should call
625
* {@code getPermissions(ProtectionDomain)}.
626
*
627
* <p> The default implementation of this method returns
628
* Policy.UNSUPPORTED_EMPTY_COLLECTION. This method can be
629
* overridden if the policy implementation can return a set of
630
* permissions granted to a CodeSource.
631
*
632
* @param codesource the CodeSource to which the returned
633
* PermissionCollection has been granted.
634
*
635
* @return a set of permissions granted to the specified CodeSource.
636
* If this operation is supported, the returned
637
* set of permissions must be a new mutable instance
638
* and it must support heterogeneous Permission types.
639
* If this operation is not supported,
640
* Policy.UNSUPPORTED_EMPTY_COLLECTION is returned.
641
*/
642
public PermissionCollection getPermissions(CodeSource codesource) {
643
return Policy.UNSUPPORTED_EMPTY_COLLECTION;
644
}
645
646
/**
647
* Return a PermissionCollection object containing the set of
648
* permissions granted to the specified ProtectionDomain.
649
*
650
* <p> Applications are discouraged from calling this method
651
* since this operation may not be supported by all policy implementations.
652
* Applications should rely on the {@code implies} method
653
* to perform policy checks.
654
*
655
* <p> The default implementation of this method first retrieves
656
* the permissions returned via {@code getPermissions(CodeSource)}
657
* (the CodeSource is taken from the specified ProtectionDomain),
658
* as well as the permissions located inside the specified ProtectionDomain.
659
* All of these permissions are then combined and returned in a new
660
* PermissionCollection object. If {@code getPermissions(CodeSource)}
661
* returns Policy.UNSUPPORTED_EMPTY_COLLECTION, then this method
662
* returns the permissions contained inside the specified ProtectionDomain
663
* in a new PermissionCollection object.
664
*
665
* <p> This method can be overridden if the policy implementation
666
* supports returning a set of permissions granted to a ProtectionDomain.
667
*
668
* @param domain the ProtectionDomain to which the returned
669
* PermissionCollection has been granted.
670
*
671
* @return a set of permissions granted to the specified ProtectionDomain.
672
* If this operation is supported, the returned
673
* set of permissions must be a new mutable instance
674
* and it must support heterogeneous Permission types.
675
* If this operation is not supported,
676
* Policy.UNSUPPORTED_EMPTY_COLLECTION is returned.
677
*
678
* @since 1.4
679
*/
680
public PermissionCollection getPermissions(ProtectionDomain domain) {
681
PermissionCollection pc = null;
682
683
if (domain == null)
684
return new Permissions();
685
686
if (pdMapping == null) {
687
initPolicy(this);
688
}
689
690
synchronized (pdMapping) {
691
pc = pdMapping.get(domain.key);
692
}
693
694
if (pc != null) {
695
Permissions perms = new Permissions();
696
synchronized (pc) {
697
for (Enumeration<Permission> e = pc.elements() ; e.hasMoreElements() ;) {
698
perms.add(e.nextElement());
699
}
700
}
701
return perms;
702
}
703
704
pc = getPermissions(domain.getCodeSource());
705
if (pc == null || pc == UNSUPPORTED_EMPTY_COLLECTION) {
706
pc = new Permissions();
707
}
708
709
addStaticPerms(pc, domain.getPermissions());
710
return pc;
711
}
712
713
/**
714
* add static permissions to provided permission collection
715
*/
716
private void addStaticPerms(PermissionCollection perms,
717
PermissionCollection statics) {
718
if (statics != null) {
719
synchronized (statics) {
720
Enumeration<Permission> e = statics.elements();
721
while (e.hasMoreElements()) {
722
perms.add(e.nextElement());
723
}
724
}
725
}
726
}
727
728
/**
729
* Evaluates the global policy for the permissions granted to
730
* the ProtectionDomain and tests whether the permission is
731
* granted.
732
*
733
* @param domain the ProtectionDomain to test
734
* @param permission the Permission object to be tested for implication.
735
*
736
* @return true if "permission" is a proper subset of a permission
737
* granted to this ProtectionDomain.
738
*
739
* @see java.security.ProtectionDomain
740
* @since 1.4
741
*/
742
public boolean implies(ProtectionDomain domain, Permission permission) {
743
PermissionCollection pc;
744
745
if (pdMapping == null) {
746
initPolicy(this);
747
}
748
749
synchronized (pdMapping) {
750
pc = pdMapping.get(domain.key);
751
}
752
753
if (pc != null) {
754
return pc.implies(permission);
755
}
756
757
pc = getPermissions(domain);
758
if (pc == null) {
759
return false;
760
}
761
762
synchronized (pdMapping) {
763
// cache it
764
pdMapping.put(domain.key, pc);
765
}
766
767
return pc.implies(permission);
768
}
769
770
/**
771
* Refreshes/reloads the policy configuration. The behavior of this method
772
* depends on the implementation. For example, calling {@code refresh}
773
* on a file-based policy will cause the file to be re-read.
774
*
775
* <p> The default implementation of this method does nothing.
776
* This method should be overridden if a refresh operation is supported
777
* by the policy implementation.
778
*/
779
public void refresh() { }
780
781
/**
782
* This subclass is returned by the getInstance calls. All Policy calls
783
* are delegated to the underlying PolicySpi.
784
*/
785
private static class PolicyDelegate extends Policy {
786
787
@SuppressWarnings("removal")
788
private PolicySpi spi;
789
private Provider p;
790
private String type;
791
private Policy.Parameters params;
792
793
private PolicyDelegate(@SuppressWarnings("removal") PolicySpi spi, Provider p,
794
String type, Policy.Parameters params) {
795
this.spi = spi;
796
this.p = p;
797
this.type = type;
798
this.params = params;
799
}
800
801
@Override public String getType() { return type; }
802
803
@Override public Policy.Parameters getParameters() { return params; }
804
805
@Override public Provider getProvider() { return p; }
806
807
@Override
808
public PermissionCollection getPermissions(CodeSource codesource) {
809
return spi.engineGetPermissions(codesource);
810
}
811
@Override
812
public PermissionCollection getPermissions(ProtectionDomain domain) {
813
return spi.engineGetPermissions(domain);
814
}
815
@Override
816
public boolean implies(ProtectionDomain domain, Permission perm) {
817
return spi.engineImplies(domain, perm);
818
}
819
@Override
820
public void refresh() {
821
spi.engineRefresh();
822
}
823
}
824
825
/**
826
* This represents a marker interface for Policy parameters.
827
*
828
* @since 1.6
829
* @deprecated This class is only useful in conjunction with
830
* {@linkplain SecurityManager the Security Manager}, which is
831
* deprecated and subject to removal in a future release.
832
* Consequently, this class is also deprecated and subject to removal.
833
* There is no replacement for the Security Manager or this class.
834
*/
835
@Deprecated(since="17", forRemoval=true)
836
public static interface Parameters { }
837
838
/**
839
* This class represents a read-only empty PermissionCollection object that
840
* is returned from the {@code getPermissions(CodeSource)} and
841
* {@code getPermissions(ProtectionDomain)}
842
* methods in the Policy class when those operations are not
843
* supported by the Policy implementation.
844
*/
845
private static class UnsupportedEmptyCollection
846
extends PermissionCollection {
847
848
@java.io.Serial
849
private static final long serialVersionUID = -8492269157353014774L;
850
851
private Permissions perms;
852
853
/**
854
* Create a read-only empty PermissionCollection object.
855
*/
856
public UnsupportedEmptyCollection() {
857
this.perms = new Permissions();
858
perms.setReadOnly();
859
}
860
861
/**
862
* Adds a permission object to the current collection of permission
863
* objects.
864
*
865
* @param permission the Permission object to add.
866
*
867
* @throws SecurityException if this PermissionCollection object
868
* has been marked readonly
869
*/
870
@Override public void add(Permission permission) {
871
perms.add(permission);
872
}
873
874
/**
875
* Checks to see if the specified permission is implied by the
876
* collection of Permission objects held in this PermissionCollection.
877
*
878
* @param permission the Permission object to compare.
879
*
880
* @return true if "permission" is implied by the permissions in
881
* the collection, false if not.
882
*/
883
@Override public boolean implies(Permission permission) {
884
return perms.implies(permission);
885
}
886
887
/**
888
* Returns an enumeration of all the Permission objects in the
889
* collection.
890
*
891
* @return an enumeration of all the Permissions.
892
*/
893
@Override public Enumeration<Permission> elements() {
894
return perms.elements();
895
}
896
}
897
}
898
899