All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glassfish.external.amx.AMXGlassfish Maven / Gradle / Ivy

There is a newer version: 4.0.4
Show newest version
/*
 * Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.glassfish.external.amx;

import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;

import java.io.IOException;


/**
 * AMX behavior specific to Glassfish V3.
 */
public final class AMXGlassfish
{
    public static final String DEFAULT_JMX_DOMAIN = "amx";
    
    /** Default domain support */
    public static final AMXGlassfish DEFAULT = new AMXGlassfish(DEFAULT_JMX_DOMAIN);
    
    private final String     mJMXDomain;
    private final ObjectName mDomainRoot;
    
    /** Anything other than {@link #DEFAULT} is not supported in Glassfish V3 */
    public AMXGlassfish(final String jmxDomain)
    {
        mJMXDomain = jmxDomain;
        mDomainRoot = newObjectName("", "domain-root", null);
    }
    
    /** Return a version string, or null if not running in Glassfish */
    public static String getGlassfishVersion()
    {
        // must all exist as a check to verify that it's Glassfish V3
        final String version = System.getProperty( "glassfish.version" );
        return version;
    }


    /** JMX domain used by AMX MBeans.
     * 

* All MBeans in this domain must be AMX-compliant, see http://tinyurl.com/nryoqp = https://glassfish.dev.java.net/nonav/v3/admin/planning/V3Changes/V3_AMX_SPI.html */ public String amxJMXDomain() { return mJMXDomain; } /** JMX domain used by AMX support MBeans. Private use only */ public String amxSupportDomain() { return amxJMXDomain() + "-support"; } /** name of the Domain Admin Server (DAS) as found in an ObjectName */ public String dasName() { return "server"; } /** name of the Domain Admin Server (DAS) <config> */ public String dasConfig() { return dasName() + "-config"; } /** return the ObjectName of the AMX DomainRoot MBean */ public ObjectName domainRoot() { return mDomainRoot; } /** ObjectName for top-level monitoring MBean (parent of those for each server) */ public ObjectName monitoringRoot() { return newObjectName("/", "mon", null); } /** ObjectName for top-level monitoring MBean for specified server */ public ObjectName serverMon(final String serverName) { return newObjectName("/mon", "server-mon", serverName); } /** ObjectName for top-level monitoring MBean for the DAS. */ public ObjectName serverMonForDAS() { return serverMon( "server" ) ; } /** Make a new AMX ObjectName with unchecked exception. * name must be null to create a singleton ObjectName. * Note that the arguments must not contain the characters * @param pp The parent part * @param type The ObjectName type * @param name The ObjectName name * @return The objectname with pp, type, and (optionally) name. */ public ObjectName newObjectName( final String pp, final String type, final String name) { String props = prop(AMX.PARENT_PATH_KEY, pp) + "," + prop(AMX.TYPE_KEY, type); if (name != null) { props = props + "," + prop(AMX.NAME_KEY, name); } return newObjectName( props); } /** Make a new ObjectName for AMX domain with unchecked exception */ public ObjectName newObjectName(final String s) { String name = s; if ( ! name.startsWith( amxJMXDomain() ) ) { name = amxJMXDomain() + ":" + name; } return AMXUtil.newObjectName( name ); } private static String prop(final String key, final String value) { return key + "=" + value; } /** ObjectName for {@link BootAMXMBean} */ public ObjectName getBootAMXMBeanObjectName() { return AMXUtil.newObjectName( amxSupportDomain() + ":type=boot-amx" ); } /** Invoke the bootAMX() method on {@link BootAMXMBean}. Upon return, AMX continues to load. A cilent should call {@link invokeWaitAMXReady} prior to use. */ public void invokeBootAMX(final MBeanServerConnection conn) { // start AMX and wait for it to be ready try { conn.invoke( getBootAMXMBeanObjectName(), BootAMXMBean.BOOT_AMX_OPERATION_NAME, null, null); } catch (final Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** Invoke the waitAMXReady() method on the DomainRoot MBean, which must already be loaded. */ private static void invokeWaitAMXReady(final MBeanServerConnection conn, final ObjectName objectName) { try { conn.invoke( objectName, "waitAMXReady", null, null ); } catch( final Exception e ) { throw new RuntimeException(e); } } /** Listen for the registration of AMX DomainRoot Listening starts automatically. */ public MBeanListener listenForDomainRoot( final MBeanServerConnection server, final T callback) { final MBeanListener listener = new MBeanListener( server, domainRoot(), callback); listener.startListening(); return listener; } private static final class WaitForDomainRootListenerCallback extends MBeanListener.CallbackImpl { private final MBeanServerConnection mConn; public WaitForDomainRootListenerCallback( final MBeanServerConnection conn ) { mConn = conn; } @Override public void mbeanRegistered(final ObjectName objectName, final MBeanListener listener) { super.mbeanRegistered(objectName,listener); invokeWaitAMXReady(mConn, objectName); mLatch.countDown(); } } /** Wait until AMX has loaded and is ready for use.

This will not cause AMX to load; it will block forever until AMX is ready. In other words, don't call this method unless it's a convenient thread that can wait forever. */ public ObjectName waitAMXReady( final MBeanServerConnection server) { final WaitForDomainRootListenerCallback callback = new WaitForDomainRootListenerCallback(server); listenForDomainRoot( server, callback ); callback.await(); return callback.getRegistered(); } /** Listen for the registration of the {@link BootAMXMBean}. Listening starts automatically. See {@link AMXBooter#BootAMXCallback}. */ public MBeanListener listenForBootAMX( final MBeanServerConnection server, final T callback) { final MBeanListener listener = new MBeanListener( server, getBootAMXMBeanObjectName(), callback); listener.startListening(); return listener; } /** Callback for {@link MBeanListener} that waits for the BootAMXMBean to appear; it always will load early in server startup. Once it has loaded, AMX can be booted via {@link #bootAMX}. A client should normally just call {@link #bootAMX}, but this callback may be suclassed if desired, and used as a trigger to boot AMX and then take other dependent actions. */ public static class BootAMXCallback extends MBeanListener.CallbackImpl { private final MBeanServerConnection mConn; public BootAMXCallback(final MBeanServerConnection conn) { mConn = conn; } @Override public void mbeanRegistered(final ObjectName objectName, final MBeanListener listener) { super.mbeanRegistered(objectName, listener); mLatch.countDown(); } } /** Ensure that AMX is loaded and ready to use. This method returns only when all AMX subsystems have been loaded. It can be called more than once without ill effect, subsequent calls are ignored. @param conn connection to the MBeanServer @return the ObjectName of the domain-root MBean */ public ObjectName bootAMX(final MBeanServerConnection conn) throws IOException { final ObjectName domainRoot = domainRoot(); if ( !conn.isRegistered( domainRoot ) ) { // wait for the BootAMXMBean to be available (loads at startup) final BootAMXCallback callback = new BootAMXCallback(conn); listenForBootAMX(conn, callback); callback.await(); // block until the MBean appears invokeBootAMX(conn); final WaitForDomainRootListenerCallback drCallback = new WaitForDomainRootListenerCallback(conn); listenForDomainRoot(conn, drCallback); drCallback.await(); invokeWaitAMXReady(conn, domainRoot); } else { invokeWaitAMXReady(conn, domainRoot ); } return domainRoot; } public ObjectName bootAMX(final MBeanServer server) { try { return bootAMX( (MBeanServerConnection)server); } catch( final IOException e ) { throw new RuntimeException(e); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy