jdk/src/share/classes/java/security/BasicPermission.java
author mullan
Fri, 25 Apr 2008 08:58:07 -0400
changeset 485 a66666a5898c
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6690169: Specification for BasicPermission.equals() is not consistent Summary: Clarified @return to be consistent with method description Reviewed-by: vinnie
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-2007 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.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The BasicPermission class extends the Permission class, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * can be used as the base class for permissions that want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * follow the same naming convention as BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * The name for a BasicPermission is the name of the given permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * (for example, "exit",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * "setFactory", "print.queueJob", etc). The naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * convention follows the  hierarchical property naming convention.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * An asterisk may appear by itself, or if immediately preceded by a "."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * may appear at the end of the name, to signify a wildcard match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * For example, "*" and "java.*" are valid, while "*java", "a*b",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * and "java*" are not valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * The action string (inherited from Permission) is unused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Thus, BasicPermission is commonly used as the base class for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * "named" permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * (ones that contain a name but no actions list; you either have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * named permission or you don't.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Subclasses may implement actions on top of BasicPermission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @see java.security.PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @see java.lang.SecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author Marianne Mueller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public abstract class BasicPermission extends Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final long serialVersionUID = 6279438298436773498L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // does this permission have a wildcard at the end?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private transient boolean wildcard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // the name without the wildcard on the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private transient String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // is this permission the old-style exitVM permission (pre JDK 1.6)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private transient boolean exitVM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * initialize a BasicPermission object. Common to all constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private void init(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new NullPointerException("name can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        int len = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new IllegalArgumentException("name can't be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        char last = name.charAt(len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // Is wildcard or ends with ".*"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (last == '*' && (len == 1 || name.charAt(len - 2) == '.')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            wildcard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (len == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                path = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                path = name.substring(0, len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (name.equals("exitVM")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                wildcard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                path = "exitVM.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                exitVM = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                path = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Creates a new BasicPermission with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Name is the symbolic name of the permission, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * "setFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * "print.queueJob", or "topLevelWindow", etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param name the name of the BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws NullPointerException if <code>name</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @throws IllegalArgumentException if <code>name</code> is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public BasicPermission(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        init(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Creates a new BasicPermission object with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * The name is the symbolic name of the BasicPermission, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * actions String is currently unused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param name the name of the BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param actions ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @throws NullPointerException if <code>name</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @throws IllegalArgumentException if <code>name</code> is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public BasicPermission(String name, String actions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        init(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Checks if the specified permission is "implied" by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * More specifically, this method returns true if:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <li> <i>p</i>'s class is the same as this object's class, and<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <li> <i>p</i>'s name equals or (in the case of wildcards)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *      is implied by this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *      name. For example, "a.b.*" implies "a.b.c".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return true if the passed permission is equal to or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * implied by this permission, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if ((p == null) || (p.getClass() != getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        BasicPermission that = (BasicPermission) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (this.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (that.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                // one wildcard can imply another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                return that.path.startsWith(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                // make sure ap.path is longer so a.b.* doesn't imply a.b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                return (that.path.length() > this.path.length()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    that.path.startsWith(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (that.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                // a non-wildcard can't imply a wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                return this.path.equals(that.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Checks two BasicPermission objects for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Checks that <i>obj</i>'s class is the same as this object's class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * and has the same name as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param obj the object we are testing for equality with this object.
485
a66666a5898c 6690169: Specification for BasicPermission.equals() is not consistent
mullan
parents: 2
diff changeset
   207
     * @return true if <i>obj</i>'s class is the same as this object's class
a66666a5898c 6690169: Specification for BasicPermission.equals() is not consistent
mullan
parents: 2
diff changeset
   208
     *  and has the same name as this BasicPermission object, false otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if ((obj == null) || (obj.getClass() != getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        BasicPermission bp = (BasicPermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return getName().equals(bp.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * The hash code used is the hash code of the name, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <code>getName().hashCode()</code>, where <code>getName</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * from the Permission superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return this.getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Returns the canonical string representation of the actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * which currently is the empty string "", since there are no actions for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * a BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return the empty string "".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public String getActions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Returns a new PermissionCollection object for storing BasicPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <p>BasicPermission objects must be stored in a manner that allows them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * to be inserted in any order, but that also enables the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * PermissionCollection <code>implies</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * to be implemented in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @return a new PermissionCollection object suitable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * storing BasicPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return new BasicPermissionCollection(this.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * readObject is called to restore the state of the BasicPermission from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // init is called to initialize the rest of the values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        init(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Returns the canonical name of this BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * All internal invocations of getName should invoke this method, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * that the pre-JDK 1.6 "exitVM" and current "exitVM.*" permission are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * equivalent in equals/hashCode methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @return the canonical name of this BasicPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    final String getCanonicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return exitVM ? "exitVM.*" : getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 * A BasicPermissionCollection stores a collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 * of BasicPermission permissions. BasicPermission objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 * must be stored in a manner that allows them to be inserted in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 * order, but enable the implies function to evaluate the implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 * method in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 * A BasicPermissionCollection handles comparing a permission like "a.b.c.d.e"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 * with a Permission such as "a.b.*", or "*".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * @see java.security.PermissionsImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
final class BasicPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private static final long serialVersionUID = 739301742472979399L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
      * Key is name, value is permission. All permission objects in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
      * collection must be of the same type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
      * Not serialized; see serialization section at end of class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private transient Map<String, Permission> perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * This is set to <code>true</code> if this BasicPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * contains a BasicPermission with '*' as its permission name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @see #serialPersistentFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private boolean all_allowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * The class to which all BasicPermissions in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * BasicPermissionCollection belongs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @see #serialPersistentFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    private Class permClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Create an empty BasicPermissionCollection object.
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
    public BasicPermissionCollection(Class clazz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        perms = new HashMap<String, Permission>(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        all_allowed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        permClass = clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Adds a permission to the BasicPermissions. The key for the hash is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * permission.path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *                                       BasicPermission, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *                                       the permission is not of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *                                       same Class as the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *                                       permissions in this collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @exception SecurityException - if this BasicPermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *                                has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public void add(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (! (permission instanceof BasicPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        BasicPermission bp = (BasicPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // make sure we only add new BasicPermissions of the same class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        // Also check null for compatibility with deserialized form from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        // previous versions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (permClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            // adding first permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            permClass = bp.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            if (bp.getClass() != permClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                throw new IllegalArgumentException("invalid permission: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                                permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            perms.put(bp.getCanonicalName(), permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // No sync on all_allowed; staleness OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (!all_allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (bp.getCanonicalName().equals("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                all_allowed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Check and see if this set of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @param p the Permission object to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * the set, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public boolean implies(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (! (permission instanceof BasicPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        BasicPermission bp = (BasicPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // random subclasses of BasicPermission do not imply each other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (bp.getClass() != permClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        // short circuit if the "*" Permission was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (all_allowed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // strategy:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        // Check for full match first. Then work our way up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // path looking for matches on a.b..*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        String path = bp.getCanonicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        //System.out.println("check "+path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        Permission x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            x = perms.get(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            // we have a direct hit!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            return x.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        // work our way up the tree...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        int last, offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        offset = path.length()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        while ((last = path.lastIndexOf(".", offset)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            path = path.substring(0, last+1) + "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            //System.out.println("check "+path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                x = perms.get(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                return x.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            offset = last -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // we don't have to check for "*" as it was already checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // at the top (all_allowed), so we just return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Returns an enumeration of all the BasicPermission objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @return an enumeration of all the BasicPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public Enumeration<Permission> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // Convert Iterator of Map values into an Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            return Collections.enumeration(perms.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    // @serial the Hashtable is indexed by the BasicPermission name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    // private Hashtable permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @serialField permissions java.util.Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *    The BasicPermissions in this BasicPermissionCollection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *    All BasicPermissions in the collection must belong to the same class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *    The Hashtable is indexed by the BasicPermission name; the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *    of the Hashtable entry is the permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @serialField all_allowed boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *   This is set to <code>true</code> if this BasicPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *   contains a BasicPermission with '*' as its permission name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @serialField permClass java.lang.Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *   The class to which all BasicPermissions in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *   BasicPermissionCollection belongs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        new ObjectStreamField("permissions", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        new ObjectStreamField("all_allowed", Boolean.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        new ObjectStreamField("permClass", Class.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @serialData Default fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Writes the contents of the perms field out as a Hashtable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * serialization compatibility with earlier releases. all_allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * and permClass unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        // Copy perms into a Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        Hashtable<String, Permission> permissions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                new Hashtable<String, Permission>(perms.size()*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            permissions.putAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        // Write out serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        pfields.put("all_allowed", all_allowed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        pfields.put("permClass", permClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * readObject is called to restore the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * BasicPermissionCollection from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    private void readObject(java.io.ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        // Get permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        Hashtable<String, Permission> permissions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                (Hashtable<String, Permission>)gfields.get("permissions", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        perms = new HashMap<String, Permission>(permissions.size()*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        perms.putAll(permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        // Get all_allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        all_allowed = gfields.get("all_allowed", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        // Get permClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        permClass = (Class) gfields.get("permClass", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (permClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            // set permClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            Enumeration<Permission> e = permissions.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                Permission p = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                permClass = p.getClass();
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
}