View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.management.support;
8   
9   import org.mule.util.ClassUtils;
10  
11  import java.io.ObjectStreamException;
12  import java.lang.reflect.Method;
13  
14  import javax.management.ObjectName;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  /**
20   * Will discover if newer JMX version is available, otherwise fallback to JMX 1.1
21   * style support.
22   */
23  // @Immutable
24  public class AutoDiscoveryJmxSupportFactory implements JmxSupportFactory
25  {
26      /**
27       * Safe initialization for a singleton.
28       */
29      private static final JmxSupportFactory instance = new AutoDiscoveryJmxSupportFactory();
30  
31      /**
32       * logger used by this class
33       */
34      private transient Log logger = LogFactory.getLog(getClass());
35  
36      /**
37       * A cached JMX support class instance.
38       */
39      private JmxSupport jmxSupport;
40  
41  
42      /** Constructs a new AutoDiscoveryJmxSupportFactory. */
43      protected AutoDiscoveryJmxSupportFactory ()
44      {
45          final boolean jmxModernAvailable = isModernSpecAvailable();
46  
47          // tertiary operand does not work anymore after hiererachy refactoring ?!
48          if (jmxModernAvailable)
49          {
50              jmxSupport = new JmxModernSupport();
51          }
52          else
53          {
54              jmxSupport = new JmxLegacySupport();
55          }
56          if (logger.isDebugEnabled())
57          {
58              logger.debug("JMX support instance is " + jmxSupport);
59          }
60      }
61  
62      /**
63       * Obtain an instance of the factory class.
64       * @return a cached singleton instance
65       */
66      public static JmxSupportFactory getInstance()
67      {
68          return instance;
69      }
70  
71  
72      /**
73       * Will try to detect if JMX 1.2 or later is available, otherwise will fallback
74       * to the JMX 1.1 version of the support class.
75       *
76       * @return matching support class instance
77       * @see JmxLegacySupport
78       */
79      public JmxSupport getJmxSupport ()
80      {
81          return this.jmxSupport;
82      }
83  
84      /**
85       * Is JMX 1.2 and up available for use?
86       * @return false if only JMX 1.1 can be used
87       */
88      protected boolean isModernSpecAvailable ()
89      {
90          Class<ObjectName> clazz = ObjectName.class;
91          // method escape() is available since JMX 1.2
92          Method method = ClassUtils.getMethod(clazz, "quote", new Class[]{String.class});
93  
94          return  method != null;
95      }
96  
97      /**
98       * Safe deserialization.
99       * @throws ObjectStreamException will never throw it for this class
100      * @return singleton instance for this classloader/JVM
101      */
102     private Object readResolve() throws ObjectStreamException
103     {
104         return instance;
105     }
106 }