|
|
|
@ -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"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -96,8 +96,8 @@ import org.springframework.util.ObjectUtils; |
|
|
|
* @see org.springframework.jmx.export.assembler.MBeanInfoAssembler |
|
|
|
* @see org.springframework.jmx.export.assembler.MBeanInfoAssembler |
|
|
|
* @see MBeanExporterListener |
|
|
|
* @see MBeanExporterListener |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExportOperations, |
|
|
|
implements MBeanExportOperations, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean { |
|
|
|
BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Autodetection mode indicating that no autodetection should be used. |
|
|
|
* Autodetection mode indicating that no autodetection should be used. |
|
|
|
@ -146,6 +146,12 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
/** Whether to eagerly initialize candidate beans when autodetecting MBeans */ |
|
|
|
/** Whether to eagerly initialize candidate beans when autodetecting MBeans */ |
|
|
|
private boolean allowEagerInit = false; |
|
|
|
private boolean allowEagerInit = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Stores the MBeanInfoAssembler to use for this exporter */ |
|
|
|
|
|
|
|
private MBeanInfoAssembler assembler = new SimpleReflectiveMBeanInfoAssembler(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** The strategy to use for creating ObjectNames for an object */ |
|
|
|
|
|
|
|
private ObjectNamingStrategy namingStrategy = new KeyNamingStrategy(); |
|
|
|
|
|
|
|
|
|
|
|
/** Indicates whether Spring should modify generated ObjectNames */ |
|
|
|
/** Indicates whether Spring should modify generated ObjectNames */ |
|
|
|
private boolean ensureUniqueRuntimeObjectNames = true; |
|
|
|
private boolean ensureUniqueRuntimeObjectNames = true; |
|
|
|
|
|
|
|
|
|
|
|
@ -165,12 +171,6 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
private final Map<NotificationListenerBean, ObjectName[]> registeredNotificationListeners = |
|
|
|
private final Map<NotificationListenerBean, ObjectName[]> registeredNotificationListeners = |
|
|
|
new LinkedHashMap<NotificationListenerBean, ObjectName[]>(); |
|
|
|
new LinkedHashMap<NotificationListenerBean, ObjectName[]>(); |
|
|
|
|
|
|
|
|
|
|
|
/** Stores the MBeanInfoAssembler to use for this exporter */ |
|
|
|
|
|
|
|
private MBeanInfoAssembler assembler = new SimpleReflectiveMBeanInfoAssembler(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** The strategy to use for creating ObjectNames for an object */ |
|
|
|
|
|
|
|
private ObjectNamingStrategy namingStrategy = new KeyNamingStrategy(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Stores the ClassLoader to use for generating lazy-init proxies */ |
|
|
|
/** Stores the ClassLoader to use for generating lazy-init proxies */ |
|
|
|
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); |
|
|
|
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); |
|
|
|
|
|
|
|
|
|
|
|
@ -283,22 +283,6 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
this.namingStrategy = namingStrategy; |
|
|
|
this.namingStrategy = namingStrategy; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Set the {@code MBeanExporterListener}s that should be notified |
|
|
|
|
|
|
|
* of MBean registration and unregistration events. |
|
|
|
|
|
|
|
* @see MBeanExporterListener |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void setListeners(MBeanExporterListener[] listeners) { |
|
|
|
|
|
|
|
this.listeners = listeners; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Set the list of names for beans that should be excluded from autodetection. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void setExcludedBeans(String[] excludedBeans) { |
|
|
|
|
|
|
|
this.excludedBeans = (excludedBeans != null ? new HashSet<String>(Arrays.asList(excludedBeans)) : null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Indicates whether Spring should ensure that {@link ObjectName ObjectNames} |
|
|
|
* Indicates whether Spring should ensure that {@link ObjectName ObjectNames} |
|
|
|
* generated by the configured {@link ObjectNamingStrategy} for |
|
|
|
* generated by the configured {@link ObjectNamingStrategy} for |
|
|
|
@ -324,6 +308,22 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
this.exposeManagedResourceClassLoader = exposeManagedResourceClassLoader; |
|
|
|
this.exposeManagedResourceClassLoader = exposeManagedResourceClassLoader; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Set the list of names for beans that should be excluded from autodetection. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void setExcludedBeans(String... excludedBeans) { |
|
|
|
|
|
|
|
this.excludedBeans = (excludedBeans != null ? new HashSet<String>(Arrays.asList(excludedBeans)) : null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Set the {@code MBeanExporterListener}s that should be notified |
|
|
|
|
|
|
|
* of MBean registration and unregistration events. |
|
|
|
|
|
|
|
* @see MBeanExporterListener |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void setListeners(MBeanExporterListener... listeners) { |
|
|
|
|
|
|
|
this.listeners = listeners; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Set the {@link NotificationListenerBean NotificationListenerBeans} |
|
|
|
* Set the {@link NotificationListenerBean NotificationListenerBeans} |
|
|
|
* containing the |
|
|
|
* containing the |
|
|
|
@ -332,7 +332,7 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
* @see #setNotificationListenerMappings(java.util.Map) |
|
|
|
* @see #setNotificationListenerMappings(java.util.Map) |
|
|
|
* @see NotificationListenerBean |
|
|
|
* @see NotificationListenerBean |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setNotificationListeners(NotificationListenerBean[] notificationListeners) { |
|
|
|
public void setNotificationListeners(NotificationListenerBean... notificationListeners) { |
|
|
|
this.notificationListeners = notificationListeners; |
|
|
|
this.notificationListeners = notificationListeners; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -398,16 +398,17 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
//---------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Start bean registration automatically when deployed in an |
|
|
|
* Kick off bean registration automatically when deployed in an |
|
|
|
* {@code ApplicationContext}. |
|
|
|
* {@code ApplicationContext}. |
|
|
|
* @see #registerBeans() |
|
|
|
* @see #registerBeans() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void afterPropertiesSet() { |
|
|
|
public void afterPropertiesSet() { |
|
|
|
// If no server was provided then try to find one. This is useful in an environment
|
|
|
|
// If no server was provided then try to find one. This is useful in an environment
|
|
|
|
// such as JDK 1.5, Tomcat or JBoss where there is already an MBeanServer loaded.
|
|
|
|
// where there is already an MBeanServer loaded.
|
|
|
|
if (this.server == null) { |
|
|
|
if (this.server == null) { |
|
|
|
this.server = JmxUtils.locateMBeanServer(); |
|
|
|
this.server = JmxUtils.locateMBeanServer(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
logger.info("Registering beans for JMX exposure on startup"); |
|
|
|
logger.info("Registering beans for JMX exposure on startup"); |
|
|
|
registerBeans(); |
|
|
|
registerBeans(); |
|
|
|
@ -512,7 +513,7 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
} |
|
|
|
} |
|
|
|
if (mode == AUTODETECT_MBEAN || mode == AUTODETECT_ALL) { |
|
|
|
if (mode == AUTODETECT_MBEAN || mode == AUTODETECT_ALL) { |
|
|
|
// Autodetect any beans that are already MBeans.
|
|
|
|
// Autodetect any beans that are already MBeans.
|
|
|
|
this.logger.debug("Autodetecting user-defined JMX MBeans"); |
|
|
|
logger.debug("Autodetecting user-defined JMX MBeans"); |
|
|
|
autodetectMBeans(); |
|
|
|
autodetectMBeans(); |
|
|
|
} |
|
|
|
} |
|
|
|
// Allow the assembler a chance to vote for bean inclusion.
|
|
|
|
// Allow the assembler a chance to vote for bean inclusion.
|
|
|
|
@ -739,7 +740,7 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
* @return whether the class qualifies as an MBean |
|
|
|
* @return whether the class qualifies as an MBean |
|
|
|
* @see org.springframework.jmx.support.JmxUtils#isMBean(Class) |
|
|
|
* @see org.springframework.jmx.support.JmxUtils#isMBean(Class) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected boolean isMBean(Class beanClass) { |
|
|
|
protected boolean isMBean(Class<?> beanClass) { |
|
|
|
return JmxUtils.isMBean(beanClass); |
|
|
|
return JmxUtils.isMBean(beanClass); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -753,24 +754,24 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
protected DynamicMBean adaptMBeanIfPossible(Object bean) throws JMException { |
|
|
|
protected DynamicMBean adaptMBeanIfPossible(Object bean) throws JMException { |
|
|
|
Class targetClass = AopUtils.getTargetClass(bean); |
|
|
|
Class<?> targetClass = AopUtils.getTargetClass(bean); |
|
|
|
if (targetClass != bean.getClass()) { |
|
|
|
if (targetClass != bean.getClass()) { |
|
|
|
Class ifc = JmxUtils.getMXBeanInterface(targetClass); |
|
|
|
Class<?> ifc = JmxUtils.getMXBeanInterface(targetClass); |
|
|
|
if (ifc != null) { |
|
|
|
if (ifc != null) { |
|
|
|
if (!(ifc.isInstance(bean))) { |
|
|
|
if (!ifc.isInstance(bean)) { |
|
|
|
throw new NotCompliantMBeanException("Managed bean [" + bean + |
|
|
|
throw new NotCompliantMBeanException("Managed bean [" + bean + |
|
|
|
"] has a target class with an MXBean interface but does not expose it in the proxy"); |
|
|
|
"] has a target class with an MXBean interface but does not expose it in the proxy"); |
|
|
|
} |
|
|
|
} |
|
|
|
return new StandardMBean(bean, ifc, true); |
|
|
|
return new StandardMBean(bean, ((Class<Object>) ifc), true); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
ifc = JmxUtils.getMBeanInterface(targetClass); |
|
|
|
ifc = JmxUtils.getMBeanInterface(targetClass); |
|
|
|
if (ifc != null) { |
|
|
|
if (ifc != null) { |
|
|
|
if (!(ifc.isInstance(bean))) { |
|
|
|
if (!ifc.isInstance(bean)) { |
|
|
|
throw new NotCompliantMBeanException("Managed bean [" + bean + |
|
|
|
throw new NotCompliantMBeanException("Managed bean [" + bean + |
|
|
|
"] has a target class with an MBean interface but does not expose it in the proxy"); |
|
|
|
"] has a target class with an MBean interface but does not expose it in the proxy"); |
|
|
|
} |
|
|
|
} |
|
|
|
return new StandardMBean(bean, ifc); |
|
|
|
return new StandardMBean(bean, ((Class<Object>) ifc)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -840,7 +841,7 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void autodetectBeans(final AutodetectCapableMBeanInfoAssembler assembler) { |
|
|
|
private void autodetectBeans(final AutodetectCapableMBeanInfoAssembler assembler) { |
|
|
|
autodetect(new AutodetectCallback() { |
|
|
|
autodetect(new AutodetectCallback() { |
|
|
|
public boolean include(Class beanClass, String beanName) { |
|
|
|
public boolean include(Class<?> beanClass, String beanName) { |
|
|
|
return assembler.includeBean(beanClass, beanName); |
|
|
|
return assembler.includeBean(beanClass, beanName); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
@ -852,7 +853,7 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void autodetectMBeans() { |
|
|
|
private void autodetectMBeans() { |
|
|
|
autodetect(new AutodetectCallback() { |
|
|
|
autodetect(new AutodetectCallback() { |
|
|
|
public boolean include(Class beanClass, String beanName) { |
|
|
|
public boolean include(Class<?> beanClass, String beanName) { |
|
|
|
return isMBean(beanClass); |
|
|
|
return isMBean(beanClass); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
@ -874,7 +875,7 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
for (String beanName : beanNames) { |
|
|
|
for (String beanName : beanNames) { |
|
|
|
if (!isExcluded(beanName) && !isBeanDefinitionAbstract(this.beanFactory, beanName)) { |
|
|
|
if (!isExcluded(beanName) && !isBeanDefinitionAbstract(this.beanFactory, beanName)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
Class beanClass = this.beanFactory.getType(beanName); |
|
|
|
Class<?> beanClass = this.beanFactory.getType(beanName); |
|
|
|
if (beanClass != null && callback.include(beanClass, beanName)) { |
|
|
|
if (beanClass != null && callback.include(beanClass, beanName)) { |
|
|
|
boolean lazyInit = isBeanDefinitionLazyInit(this.beanFactory, beanName); |
|
|
|
boolean lazyInit = isBeanDefinitionLazyInit(this.beanFactory, beanName); |
|
|
|
Object beanInstance = (!lazyInit ? this.beanFactory.getBean(beanName) : null); |
|
|
|
Object beanInstance = (!lazyInit ? this.beanFactory.getBean(beanName) : null); |
|
|
|
@ -1060,7 +1061,7 @@ public class MBeanExporter extends MBeanRegistrationSupport |
|
|
|
* @param beanClass the class of the bean |
|
|
|
* @param beanClass the class of the bean |
|
|
|
* @param beanName the name of the bean |
|
|
|
* @param beanName the name of the bean |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
boolean include(Class beanClass, String beanName); |
|
|
|
boolean include(Class<?> beanClass, String beanName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|