jdk/src/share/classes/com/sun/rmi/rmid/ExecOptionPermission.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 12040 558b0e0d5910
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 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
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @serial exclude
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public final class ExecOptionPermission extends Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * does this permission have a wildcard at the end?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private transient boolean wildcard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * the name without the wildcard on the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private transient String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * UID for serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final long serialVersionUID = 5842294756823092756L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public ExecOptionPermission(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        init(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public ExecOptionPermission(String name, String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Checks if the specified permission is "implied" by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * More specifically, this method returns true if:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <li> <i>p</i>'s class is the same as this object's class, and<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <li> <i>p</i>'s name equals or (in the case of wildcards)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *      is implied by this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *      name. For example, "a.b.*" implies "a.b.c", and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *      "a.b=*" implies "a.b=c"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @return true if the passed permission is equal to or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * implied by this permission, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (!(p instanceof ExecOptionPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        ExecOptionPermission that = (ExecOptionPermission) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (this.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (that.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                // one wildcard can imply another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                return that.name.startsWith(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                // make sure p.name is longer so a.b.* doesn't imply a.b
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                return (that.name.length() > this.name.length()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    that.name.startsWith(this.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (that.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                // a non-wildcard can't imply a wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                return this.name.equals(that.name);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Checks two ExecOptionPermission objects for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Checks that <i>obj</i>'s class is the same as this object's class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * and has the same name as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param obj the object we are testing for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @return true if <i>obj</i> is an ExecOptionPermission, and has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * name as this ExecOptionPermission object, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if ((obj == null) || (obj.getClass() != getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        ExecOptionPermission that = (ExecOptionPermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return this.getName().equals(that.getName());
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * The hash code used is the hash code of the name, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <code>getName().hashCode()</code>, where <code>getName</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * from the Permission superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return this.getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Returns the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public String getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Returns a new PermissionCollection object for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * ExecOptionPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * A ExecOptionPermissionCollection stores a collection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * ExecOptionPermission permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <p>ExecOptionPermission objects must be stored in a manner that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * them to be inserted in any order, but that also enables the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * PermissionCollection <code>implies</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * to be implemented in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return a new PermissionCollection object suitable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * storing ExecOptionPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return new ExecOptionPermissionCollection();
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
     * readObject is called to restore the state of the ExecOptionPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private synchronized void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // init is called to initialize the rest of the values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        init(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Initialize a ExecOptionPermission object. Common to all constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Also called during de-serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private void init(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new NullPointerException("name can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new IllegalArgumentException("name can't be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (name.endsWith(".*") || name.endsWith("=*") || name.equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            wildcard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (name.length() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                this.name = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                this.name = name.substring(0, name.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            this.name = name;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * A ExecOptionPermissionCollection stores a collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * of ExecOptionPermission permissions. ExecOptionPermission objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * must be stored in a manner that allows them to be inserted in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * order, but enable the implies function to evaluate the implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * method in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * A ExecOptionPermissionCollection handles comparing a permission like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * "a.b.c.d.e" * with a Permission such as "a.b.*", or "*".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static class ExecOptionPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        private Hashtable permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        private boolean all_allowed; // true if "*" is in the collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        private static final long serialVersionUID = -1242475729790124375L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         * Create an empty ExecOptionPermissionCollection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        public ExecOptionPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            permissions = new Hashtable(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            all_allowed = 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
         * Adds a permission to the collection. The key for the hash is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         * permission.name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
         * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         *                                       ExecOptionPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * @exception SecurityException - if this ExecOptionPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         *                                object has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        public void add(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (! (permission instanceof ExecOptionPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                                   permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            ExecOptionPermission p = (ExecOptionPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            permissions.put(p.getName(), permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (!all_allowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (p.getName().equals("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    all_allowed = true;
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
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * Check and see if this set of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * @param p the Permission object to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         * the set, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        public boolean implies(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (! (permission instanceof ExecOptionPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            ExecOptionPermission p = (ExecOptionPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // short circuit if the "*" Permission was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (all_allowed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // strategy:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // Check for full match first. Then work our way up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // name looking for matches on a.b.*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            String pname = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            Permission x = (Permission) permissions.get(pname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (x != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                // we have a direct hit!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                return x.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // work our way up the tree...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            int last, offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            offset = pname.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            while ((last = pname.lastIndexOf(".", offset)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                pname = pname.substring(0, last+1) + "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                x = (Permission) permissions.get(pname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    return x.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                offset = last - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            // check for "=*" wildcard match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            pname = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            offset = pname.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            while ((last = pname.lastIndexOf("=", offset)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                pname = pname.substring(0, last+1) + "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                x = (Permission) permissions.get(pname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    return x.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                offset = last - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // we don't have to check for "*" as it was already checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            // at the top (all_allowed), so we just return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * Returns an enumeration of all the ExecOptionPermission objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         * @return an enumeration of all the ExecOptionPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        public Enumeration elements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return permissions.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
}