src/java.management/share/classes/javax/management/MBeanServerFactory.java
author darcy
Wed, 23 Oct 2019 13:01:40 -0700
changeset 58766 54ffb15c4839
parent 47216 71c04702a3d5
child 55994 9721e36abeb0
permissions -rw-r--r--
8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.* Reviewed-by: rriggs, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 37782
diff changeset
     2
 * Copyright (c) 1999, 2017, 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: 4156
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: 4156
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: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
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
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
    28
import com.sun.jmx.defaults.JmxProperties;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import static com.sun.jmx.defaults.JmxProperties.JMX_INITIAL_BUILDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jmx.mbeanserver.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.ArrayList;
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 37782
diff changeset
    35
import java.lang.System.Logger.Level;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.loading.ClassLoaderRepository;
18206
136373d8d805 8008615: Improve robustness of JMX internal APIs
sjiang
parents: 5506
diff changeset
    37
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>Provides MBean server references.  There are no instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * this class.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>Since JMX 1.2 this class makes it possible to replace the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * MBeanServer implementation. This is done using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * {@link javax.management.MBeanServerBuilder} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The class of the initial MBeanServerBuilder to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * instantiated can be specified through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <b>javax.management.builder.initial</b> system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The specified class must be a public subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * {@link javax.management.MBeanServerBuilder}, and must have a public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * empty constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>By default, if no value for that property is specified, an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * javax.management.MBeanServerBuilder javax.management.MBeanServerBuilder}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * is created. Otherwise, the MBeanServerFactory attempts to load the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * specified class using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * {@link java.lang.Thread#getContextClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   Thread.currentThread().getContextClassLoader()}, or if that is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * {@link java.lang.Class#forName(java.lang.String) Class.forName()}. Then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * it creates an initial instance of that Class using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * {@link java.lang.Class#newInstance()}. If any checked exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * is raised during this process (e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * {@link java.lang.ClassNotFoundException},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * {@link java.lang.InstantiationException}) the MBeanServerFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * will propagate this exception from within a RuntimeException.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>The <b>javax.management.builder.initial</b> system property is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * consulted every time a new MBeanServer needs to be created, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * class pointed to by that property is loaded. If that class is different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * from that of the current MBeanServerBuilder, then a new MBeanServerBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * is created. Otherwise, the MBeanServerFactory may create a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * MBeanServerBuilder or reuse the current one.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>If the class pointed to by the property cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * loaded, or does not correspond to a valid subclass of MBeanServerBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * then an exception is propagated, and no MBeanServer can be created until
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * the <b>javax.management.builder.initial</b> system property is reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * valid value.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>The MBeanServerBuilder makes it possible to wrap the MBeanServers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * returned by the default MBeanServerBuilder implementation, for the purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * of e.g. adding an additional security layer.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
public class MBeanServerFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * There are no instances of this class so don't generate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * default public constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private MBeanServerFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The builder that will be used to construct MBeanServers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static MBeanServerBuilder builder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Provide a new {@link javax.management.MBeanServerBuilder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param builder The new MBeanServerBuilder that will be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *        create {@link javax.management.MBeanServer}s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @exception IllegalArgumentException if the given builder is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @exception SecurityException if there is a SecurityManager and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * the caller's permissions do not include or imply <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * MBeanServerPermission}("setMBeanServerBuilder")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // public static synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    //    setMBeanServerBuilder(MBeanServerBuilder builder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    //    checkPermission("setMBeanServerBuilder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    //    MBeanServerFactory.builder = builder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Get the current {@link javax.management.MBeanServerBuilder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @return the current {@link javax.management.MBeanServerBuilder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @exception SecurityException if there is a SecurityManager and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * the caller's permissions do not include or imply <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * MBeanServerPermission}("getMBeanServerBuilder")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // public static synchronized MBeanServerBuilder getMBeanServerBuilder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    //     checkPermission("getMBeanServerBuilder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    //     return builder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Remove internal MBeanServerFactory references to a created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * MBeanServer. This allows the garbage collector to remove the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * MBeanServer object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param mbeanServer the MBeanServer object to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @exception java.lang.IllegalArgumentException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <code>mbeanServer</code> was not generated by one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <code>createMBeanServer</code> methods, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <code>releaseMBeanServer</code> was already called on it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @exception SecurityException if there is a SecurityManager and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * the caller's permissions do not include or imply <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * MBeanServerPermission}("releaseMBeanServer")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static void releaseMBeanServer(MBeanServer mbeanServer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        checkPermission("releaseMBeanServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        removeMBeanServer(mbeanServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <p>Return a new object implementing the MBeanServer interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * with a standard default domain name.  The default domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * is used as the domain part in the ObjectName of MBeans when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * domain is specified by the user is null.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <p>The standard default domain name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <code>DefaultDomain</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>The MBeanServer reference is internally kept. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * allow <CODE>findMBeanServer</CODE> to return a reference to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * this MBeanServer object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <p>This method is equivalent to <code>createMBeanServer(null)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return the newly created MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @exception SecurityException if there is a SecurityManager and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * caller's permissions do not include or imply <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * MBeanServerPermission}("createMBeanServer")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @exception JMRuntimeException if the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <code>javax.management.builder.initial</code> exists but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * class it names cannot be instantiated through a public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * no-argument constructor; or if the instantiated builder returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * null from its {@link MBeanServerBuilder#newMBeanServerDelegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * newMBeanServerDelegate} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * MBeanServerBuilder#newMBeanServer newMBeanServer} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception ClassCastException if the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <code>javax.management.builder.initial</code> exists and can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * instantiated but is not assignment compatible with {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * MBeanServerBuilder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public static MBeanServer createMBeanServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return createMBeanServer(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p>Return a new object implementing the {@link MBeanServer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * interface with the specified default domain name.  The given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * domain name is used as the domain part in the ObjectName of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * MBeans when the domain is specified by the user is null.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p>The MBeanServer reference is internally kept. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * allow <CODE>findMBeanServer</CODE> to return a reference to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * this MBeanServer object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param domain the default domain name for the created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * MBeanServer.  This is the value that will be returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * MBeanServer#getDefaultDomain}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return the newly created MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @exception SecurityException if there is a SecurityManager and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * the caller's permissions do not include or imply <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * MBeanServerPermission}("createMBeanServer")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @exception JMRuntimeException if the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <code>javax.management.builder.initial</code> exists but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * class it names cannot be instantiated through a public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * no-argument constructor; or if the instantiated builder returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * null from its {@link MBeanServerBuilder#newMBeanServerDelegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * newMBeanServerDelegate} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * MBeanServerBuilder#newMBeanServer newMBeanServer} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @exception ClassCastException if the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <code>javax.management.builder.initial</code> exists and can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * instantiated but is not assignment compatible with {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * MBeanServerBuilder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public static MBeanServer createMBeanServer(String domain)  {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1510
diff changeset
   229
        checkPermission("createMBeanServer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1510
diff changeset
   231
        final MBeanServer mBeanServer = newMBeanServer(domain);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1510
diff changeset
   232
        addMBeanServer(mBeanServer);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1510
diff changeset
   233
        return mBeanServer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p>Return a new object implementing the MBeanServer interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * with a standard default domain name, without keeping an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * internal reference to this new object.  The default domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * is used as the domain part in the ObjectName of MBeans when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * domain is specified by the user is null.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p>The standard default domain name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <code>DefaultDomain</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <p>No reference is kept. <CODE>findMBeanServer</CODE> will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * be able to return a reference to this MBeanServer object, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * the garbage collector will be able to remove the MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * object when it is no longer referenced.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <p>This method is equivalent to <code>newMBeanServer(null)</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return the newly created MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @exception SecurityException if there is a SecurityManager and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * caller's permissions do not include or imply <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * MBeanServerPermission}("newMBeanServer")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @exception JMRuntimeException if the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <code>javax.management.builder.initial</code> exists but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * class it names cannot be instantiated through a public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * no-argument constructor; or if the instantiated builder returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * null from its {@link MBeanServerBuilder#newMBeanServerDelegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * newMBeanServerDelegate} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * MBeanServerBuilder#newMBeanServer newMBeanServer} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @exception ClassCastException if the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <code>javax.management.builder.initial</code> exists and can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * instantiated but is not assignment compatible with {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * MBeanServerBuilder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public static MBeanServer newMBeanServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return newMBeanServer(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * <p>Return a new object implementing the MBeanServer interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * with the specified default domain name, without keeping an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * internal reference to this new object.  The given domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * is used as the domain part in the ObjectName of MBeans when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * domain is specified by the user is null.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <p>No reference is kept. <CODE>findMBeanServer</CODE> will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * be able to return a reference to this MBeanServer object, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * the garbage collector will be able to remove the MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * object when it is no longer referenced.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param domain the default domain name for the created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * MBeanServer.  This is the value that will be returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * MBeanServer#getDefaultDomain}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return the newly created MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @exception SecurityException if there is a SecurityManager and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * caller's permissions do not include or imply <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * MBeanServerPermission}("newMBeanServer")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @exception JMRuntimeException if the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <code>javax.management.builder.initial</code> exists but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * class it names cannot be instantiated through a public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * no-argument constructor; or if the instantiated builder returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * null from its {@link MBeanServerBuilder#newMBeanServerDelegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * newMBeanServerDelegate} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * MBeanServerBuilder#newMBeanServer newMBeanServer} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @exception ClassCastException if the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <code>javax.management.builder.initial</code> exists and can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * instantiated but is not assignment compatible with {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * MBeanServerBuilder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public static MBeanServer newMBeanServer(String domain)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        checkPermission("newMBeanServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // Get the builder. Creates a new one if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        final MBeanServerBuilder mbsBuilder = getNewMBeanServerBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        // Returned value cannot be null.  NullPointerException if violated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        synchronized(mbsBuilder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            final MBeanServerDelegate delegate  =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   321
                    mbsBuilder.newMBeanServerDelegate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (delegate == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                final String msg =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   324
                        "MBeanServerBuilder.newMBeanServerDelegate() " +
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   325
                        "returned null";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                throw new JMRuntimeException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            final MBeanServer mbeanServer =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   329
                    mbsBuilder.newMBeanServer(domain,null,delegate);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (mbeanServer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                final String msg =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   332
                        "MBeanServerBuilder.newMBeanServer() returned null";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                throw new JMRuntimeException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return mbeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <p>Return a list of registered MBeanServer objects.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * registered MBeanServer object is one that was created by one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * the <code>createMBeanServer</code> methods and not subsequently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * released with <code>releaseMBeanServer</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param agentId The agent identifier of the MBeanServer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * retrieve.  If this parameter is null, all registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * MBeanServers in this JVM are returned.  Otherwise, only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * MBeanServers whose id is equal to <code>agentId</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * returned.  The id of an MBeanServer is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <code>MBeanServerId</code> attribute of its delegate MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @return A list of MBeanServer objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @exception SecurityException if there is a SecurityManager and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * caller's permissions do not include or imply <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * MBeanServerPermission}("findMBeanServer")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public synchronized static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            ArrayList<MBeanServer> findMBeanServer(String agentId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        checkPermission("findMBeanServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (agentId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return new ArrayList<MBeanServer>(mBeanServerList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        ArrayList<MBeanServer> result = new ArrayList<MBeanServer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        for (MBeanServer mbs : mBeanServerList) {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   368
            String name = mBeanServerId(mbs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (agentId.equals(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                result.add(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Return the ClassLoaderRepository used by the given MBeanServer.
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   377
     * This method is equivalent to {@link
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   378
     * MBeanServer#getClassLoaderRepository() server.getClassLoaderRepository()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @param server The MBeanServer under examination. Since JMX 1.2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * if <code>server</code> is <code>null</code>, the result is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * {@link NullPointerException}.  This behavior differs from what
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * was implemented in JMX 1.1 - where the possibility to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <code>null</code> was deprecated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @return The Class Loader Repository used by the given MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @exception SecurityException if there is a SecurityManager and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * the caller's permissions do not include or imply <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * MBeanPermission}("getClassLoaderRepository")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @exception NullPointerException if <code>server</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public static ClassLoaderRepository getClassLoaderRepository(
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   393
            MBeanServer server) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return server.getClassLoaderRepository();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   397
    private static String mBeanServerId(MBeanServer mbs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return (String) mbs.getAttribute(MBeanServerDelegate.DELEGATE_NAME,
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   400
                    "MBeanServerId");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        } catch (JMException e) {
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 37782
diff changeset
   402
            JmxProperties.MISC_LOGGER.log(Level.TRACE,
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   403
                    "Ignoring exception while getting MBeanServerId: "+e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private static void checkPermission(String action)
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   409
    throws SecurityException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            Permission perm = new MBeanServerPermission(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            sm.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    private static synchronized void addMBeanServer(MBeanServer mbs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        mBeanServerList.add(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    private static synchronized void removeMBeanServer(MBeanServer mbs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        boolean removed = mBeanServerList.remove(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (!removed) {
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 37782
diff changeset
   424
            MBEANSERVER_LOGGER.log(Level.TRACE,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    "MBeanServer was not in list!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            throw new IllegalArgumentException("MBeanServer was not in list!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    private static final ArrayList<MBeanServer> mBeanServerList =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   431
            new ArrayList<MBeanServer>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Load the builder class through the context class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @param builderClassName The name of the builder class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   437
    private static Class<?> loadBuilderClass(String builderClassName)
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   438
    throws ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        final ClassLoader loader =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   440
                Thread.currentThread().getContextClassLoader();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            // Try with context class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            return loader.loadClass(builderClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        // No context class loader? Try with Class.forName()
18206
136373d8d805 8008615: Improve robustness of JMX internal APIs
sjiang
parents: 5506
diff changeset
   448
        return ReflectUtil.forName(builderClassName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Creates the initial builder according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * javax.management.builder.initial System property - if specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * If any checked exception needs to be thrown, it is embedded in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * a JMRuntimeException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   457
    private static MBeanServerBuilder newBuilder(Class<?> builderClass) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        try {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 25859
diff changeset
   459
            @SuppressWarnings("deprecation")
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   460
            final Object abuilder = builderClass.newInstance();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   461
            return (MBeanServerBuilder)abuilder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        } catch (RuntimeException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            final String msg =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   466
                    "Failed to instantiate a MBeanServerBuilder from " +
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   467
                    builderClass + ": " + x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            throw new JMRuntimeException(msg, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Instantiate a new builder according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * javax.management.builder.initial System property - if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    private static synchronized void checkMBeanServerBuilder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            GetPropertyAction act =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   479
                    new GetPropertyAction(JMX_INITIAL_BUILDER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            String builderClassName = AccessController.doPrivileged(act);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            try {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   483
                final Class<?> newBuilderClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                if (builderClassName == null || builderClassName.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    newBuilderClass = MBeanServerBuilder.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    newBuilderClass = loadBuilderClass(builderClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                // Check whether a new builder needs to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                if (builder != null) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   491
                    final Class<?> builderClass = builder.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    if (newBuilderClass == builderClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                        return; // no need to create a new builder...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                // Create a new builder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                builder = newBuilder(newBuilderClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                final String msg =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   500
                        "Failed to load MBeanServerBuilder class " +
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 2
diff changeset
   501
                        builderClassName + ": " + x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                throw new JMRuntimeException(msg, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        } catch (RuntimeException x) {
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 37782
diff changeset
   505
            if (MBEANSERVER_LOGGER.isLoggable(Level.DEBUG)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                .append("Failed to instantiate MBeanServerBuilder: ").append(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                .append("\n\t\tCheck the value of the ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                .append(JMX_INITIAL_BUILDER).append(" property.");
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 37782
diff changeset
   510
                MBEANSERVER_LOGGER.log(Level.DEBUG, strb::toString);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * Get the current {@link javax.management.MBeanServerBuilder},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * as specified by the current value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * javax.management.builder.initial property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * This method consults the property and instantiates a new builder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @return the new current {@link javax.management.MBeanServerBuilder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @exception SecurityException if there is a SecurityManager and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * the caller's permissions do not make it possible to instantiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * a new builder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @exception JMRuntimeException if the builder instantiation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *   fails with a checked exception -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *   {@link java.lang.ClassNotFoundException} etc...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    private static synchronized MBeanServerBuilder getNewMBeanServerBuilder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        checkMBeanServerBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return builder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
}