jdk/src/share/classes/javax/management/MBeanServerPermission.java
author yan
Mon, 12 May 2014 14:33:13 +0400
changeset 24368 2b4801b94265
parent 5506 202f599c92aa
permissions -rw-r--r--
8038795: Tidy warnings cleanup for javax.management Reviewed-by: dfuchs Contributed-by: Alexander Stepanov <alexander.v.stepanov@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24368
2b4801b94265 8038795: Tidy warnings cleanup for javax.management
yan
parents: 5506
diff changeset
     2
 * Copyright (c) 2001, 2014, 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 javax.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.BasicPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/** A Permission to perform actions related to MBeanServers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    The <em>name</em> of the permission specifies the operation requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    or granted by the permission.  For a granted permission, it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    <code>*</code> to allow all of the MBeanServer operations specified below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    Otherwise, for a granted or requested permission, it must be one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    <dt>createMBeanServer</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    <dd>Create a new MBeanServer object using the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    {@link MBeanServerFactory#createMBeanServer()} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    {@link MBeanServerFactory#createMBeanServer(java.lang.String)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    <dt>findMBeanServer</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    <dd>Find an MBeanServer with a given name, or all MBeanServers in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    JVM, using the method {@link MBeanServerFactory#findMBeanServer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    <dt>newMBeanServer</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    <dd>Create a new MBeanServer object without keeping a reference to it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    using the method {@link MBeanServerFactory#newMBeanServer()} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    {@link MBeanServerFactory#newMBeanServer(java.lang.String)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    <dt>releaseMBeanServer</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    <dd>Remove the MBeanServerFactory's reference to an MBeanServer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    using the method {@link MBeanServerFactory#releaseMBeanServer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    The <em>name</em> of the permission can also denote a list of one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    comma-separated operations.  Spaces are allowed at the beginning and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    end of the <em>name</em> and before and after commas.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    <code>MBeanServerPermission("createMBeanServer")</code> implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    <code>MBeanServerPermission("newMBeanServer")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
public class MBeanServerPermission extends BasicPermission {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final long serialVersionUID = -5661980843569388590L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private final static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        CREATE = 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        FIND = 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        NEW = 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        RELEASE = 3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        N_NAMES = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private final static String[] names = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        "createMBeanServer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        "findMBeanServer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        "newMBeanServer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        "releaseMBeanServer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private final static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        CREATE_MASK = 1<<CREATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        FIND_MASK = 1<<FIND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        NEW_MASK = 1<<NEW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        RELEASE_MASK = 1<<RELEASE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        ALL_MASK = CREATE_MASK|FIND_MASK|NEW_MASK|RELEASE_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Map from permission masks to canonical names.  This array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * filled in on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * This isn't very scalable.  If we have more than five or six
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * permissions, we should consider doing this differently,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * e.g. with a Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private final static String[] canonicalNames = new String[1 << N_NAMES];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * The target names mask.  This is not private to avoid having to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * generate accessor methods for accesses from the collection class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * This mask includes implied bits.  So if it has CREATE_MASK then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * it necessarily has NEW_MASK too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    transient int mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /** <p>Create a new MBeanServerPermission with the given name.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        <p>This constructor is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        <code>MBeanServerPermission(name,null)</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        @param name the name of the granted permission.  It must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        respect the constraints spelt out in the description of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        {@link MBeanServerPermission} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        @exception NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        @exception IllegalArgumentException if the name is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        <code>*</code> or one of the allowed names or a comma-separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        list of the allowed names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public MBeanServerPermission(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this(name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /** <p>Create a new MBeanServerPermission with the given name.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        @param name the name of the granted permission.  It must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        respect the constraints spelt out in the description of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        {@link MBeanServerPermission} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        @param actions the associated actions.  This parameter is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        currently used and must be null or the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        @exception NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        @exception IllegalArgumentException if the name is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        <code>*</code> or one of the allowed names or a comma-separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        list of the allowed names, or if <code>actions</code> is a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        non-empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @throws NullPointerException if <code>name</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws IllegalArgumentException if <code>name</code> is empty or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * if arguments are invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public MBeanServerPermission(String name, String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        super(getCanonicalName(parseMask(name)), actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        /* It's annoying to have to parse the name twice, but since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
           Permission.getName() is final and since we can't access "this"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
           until after the call to the superclass constructor, there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
           isn't any very clean way to do this.  MBeanServerPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
           objects aren't constructed very often, luckily.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        mask = parseMask(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /* Check that actions is a null empty string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (actions != null && actions.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new IllegalArgumentException("MBeanServerPermission " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                               "actions must be null: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                               actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    MBeanServerPermission(int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        super(getCanonicalName(mask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.mask = impliedMask(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private void readObject(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        in.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        mask = parseMask(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    static int simplifyMask(int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if ((mask & CREATE_MASK) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            mask &= ~NEW_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static int impliedMask(int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if ((mask & CREATE_MASK) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            mask |= NEW_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    static String getCanonicalName(int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (mask == ALL_MASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        mask = simplifyMask(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        synchronized (canonicalNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (canonicalNames[mask] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                canonicalNames[mask] = makeCanonicalName(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return canonicalNames[mask];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private static String makeCanonicalName(int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        final StringBuilder buf = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        for (int i = 0; i < N_NAMES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if ((mask & (1<<i)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                if (buf.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    buf.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                buf.append(names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return buf.toString().intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        /* intern() avoids duplication when the mask has only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
           one bit, so is equivalent to the string constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
           we have for the names[] array.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /* Convert the string into a bitmask, including bits that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
       are implied by the permissions in the string.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private static int parseMask(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        /* Check that target name is a non-null non-empty string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throw new NullPointerException("MBeanServerPermission: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                           "target name can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        name = name.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (name.equals("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return ALL_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        /* If the name is empty, nameIndex will barf. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (name.indexOf(',') < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return impliedMask(1 << nameIndex(name.trim()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int mask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        StringTokenizer tok = new StringTokenizer(name, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        while (tok.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            String action = tok.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            int i = nameIndex(action.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            mask |= (1 << i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return impliedMask(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private static int nameIndex(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        for (int i = 0; i < N_NAMES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (names[i].equals(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            "Invalid MBeanServerPermission name: \"" + name + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <p>Checks if this MBeanServerPermission object "implies" the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * permission.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <p>More specifically, this method returns true if:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <li> <i>p</i> is an instance of MBeanServerPermission,</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <li> <i>p</i>'s target names are a subset of this object's target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * names</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <p>The <code>createMBeanServer</code> permission implies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <code>newMBeanServer</code> permission.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return true if the specified permission is implied by this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (!(p instanceof MBeanServerPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        MBeanServerPermission that = (MBeanServerPermission) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return ((this.mask & that.mask) == that.mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Checks two MBeanServerPermission objects for equality. Checks that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <i>obj</i> is an MBeanServerPermission, and represents the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * list of allowable actions as this object.
24368
2b4801b94265 8038795: Tidy warnings cleanup for javax.management
yan
parents: 5506
diff changeset
   288
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param obj the object we are testing for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @return true if the objects are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (! (obj instanceof MBeanServerPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        MBeanServerPermission that = (MBeanServerPermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return (this.mask == that.mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return new MBeanServerPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * Class returned by {@link MBeanServerPermission#newPermissionCollection()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 * Since every collection of MBSP can be represented by a single MBSP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 * that is what our PermissionCollection does.  We need to define a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 * PermissionCollection because the one inherited from BasicPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 * doesn't know that createMBeanServer implies newMBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 * Though the serial form is defined, the TCK does not check it.  We do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 * not require independent implementations to duplicate it.  Even though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 * PermissionCollection is Serializable, instances of this class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * hardly ever be serialized, and different implementations do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 * typically exchange serialized permission collections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 * If we did require that a particular form be respected here, we would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 * logically also have to require it for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 * MBeanPermission.newPermissionCollection, which would preclude an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * implementation from defining a PermissionCollection there with an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 * optimized "implies" method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
class MBeanServerPermissionCollection extends PermissionCollection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /** @serial Null if no permissions in collection, otherwise a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        single permission that is the union of all permissions that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        have been added.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private MBeanServerPermission collectionPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private static final long serialVersionUID = -5661980843569388590L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public synchronized void add(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (!(permission instanceof MBeanServerPermission)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                "Permission not an MBeanServerPermission: " + permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new SecurityException("Read-only permission collection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        MBeanServerPermission mbsp = (MBeanServerPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (collectionPermission == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            collectionPermission = mbsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        else if (!collectionPermission.implies(permission)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            int newmask = collectionPermission.mask | mbsp.mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            collectionPermission = new MBeanServerPermission(newmask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public synchronized boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return (collectionPermission != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                collectionPermission.implies(permission));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public synchronized Enumeration<Permission> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        Set<Permission> set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (collectionPermission == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            set = Collections.emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            set = Collections.singleton((Permission) collectionPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return Collections.enumeration(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
}