From 82d1e61ab13d77cb051321cc08c3fb8edf3258d1 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 9 Feb 2015 13:05:07 +0000 Subject: [PATCH] Prevent attempt to export MBean for DataSource retreived from JNDI Tomcat 8's default DataSource implemention is an MBean. Previously, when such a DataSource was consumed from JNDI and an MBeanExporter had been auto-configured an attempt would be made to export this MBean to the MBean server. This would fail due to Tomcat having already registered the MBean. This commit updates JndiDataSourceAutoConfiguration to instruct the MBeanExporter (if there is one) not to export a DataSource MBean that's been retrieved from JNDI. The assumption is that any MBean in JNDI will have already been registered with the MBean server by the same infrastructure that registered it with JNDI. Fixes gh-2397 --- .../jdbc/JndiDataSourceAutoConfiguration.java | 19 ++- .../JndiDataSourceAutoConfigurationTests.java | 133 ++++++++++++++++++ 2 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java index 8dfe8079c7..f929826830 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.jdbc; import javax.sql.DataSource; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -28,12 +29,15 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup; +import org.springframework.jmx.export.MBeanExporter; +import org.springframework.jmx.support.JmxUtils; /** * {@link EnableAutoConfiguration Auto-configuration} for a JNDI located * {@link DataSource}. * * @author Phillip Webb + * @author Andy Wilkinson * @since 1.2.0 */ @Configuration @@ -44,11 +48,22 @@ import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup; @EnableConfigurationProperties(DataSourceProperties.class) public class JndiDataSourceAutoConfiguration { + @Autowired(required = false) + private MBeanExporter mbeanExporter; + @Bean(destroyMethod = "") @ConditionalOnMissingBean public DataSource dataSource(DataSourceProperties properties) { JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); - return dataSourceLookup.getDataSource(properties.getJndiName()); + DataSource dataSource = dataSourceLookup.getDataSource(properties.getJndiName()); + excludeMBeanIfNecessary(dataSource, "dataSource"); + return dataSource; + } + + private void excludeMBeanIfNecessary(Object candidate, String beanName) { + if (this.mbeanExporter != null && JmxUtils.isMBean(candidate.getClass())) { + this.mbeanExporter.addExcludedBean(beanName); + } } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java new file mode 100644 index 0000000000..30ada4b97c --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java @@ -0,0 +1,133 @@ +/* + * Copyright 2012-2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.jdbc; + +import java.util.Set; + +import javax.naming.NamingException; +import javax.sql.DataSource; + +import org.apache.commons.dbcp2.BasicDataSource; +import org.junit.After; +import org.junit.Test; +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.jmx.export.MBeanExporter; +import org.springframework.mock.jndi.SimpleNamingContextBuilder; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link JndiDataSourceAutoConfiguration} + * + * @author Andy Wilkinson + */ +public class JndiDataSourceAutoConfigurationTests { + + private AnnotationConfigApplicationContext context; + + private SimpleNamingContextBuilder jndi; + + @After + public void cleanup() { + if (this.jndi != null) { + this.jndi.clear(); + } + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void dataSourceIsAvailableFromJndi() throws IllegalStateException, + NamingException { + DataSource dataSource = new BasicDataSource(); + this.jndi = configureJndi("foo", dataSource); + + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.datasource.jndi-name:foo"); + this.context.register(JndiDataSourceAutoConfiguration.class); + this.context.refresh(); + + assertEquals(dataSource, this.context.getBean(DataSource.class)); + } + + @SuppressWarnings("unchecked") + @Test + public void mbeanDataSourceIsExcludedFromExport() throws IllegalStateException, + NamingException { + DataSource dataSource = new BasicDataSource(); + this.jndi = configureJndi("foo", dataSource); + + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.datasource.jndi-name:foo"); + this.context.register(JndiDataSourceAutoConfiguration.class, + MBeanExporterConfiguration.class); + this.context.refresh(); + + assertEquals(dataSource, this.context.getBean(DataSource.class)); + MBeanExporter exporter = this.context.getBean(MBeanExporter.class); + Set excludedBeans = (Set) new DirectFieldAccessor(exporter) + .getPropertyValue("excludedBeans"); + assertThat(excludedBeans, contains("dataSource")); + } + + @SuppressWarnings("unchecked") + @Test + public void standardDataSourceIsNotExcludedFromExport() throws IllegalStateException, + NamingException { + DataSource dataSource = new org.apache.commons.dbcp.BasicDataSource(); + this.jndi = configureJndi("foo", dataSource); + + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.datasource.jndi-name:foo"); + this.context.register(JndiDataSourceAutoConfiguration.class, + MBeanExporterConfiguration.class); + this.context.refresh(); + + assertEquals(dataSource, this.context.getBean(DataSource.class)); + MBeanExporter exporter = this.context.getBean(MBeanExporter.class); + Set excludedBeans = (Set) new DirectFieldAccessor(exporter) + .getPropertyValue("excludedBeans"); + assertThat(excludedBeans, hasSize(0)); + } + + private SimpleNamingContextBuilder configureJndi(String name, DataSource dataSource) + throws IllegalStateException, NamingException { + SimpleNamingContextBuilder builder = SimpleNamingContextBuilder + .emptyActivatedContextBuilder(); + builder.bind(name, dataSource); + return builder; + } + + private static class MBeanExporterConfiguration { + + @Bean + MBeanExporter mbeanExporter() { + return new MBeanExporter(); + } + } + +}