Unwrap DataSource target rather than plain instanceof calls
Closes gh-15227
This commit is contained in:
		
							parent
							
								
									8cdbbddc6d
								
							
						
					
					
						commit
						5f0ac46d78
					
				| 
						 | 
					@ -63,22 +63,14 @@ class DataSourceJmxConfiguration {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		@PostConstruct
 | 
							@PostConstruct
 | 
				
			||||||
		public void validateMBeans() {
 | 
							public void validateMBeans() {
 | 
				
			||||||
			HikariDataSource hikariDataSource = unwrapHikariDataSource();
 | 
								HikariDataSource hikariDataSource = DataSourceUnwrapper
 | 
				
			||||||
 | 
										.unwrap(this.dataSource, HikariDataSource.class);
 | 
				
			||||||
			if (hikariDataSource != null && hikariDataSource.isRegisterMbeans()) {
 | 
								if (hikariDataSource != null && hikariDataSource.isRegisterMbeans()) {
 | 
				
			||||||
				this.mBeanExporter
 | 
									this.mBeanExporter
 | 
				
			||||||
						.ifUnique((exporter) -> exporter.addExcludedBean("dataSource"));
 | 
											.ifUnique((exporter) -> exporter.addExcludedBean("dataSource"));
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		private HikariDataSource unwrapHikariDataSource() {
 | 
					 | 
				
			||||||
			try {
 | 
					 | 
				
			||||||
				return this.dataSource.unwrap(HikariDataSource.class);
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			catch (SQLException ex) {
 | 
					 | 
				
			||||||
				return null;
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Configuration
 | 
						@Configuration
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,5 @@
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * Copyright 2012-2017 the original author or authors.
 | 
					 * Copyright 2012-2018 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.
 | 
				
			||||||
| 
						 | 
					@ -20,6 +20,7 @@ import com.zaxxer.hikari.HikariDataSource;
 | 
				
			||||||
import org.apache.commons.dbcp2.BasicDataSource;
 | 
					import org.apache.commons.dbcp2.BasicDataSource;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 | 
					import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 | 
				
			||||||
 | 
					import org.springframework.boot.jdbc.DataSourceUnwrapper;
 | 
				
			||||||
import org.springframework.boot.jdbc.metadata.CommonsDbcp2DataSourcePoolMetadata;
 | 
					import org.springframework.boot.jdbc.metadata.CommonsDbcp2DataSourcePoolMetadata;
 | 
				
			||||||
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
 | 
					import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
 | 
				
			||||||
import org.springframework.boot.jdbc.metadata.HikariDataSourcePoolMetadata;
 | 
					import org.springframework.boot.jdbc.metadata.HikariDataSourcePoolMetadata;
 | 
				
			||||||
| 
						 | 
					@ -44,9 +45,10 @@ public class DataSourcePoolMetadataProvidersConfiguration {
 | 
				
			||||||
		@Bean
 | 
							@Bean
 | 
				
			||||||
		public DataSourcePoolMetadataProvider tomcatPoolDataSourceMetadataProvider() {
 | 
							public DataSourcePoolMetadataProvider tomcatPoolDataSourceMetadataProvider() {
 | 
				
			||||||
			return (dataSource) -> {
 | 
								return (dataSource) -> {
 | 
				
			||||||
				if (dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource) {
 | 
									org.apache.tomcat.jdbc.pool.DataSource tomcatDataSource = DataSourceUnwrapper
 | 
				
			||||||
					return new TomcatDataSourcePoolMetadata(
 | 
											.unwrap(dataSource, org.apache.tomcat.jdbc.pool.DataSource.class);
 | 
				
			||||||
							(org.apache.tomcat.jdbc.pool.DataSource) dataSource);
 | 
									if (tomcatDataSource != null) {
 | 
				
			||||||
 | 
										return new TomcatDataSourcePoolMetadata(tomcatDataSource);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				return null;
 | 
									return null;
 | 
				
			||||||
			};
 | 
								};
 | 
				
			||||||
| 
						 | 
					@ -61,9 +63,10 @@ public class DataSourcePoolMetadataProvidersConfiguration {
 | 
				
			||||||
		@Bean
 | 
							@Bean
 | 
				
			||||||
		public DataSourcePoolMetadataProvider hikariPoolDataSourceMetadataProvider() {
 | 
							public DataSourcePoolMetadataProvider hikariPoolDataSourceMetadataProvider() {
 | 
				
			||||||
			return (dataSource) -> {
 | 
								return (dataSource) -> {
 | 
				
			||||||
				if (dataSource instanceof HikariDataSource) {
 | 
									HikariDataSource hikariDataSource = DataSourceUnwrapper.unwrap(dataSource,
 | 
				
			||||||
					return new HikariDataSourcePoolMetadata(
 | 
											HikariDataSource.class);
 | 
				
			||||||
							(HikariDataSource) dataSource);
 | 
									if (hikariDataSource != null) {
 | 
				
			||||||
 | 
										return new HikariDataSourcePoolMetadata(hikariDataSource);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				return null;
 | 
									return null;
 | 
				
			||||||
			};
 | 
								};
 | 
				
			||||||
| 
						 | 
					@ -78,9 +81,10 @@ public class DataSourcePoolMetadataProvidersConfiguration {
 | 
				
			||||||
		@Bean
 | 
							@Bean
 | 
				
			||||||
		public DataSourcePoolMetadataProvider commonsDbcp2PoolDataSourceMetadataProvider() {
 | 
							public DataSourcePoolMetadataProvider commonsDbcp2PoolDataSourceMetadataProvider() {
 | 
				
			||||||
			return (dataSource) -> {
 | 
								return (dataSource) -> {
 | 
				
			||||||
				if (dataSource instanceof BasicDataSource) {
 | 
									BasicDataSource dbcpDataSource = DataSourceUnwrapper.unwrap(dataSource,
 | 
				
			||||||
					return new CommonsDbcp2DataSourcePoolMetadata(
 | 
											BasicDataSource.class);
 | 
				
			||||||
							(BasicDataSource) dataSource);
 | 
									if (dbcpDataSource != null) {
 | 
				
			||||||
 | 
										return new CommonsDbcp2DataSourcePoolMetadata(dbcpDataSource);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				return null;
 | 
									return null;
 | 
				
			||||||
			};
 | 
								};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue