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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial