jdk/src/share/classes/java/security/ProtectionDomain.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5183 7825d97c4cf2
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This ProtectionDomain class encapsulates the characteristics of a domain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * which encloses a set of classes whose instances are granted a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * of permissions when being executed on behalf of a given set of Principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A static set of permissions can be bound to a ProtectionDomain when it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * constructed; such permissions are granted to the domain regardless of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Policy in force. However, to support dynamic security policies, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * ProtectionDomain can also be constructed such that it is dynamically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * mapped to a set of permissions by the current Policy whenever a permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Gary Ellison
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class ProtectionDomain {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /* CodeSource */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private CodeSource codesource ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /* ClassLoader the protection domain was consed from */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private ClassLoader classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* Principals running-as within this protection domain */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Principal[] principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* the rights this protection domain is granted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private PermissionCollection permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /* if the permissions object has AllPermission */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private boolean hasAllPerm = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* the PermissionCollection is static (pre 1.4 constructor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
       or dynamic (via a policy refresh) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private boolean staticPermissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final Debug debug = Debug.getInstance("domain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Creates a new ProtectionDomain with the given CodeSource and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Permissions. If the permissions object is not null, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *  <code>setReadOnly())</code> will be called on the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Permissions object. The only permissions granted to this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * are the ones specified; the current Policy will not be consulted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param codesource the codesource associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param permissions the permissions granted to this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public ProtectionDomain(CodeSource codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                            PermissionCollection permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.codesource = codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            this.permissions = permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            this.permissions.setReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (permissions instanceof Permissions &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                ((Permissions)permissions).allPermission != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                hasAllPerm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.classloader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.principals = new Principal[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        staticPermissions = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Creates a new ProtectionDomain qualified by the given CodeSource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Permissions, ClassLoader and array of Principals. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * permissions object is not null, then <code>setReadOnly()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * will be called on the passed in Permissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The permissions granted to this domain are dynamic; they include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * both the static permissions passed to this constructor, and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * permissions granted to this domain by the current Policy at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * time a permission is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * This constructor is typically used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * {@link SecureClassLoader ClassLoaders}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * and {@link DomainCombiner DomainCombiners} which delegate to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <code>Policy</code> to actively associate the permissions granted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * this domain. This constructor affords the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Policy provider the opportunity to augment the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * PermissionCollection to reflect policy changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param codesource the CodeSource associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param permissions the permissions granted to this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param classloader the ClassLoader associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param principals the array of Principals associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * domain. The contents of the array are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @see Policy#refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @see Policy#getPermissions(ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public ProtectionDomain(CodeSource codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                            PermissionCollection permissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                            ClassLoader classloader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                            Principal[] principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.codesource = codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            this.permissions = permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            this.permissions.setReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (permissions instanceof Permissions &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                ((Permissions)permissions).allPermission != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                hasAllPerm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.classloader = classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.principals = (principals != null ? principals.clone():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                           new Principal[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        staticPermissions = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Returns the CodeSource of this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return the CodeSource of this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public final CodeSource getCodeSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return this.codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Returns the ClassLoader of this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @return the ClassLoader of this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public final ClassLoader getClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return this.classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Returns an array of principals for this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return a non-null array of principals for this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Returns a new array each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public final Principal[] getPrincipals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return this.principals.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns the static permissions granted to this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return the static set of permissions for this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @see Policy#refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @see Policy#getPermissions(ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public final PermissionCollection getPermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Check and see if this ProtectionDomain implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * expressed in the Permission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * The set of permissions evaluated is a function of whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * ProtectionDomain was constructed with a static set of permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * or it was bound to a dynamically mapped set of permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * If the ProtectionDomain was constructed to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * {@link #ProtectionDomain(CodeSource, PermissionCollection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * statically bound} PermissionCollection then the permission will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * only be checked against the PermissionCollection supplied at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * However, if the ProtectionDomain was constructed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * the constructor variant which supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * {@link #ProtectionDomain(CodeSource, PermissionCollection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * ClassLoader, java.security.Principal[]) dynamically binding}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * permissions, then the permission will be checked against the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * combination of the PermissionCollection supplied at construction and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * the current Policy binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param permission the Permission object to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return true if "permission" is implicit to this ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (hasAllPerm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // internal permission collection already has AllPermission -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // no need to go to policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (!staticPermissions &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            Policy.getPolicyNoCheck().implies(this, permission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (permissions != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return permissions.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Convert a ProtectionDomain to a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        String pals = "<no principals>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (principals != null && principals.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            StringBuilder palBuf = new StringBuilder("(principals ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            for (int i = 0; i < principals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                palBuf.append(principals[i].getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            " \"" + principals[i].getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                            "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if (i < principals.length-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    palBuf.append(",\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    palBuf.append(")\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            pals = palBuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // Check if policy is set; we don't want to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // the policy prematurely here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        PermissionCollection pc = Policy.isSet() && seeAllp() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                      mergePermissions():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                      getPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return "ProtectionDomain "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            " "+codesource+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            " "+classloader+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            " "+pals+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            " "+pc+"\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Return true (merge policy permissions) in the following cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * . SecurityManager is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * . SecurityManager is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *          debug is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *          SecurityManager impelmentation is in bootclasspath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *          Policy implementation is in bootclasspath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *          (the bootclasspath restrictions avoid recursion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * . SecurityManager is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *          debug is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *          caller has Policy.getPolicy permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private static boolean seeAllp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                if (sm.getClass().getClassLoader() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    Policy.getPolicyNoCheck().getClass().getClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                                                == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    sm.checkPermission(SecurityConstants.GET_POLICY_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    // fall thru and return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private PermissionCollection mergePermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (staticPermissions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        PermissionCollection perms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            (new java.security.PrivilegedAction<PermissionCollection>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    public PermissionCollection run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        Policy p = Policy.getPolicyNoCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        return p.getPermissions(ProtectionDomain.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        Permissions mergedPerms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int swag = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int vcap = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        Enumeration<Permission> e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        List<Permission> pdVector = new ArrayList<Permission>(vcap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        List<Permission> plVector = new ArrayList<Permission>(swag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // Build a vector of domain permissions for subsequent merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            synchronized (permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                e = permissions.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    pdVector.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // Build a vector of Policy permissions for subsequent merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (perms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            synchronized (perms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                e = perms.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    plVector.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    vcap++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (perms != null && permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            // Weed out the duplicates from the policy. Unless a refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            // has occured since the pd was consed this should result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            // an empty vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            synchronized (permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                e = permissions.elements();   // domain vs policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    Permission pdp = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    Class pdpClass = pdp.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    String pdpActions = pdp.getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    String pdpName = pdp.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    for (int i = 0; i < plVector.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        Permission pp = plVector.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        if (pdpClass.isInstance(pp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                            // The equals() method on some permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                            // have some side effects so this manual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                            // comparison is sufficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                            if (pdpName.equals(pp.getName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                pdpActions.equals(pp.getActions())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                plVector.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                }
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
        if (perms !=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            // the order of adding to merged perms and permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            // needs to preserve the bugfix 4301064
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            for (int i = plVector.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                mergedPerms.add(plVector.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            for (int i = pdVector.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                mergedPerms.add(pdVector.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return mergedPerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
}