View Javadoc
1   package com.atlassian.plugin.jmx;
2   
3   import com.google.common.collect.ImmutableMap;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   
7   import javax.management.InstanceAlreadyExistsException;
8   import javax.management.InstanceNotFoundException;
9   import javax.management.MBeanRegistrationException;
10  import javax.management.MalformedObjectNameException;
11  import javax.management.NotCompliantMBeanException;
12  import javax.management.ObjectInstance;
13  import javax.management.ObjectName;
14  import java.lang.management.ManagementFactory;
15  import java.util.Hashtable;
16  import java.util.concurrent.atomic.AtomicInteger;
17  
18  public class JmxUtil {
19      private static final Logger log = LoggerFactory.getLogger(JmxUtil.class);
20  
21      private static final String DOMAIN = "com.atlassian.plugin";
22  
23      /**
24       * Obtain a JMX ObjectName for a new instance of a given type.
25       *
26       * @param counter a counter used to ensure a unique instance id amongst instances with given type.
27       * @param type    the friendly type name for the instance.
28       * @return a JMX ObjectName for the instance, or null if creation fails.
29       */
30      public static ObjectName objectName(final AtomicInteger counter, final String type) {
31          try {
32              final String instance = Integer.toString(counter.getAndIncrement());
33              return new ObjectName(DOMAIN, new Hashtable<String, String>(ImmutableMap.of("instance", instance, "type", type)));
34          } catch (final MalformedObjectNameException emon) {
35              log.warn("Failed to create ObjectName: ", emon.getMessage());
36          }
37          return null;
38      }
39  
40      /**
41       * Register a JMX MBean against a given ObjectName.
42       * <p>
43       * This is simply an error logging wrapper around {@link javax.management.MBeanServer#registerMBean}.
44       *
45       * @param object     the MBean implementation to register.
46       * @param objectName the ObjectName to register against.
47       * @return the ObjectInstance encapsulating the registration, or null if registration failed.
48       */
49      public static ObjectInstance register(final Object object, final ObjectName objectName) {
50          try {
51              if (null != objectName) {
52                  return ManagementFactory.getPlatformMBeanServer().registerMBean(object, objectName);
53              } else {
54                  log.warn("Failed to register, objectName null");
55              }
56          } catch (final InstanceAlreadyExistsException eiae) {
57              log.warn("Failed to register, instance already exists: ", eiae.getMessage());
58          } catch (final MBeanRegistrationException emr) {
59              log.warn("Failed to register, registration exception: ", emr.getMessage());
60          } catch (final NotCompliantMBeanException encm) {
61              log.warn("Failed to register, not compliant: ", encm.getMessage());
62          }
63          return null;
64      }
65  
66      /**
67       * Unregister the JMX MBean with a given ObjectName.
68       * <p>
69       * This is simply an error logging wrapper around {@link javax.management.MBeanServer#unregisterMBean}.
70       *
71       * @param objectName the ObjectName of the MBean to unregister.
72       * @return true if unregister succeeded, or false if unregister failed.
73       */
74      public static boolean unregister(final ObjectName objectName) {
75          try {
76              ManagementFactory.getPlatformMBeanServer().unregisterMBean(objectName);
77              return true;
78          } catch (final InstanceNotFoundException einf) {
79              log.warn("Failed to unregister, instance not found: ", einf.getMessage());
80          } catch (final MBeanRegistrationException emr) {
81              log.warn("Failed to unregister, registration exception: ", emr.getMessage());
82          }
83          return false;
84      }
85  }