MBeanServerFactory.java 36.7 KB
Newer Older
D
duke 已提交
1
/*
X
xdono 已提交
2
 * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package javax.management;

28
import com.sun.jmx.defaults.JmxProperties;
D
duke 已提交
29 30 31
import static com.sun.jmx.defaults.JmxProperties.JMX_INITIAL_BUILDER;
import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
import com.sun.jmx.mbeanserver.GetPropertyAction;
32
import com.sun.jmx.mbeanserver.Util;
D
duke 已提交
33 34 35
import java.security.AccessController;
import java.security.Permission;
import java.util.ArrayList;
36
import java.util.List;
D
duke 已提交
37 38 39
import java.util.logging.Level;
import javax.management.loading.ClassLoaderRepository;

40

D
duke 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/**
 * <p>Provides MBean server references.  There are no instances of
 * this class.</p>
 *
 * <p>Since JMX 1.2 this class makes it possible to replace the default
 * MBeanServer implementation. This is done using the
 * {@link javax.management.MBeanServerBuilder} class.
 * The class of the initial MBeanServerBuilder to be
 * instantiated can be specified through the
 * <b>javax.management.builder.initial</b> system property.
 * The specified class must be a public subclass of
 * {@link javax.management.MBeanServerBuilder}, and must have a public
 * empty constructor.
 * <p>By default, if no value for that property is specified, an instance of
 * {@link
 * javax.management.MBeanServerBuilder javax.management.MBeanServerBuilder}
 * is created. Otherwise, the MBeanServerFactory attempts to load the
 * specified class using
 * {@link java.lang.Thread#getContextClassLoader()
 *   Thread.currentThread().getContextClassLoader()}, or if that is null,
 * {@link java.lang.Class#forName(java.lang.String) Class.forName()}. Then
 * it creates an initial instance of that Class using
 * {@link java.lang.Class#newInstance()}. If any checked exception
 * is raised during this process (e.g.
 * {@link java.lang.ClassNotFoundException},
 * {@link java.lang.InstantiationException}) the MBeanServerFactory
 * will propagate this exception from within a RuntimeException.</p>
 *
 * <p>The <b>javax.management.builder.initial</b> system property is
 * consulted every time a new MBeanServer needs to be created, and the
 * class pointed to by that property is loaded. If that class is different
 * from that of the current MBeanServerBuilder, then a new MBeanServerBuilder
 * is created. Otherwise, the MBeanServerFactory may create a new
 * MBeanServerBuilder or reuse the current one.</p>
 *
 * <p>If the class pointed to by the property cannot be
 * loaded, or does not correspond to a valid subclass of MBeanServerBuilder
 * then an exception is propagated, and no MBeanServer can be created until
 * the <b>javax.management.builder.initial</b> system property is reset to
 * valid value.</p>
 *
 * <p>The MBeanServerBuilder makes it possible to wrap the MBeanServers
 * returned by the default MBeanServerBuilder implementation, for the purpose
 * of e.g. adding an additional security layer.</p>
 *
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
 * <p id="MBeanServerName">Since version 2.0 of the JMX API, when creating
 * an MBeanServer,
 * it is possible to specify an {@linkplain #getMBeanServerName
 * MBean Server name}.
 * To create an MBean Server with a name, the MBeanServerFactory provides two
 * new methods:</p>
 * <ul><li>{@link #createNamedMBeanServer
 * createNamedMBeanServer(mbeanServerName, defaultDomain)}: creates a named
 * MBeanServer and keeps an internal reference to the created object. The
 * MBeanServer can be later retrieved using {@link #findMBeanServer
 * findMBeanServer(mbeanServerId)} or
 * {@link #findMBeanServerByName findMBeanServerByName(mbeanServerName)}, and
 * can be released through {@link
 * #releaseMBeanServer releaseMBeanServer(mbeanServer)}.</li>
 * <li>{@link #newNamedMBeanServer
 * newNamedMBeanServer(mbeanServerName, defaultDomain)}:
 * creates a named MBeanServer without keeping any internal reference to the
 * named server.</li>
 * </ul>
 * <p>The name of the MBeanServer is stored in the
 * {@linkplain MBeanServerDelegate MBean Server delegate MBean}
 * and is embedded in its {@link MBeanServerDelegate#getMBeanServerId
 * MBeanServerId} attribute.</p>
 * <p>The name of the MBeanServer is particularly useful when
 * <a href="MBeanServer.html#security">MBean permissions</a> are checked:
 * it makes it
 * possible to distinguish between an MBean named "X" in MBeanServer named
 * "M1", and another MBean of the same name "X" in another MBeanServer named
 * "M2".</p>
 * <p>When naming MBean servers it is recommended to use a name that starts
 * with a Java package name. It is also recommended that the default domain and
 * the MBeanServer name be the same.</p>
 *
D
duke 已提交
119 120 121 122
 * @since 1.5
 */
public class MBeanServerFactory {

123 124 125 126 127 128 129 130 131 132
    /**
     * The <a href="#MBeanServerName">MBean Server name</a> that will be
     * checked by a <a href="MBeanServer.html#security">permission you need</a>
     * when checking access to an MBean registered in an MBeanServer for
     * which no MBeanServer name was specified.
     *
     * @since 1.7
     */
    public final static String DEFAULT_MBEANSERVER_NAME = "default";

D
duke 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    /*
     * There are no instances of this class so don't generate the
     * default public constructor.
     */
    private MBeanServerFactory() {

    }

    /**
     * The builder that will be used to construct MBeanServers.
     *
     **/
    private static MBeanServerBuilder builder = null;

    /**
     * Provide a new {@link javax.management.MBeanServerBuilder}.
     * @param builder The new MBeanServerBuilder that will be used to
     *        create {@link javax.management.MBeanServer}s.
     * @exception IllegalArgumentException if the given builder is null.
     *
     * @exception SecurityException if there is a SecurityManager and
     * the caller's permissions do not include or imply <code>{@link
     * MBeanServerPermission}("setMBeanServerBuilder")</code>.
     *
     **/
    // public static synchronized void
    //    setMBeanServerBuilder(MBeanServerBuilder builder) {
    //    checkPermission("setMBeanServerBuilder");
    //    MBeanServerFactory.builder = builder;
    // }

    /**
     * Get the current {@link javax.management.MBeanServerBuilder}.
     *
     * @return the current {@link javax.management.MBeanServerBuilder}.
     *
     * @exception SecurityException if there is a SecurityManager and
     * the caller's permissions do not include or imply <code>{@link
     * MBeanServerPermission}("getMBeanServerBuilder")</code>.
     *
     **/
    // public static synchronized MBeanServerBuilder getMBeanServerBuilder() {
    //     checkPermission("getMBeanServerBuilder");
    //     return builder;
    // }

    /**
     * Remove internal MBeanServerFactory references to a created
     * MBeanServer. This allows the garbage collector to remove the
     * MBeanServer object.
     *
     * @param mbeanServer the MBeanServer object to remove.
     *
     * @exception java.lang.IllegalArgumentException if
     * <code>mbeanServer</code> was not generated by one of the
     * <code>createMBeanServer</code> methods, or if
     * <code>releaseMBeanServer</code> was already called on it.
     *
     * @exception SecurityException if there is a SecurityManager and
     * the caller's permissions do not include or imply <code>{@link
     * MBeanServerPermission}("releaseMBeanServer")</code>.
     */
    public static void releaseMBeanServer(MBeanServer mbeanServer) {
        checkPermission("releaseMBeanServer");

        removeMBeanServer(mbeanServer);
    }

    /**
     * <p>Return a new object implementing the MBeanServer interface
     * with a standard default domain name.  The default domain name
     * is used as the domain part in the ObjectName of MBeans when the
     * domain is specified by the user is null.</p>
     *
     * <p>The standard default domain name is
     * <code>DefaultDomain</code>.</p>
     *
     * <p>The MBeanServer reference is internally kept. This will
     * allow <CODE>findMBeanServer</CODE> to return a reference to
     * this MBeanServer object.</p>
     *
     * <p>This method is equivalent to <code>createMBeanServer(null)</code>.
     *
     * @return the newly created MBeanServer.
     *
     * @exception SecurityException if there is a SecurityManager and the
     * caller's permissions do not include or imply <code>{@link
     * MBeanServerPermission}("createMBeanServer")</code>.
     *
     * @exception JMRuntimeException if the property
     * <code>javax.management.builder.initial</code> exists but the
     * class it names cannot be instantiated through a public
     * no-argument constructor; or if the instantiated builder returns
     * null from its {@link MBeanServerBuilder#newMBeanServerDelegate
     * newMBeanServerDelegate} or {@link
     * MBeanServerBuilder#newMBeanServer newMBeanServer} methods.
     *
     * @exception ClassCastException if the property
     * <code>javax.management.builder.initial</code> exists and can be
     * instantiated but is not assignment compatible with {@link
     * MBeanServerBuilder}.
     */
    public static MBeanServer createMBeanServer() {
        return createMBeanServer(null);
    }

    /**
     * <p>Return a new object implementing the {@link MBeanServer}
     * interface with the specified default domain name.  The given
     * domain name is used as the domain part in the ObjectName of
     * MBeans when the domain is specified by the user is null.</p>
     *
     * <p>The MBeanServer reference is internally kept. This will
     * allow <CODE>findMBeanServer</CODE> to return a reference to
     * this MBeanServer object.</p>
     *
     * @param domain the default domain name for the created
     * MBeanServer.  This is the value that will be returned by {@link
     * MBeanServer#getDefaultDomain}.
     *
     * @return the newly created MBeanServer.
     *
     * @exception SecurityException if there is a SecurityManager and
     * the caller's permissions do not include or imply <code>{@link
     * MBeanServerPermission}("createMBeanServer")</code>.
     *
     * @exception JMRuntimeException if the property
     * <code>javax.management.builder.initial</code> exists but the
     * class it names cannot be instantiated through a public
     * no-argument constructor; or if the instantiated builder returns
     * null from its {@link MBeanServerBuilder#newMBeanServerDelegate
     * newMBeanServerDelegate} or {@link
     * MBeanServerBuilder#newMBeanServer newMBeanServer} methods.
     *
     * @exception ClassCastException if the property
     * <code>javax.management.builder.initial</code> exists and can be
     * instantiated but is not assignment compatible with {@link
     * MBeanServerBuilder}.
271 272
     *
     * @see #createNamedMBeanServer
D
duke 已提交
273 274
     */
    public static MBeanServer createMBeanServer(String domain)  {
275 276
        return createMBeanServer(null,domain);
    }
D
duke 已提交
277

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    /**
     * <p>Return a new object implementing the {@link MBeanServer}
     * interface with the specified
     * <a href="#MBeanServerName">MBean Server name</a>
     * and default domain name. The given MBean server name
     * is used in <a href="MBeanServer.html#security">security checks</a>, and
     * can also be used to {@linkplain #findMBeanServerByName(java.lang.String)
     * find an MBeanServer by name}. The given
     * domain name is used as the domain part in the ObjectName of
     * MBeans when the domain is specified by the user is null.</p>
     *
     * <p>The MBeanServer reference is internally kept. This will
     * allow <CODE>findMBeanServer</CODE> to return a reference to
     * this MBeanServer object.</p>
     *
     * @param mbeanServerName the name for the created
     * MBeanServer.  This is the name that will be included in the
     * {@linkplain MBeanPermission permission you need} when checking
     * <a href="MBeanServer.html#security">MBean Permissions</a> for accessing
     * an MBean registered in the returned MBeanServer. The characters
     * {@code ':'} (colon), {@code ';'} (semicolon), {@code '*'} (star)
     * and  {@code '?'} are not legal.
     * It is recommended that the {@code mbeanServerName}
     * be unique in the context of a JVM, and in the form of a java package
     * identifier. If {@code mbeanServerName} is {@code null} then the created
     * MBean Server has no name - and {@value #DEFAULT_MBEANSERVER_NAME} is used.
     * Calling {@code createNamedMBeanServer(null,domain)} is equivalent
     * to calling {@link #createMBeanServer(String) createMBeanServer(domain)}.
     *
     * @param domain the default domain name for the created
     * MBeanServer.  This is the value that will be returned by {@link
     * MBeanServer#getDefaultDomain}. If a non null mbeanServerName is given,
     * it is recommended to pass the same value as default domain.
     *
     * @return the newly created MBeanServer.
     *
     * @exception SecurityException if there is a SecurityManager and
     * the caller's permissions do not include or imply <code>{@link
     * MBeanServerPermission}("createMBeanServer")</code>.
     *
     * @exception JMRuntimeException if the property
     * <code>javax.management.builder.initial</code> exists but the
     * class it names cannot be instantiated through a public
     * no-argument constructor; or if the instantiated builder returns
     * null from its {@link MBeanServerBuilder#newMBeanServerDelegate
     * newMBeanServerDelegate} or {@link
     * MBeanServerBuilder#newMBeanServer newMBeanServer} methods.
     *
     * @exception ClassCastException if the property
     * <code>javax.management.builder.initial</code> exists and can be
     * instantiated but is not assignment compatible with {@link
     * MBeanServerBuilder}.
     *
     * @exception IllegalArgumentException if the specified
     * {@code mbeanServerName} is empty, or is {@code "-"}, or contains a
     * character which is not legal.
     *
     * @exception UnsupportedOperationException if the specified
     * {@code mbeanServerName} cannot be set.
     *
     * @since 1.7
     */
    public static MBeanServer createNamedMBeanServer(String mbeanServerName,
            String domain)  {
        return createMBeanServer(mbeanServerName, domain);
D
duke 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    }

    /**
     * <p>Return a new object implementing the MBeanServer interface
     * with a standard default domain name, without keeping an
     * internal reference to this new object.  The default domain name
     * is used as the domain part in the ObjectName of MBeans when the
     * domain is specified by the user is null.</p>
     *
     * <p>The standard default domain name is
     * <code>DefaultDomain</code>.</p>
     *
     * <p>No reference is kept. <CODE>findMBeanServer</CODE> will not
     * be able to return a reference to this MBeanServer object, but
     * the garbage collector will be able to remove the MBeanServer
     * object when it is no longer referenced.</p>
     *
     * <p>This method is equivalent to <code>newMBeanServer(null)</code>.</p>
     *
     * @return the newly created MBeanServer.
     *
     * @exception SecurityException if there is a SecurityManager and the
     * caller's permissions do not include or imply <code>{@link
     * MBeanServerPermission}("newMBeanServer")</code>.
     *
     * @exception JMRuntimeException if the property
     * <code>javax.management.builder.initial</code> exists but the
     * class it names cannot be instantiated through a public
     * no-argument constructor; or if the instantiated builder returns
     * null from its {@link MBeanServerBuilder#newMBeanServerDelegate
     * newMBeanServerDelegate} or {@link
     * MBeanServerBuilder#newMBeanServer newMBeanServer} methods.
     *
     * @exception ClassCastException if the property
     * <code>javax.management.builder.initial</code> exists and can be
     * instantiated but is not assignment compatible with {@link
     * MBeanServerBuilder}.
     */
    public static MBeanServer newMBeanServer() {
        return newMBeanServer(null);
    }

    /**
     * <p>Return a new object implementing the MBeanServer interface
     * with the specified default domain name, without keeping an
     * internal reference to this new object.  The given domain name
     * is used as the domain part in the ObjectName of MBeans when the
     * domain is specified by the user is null.</p>
     *
     * <p>No reference is kept. <CODE>findMBeanServer</CODE> will not
     * be able to return a reference to this MBeanServer object, but
     * the garbage collector will be able to remove the MBeanServer
     * object when it is no longer referenced.</p>
     *
     * @param domain the default domain name for the created
     * MBeanServer.  This is the value that will be returned by {@link
     * MBeanServer#getDefaultDomain}.
     *
     * @return the newly created MBeanServer.
     *
     * @exception SecurityException if there is a SecurityManager and the
     * caller's permissions do not include or imply <code>{@link
     * MBeanServerPermission}("newMBeanServer")</code>.
     *
     * @exception JMRuntimeException if the property
     * <code>javax.management.builder.initial</code> exists but the
     * class it names cannot be instantiated through a public
     * no-argument constructor; or if the instantiated builder returns
     * null from its {@link MBeanServerBuilder#newMBeanServerDelegate
     * newMBeanServerDelegate} or {@link
     * MBeanServerBuilder#newMBeanServer newMBeanServer} methods.
     *
     * @exception ClassCastException if the property
     * <code>javax.management.builder.initial</code> exists and can be
     * instantiated but is not assignment compatible with {@link
     * MBeanServerBuilder}.
     */
    public static MBeanServer newMBeanServer(String domain)  {
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
        return newMBeanServer(null,domain);
    }

    /**
     * <p>Return a new object implementing the MBeanServer interface
     * with the specified <a href="#MBeanServerName">MBean server name</a>
     * and default domain name, without keeping an
     * internal reference to this new object.  The given MBean server name
     * is used in <a href="MBeanServer.html#security">security checks</a>.
     * The given domain name
     * is used as the domain part in the ObjectName of MBeans when the
     * domain is specified by the user is null.</p>
     *
     * <p>No reference is kept. <CODE>findMBeanServer</CODE> and
     * <CODE>findMBeanServerByName</CODE> will not
     * be able to return a reference to this MBeanServer object, but
     * the garbage collector will be able to remove the MBeanServer
     * object when it is no longer referenced.</p>
     *
     * @param mbeanServerName the name for the created
     * MBeanServer.  This is the name that will be included in the
     * {@linkplain MBeanPermission permission you need} when checking
     * <a href="MBeanServer.html#security">MBean Permissions</a> for accessing
     * an MBean registered in the returned MBeanServer. The characters
     * {@code ':'} (colon), {@code ';'} (semicolon), {@code '*'} (star)
     * and  {@code '?'} are not legal.
     * It is recommended that the mbeanServerName
     * be unique in the context of a JVM, and in the form of a java package
     * identifier. If {@code mbeanServerName} is {@code null} then the created
     * MBean Server has no name - and {@value #DEFAULT_MBEANSERVER_NAME} is used.
     * Calling {@code newNamedMBeanServer(null,domain)} is equivalent
     * to calling {@link #newMBeanServer(String) newMBeanServer(domain)}.
     *
     * @param domain the default domain name for the created
     * MBeanServer.  This is the value that will be returned by {@link
     * MBeanServer#getDefaultDomain}.
     *
     * @return the newly created MBeanServer.
     *
     * @exception SecurityException if there is a SecurityManager and the
     * caller's permissions do not include or imply <code>{@link
     * MBeanServerPermission}("newMBeanServer")</code>.
     *
     * @exception JMRuntimeException if the property
     * <code>javax.management.builder.initial</code> exists but the
     * class it names cannot be instantiated through a public
     * no-argument constructor; or if the instantiated builder returns
     * null from its {@link MBeanServerBuilder#newMBeanServerDelegate
     * newMBeanServerDelegate} or {@link
     * MBeanServerBuilder#newMBeanServer newMBeanServer} methods.
     *
     * @exception ClassCastException if the property
     * <code>javax.management.builder.initial</code> exists and can be
     * instantiated but is not assignment compatible with {@link
     * MBeanServerBuilder}.
     *
     * @exception IllegalArgumentException if the specified
     * {@code mbeanServerName} is empty, or is {@code "-"},
     * or contains a character which is not legal.
     *
     * @exception UnsupportedOperationException if the specified
     * {@code mbeanServerName} cannot be set.
     *
     * @since 1.7
     */
    public static MBeanServer newNamedMBeanServer(String mbeanServerName,
            String domain)  {
        return newMBeanServer(mbeanServerName, domain);
    }

    private static MBeanServer createMBeanServer(String mbeanServerName,
            String domain)  {
        checkPermission("createMBeanServer");

        final MBeanServer mBeanServer =
                newMBeanServer(mbeanServerName,domain);
        addMBeanServer(mBeanServer);
        return mBeanServer;
    }

    private static MBeanServer newMBeanServer(String mbeanServerName,
            String domain) {
D
duke 已提交
503 504 505 506 507 508 509 510 511
        checkPermission("newMBeanServer");

        // Get the builder. Creates a new one if necessary.
        //
        final MBeanServerBuilder mbsBuilder = getNewMBeanServerBuilder();
        // Returned value cannot be null.  NullPointerException if violated.

        synchronized(mbsBuilder) {
            final MBeanServerDelegate delegate  =
512
                    mbsBuilder.newMBeanServerDelegate();
D
duke 已提交
513 514
            if (delegate == null) {
                final String msg =
515 516
                        "MBeanServerBuilder.newMBeanServerDelegate() " +
                        "returned null";
D
duke 已提交
517 518
                throw new JMRuntimeException(msg);
            }
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

            // Sets the name on the delegate. For complex backward
            // compatibility reasons it is not possible to give the
            // name to the MBeanServerDelegate constructor.
            //
            // The method setMBeanServerName() will call getMBeanServerId()
            // to check that the name is accurately set in the MBeanServerId.
            // If not (which could happen if a custom MBeanServerDelegate
            // implementation overrides getMBeanServerId() and was not updated
            // with respect to JMX 2.0 spec, this method will throw an
            // IllegalStateException...
            //
            if (!Util.isMBeanServerNameUndefined(mbeanServerName)) {
                delegate.setMBeanServerName(mbeanServerName);
            }

D
duke 已提交
535
            final MBeanServer mbeanServer =
536
                    mbsBuilder.newMBeanServer(domain,null,delegate);
D
duke 已提交
537 538
            if (mbeanServer == null) {
                final String msg =
539
                        "MBeanServerBuilder.newMBeanServer() returned null";
D
duke 已提交
540 541
                throw new JMRuntimeException(msg);
            }
542 543 544 545 546 547 548 549 550 551 552 553 554 555

            // double check that the MBeanServer name is correctly set.
            // "*" might mean that the caller doesn't have the permission
            // to see the MBeanServer name.
            //
            final String mbsName = Util.getMBeanServerSecurityName(mbeanServer);
            if (!mbsName.equals(Util.checkServerName(mbeanServerName))
                && !mbsName.equals("*")) {
                throw new UnsupportedOperationException(
                        "can't create MBeanServer with name \""+
                        mbeanServerName+"\" using "+
                        builder.getClass().getName());
            }

D
duke 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
            return mbeanServer;
        }
    }

    /**
     * <p>Return a list of registered MBeanServer objects.  A
     * registered MBeanServer object is one that was created by one of
     * the <code>createMBeanServer</code> methods and not subsequently
     * released with <code>releaseMBeanServer</code>.</p>
     *
     * @param agentId The agent identifier of the MBeanServer to
     * retrieve.  If this parameter is null, all registered
     * MBeanServers in this JVM are returned.  Otherwise, only
     * MBeanServers whose id is equal to <code>agentId</code> are
     * returned.  The id of an MBeanServer is the
     * <code>MBeanServerId</code> attribute of its delegate MBean.
     *
     * @return A list of MBeanServer objects.
     *
     * @exception SecurityException if there is a SecurityManager and the
     * caller's permissions do not include or imply <code>{@link
     * MBeanServerPermission}("findMBeanServer")</code>.
     */
    public synchronized static
            ArrayList<MBeanServer> findMBeanServer(String agentId) {

        checkPermission("findMBeanServer");

        if (agentId == null)
            return new ArrayList<MBeanServer>(mBeanServerList);

        ArrayList<MBeanServer> result = new ArrayList<MBeanServer>();
        for (MBeanServer mbs : mBeanServerList) {
589
            String name = mBeanServerId(mbs);
D
duke 已提交
590 591 592 593 594 595
            if (agentId.equals(name))
                result.add(mbs);
        }
        return result;
    }

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    /**
     * <p>Returns a list of registered MBeanServer objects with the given name.  A
     * registered MBeanServer object is one that was created by one of
     * the <code>createMBeanServer</code> or <code>createNamedMBeanServer</code>
     * methods and not subsequently released with <code>releaseMBeanServer</code>.</p>
     * <p>See the section about <a href="#MBeanServerName">MBean Server names</a>
     * above.</p>
     *
     * @param mbeanServerName The name of the MBeanServer to
     * retrieve.  If this parameter is null, all registered MBeanServers
     * in this JVM are returned.
     * Otherwise, only those MBeanServers that have a name
     * matching <code>mbeanServerName</code> are returned: this
     * parameter can be a pattern, where {@code '*'} matches any
     * sequence of characters and {@code '?'} matches any character.<br>
     * The name of an MBeanServer, if specified, is embedded in the
     * <code>MBeanServerId</code> attribute of its delegate MBean:
     * this method will parse the <code>MBeanServerId</code> to get the
     * MBeanServer name. If this parameter is equal to {@code "*"} then
     * all registered MBeanServers in this JVM are returned, whether they have
     * a name or not: {@code findMBeanServerByName(null)},
     * {@code findMBeanServerByName("*")} and {@code findMBeanServer(null)},
     * are equivalent. It is also possible to get all MBeanServers for which
     * no name was specified by calling <code>findMBeanServerByName({@value
     * #DEFAULT_MBEANSERVER_NAME})</code>.
     *
     * @return A list of MBeanServer objects.
     *
     * @exception SecurityException if there is a SecurityManager and the
     * caller's permissions do not include or imply <code>{@link
     * MBeanServerPermission}("findMBeanServer")</code>.
     *
     * @see #getMBeanServerName(MBeanServer)
     * @since 1.7
     */
    public synchronized static
            List<MBeanServer> findMBeanServerByName(String mbeanServerName) {

        checkPermission("findMBeanServer");

        if (mbeanServerName==null || "*".equals(mbeanServerName))
            return new ArrayList<MBeanServer>(mBeanServerList);

        // noname=true iff we are looking for MBeanServers for which no name
        // were specified.
        ArrayList<MBeanServer> result = new ArrayList<MBeanServer>();
        for (MBeanServer mbs : mBeanServerList) {
            final String name = Util.getMBeanServerSecurityName(mbs);
            if (Util.wildmatch(name, mbeanServerName)) result.add(mbs);
        }
        return result;
    }

    /**
     * Returns the name of the MBeanServer embedded in the MBeanServerId of
     * the given {@code server}. If the given MBeanServerId doesn't contain
     * any name, {@value #DEFAULT_MBEANSERVER_NAME} is returned.
     * The MBeanServerId is expected to be of the form:
     * {@code *[;mbeanServerName=<mbeanServerName>[;*]]}
     * <br>where {@code *} denotes any sequence of characters, and {@code [ ]}
     * indicate optional parts.
     * </p>
     * <p>For instance, if an MBeanServer is created using {@link
     * #createNamedMBeanServer(java.lang.String, java.lang.String)
     * server =
     * MBeanServerFactory.createNamedMBeanServer("com.mycompany.myapp.server1",
     * null)} then {@code MBeanServerFactory.getMBeanServerName(server)}
     * will return {@code "com.mycompany.myapp.server1"} and
     * <code>server.getAttribute({@link
     * javax.management.MBeanServerDelegate#DELEGATE_NAME
     * MBeanServerDelegate.DELEGATE_NAME}, "MBeanServerId")</code> will return
     * something like
     * {@code "myhost_1213353064145;mbeanServerName=com.mycompany.myapp.server1"}.
     * </p>
     * <p>See the section about <a href="#MBeanServerName">MBean Server names</a>
     * above.</p>
     * @param  server A named (or unnamed) MBeanServer.
     * @return the name of the MBeanServer if found, or
     *         {@value #DEFAULT_MBEANSERVER_NAME} if no name is
     *         present in its MBeanServerId, or "*" if its
     *         MBeanServerId couldn't be obtained. Returning "*" means that
     *         only {@link MBeanPermission}s that allow all MBean Server names
     *         will apply to this MBean Server.
     * @see MBeanServerDelegate
     * @since 1.7
     */
    public static String getMBeanServerName(MBeanServer server) {
        return Util.getMBeanServerSecurityName(server);
    }

D
duke 已提交
686 687
    /**
     * Return the ClassLoaderRepository used by the given MBeanServer.
688 689
     * This method is equivalent to {@link
     * MBeanServer#getClassLoaderRepository() server.getClassLoaderRepository()}.
D
duke 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703
     * @param server The MBeanServer under examination. Since JMX 1.2,
     * if <code>server</code> is <code>null</code>, the result is a
     * {@link NullPointerException}.  This behavior differs from what
     * was implemented in JMX 1.1 - where the possibility to use
     * <code>null</code> was deprecated.
     * @return The Class Loader Repository used by the given MBeanServer.
     * @exception SecurityException if there is a SecurityManager and
     * the caller's permissions do not include or imply <code>{@link
     * MBeanPermission}("getClassLoaderRepository")</code>.
     *
     * @exception NullPointerException if <code>server</code> is null.
     *
     **/
    public static ClassLoaderRepository getClassLoaderRepository(
704
            MBeanServer server) {
D
duke 已提交
705 706 707
        return server.getClassLoaderRepository();
    }

708
    private static String mBeanServerId(MBeanServer mbs) {
D
duke 已提交
709 710
        try {
            return (String) mbs.getAttribute(MBeanServerDelegate.DELEGATE_NAME,
711
                    "MBeanServerId");
D
duke 已提交
712
        } catch (JMException e) {
713 714
            JmxProperties.MISC_LOGGER.finest(
                    "Ignoring exception while getting MBeanServerId: "+e);
D
duke 已提交
715 716 717 718 719
            return null;
        }
    }

    private static void checkPermission(String action)
720
    throws SecurityException {
D
duke 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            Permission perm = new MBeanServerPermission(action);
            sm.checkPermission(perm);
        }
    }

    private static synchronized void addMBeanServer(MBeanServer mbs) {
        mBeanServerList.add(mbs);
    }

    private static synchronized void removeMBeanServer(MBeanServer mbs) {
        boolean removed = mBeanServerList.remove(mbs);
        if (!removed) {
            MBEANSERVER_LOGGER.logp(Level.FINER,
                    MBeanServerFactory.class.getName(),
                    "removeMBeanServer(MBeanServer)",
                    "MBeanServer was not in list!");
            throw new IllegalArgumentException("MBeanServer was not in list!");
        }
    }

    private static final ArrayList<MBeanServer> mBeanServerList =
744
            new ArrayList<MBeanServer>();
D
duke 已提交
745 746 747 748 749 750

    /**
     * Load the builder class through the context class loader.
     * @param builderClassName The name of the builder class.
     **/
    private static Class loadBuilderClass(String builderClassName)
751
    throws ClassNotFoundException {
D
duke 已提交
752
        final ClassLoader loader =
753
                Thread.currentThread().getContextClassLoader();
D
duke 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

        if (loader != null) {
            // Try with context class loader
            return loader.loadClass(builderClassName);
        }

        // No context class loader? Try with Class.forName()
        return Class.forName(builderClassName);
    }

    /**
     * Creates the initial builder according to the
     * javax.management.builder.initial System property - if specified.
     * If any checked exception needs to be thrown, it is embedded in
     * a JMRuntimeException.
     **/
    private static MBeanServerBuilder newBuilder(Class builderClass) {
        try {
772 773
            final Object abuilder = builderClass.newInstance();
            return (MBeanServerBuilder)abuilder;
D
duke 已提交
774 775 776 777
        } catch (RuntimeException x) {
            throw x;
        } catch (Exception x) {
            final String msg =
778 779
                    "Failed to instantiate a MBeanServerBuilder from " +
                    builderClass + ": " + x;
D
duke 已提交
780 781 782 783 784 785 786 787 788 789 790
            throw new JMRuntimeException(msg, x);
        }
    }

    /**
     * Instantiate a new builder according to the
     * javax.management.builder.initial System property - if needed.
     **/
    private static synchronized void checkMBeanServerBuilder() {
        try {
            GetPropertyAction act =
791
                    new GetPropertyAction(JMX_INITIAL_BUILDER);
D
duke 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
            String builderClassName = AccessController.doPrivileged(act);

            try {
                final Class newBuilderClass;
                if (builderClassName == null || builderClassName.length() == 0)
                    newBuilderClass = MBeanServerBuilder.class;
                else
                    newBuilderClass = loadBuilderClass(builderClassName);

                // Check whether a new builder needs to be created
                if (builder != null) {
                    final Class builderClass = builder.getClass();
                    if (newBuilderClass == builderClass)
                        return; // no need to create a new builder...
                }

                // Create a new builder
                builder = newBuilder(newBuilderClass);
            } catch (ClassNotFoundException x) {
                final String msg =
812 813
                        "Failed to load MBeanServerBuilder class " +
                        builderClassName + ": " + x;
D
duke 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
                throw new JMRuntimeException(msg, x);
            }
        } catch (RuntimeException x) {
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
                StringBuilder strb = new StringBuilder()
                .append("Failed to instantiate MBeanServerBuilder: ").append(x)
                .append("\n\t\tCheck the value of the ")
                .append(JMX_INITIAL_BUILDER).append(" property.");
                MBEANSERVER_LOGGER.logp(Level.FINEST,
                        MBeanServerFactory.class.getName(),
                        "checkMBeanServerBuilder",
                        strb.toString());
            }
            throw x;
        }
    }

    /**
     * Get the current {@link javax.management.MBeanServerBuilder},
     * as specified by the current value of the
     * javax.management.builder.initial property.
     *
     * This method consults the property and instantiates a new builder
     * if needed.
     *
     * @return the new current {@link javax.management.MBeanServerBuilder}.
     *
     * @exception SecurityException if there is a SecurityManager and
     * the caller's permissions do not make it possible to instantiate
     * a new builder.
     * @exception JMRuntimeException if the builder instantiation
     *   fails with a checked exception -
     *   {@link java.lang.ClassNotFoundException} etc...
     *
     **/
    private static synchronized MBeanServerBuilder getNewMBeanServerBuilder() {
        checkMBeanServerBuilder();
        return builder;
    }

}