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