View Javadoc

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