From 982f9ce6c9a731fdfbfb49c90c19885ba5788da2 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 9 Feb 2015 09:41:03 +0100 Subject: [PATCH] Allow MBeans to be excluded additively Previously, one could only set the list of bean names to exclude from auto-detection and there was no way to add additional bean names. MBeanExporter now exposes a addExcludedBean method that can be invoked during the initialization phase to add bean names to ignore. Issue: SPR-12686 --- .../jmx/export/MBeanExporter.java | 22 +++++++++---- .../jmx/export/MBeanExporterTests.java | 32 ++++++++++++++++--- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java b/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java index b8d1c8d3c6e..6fc0013ebcf 100644 --- a/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java +++ b/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java @@ -161,7 +161,7 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo private boolean exposeManagedResourceClassLoader = true; /** A set of bean names that should be excluded from autodetection */ - private Set excludedBeans; + private Set excludedBeans = new HashSet(); /** The MBeanExporterListeners registered with this exporter. */ private MBeanExporterListener[] listeners; @@ -314,7 +314,18 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo * Set the list of names for beans that should be excluded from autodetection. */ public void setExcludedBeans(String... excludedBeans) { - this.excludedBeans = (excludedBeans != null ? new HashSet(Arrays.asList(excludedBeans)) : null); + this.excludedBeans.clear(); + if (excludedBeans != null) { + this.excludedBeans.addAll(Arrays.asList(excludedBeans)); + } + } + + /** + * Add the name of bean that should be excluded from autodetection. + */ + public void addExcludedBean(String excludedBean) { + Assert.notNull(excludedBean, "ExcludedBean must not be null"); + this.excludedBeans.add(excludedBean); } /** @@ -922,10 +933,9 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo * Indicates whether or not a particular bean name is present in the excluded beans list. */ private boolean isExcluded(String beanName) { - return (this.excludedBeans != null && - (this.excludedBeans.contains(beanName) || - (beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX) && - this.excludedBeans.contains(beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length()))))); + return (this.excludedBeans.contains(beanName) || + (beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX) && + this.excludedBeans.contains(beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length())))); } /** diff --git a/spring-context/src/test/java/org/springframework/jmx/export/MBeanExporterTests.java b/spring-context/src/test/java/org/springframework/jmx/export/MBeanExporterTests.java index 4afc0f58350..718d03e865d 100644 --- a/spring-context/src/test/java/org/springframework/jmx/export/MBeanExporterTests.java +++ b/spring-context/src/test/java/org/springframework/jmx/export/MBeanExporterTests.java @@ -17,6 +17,8 @@ package org.springframework.jmx.export; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -643,6 +645,28 @@ public final class MBeanExporterTests extends AbstractMBeanServerTests { ObjectNameManager.getInstance(objectName2)); } + @Test + public void testIgnoreBeanName() throws MalformedObjectNameException { + DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); + String firstBeanName = "spring:type=TestBean"; + factory.registerSingleton(firstBeanName, new TestBean("test")); + String secondBeanName = "spring:type=TestBean2"; + factory.registerSingleton(secondBeanName, new TestBean("test2")); + + MBeanExporter exporter = new MBeanExporter(); + exporter.setServer(getServer()); + exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(firstBeanName, secondBeanName)); + exporter.setBeanFactory(factory); + exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ALL); + exporter.addExcludedBean(secondBeanName); + + start(exporter); + assertIsRegistered("Bean not autodetected in (AUTODETECT_ALL) mode", + ObjectNameManager.getInstance(firstBeanName)); + assertIsNotRegistered("Bean should have been excluded", + ObjectNameManager.getInstance(secondBeanName)); + } + private ConfigurableApplicationContext load(String context) { return new ClassPathXmlApplicationContext(context, getClass()); } @@ -763,15 +787,15 @@ public final class MBeanExporterTests extends AbstractMBeanServerTests { private static final class NamedBeanAutodetectCapableMBeanInfoAssemblerStub extends SimpleReflectiveMBeanInfoAssembler implements AutodetectCapableMBeanInfoAssembler { - private String namedBean; + private Collection namedBeans; - public NamedBeanAutodetectCapableMBeanInfoAssemblerStub(String namedBean) { - this.namedBean = namedBean; + public NamedBeanAutodetectCapableMBeanInfoAssemblerStub(String... namedBeans) { + this.namedBeans = Arrays.asList(namedBeans); } @Override public boolean includeBean(Class beanClass, String beanName) { - return this.namedBean.equals(beanName); + return this.namedBeans.contains(beanName); } }