src/java.rmi/share/classes/com/sun/rmi/rmid/ExecOptionPermission.java
author jlaskey
Thu, 14 Nov 2019 12:39:49 -0400
branchJDK-8193209-branch
changeset 59086 214afc7a1e02
parent 52902 e3398b2e1ab0
permissions -rw-r--r--
[mq]: refresh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12040
diff changeset
     2
 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.rmi.rmid;
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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The ExecOptionPermission class represents permission for rmid to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a specific command-line option when launching an activation group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @serial exclude
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public final class ExecOptionPermission extends Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * does this permission have a wildcard at the end?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private transient boolean wildcard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * the name without the wildcard on the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private transient String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * UID for serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final long serialVersionUID = 5842294756823092756L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public ExecOptionPermission(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        init(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public ExecOptionPermission(String name, String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Checks if the specified permission is "implied" by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * <P>
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    70
     * More specifically, this method returns true if:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <ul>
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    72
     * <li> <i>p</i>'s class is the same as this object's class, and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <li> <i>p</i>'s name equals or (in the case of wildcards)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *      is implied by this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *      name. For example, "a.b.*" implies "a.b.c", and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *      "a.b=*" implies "a.b=c"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @return true if the passed permission is equal to or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * implied by this permission, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (!(p instanceof ExecOptionPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        ExecOptionPermission that = (ExecOptionPermission) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (this.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (that.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                // one wildcard can imply another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                return that.name.startsWith(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                // make sure p.name is longer so a.b.* doesn't imply a.b
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                return (that.name.length() > this.name.length()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    that.name.startsWith(this.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (that.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                // a non-wildcard can't imply a wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                return this.name.equals(that.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Checks two ExecOptionPermission objects for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Checks that <i>obj</i>'s class is the same as this object's class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * and has the same name as this object.
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   113
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param obj the object we are testing for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return true if <i>obj</i> is an ExecOptionPermission, and has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * name as this ExecOptionPermission object, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if ((obj == null) || (obj.getClass() != getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        ExecOptionPermission that = (ExecOptionPermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return this.getName().equals(that.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * The hash code used is the hash code of the name, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <code>getName().hashCode()</code>, where <code>getName</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * from the Permission superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return this.getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Returns the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public String getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Returns a new PermissionCollection object for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * ExecOptionPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <p>
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   156
     * An ExecOptionPermissionCollection stores a collection of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * ExecOptionPermission permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <p>ExecOptionPermission objects must be stored in a manner that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * them to be inserted in any order, but that also enables the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * PermissionCollection <code>implies</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * to be implemented in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return a new PermissionCollection object suitable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * storing ExecOptionPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return new ExecOptionPermissionCollection();
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
     * readObject is called to restore the state of the ExecOptionPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private synchronized void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // init is called to initialize the rest of the values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        init(getName());
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
     * Initialize a ExecOptionPermission object. Common to all constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Also called during de-serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private void init(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new NullPointerException("name can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   192
        if (name.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new IllegalArgumentException("name can't be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (name.endsWith(".*") || name.endsWith("=*") || name.equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            wildcard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (name.length() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                this.name = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                this.name = name.substring(0, name.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * A ExecOptionPermissionCollection stores a collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * of ExecOptionPermission permissions. ExecOptionPermission objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * must be stored in a manner that allows them to be inserted in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * order, but enable the implies function to evaluate the implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * method in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * A ExecOptionPermissionCollection handles comparing a permission like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * "a.b.c.d.e" * with a Permission such as "a.b.*", or "*".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private static class ExecOptionPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   225
        private Hashtable<String, Permission> permissions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        private boolean all_allowed; // true if "*" is in the collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        private static final long serialVersionUID = -1242475729790124375L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
         * Create an empty ExecOptionPermissionCollection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        public ExecOptionPermissionCollection() {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   233
            permissions = new Hashtable<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            all_allowed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * Adds a permission to the collection. The key for the hash is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         * permission.name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         *                                       ExecOptionPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * @exception SecurityException - if this ExecOptionPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         *                                object has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        public void add(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (! (permission instanceof ExecOptionPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                                   permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            ExecOptionPermission p = (ExecOptionPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            permissions.put(p.getName(), permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (!all_allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                if (p.getName().equals("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    all_allowed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         * Check and see if this set of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * @param p the Permission object to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         * the set, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        public boolean implies(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (! (permission instanceof ExecOptionPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            ExecOptionPermission p = (ExecOptionPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // short circuit if the "*" Permission was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (all_allowed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // strategy:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // Check for full match first. Then work our way up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // name looking for matches on a.b.*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            String pname = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   293
            Permission x = permissions.get(pname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (x != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                // we have a direct hit!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                return x.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // work our way up the tree...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            int last, offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            offset = pname.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 14342
diff changeset
   305
            while ((last = pname.lastIndexOf('.', offset)) != -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                pname = pname.substring(0, last+1) + "*";
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   308
                x = permissions.get(pname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    return x.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                offset = last - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // check for "=*" wildcard match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            pname = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            offset = pname.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 14342
diff changeset
   320
            while ((last = pname.lastIndexOf('=', offset)) != -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                pname = pname.substring(0, last+1) + "*";
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   323
                x = permissions.get(pname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    return x.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                offset = last - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // we don't have to check for "*" as it was already checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // at the top (all_allowed), so we just return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * Returns an enumeration of all the ExecOptionPermission objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         * @return an enumeration of all the ExecOptionPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   343
        public Enumeration<Permission> elements()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            return permissions.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
}