01: /*
02: * $Id: AutoDiscoveryJmxSupportFactory.java 11234 2008-03-06 23:44:34Z tcarlson $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10: package org.mule.module.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
28: JmxSupportFactory {
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: /** Constructs a new AutoDiscoveryJmxSupportFactory. */
45: protected AutoDiscoveryJmxSupportFactory() {
46: final boolean jmxModernAvailable = isModernSpecAvailable();
47:
48: // tertiary operand does not work anymore after hiererachy refactoring ?!
49: if (jmxModernAvailable) {
50: jmxSupport = new JmxModernSupport();
51: } else {
52: jmxSupport = new JmxLegacySupport();
53: }
54: if (logger.isDebugEnabled()) {
55: logger.debug("JMX support instance is " + jmxSupport);
56: }
57: }
58:
59: /**
60: * Obtain an instance of the factory class.
61: * @return a cached singleton instance
62: */
63: public static JmxSupportFactory getInstance() {
64: return instance;
65: }
66:
67: /**
68: * Will try to detect if JMX 1.2 or later is available, otherwise will fallback
69: * to the JMX 1.1 version of the support class.
70: *
71: * @return matching support class instance
72: * @see JmxLegacySupport
73: */
74: public JmxSupport getJmxSupport() {
75: return this .jmxSupport;
76: }
77:
78: /**
79: * Is JMX 1.2 and up available for use?
80: * @return false if only JMX 1.1 can be used
81: */
82: protected boolean isModernSpecAvailable() {
83: Class clazz = ObjectName.class;
84: // method escape() is available since JMX 1.2
85: Method method = ClassUtils.getMethod(clazz, "quote",
86: new Class[] { String.class });
87:
88: return method != null;
89: }
90:
91: /**
92: * Safe deserialization.
93: * @throws ObjectStreamException will never throw it for this class
94: * @return singleton instance for this classloader/JVM
95: */
96: private Object readResolve() throws ObjectStreamException {
97: return instance;
98: }
99: }
|