jdk/src/share/classes/java/security/Policy.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8152 94e5966bdf22
child 10336 0bb1999251f8
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8152
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.RuntimePermission;
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    31
import java.lang.reflect.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    36
import java.util.PropertyPermission;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.StringTokenizer;
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    38
import java.util.Vector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.WeakHashMap;
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    40
import sun.security.jca.GetInstance;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * A Policy object is responsible for determining whether code executing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * in the Java runtime environment has permission to perform a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * security-sensitive operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p> There is only one Policy object installed in the runtime at any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * given time.  A Policy object can be installed by calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>setPolicy</code> method.  The installed Policy object can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * obtained by calling the <code>getPolicy</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p> If no Policy object has been installed in the runtime, a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <code>getPolicy</code> installs an instance of the default Policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * implementation (a default subclass implementation of this abstract class).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * The default Policy implementation can be changed by setting the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * of the "policy.provider" security property (in the Java security properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * file) to the fully qualified name of the desired Policy subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * implementation.  The Java security properties file is located in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * file named &lt;JAVA_HOME&gt;/lib/security/java.security.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * &lt;JAVA_HOME&gt; refers to the value of the java.home system property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * and specifies the directory where the JRE is installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p> Application code can directly subclass Policy to provide a custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * implementation.  In addition, an instance of a Policy object can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * constructed by invoking one of the <code>getInstance</code> factory methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * with a standard type.  The default policy type is "JavaPolicy".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p> Once a Policy instance has been installed (either by default, or by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * calling <code>setPolicy</code>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * the Java runtime invokes its <code>implies</code> when it needs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * determine whether executing code (encapsulated in a ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * can perform SecurityManager-protected operations.  How a Policy object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * retrieves its policy data is up to the Policy implementation itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * The policy data may be stored, for example, in a flat ASCII file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * in a serialized binary file of the Policy class, or in a database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p> The <code>refresh</code> method causes the policy object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * refresh/reload its data.  This operation is implementation-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * For example, if the policy object stores its data in configuration files,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * calling <code>refresh</code> will cause it to re-read the configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * policy files.  If a refresh operation is not supported, this method does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * nothing.  Note that refreshed policy may not have an effect on classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * in a particular ProtectionDomain. This is dependent on the Policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * provider's implementation of the <code>implies</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * method and its PermissionCollection caching strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @author Gary Ellison
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @see java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
public abstract class Policy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * A read-only empty PermissionCollection instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static final PermissionCollection UNSUPPORTED_EMPTY_COLLECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        new UnsupportedEmptyCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /** the system-wide policy. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static Policy policy; // package private for AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final Debug debug = Debug.getInstance("policy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   111
    // Cache mapping ProtectionDomain.Key to PermissionCollection
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   112
    private WeakHashMap<ProtectionDomain.Key, PermissionCollection> pdMapping;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /** package private for AccessControlContext */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static boolean isSet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return policy != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static void checkPermission(String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            sm.checkPermission(new SecurityPermission("createPolicy." + type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Returns the installed Policy object. This value should not be cached,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * as it may be changed by a call to <code>setPolicy</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * This method first calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <code>SecurityManager.checkPermission</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <code>SecurityPermission("getPolicy")</code> permission
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   133
     * to ensure it's ok to get the Policy object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @return the installed Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *        <code>checkPermission</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *        getting the Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @see SecurityManager#checkPermission(Permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @see #setPolicy(java.security.Policy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static Policy getPolicy()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            sm.checkPermission(SecurityConstants.GET_POLICY_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return getPolicyNoCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Returns the installed Policy object, skipping the security check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Used by SecureClassLoader and getPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return the installed Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    static synchronized Policy getPolicyNoCheck()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (policy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            String policy_class = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            policy_class = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        return Security.getProperty("policy.provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (policy_class == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                policy_class = "sun.security.provider.PolicyFile";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                policy = (Policy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    Class.forName(policy_class).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                 * The policy_class seems to be an extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                 * so we have to bootstrap loading it via a policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                 * provider that is on the bootclasspath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                 * If it loads then shift gears to using the configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                 * provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                // install the bootstrap provider to avoid recursion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                policy = new sun.security.provider.PolicyFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                final String pc = policy_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                Policy p = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    new PrivilegedAction<Policy>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        public Policy run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                ClassLoader cl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                        ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                // we want the extension loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                ClassLoader extcl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                while (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                    extcl = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                    cl = cl.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                return (extcl != null ? (Policy)Class.forName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                        pc, true, extcl).newInstance() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                    debug.println("policy provider " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                                pc +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                                " not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                 * if it loaded install it as the policy provider. Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                 * continue to use the system default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    policy = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        debug.println("using sun.security.provider.PolicyFile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Sets the system-wide Policy object. This method first calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <code>SecurityManager.checkPermission</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <code>SecurityPermission("setPolicy")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * permission to ensure it's ok to set the Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param p the new system Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *        <code>checkPermission</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *        setting the Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @see SecurityManager#checkPermission(Permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @see #getPolicy()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public static void setPolicy(Policy p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (sm != null) sm.checkPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                 new SecurityPermission("setPolicy"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            initPolicy(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        synchronized (Policy.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Policy.policy = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Initialize superclass state such that a legacy provider can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * handle queries for itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private static void initPolicy (final Policy p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * A policy provider not on the bootclasspath could trigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * security checks fulfilling a call to either Policy.implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * or Policy.getPermissions. If this does occur the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * must be able to answer for it's own ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         * without triggering additional security checks, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         * the policy implementation will end up in an infinite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         * recursion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * To mitigate this, the provider can collect it's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         * ProtectionDomain and associate a PermissionCollection while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         * it is being installed. The currently installed policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         * provider (if there is one) will handle calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         * Policy.implies or Policy.getPermissions during this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         * This Policy superclass caches away the ProtectionDomain and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * statically binds permissions so that legacy Policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * implementations will continue to function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        ProtectionDomain policyDomain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        AccessController.doPrivileged(new PrivilegedAction<ProtectionDomain>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            public ProtectionDomain run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                return p.getClass().getProtectionDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
         * Collect the permissions granted to this protection domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
         * so that the provider can be security checked while processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         * calls to Policy.implies or Policy.getPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        PermissionCollection policyPerms = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        synchronized (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (p.pdMapping == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                p.pdMapping =
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   305
                    new WeakHashMap<ProtectionDomain.Key, PermissionCollection>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (policyDomain.getCodeSource() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (Policy.isSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                policyPerms = policy.getPermissions(policyDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (policyPerms == null) { // assume it has all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                policyPerms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                policyPerms.add(SecurityConstants.ALL_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            synchronized (p.pdMapping) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                // cache of pd to permissions
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   321
                p.pdMapping.put(policyDomain.key, policyPerms);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Returns a Policy object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <p> This method traverses the list of registered security providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * A new Policy object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * PolicySpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Provider that supports the specified type is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   340
     * @param type the specified Policy type.  See the Policy section in the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   341
     *    <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   342
     *    "{@docRoot}/../technotes/guides/security/StandardNames.html#Policy">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   343
     *    Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *    for a list of standard Policy types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param params parameters for the Policy, which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return the new Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *          to get a Policy instance for the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @exception NullPointerException if the specified type is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @exception IllegalArgumentException if the specified parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *          are not understood by the PolicySpi implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *          from the selected Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @exception NoSuchAlgorithmException if no Provider supports a PolicySpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *          implementation for the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public static Policy getInstance(String type, Policy.Parameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        checkPermission(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            GetInstance.Instance instance = GetInstance.getInstance("Policy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                                                        PolicySpi.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                                        type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                                        params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return new PolicyDelegate((PolicySpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                                        instance.provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                                        type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                                        params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return handleException(nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Returns a Policy object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <p> A new Policy object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * PolicySpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * is returned.   The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   394
     * @param type the specified Policy type.  See the Policy section in the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   395
     *    <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   396
     *    "{@docRoot}/../technotes/guides/security/StandardNames.html#Policy">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   397
     *    Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *    for a list of standard Policy types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param params parameters for the Policy, which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @return the new Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *          to get a Policy instance for the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @exception NullPointerException if the specified type is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @exception IllegalArgumentException if the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *          is null or empty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *          or if the specified parameters are not understood by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *          the PolicySpi implementation from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @exception NoSuchAlgorithmException if the specified provider does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *          support a PolicySpi implementation for the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public static Policy getInstance(String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                Policy.Parameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                                String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                throws NoSuchProviderException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (provider == null || provider.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        checkPermission(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            GetInstance.Instance instance = GetInstance.getInstance("Policy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                                        PolicySpi.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                                                        type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                                                        params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                                                        provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            return new PolicyDelegate((PolicySpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                                        instance.provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                                        type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                                                        params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            return handleException (nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Returns a Policy object of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * <p> A new Policy object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * PolicySpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   458
     * @param type the specified Policy type.  See the Policy section in the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   459
     *    <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   460
     *    "{@docRoot}/../technotes/guides/security/StandardNames.html#Policy">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   461
     *    Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *    for a list of standard Policy types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @param params parameters for the Policy, which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param provider the Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @return the new Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *          to get a Policy instance for the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @exception NullPointerException if the specified type is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @exception IllegalArgumentException if the specified Provider is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *          or if the specified parameters are not understood by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *          the PolicySpi implementation from the specified Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @exception NoSuchAlgorithmException if the specified Provider does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *          support a PolicySpi implementation for the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public static Policy getInstance(String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                Policy.Parameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        checkPermission(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            GetInstance.Instance instance = GetInstance.getInstance("Policy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                                        PolicySpi.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                                                        type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                                                        params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                                                        provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            return new PolicyDelegate((PolicySpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                                                        instance.provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                                                        type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                                                        params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            return handleException (nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    private static Policy handleException(NoSuchAlgorithmException nsae)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        Throwable cause = nsae.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (cause instanceof IllegalArgumentException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            throw (IllegalArgumentException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        throw nsae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Return the Provider of this Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * <p> This Policy instance will only have a Provider if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * was obtained via a call to <code>Policy.getInstance</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Otherwise this method returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @return the Provider of this Policy, or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Return the type of this Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * <p> This Policy instance will only have a type if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * was obtained via a call to <code>Policy.getInstance</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Otherwise this method returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @return the type of this Policy, or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Return Policy parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <p> This Policy instance will only have parameters if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * was obtained via a call to <code>Policy.getInstance</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Otherwise this method returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @return Policy parameters, or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public Policy.Parameters getParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Return a PermissionCollection object containing the set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * permissions granted to the specified CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * <p> Applications are discouraged from calling this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * since this operation may not be supported by all policy implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * Applications should solely rely on the <code>implies</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * to perform policy checks.  If an application absolutely must call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * a getPermissions method, it should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <code>getPermissions(ProtectionDomain)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <p> The default implementation of this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Policy.UNSUPPORTED_EMPTY_COLLECTION.  This method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * overridden if the policy implementation can return a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * permissions granted to a CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @param codesource the CodeSource to which the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *          PermissionCollection has been granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @return a set of permissions granted to the specified CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *          If this operation is supported, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *          set of permissions must be a new mutable instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *          and it must support heterogeneous Permission types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *          If this operation is not supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *          Policy.UNSUPPORTED_EMPTY_COLLECTION is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    public PermissionCollection getPermissions(CodeSource codesource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        return Policy.UNSUPPORTED_EMPTY_COLLECTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * Return a PermissionCollection object containing the set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * permissions granted to the specified ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <p> Applications are discouraged from calling this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * since this operation may not be supported by all policy implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * Applications should rely on the <code>implies</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * to perform policy checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <p> The default implementation of this method first retrieves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * the permissions returned via <code>getPermissions(CodeSource)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * (the CodeSource is taken from the specified ProtectionDomain),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * as well as the permissions located inside the specified ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * All of these permissions are then combined and returned in a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * PermissionCollection object.  If <code>getPermissions(CodeSource)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * returns Policy.UNSUPPORTED_EMPTY_COLLECTION, then this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * returns the permissions contained inside the specified ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * in a new PermissionCollection object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * <p> This method can be overridden if the policy implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * supports returning a set of permissions granted to a ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @param domain the ProtectionDomain to which the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *          PermissionCollection has been granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @return a set of permissions granted to the specified ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *          If this operation is supported, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *          set of permissions must be a new mutable instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *          and it must support heterogeneous Permission types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *          If this operation is not supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *          Policy.UNSUPPORTED_EMPTY_COLLECTION is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public PermissionCollection getPermissions(ProtectionDomain domain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        PermissionCollection pc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (domain == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            return new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        if (pdMapping == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            initPolicy(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        synchronized (pdMapping) {
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   639
            pc = pdMapping.get(domain.key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (pc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            Permissions perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            synchronized (pc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                for (Enumeration<Permission> e = pc.elements() ; e.hasMoreElements() ;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    perms.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        pc = getPermissions(domain.getCodeSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        if (pc == null || pc == UNSUPPORTED_EMPTY_COLLECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            pc = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        addStaticPerms(pc, domain.getPermissions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        return pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * add static permissions to provided permission collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    private void addStaticPerms(PermissionCollection perms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                                PermissionCollection statics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (statics != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            synchronized (statics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                Enumeration<Permission> e = statics.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    perms.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Evaluates the global policy for the permissions granted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * the ProtectionDomain and tests whether the permission is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @param domain the ProtectionDomain to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @param permission the Permission object to be tested for implication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @return true if "permission" is a proper subset of a permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * granted to this ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @see java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public boolean implies(ProtectionDomain domain, Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        PermissionCollection pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (pdMapping == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            initPolicy(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        synchronized (pdMapping) {
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   698
            pc = pdMapping.get(domain.key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (pc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            return pc.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        pc = getPermissions(domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (pc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        synchronized (pdMapping) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            // cache it
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   712
            pdMapping.put(domain.key, pc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        return pc.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Refreshes/reloads the policy configuration. The behavior of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * depends on the implementation. For example, calling <code>refresh</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * on a file-based policy will cause the file to be re-read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * <p> The default implementation of this method does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * This method should be overridden if a refresh operation is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * by the policy implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    public void refresh() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * This subclass is returned by the getInstance calls.  All Policy calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * are delegated to the underlying PolicySpi.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    private static class PolicyDelegate extends Policy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        private PolicySpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        private Provider p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        private Policy.Parameters params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        private PolicyDelegate(PolicySpi spi, Provider p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        String type, Policy.Parameters params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            this.p = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            this.params = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   748
        @Override public String getType() { return type; }
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   749
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   750
        @Override public Policy.Parameters getParameters() { return params; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   752
        @Override public Provider getProvider() { return p; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   754
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        public PermissionCollection getPermissions(CodeSource codesource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            return spi.engineGetPermissions(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   758
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        public PermissionCollection getPermissions(ProtectionDomain domain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            return spi.engineGetPermissions(domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        }
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   762
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        public boolean implies(ProtectionDomain domain, Permission perm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            return spi.engineImplies(domain, perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   766
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        public void refresh() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            spi.engineRefresh();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * This represents a marker interface for Policy parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    public static interface Parameters { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * This class represents a read-only empty PermissionCollection object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * is returned from the <code>getPermissions(CodeSource)</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * <code>getPermissions(ProtectionDomain)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * methods in the Policy class when those operations are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * supported by the Policy implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    private static class UnsupportedEmptyCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        extends PermissionCollection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        private Permissions perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
         * Create a read-only empty PermissionCollection object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        public UnsupportedEmptyCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            this.perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            perms.setReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
         * Adds a permission object to the current collection of permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
         * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
         * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
         * @exception SecurityException - if this PermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
         *                                has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
         */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   808
        @Override public void add(Permission permission) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            perms.add(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         * Checks to see if the specified permission is implied by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         * collection of Permission objects held in this PermissionCollection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * @param permission the Permission object to compare.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
         * @return true if "permission" is implied by the  permissions in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
         * the collection, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
         */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   821
        @Override public boolean implies(Permission permission) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            return perms.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         * Returns an enumeration of all the Permission objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         * collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
         * @return an enumeration of all the Permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
         */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   831
        @Override public Enumeration<Permission> elements() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            return perms.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
}