src/share/jaxws_classes/com/sun/org/glassfish/external/amx/AMXGlassfish.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26
aoqi@0 27 package com.sun.org.glassfish.external.amx;
aoqi@0 28
aoqi@0 29 import javax.management.ObjectName;
aoqi@0 30 import javax.management.MBeanServer;
aoqi@0 31 import javax.management.MBeanServerConnection;
aoqi@0 32
aoqi@0 33 import java.io.IOException;
aoqi@0 34
aoqi@0 35
aoqi@0 36 /**
aoqi@0 37 * AMX behavior specific to Glassfish V3.
aoqi@0 38 */
aoqi@0 39 public final class AMXGlassfish
aoqi@0 40 {
aoqi@0 41 public static final String DEFAULT_JMX_DOMAIN = "amx";
aoqi@0 42
aoqi@0 43 /** Default domain support */
aoqi@0 44 public static final AMXGlassfish DEFAULT = new AMXGlassfish(DEFAULT_JMX_DOMAIN);
aoqi@0 45
aoqi@0 46 private final String mJMXDomain;
aoqi@0 47 private final ObjectName mDomainRoot;
aoqi@0 48
aoqi@0 49 /** Anything other than {@link #DEFAULT} is not supported in Glassfish V3 */
aoqi@0 50 public AMXGlassfish(final String jmxDomain)
aoqi@0 51 {
aoqi@0 52 mJMXDomain = jmxDomain;
aoqi@0 53 mDomainRoot = newObjectName("", "domain-root", null);
aoqi@0 54 }
aoqi@0 55
aoqi@0 56 /** Return a version string, or null if not running in Glassfish */
aoqi@0 57 public static String getGlassfishVersion()
aoqi@0 58 {
aoqi@0 59 // must all exist as a check to verify that it's Glassfish V3
aoqi@0 60 final String version = System.getProperty( "glassfish.version" );
aoqi@0 61 return version;
aoqi@0 62 }
aoqi@0 63
aoqi@0 64
aoqi@0 65 /** JMX domain used by AMX MBeans.
aoqi@0 66 * <p>
aoqi@0 67 * All MBeans in this domain must be AMX-compliant, see http://tinyurl.com/nryoqp =
aoqi@0 68 https://glassfish.dev.java.net/nonav/v3/admin/planning/V3Changes/V3_AMX_SPI.html
aoqi@0 69 */
aoqi@0 70 public String amxJMXDomain()
aoqi@0 71 {
aoqi@0 72 return mJMXDomain;
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 /** JMX domain used by AMX support MBeans. Private use only */
aoqi@0 76 public String amxSupportDomain()
aoqi@0 77 {
aoqi@0 78 return amxJMXDomain() + "-support";
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 /** name of the Domain Admin Server (DAS) as found in an ObjectName */
aoqi@0 82 public String dasName()
aoqi@0 83 {
aoqi@0 84 return "server";
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 /** name of the Domain Admin Server (DAS) &lt;config> */
aoqi@0 88 public String dasConfig()
aoqi@0 89 {
aoqi@0 90 return dasName() + "-config";
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 /** return the ObjectName of the AMX DomainRoot MBean */
aoqi@0 94 public ObjectName domainRoot()
aoqi@0 95 {
aoqi@0 96 return mDomainRoot;
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 /** ObjectName for top-level monitoring MBean (parent of those for each server) */
aoqi@0 100 public ObjectName monitoringRoot()
aoqi@0 101 {
aoqi@0 102 return newObjectName("/", "mon", null);
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 /** ObjectName for top-level monitoring MBean for specified server */
aoqi@0 106 public ObjectName serverMon(final String serverName)
aoqi@0 107 {
aoqi@0 108 return newObjectName("/mon", "server-mon", serverName);
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 /** ObjectName for top-level monitoring MBean for the DAS. */
aoqi@0 112 public ObjectName serverMonForDAS() {
aoqi@0 113 return serverMon( "server" ) ;
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 /** Make a new AMX ObjectName with unchecked exception.
aoqi@0 117 * name must be null to create a singleton ObjectName.
aoqi@0 118 * Note that the arguments must not contain the characters
aoqi@0 119 * @param pp The parent part
aoqi@0 120 * @param type The ObjectName type
aoqi@0 121 * @param name The ObjectName name
aoqi@0 122 * @return The objectname with pp, type, and (optionally) name.
aoqi@0 123 */
aoqi@0 124 public ObjectName newObjectName(
aoqi@0 125 final String pp,
aoqi@0 126 final String type,
aoqi@0 127 final String name)
aoqi@0 128 {
aoqi@0 129 String props = prop(AMX.PARENT_PATH_KEY, pp) + "," + prop(AMX.TYPE_KEY, type);
aoqi@0 130 if (name != null) {
aoqi@0 131 props = props + "," + prop(AMX.NAME_KEY, name);
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 return newObjectName( props);
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 /** Make a new ObjectName for AMX domain with unchecked exception */
aoqi@0 138 public ObjectName newObjectName(final String s)
aoqi@0 139 {
aoqi@0 140 String name = s;
aoqi@0 141 if ( ! name.startsWith( amxJMXDomain() ) ) {
aoqi@0 142 name = amxJMXDomain() + ":" + name;
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 return AMXUtil.newObjectName( name );
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 private static String prop(final String key, final String value)
aoqi@0 149 {
aoqi@0 150 return key + "=" + value;
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 /**
aoqi@0 154 ObjectName for {@link BootAMXMBean}
aoqi@0 155 */
aoqi@0 156 public ObjectName getBootAMXMBeanObjectName()
aoqi@0 157 {
aoqi@0 158 return AMXUtil.newObjectName( amxSupportDomain() + ":type=boot-amx" );
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 /**
aoqi@0 162 Invoke the bootAMX() method on {@link BootAMXMBean}. Upon return,
aoqi@0 163 AMX continues to load.
aoqi@0 164 A cilent should call {@link invokeWaitAMXReady} prior to use.
aoqi@0 165 */
aoqi@0 166 public void invokeBootAMX(final MBeanServerConnection conn)
aoqi@0 167 {
aoqi@0 168 // start AMX and wait for it to be ready
aoqi@0 169 try
aoqi@0 170 {
aoqi@0 171 conn.invoke( getBootAMXMBeanObjectName(), BootAMXMBean.BOOT_AMX_OPERATION_NAME, null, null);
aoqi@0 172 }
aoqi@0 173 catch (final Exception e)
aoqi@0 174 {
aoqi@0 175 e.printStackTrace();
aoqi@0 176 throw new RuntimeException(e);
aoqi@0 177 }
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 /**
aoqi@0 181 Invoke the waitAMXReady() method on the DomainRoot MBean, which must already be loaded.
aoqi@0 182 */
aoqi@0 183 private static void invokeWaitAMXReady(final MBeanServerConnection conn, final ObjectName objectName)
aoqi@0 184 {
aoqi@0 185 try
aoqi@0 186 {
aoqi@0 187 conn.invoke( objectName, "waitAMXReady", null, null );
aoqi@0 188 }
aoqi@0 189 catch( final Exception e )
aoqi@0 190 {
aoqi@0 191 throw new RuntimeException(e);
aoqi@0 192 }
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 /**
aoqi@0 196 Listen for the registration of AMX DomainRoot
aoqi@0 197 Listening starts automatically.
aoqi@0 198 */
aoqi@0 199 public <T extends MBeanListener.Callback> MBeanListener<T> listenForDomainRoot(
aoqi@0 200 final MBeanServerConnection server,
aoqi@0 201 final T callback)
aoqi@0 202 {
aoqi@0 203 final MBeanListener<T> listener = new MBeanListener<T>( server, domainRoot(), callback);
aoqi@0 204 listener.startListening();
aoqi@0 205 return listener;
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 private static final class WaitForDomainRootListenerCallback extends MBeanListener.CallbackImpl {
aoqi@0 209 private final MBeanServerConnection mConn;
aoqi@0 210
aoqi@0 211 public WaitForDomainRootListenerCallback( final MBeanServerConnection conn ) {
aoqi@0 212 mConn = conn;
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 @Override
aoqi@0 216 public void mbeanRegistered(final ObjectName objectName, final MBeanListener listener) {
aoqi@0 217 super.mbeanRegistered(objectName,listener);
aoqi@0 218 invokeWaitAMXReady(mConn, objectName);
aoqi@0 219 mLatch.countDown();
aoqi@0 220 }
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 /**
aoqi@0 224 Wait until AMX has loaded and is ready for use.
aoqi@0 225 <p>
aoqi@0 226 This will <em>not</em> cause AMX to load; it will block forever until AMX is ready. In other words,
aoqi@0 227 don't call this method unless it's a convenient thread that can wait forever.
aoqi@0 228 */
aoqi@0 229 public ObjectName waitAMXReady( final MBeanServerConnection server)
aoqi@0 230 {
aoqi@0 231 final WaitForDomainRootListenerCallback callback = new WaitForDomainRootListenerCallback(server);
aoqi@0 232 listenForDomainRoot( server, callback );
aoqi@0 233 callback.await();
aoqi@0 234 return callback.getRegistered();
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 /**
aoqi@0 238 Listen for the registration of the {@link BootAMXMBean}.
aoqi@0 239 Listening starts automatically. See {@link AMXBooter#BootAMXCallback}.
aoqi@0 240 */
aoqi@0 241 public <T extends MBeanListener.Callback> MBeanListener<T> listenForBootAMX(
aoqi@0 242 final MBeanServerConnection server,
aoqi@0 243 final T callback)
aoqi@0 244 {
aoqi@0 245 final MBeanListener<T> listener = new MBeanListener<T>( server, getBootAMXMBeanObjectName(), callback);
aoqi@0 246 listener.startListening();
aoqi@0 247 return listener;
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 /**
aoqi@0 251 Callback for {@link MBeanListener} that waits for the BootAMXMBean to appear;
aoqi@0 252 it always will load early in server startup. Once it has loaded, AMX can be booted
aoqi@0 253 via {@link #bootAMX}. A client should normally just call {@link #bootAMX}, but
aoqi@0 254 this callback may be suclassed if desired, and used as a trigger to
aoqi@0 255 boot AMX and then take other dependent actions.
aoqi@0 256 */
aoqi@0 257 public static class BootAMXCallback extends MBeanListener.CallbackImpl
aoqi@0 258 {
aoqi@0 259 private final MBeanServerConnection mConn;
aoqi@0 260 public BootAMXCallback(final MBeanServerConnection conn)
aoqi@0 261 {
aoqi@0 262 mConn = conn;
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 @Override
aoqi@0 266 public void mbeanRegistered(final ObjectName objectName, final MBeanListener listener)
aoqi@0 267 {
aoqi@0 268 super.mbeanRegistered(objectName, listener);
aoqi@0 269 mLatch.countDown();
aoqi@0 270 }
aoqi@0 271 }
aoqi@0 272
aoqi@0 273 /**
aoqi@0 274 Ensure that AMX is loaded and ready to use. This method returns only when all
aoqi@0 275 AMX subsystems have been loaded.
aoqi@0 276 It can be called more than once without ill effect, subsequent calls are ignored.
aoqi@0 277 @param conn connection to the MBeanServer
aoqi@0 278 @return the ObjectName of the domain-root MBean
aoqi@0 279 */
aoqi@0 280 public ObjectName bootAMX(final MBeanServerConnection conn)
aoqi@0 281 throws IOException
aoqi@0 282 {
aoqi@0 283 final ObjectName domainRoot = domainRoot();
aoqi@0 284
aoqi@0 285 if ( !conn.isRegistered( domainRoot ) )
aoqi@0 286 {
aoqi@0 287 // wait for the BootAMXMBean to be available (loads at startup)
aoqi@0 288 final BootAMXCallback callback = new BootAMXCallback(conn);
aoqi@0 289 listenForBootAMX(conn, callback);
aoqi@0 290 callback.await(); // block until the MBean appears
aoqi@0 291
aoqi@0 292 invokeBootAMX(conn);
aoqi@0 293
aoqi@0 294 final WaitForDomainRootListenerCallback drCallback = new WaitForDomainRootListenerCallback(conn);
aoqi@0 295 listenForDomainRoot(conn, drCallback);
aoqi@0 296 drCallback.await();
aoqi@0 297
aoqi@0 298 invokeWaitAMXReady(conn, domainRoot);
aoqi@0 299 }
aoqi@0 300 else
aoqi@0 301 {
aoqi@0 302 invokeWaitAMXReady(conn, domainRoot );
aoqi@0 303 }
aoqi@0 304 return domainRoot;
aoqi@0 305 }
aoqi@0 306
aoqi@0 307 public ObjectName bootAMX(final MBeanServer server)
aoqi@0 308 {
aoqi@0 309 try
aoqi@0 310 {
aoqi@0 311 return bootAMX( (MBeanServerConnection)server);
aoqi@0 312 }
aoqi@0 313 catch( final IOException e )
aoqi@0 314 {
aoqi@0 315 throw new RuntimeException(e);
aoqi@0 316 }
aoqi@0 317 }
aoqi@0 318 }

mercurial