View Javadoc

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