Skip to content

Commit cec3ffa

Browse files
committed
Polishing
Issue: SPR-8045 (cherry picked from commit 7eee7d2)
1 parent a45d49c commit cec3ffa

File tree

3 files changed

+73
-72
lines changed

3 files changed

+73
-72
lines changed

spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java

+41-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -96,8 +96,8 @@
9696
* @see org.springframework.jmx.export.assembler.MBeanInfoAssembler
9797
* @see MBeanExporterListener
9898
*/
99-
public class MBeanExporter extends MBeanRegistrationSupport
100-
implements MBeanExportOperations, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean {
99+
public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExportOperations,
100+
BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean {
101101

102102
/**
103103
* Autodetection mode indicating that no autodetection should be used.
@@ -146,6 +146,12 @@ public class MBeanExporter extends MBeanRegistrationSupport
146146
/** Whether to eagerly initialize candidate beans when autodetecting MBeans */
147147
private boolean allowEagerInit = false;
148148

149+
/** Stores the MBeanInfoAssembler to use for this exporter */
150+
private MBeanInfoAssembler assembler = new SimpleReflectiveMBeanInfoAssembler();
151+
152+
/** The strategy to use for creating ObjectNames for an object */
153+
private ObjectNamingStrategy namingStrategy = new KeyNamingStrategy();
154+
149155
/** Indicates whether Spring should modify generated ObjectNames */
150156
private boolean ensureUniqueRuntimeObjectNames = true;
151157

@@ -165,12 +171,6 @@ public class MBeanExporter extends MBeanRegistrationSupport
165171
private final Map<NotificationListenerBean, ObjectName[]> registeredNotificationListeners =
166172
new LinkedHashMap<NotificationListenerBean, ObjectName[]>();
167173

168-
/** Stores the MBeanInfoAssembler to use for this exporter */
169-
private MBeanInfoAssembler assembler = new SimpleReflectiveMBeanInfoAssembler();
170-
171-
/** The strategy to use for creating ObjectNames for an object */
172-
private ObjectNamingStrategy namingStrategy = new KeyNamingStrategy();
173-
174174
/** Stores the ClassLoader to use for generating lazy-init proxies */
175175
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
176176

@@ -283,22 +283,6 @@ public void setNamingStrategy(ObjectNamingStrategy namingStrategy) {
283283
this.namingStrategy = namingStrategy;
284284
}
285285

286-
/**
287-
* Set the {@code MBeanExporterListener}s that should be notified
288-
* of MBean registration and unregistration events.
289-
* @see MBeanExporterListener
290-
*/
291-
public void setListeners(MBeanExporterListener[] listeners) {
292-
this.listeners = listeners;
293-
}
294-
295-
/**
296-
* Set the list of names for beans that should be excluded from autodetection.
297-
*/
298-
public void setExcludedBeans(String[] excludedBeans) {
299-
this.excludedBeans = (excludedBeans != null ? new HashSet<String>(Arrays.asList(excludedBeans)) : null);
300-
}
301-
302286
/**
303287
* Indicates whether Spring should ensure that {@link ObjectName ObjectNames}
304288
* generated by the configured {@link ObjectNamingStrategy} for
@@ -324,6 +308,22 @@ public void setExposeManagedResourceClassLoader(boolean exposeManagedResourceCla
324308
this.exposeManagedResourceClassLoader = exposeManagedResourceClassLoader;
325309
}
326310

311+
/**
312+
* Set the list of names for beans that should be excluded from autodetection.
313+
*/
314+
public void setExcludedBeans(String... excludedBeans) {
315+
this.excludedBeans = (excludedBeans != null ? new HashSet<String>(Arrays.asList(excludedBeans)) : null);
316+
}
317+
318+
/**
319+
* Set the {@code MBeanExporterListener}s that should be notified
320+
* of MBean registration and unregistration events.
321+
* @see MBeanExporterListener
322+
*/
323+
public void setListeners(MBeanExporterListener... listeners) {
324+
this.listeners = listeners;
325+
}
326+
327327
/**
328328
* Set the {@link NotificationListenerBean NotificationListenerBeans}
329329
* containing the
@@ -332,7 +332,7 @@ public void setExposeManagedResourceClassLoader(boolean exposeManagedResourceCla
332332
* @see #setNotificationListenerMappings(java.util.Map)
333333
* @see NotificationListenerBean
334334
*/
335-
public void setNotificationListeners(NotificationListenerBean[] notificationListeners) {
335+
public void setNotificationListeners(NotificationListenerBean... notificationListeners) {
336336
this.notificationListeners = notificationListeners;
337337
}
338338

@@ -398,16 +398,17 @@ public void setBeanFactory(BeanFactory beanFactory) {
398398
//---------------------------------------------------------------------
399399

400400
/**
401-
* Start bean registration automatically when deployed in an
401+
* Kick off bean registration automatically when deployed in an
402402
* {@code ApplicationContext}.
403403
* @see #registerBeans()
404404
*/
405405
public void afterPropertiesSet() {
406406
// If no server was provided then try to find one. This is useful in an environment
407-
// such as JDK 1.5, Tomcat or JBoss where there is already an MBeanServer loaded.
407+
// where there is already an MBeanServer loaded.
408408
if (this.server == null) {
409409
this.server = JmxUtils.locateMBeanServer();
410410
}
411+
411412
try {
412413
logger.info("Registering beans for JMX exposure on startup");
413414
registerBeans();
@@ -512,7 +513,7 @@ protected void registerBeans() {
512513
}
513514
if (mode == AUTODETECT_MBEAN || mode == AUTODETECT_ALL) {
514515
// Autodetect any beans that are already MBeans.
515-
this.logger.debug("Autodetecting user-defined JMX MBeans");
516+
logger.debug("Autodetecting user-defined JMX MBeans");
516517
autodetectMBeans();
517518
}
518519
// Allow the assembler a chance to vote for bean inclusion.
@@ -739,7 +740,7 @@ protected ObjectName getObjectName(Object bean, String beanKey) throws Malformed
739740
* @return whether the class qualifies as an MBean
740741
* @see org.springframework.jmx.support.JmxUtils#isMBean(Class)
741742
*/
742-
protected boolean isMBean(Class beanClass) {
743+
protected boolean isMBean(Class<?> beanClass) {
743744
return JmxUtils.isMBean(beanClass);
744745
}
745746

@@ -753,24 +754,24 @@ protected boolean isMBean(Class beanClass) {
753754
*/
754755
@SuppressWarnings("unchecked")
755756
protected DynamicMBean adaptMBeanIfPossible(Object bean) throws JMException {
756-
Class targetClass = AopUtils.getTargetClass(bean);
757+
Class<?> targetClass = AopUtils.getTargetClass(bean);
757758
if (targetClass != bean.getClass()) {
758-
Class ifc = JmxUtils.getMXBeanInterface(targetClass);
759+
Class<?> ifc = JmxUtils.getMXBeanInterface(targetClass);
759760
if (ifc != null) {
760-
if (!(ifc.isInstance(bean))) {
761+
if (!ifc.isInstance(bean)) {
761762
throw new NotCompliantMBeanException("Managed bean [" + bean +
762763
"] has a target class with an MXBean interface but does not expose it in the proxy");
763764
}
764-
return new StandardMBean(bean, ifc, true);
765+
return new StandardMBean(bean, ((Class<Object>) ifc), true);
765766
}
766767
else {
767768
ifc = JmxUtils.getMBeanInterface(targetClass);
768769
if (ifc != null) {
769-
if (!(ifc.isInstance(bean))) {
770+
if (!ifc.isInstance(bean)) {
770771
throw new NotCompliantMBeanException("Managed bean [" + bean +
771772
"] has a target class with an MBean interface but does not expose it in the proxy");
772773
}
773-
return new StandardMBean(bean, ifc);
774+
return new StandardMBean(bean, ((Class<Object>) ifc));
774775
}
775776
}
776777
}
@@ -840,7 +841,7 @@ private ModelMBeanInfo getMBeanInfo(Object managedBean, String beanKey) throws J
840841
*/
841842
private void autodetectBeans(final AutodetectCapableMBeanInfoAssembler assembler) {
842843
autodetect(new AutodetectCallback() {
843-
public boolean include(Class beanClass, String beanName) {
844+
public boolean include(Class<?> beanClass, String beanName) {
844845
return assembler.includeBean(beanClass, beanName);
845846
}
846847
});
@@ -852,7 +853,7 @@ public boolean include(Class beanClass, String beanName) {
852853
*/
853854
private void autodetectMBeans() {
854855
autodetect(new AutodetectCallback() {
855-
public boolean include(Class beanClass, String beanName) {
856+
public boolean include(Class<?> beanClass, String beanName) {
856857
return isMBean(beanClass);
857858
}
858859
});
@@ -874,7 +875,7 @@ private void autodetect(AutodetectCallback callback) {
874875
for (String beanName : beanNames) {
875876
if (!isExcluded(beanName) && !isBeanDefinitionAbstract(this.beanFactory, beanName)) {
876877
try {
877-
Class beanClass = this.beanFactory.getType(beanName);
878+
Class<?> beanClass = this.beanFactory.getType(beanName);
878879
if (beanClass != null && callback.include(beanClass, beanName)) {
879880
boolean lazyInit = isBeanDefinitionLazyInit(this.beanFactory, beanName);
880881
Object beanInstance = (!lazyInit ? this.beanFactory.getBean(beanName) : null);
@@ -1060,7 +1061,7 @@ private static interface AutodetectCallback {
10601061
* @param beanClass the class of the bean
10611062
* @param beanName the name of the bean
10621063
*/
1063-
boolean include(Class beanClass, String beanName);
1064+
boolean include(Class<?> beanClass, String beanName);
10641065
}
10651066

10661067

spring-context/src/test/java/org/springframework/jmx/AbstractMBeanServerTests.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222

2323
import org.junit.After;
2424
import org.junit.Before;
25+
2526
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
2627
import org.springframework.context.ConfigurableApplicationContext;
2728
import org.springframework.context.support.GenericApplicationContext;
@@ -51,12 +52,14 @@ public abstract class AbstractMBeanServerTests {
5152

5253
protected MBeanServer server;
5354

55+
5456
@Before
5557
public final void setUp() throws Exception {
5658
this.server = MBeanServerFactory.createMBeanServer();
5759
try {
5860
onSetUp();
59-
} catch (Exception ex) {
61+
}
62+
catch (Exception ex) {
6063
releaseServer();
6164
throw ex;
6265
}

0 commit comments

Comments
 (0)