Skip to content

Commit

Permalink
Spring's JMX support can rely on native MXBean detection on Java 6+
Browse files Browse the repository at this point in the history
Issue: SPR-12574
(cherry picked from commit 0919a15)
  • Loading branch information
jhoeller committed Dec 29, 2014
1 parent 929c596 commit 4a27a98
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -263,20 +263,13 @@ public void prepare() {
}
this.invocationHandler = null;
if (this.useStrictCasing) {
// Use the JDK's own MBeanServerInvocationHandler,
// in particular for native MXBean support on Java 6.
if (JmxUtils.isMXBeanSupportAvailable()) {
this.invocationHandler =
new MBeanServerInvocationHandler(this.serverToUse, this.objectName,
(this.managementInterface != null && JMX.isMXBeanInterface(this.managementInterface)));
}
else {
this.invocationHandler = new MBeanServerInvocationHandler(this.serverToUse, this.objectName);
}
// Use the JDK's own MBeanServerInvocationHandler, in particular for native MXBean support.
this.invocationHandler = new MBeanServerInvocationHandler(this.serverToUse, this.objectName,
(this.managementInterface != null && JMX.isMXBeanInterface(this.managementInterface)));
}
else {
// Non-strict casing can only be achieved through custom
// invocation handling. Only partial MXBean support available!
// Non-strict casing can only be achieved through custom invocation handling.
// Only partial MXBean support available!
retrieveMBeanInfo();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,10 +22,10 @@
import java.util.Hashtable;
import java.util.List;
import javax.management.DynamicMBean;
import javax.management.JMX;
import javax.management.MBeanParameterInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MXBean;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

Expand Down Expand Up @@ -59,16 +59,6 @@ public abstract class JmxUtils {
*/
private static final String MBEAN_SUFFIX = "MBean";

/**
* Suffix used to identify a Java 6 MXBean interface.
*/
private static final String MXBEAN_SUFFIX = "MXBean";

private static final String MXBEAN_ANNOTATION_CLASS_NAME = "javax.management.MXBean";


private static final boolean mxBeanAnnotationAvailable =
ClassUtils.isPresent(MXBEAN_ANNOTATION_CLASS_NAME, JmxUtils.class.getClassLoader());

private static final Log logger = LogFactory.getLog(JmxUtils.class);

Expand Down Expand Up @@ -304,14 +294,7 @@ public static Class<?> getMXBeanInterface(Class<?> clazz) {
}
Class<?>[] implementedInterfaces = clazz.getInterfaces();
for (Class<?> iface : implementedInterfaces) {
boolean isMxBean = iface.getName().endsWith(MXBEAN_SUFFIX);
if (mxBeanAnnotationAvailable) {
Boolean checkResult = MXBeanChecker.evaluateMXBeanAnnotation(iface);
if (checkResult != null) {
isMxBean = checkResult;
}
}
if (isMxBean) {
if (JMX.isMXBeanInterface(iface)) {
return iface;
}
}
Expand All @@ -322,21 +305,11 @@ public static Class<?> getMXBeanInterface(Class<?> clazz) {
* Check whether MXBean support is available, i.e. whether we're running
* on Java 6 or above.
* @return {@code true} if available; {@code false} otherwise
* @deprecated as of Spring 4.0, since Java 6 is required anyway now
*/
@Deprecated
public static boolean isMXBeanSupportAvailable() {
return mxBeanAnnotationAvailable;
}


/**
* Inner class to avoid a Java 6 dependency.
*/
private static class MXBeanChecker {

public static Boolean evaluateMXBeanAnnotation(Class<?> iface) {
MXBean mxBean = iface.getAnnotation(MXBean.class);
return (mxBean != null ? mxBean.value() : null);
}
return true;
}

}

0 comments on commit 4a27a98

Please sign in to comment.