Skip to content

Commit 7eee7d2

Browse files
committed
Polishing
Issue: SPR-8045
1 parent d72ee80 commit 7eee7d2

File tree

3 files changed

+63
-64
lines changed

3 files changed

+63
-64
lines changed

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

Lines changed: 31 additions & 32 deletions
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.
@@ -25,7 +25,6 @@
2525
import java.util.List;
2626
import java.util.Map;
2727
import java.util.Set;
28-
2928
import javax.management.DynamicMBean;
3029
import javax.management.JMException;
3130
import javax.management.MBeanException;
@@ -82,8 +81,7 @@
8281
* via the {@link #setListeners(MBeanExporterListener[]) listeners} property, allowing
8382
* application code to be notified of MBean registration and unregistration events.
8483
*
85-
* <p>This exporter is compatible with JMX 1.2 on Java 5 and above.
86-
* As of Spring 2.5, it also autodetects and exports Java 6 MXBeans.
84+
* <p>This exporter is compatible with MBeans and MXBeans on Java 6 and above.
8785
*
8886
* @author Rob Harrop
8987
* @author Juergen Hoeller
@@ -97,8 +95,8 @@
9795
* @see org.springframework.jmx.export.assembler.MBeanInfoAssembler
9896
* @see MBeanExporterListener
9997
*/
100-
public class MBeanExporter extends MBeanRegistrationSupport
101-
implements MBeanExportOperations, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean {
98+
public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExportOperations,
99+
BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean {
102100

103101
/**
104102
* Autodetection mode indicating that no autodetection should be used.
@@ -147,6 +145,12 @@ public class MBeanExporter extends MBeanRegistrationSupport
147145
/** Whether to eagerly initialize candidate beans when autodetecting MBeans */
148146
private boolean allowEagerInit = false;
149147

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

@@ -166,12 +170,6 @@ public class MBeanExporter extends MBeanRegistrationSupport
166170
private final Map<NotificationListenerBean, ObjectName[]> registeredNotificationListeners =
167171
new LinkedHashMap<NotificationListenerBean, ObjectName[]>();
168172

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

@@ -284,22 +282,6 @@ public void setNamingStrategy(ObjectNamingStrategy namingStrategy) {
284282
this.namingStrategy = namingStrategy;
285283
}
286284

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

310+
/**
311+
* Set the list of names for beans that should be excluded from autodetection.
312+
*/
313+
public void setExcludedBeans(String... excludedBeans) {
314+
this.excludedBeans = (excludedBeans != null ? new HashSet<String>(Arrays.asList(excludedBeans)) : null);
315+
}
316+
317+
/**
318+
* Set the {@code MBeanExporterListener}s that should be notified
319+
* of MBean registration and unregistration events.
320+
* @see MBeanExporterListener
321+
*/
322+
public void setListeners(MBeanExporterListener... listeners) {
323+
this.listeners = listeners;
324+
}
325+
328326
/**
329327
* Set the {@link NotificationListenerBean NotificationListenerBeans}
330328
* containing the
@@ -333,7 +331,7 @@ public void setExposeManagedResourceClassLoader(boolean exposeManagedResourceCla
333331
* @see #setNotificationListenerMappings(java.util.Map)
334332
* @see NotificationListenerBean
335333
*/
336-
public void setNotificationListeners(NotificationListenerBean[] notificationListeners) {
334+
public void setNotificationListeners(NotificationListenerBean... notificationListeners) {
337335
this.notificationListeners = notificationListeners;
338336
}
339337

@@ -401,17 +399,18 @@ public void setBeanFactory(BeanFactory beanFactory) {
401399
//---------------------------------------------------------------------
402400

403401
/**
404-
* Start bean registration automatically when deployed in an
402+
* Kick off bean registration automatically when deployed in an
405403
* {@code ApplicationContext}.
406404
* @see #registerBeans()
407405
*/
408406
@Override
409407
public void afterPropertiesSet() {
410408
// If no server was provided then try to find one. This is useful in an environment
411-
// such as JDK 1.5, Tomcat or JBoss where there is already an MBeanServer loaded.
409+
// where there is already an MBeanServer loaded.
412410
if (this.server == null) {
413411
this.server = JmxUtils.locateMBeanServer();
414412
}
413+
415414
try {
416415
logger.info("Registering beans for JMX exposure on startup");
417416
registerBeans();
@@ -520,7 +519,7 @@ protected void registerBeans() {
520519
}
521520
if (mode == AUTODETECT_MBEAN || mode == AUTODETECT_ALL) {
522521
// Autodetect any beans that are already MBeans.
523-
this.logger.debug("Autodetecting user-defined JMX MBeans");
522+
logger.debug("Autodetecting user-defined JMX MBeans");
524523
autodetectMBeans();
525524
}
526525
// Allow the assembler a chance to vote for bean inclusion.

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

Lines changed: 5 additions & 2 deletions
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
}

spring-context/src/test/java/org/springframework/jmx/export/MBeanExporterTests.java

Lines changed: 27 additions & 30 deletions
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.
@@ -21,7 +21,6 @@
2121
import java.util.HashMap;
2222
import java.util.List;
2323
import java.util.Map;
24-
2524
import javax.management.Attribute;
2625
import javax.management.InstanceNotFoundException;
2726
import javax.management.JMException;
@@ -34,6 +33,7 @@
3433
import javax.management.modelmbean.ModelMBeanInfo;
3534

3635
import org.junit.Test;
36+
3737
import org.springframework.aop.framework.ProxyFactory;
3838
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
3939
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -68,25 +68,10 @@ public final class MBeanExporterTests extends AbstractMBeanServerTests {
6868

6969
private static final String OBJECT_NAME = "spring:test=jmxMBeanAdaptor";
7070

71-
@SuppressWarnings({ "rawtypes", "unchecked" })
72-
@Test
73-
public void testRegisterNonNotificationListenerType() throws Exception {
74-
Map listeners = new HashMap();
75-
// put a non-NotificationListener instance in as a value...
76-
listeners.put("*", this);
77-
MBeanExporter exporter = new MBeanExporter();
78-
try {
79-
exporter.setNotificationListenerMappings(listeners);
80-
fail("Must have thrown a ClassCastException when registering a non-NotificationListener instance as a NotificationListener.");
81-
}
82-
catch (ClassCastException expected) {
83-
}
84-
}
8571

86-
@SuppressWarnings({ "rawtypes", "unchecked" })
8772
@Test
8873
public void testRegisterNullNotificationListenerType() throws Exception {
89-
Map listeners = new HashMap();
74+
Map<String, NotificationListener> listeners = new HashMap<String, NotificationListener>();
9075
// put null in as a value...
9176
listeners.put("*", null);
9277
MBeanExporter exporter = new MBeanExporter();
@@ -98,10 +83,9 @@ public void testRegisterNullNotificationListenerType() throws Exception {
9883
}
9984
}
10085

101-
@SuppressWarnings({ "rawtypes", "unchecked" })
10286
@Test
10387
public void testRegisterNotificationListenerForNonExistentMBean() throws Exception {
104-
Map listeners = new HashMap();
88+
Map<String, NotificationListener> listeners = new HashMap<String, NotificationListener>();
10589
NotificationListener dummyListener = new NotificationListener() {
10690
@Override
10791
public void handleNotification(Notification notification, Object handback) {
@@ -175,7 +159,8 @@ public void testAutodetectMBeans() throws Exception {
175159
assertNotNull(instance);
176160
instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean3=true"));
177161
assertNotNull(instance);
178-
} finally {
162+
}
163+
finally {
179164
bf.destroySingletons();
180165
}
181166
}
@@ -193,9 +178,11 @@ public void testAutodetectWithExclude() throws Exception {
193178
try {
194179
server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=false"));
195180
fail("MBean with name spring:mbean=false should have been excluded");
196-
} catch (InstanceNotFoundException expected) {
197181
}
198-
} finally {
182+
catch (InstanceNotFoundException expected) {
183+
}
184+
}
185+
finally {
199186
bf.destroySingletons();
200187
}
201188
}
@@ -217,7 +204,8 @@ public void testAutodetectLazyMBeans() throws Exception {
217204
assertNotNull(server.getObjectInstance(oname));
218205
name = (String) server.getAttribute(oname, "Name");
219206
assertEquals("Invalid name returned", "Juergen Hoeller", name);
220-
} finally {
207+
}
208+
finally {
221209
bf.destroySingletons();
222210
}
223211
}
@@ -228,7 +216,8 @@ public void testAutodetectNoMBeans() throws Exception {
228216
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("autodetectNoMBeans.xml", getClass()));
229217
try {
230218
bf.getBean("exporter");
231-
} finally {
219+
}
220+
finally {
232221
bf.destroySingletons();
233222
}
234223
}
@@ -241,7 +230,7 @@ public void testWithMBeanExporterListeners() throws Exception {
241230
MBeanExporter exporter = new MBeanExporter();
242231
exporter.setBeans(getBeanMap());
243232
exporter.setServer(server);
244-
exporter.setListeners(new MBeanExporterListener[] { listener1, listener2 });
233+
exporter.setListeners(listener1, listener2);
245234
exporter.afterPropertiesSet();
246235
exporter.destroy();
247236

@@ -257,7 +246,7 @@ public void testExportJdkProxy() throws Exception {
257246
ProxyFactory factory = new ProxyFactory();
258247
factory.setTarget(bean);
259248
factory.addAdvice(new NopInterceptor());
260-
factory.setInterfaces(new Class<?>[] { IJmxTestBean.class });
249+
factory.setInterfaces(IJmxTestBean.class);
261250

262251
IJmxTestBean proxy = (IJmxTestBean) factory.getProxy();
263252
String name = "bean:mmm=whatever";
@@ -377,8 +366,8 @@ public void testWithExposeClassLoader() throws Exception {
377366

378367
assertIsRegistered("Bean instance not registered", objectName);
379368

380-
Object result = server.invoke(objectName, "add", new Object[] { new Integer(2), new Integer(3) }, new String[] {
381-
int.class.getName(), int.class.getName() });
369+
Object result = server.invoke(objectName, "add", new Object[] {new Integer(2), new Integer(3)}, new String[] {
370+
int.class.getName(), int.class.getName()});
382371

383372
assertEquals("Incorrect result return from add", result, new Integer(5));
384373
assertEquals("Incorrect attribute value", name, server.getAttribute(objectName, "Name"));
@@ -593,7 +582,7 @@ public void testMBeanIsNotUnregisteredSpuriouslyIfSomeExternalProcessHasUnregist
593582
exporter.setBeans(getBeanMap());
594583
exporter.setServer(this.server);
595584
MockMBeanExporterListener listener = new MockMBeanExporterListener();
596-
exporter.setListeners(new MBeanExporterListener[] { listener });
585+
exporter.setListeners(listener);
597586
exporter.afterPropertiesSet();
598587
assertIsRegistered("The bean was not registered with the MBeanServer",
599588
ObjectNameManager.getInstance(OBJECT_NAME));
@@ -702,6 +691,7 @@ private void assertListener(MockMBeanExporterListener listener) throws Malformed
702691
assertEquals("Incorrect ObjectName in unregister", desired, listener.getUnregistered().get(0));
703692
}
704693

694+
705695
private static class InvokeDetectAssembler implements MBeanInfoAssembler {
706696

707697
private boolean invoked = false;
@@ -713,6 +703,7 @@ public ModelMBeanInfo getMBeanInfo(Object managedResource, String beanKey) throw
713703
}
714704
}
715705

706+
716707
private static class MockMBeanExporterListener implements MBeanExporterListener {
717708

718709
private List<ObjectName> registered = new ArrayList<ObjectName>();
@@ -738,6 +729,7 @@ public List<ObjectName> getUnregistered() {
738729
}
739730
}
740731

732+
741733
private static class SelfNamingTestBean implements SelfNaming {
742734

743735
private ObjectName objectName;
@@ -752,11 +744,13 @@ public ObjectName getObjectName() throws MalformedObjectNameException {
752744
}
753745
}
754746

747+
755748
public static interface PersonMBean {
756749

757750
String getName();
758751
}
759752

753+
760754
public static class Person implements PersonMBean {
761755

762756
private String name;
@@ -771,6 +765,7 @@ public void setName(String name) {
771765
}
772766
}
773767

768+
774769
public static final class StubNotificationListener implements NotificationListener {
775770

776771
private List<Notification> notifications = new ArrayList<Notification>();
@@ -785,6 +780,7 @@ public List<Notification> getNotifications() {
785780
}
786781
}
787782

783+
788784
private static class RuntimeExceptionThrowingConstructorBean {
789785

790786
@SuppressWarnings("unused")
@@ -793,6 +789,7 @@ public RuntimeExceptionThrowingConstructorBean() {
793789
}
794790
}
795791

792+
796793
private static final class NamedBeanAutodetectCapableMBeanInfoAssemblerStub extends
797794
SimpleReflectiveMBeanInfoAssembler implements AutodetectCapableMBeanInfoAssembler {
798795

0 commit comments

Comments
 (0)