Unwrap DataSource target rather than plain instanceof calls

Closes gh-15227
pull/16246/head
Stephane Nicoll 6 years ago
parent 8cdbbddc6d
commit 5f0ac46d78

@ -63,22 +63,14 @@ class DataSourceJmxConfiguration {
@PostConstruct
public void validateMBeans() {
HikariDataSource hikariDataSource = unwrapHikariDataSource();
HikariDataSource hikariDataSource = DataSourceUnwrapper
.unwrap(this.dataSource, HikariDataSource.class);
if (hikariDataSource != null && hikariDataSource.isRegisterMbeans()) {
this.mBeanExporter
.ifUnique((exporter) -> exporter.addExcludedBean("dataSource"));
}
}
private HikariDataSource unwrapHikariDataSource() {
try {
return this.dataSource.unwrap(HikariDataSource.class);
}
catch (SQLException ex) {
return null;
}
}
}
@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");
* 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.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.jdbc.DataSourceUnwrapper;
import org.springframework.boot.jdbc.metadata.CommonsDbcp2DataSourcePoolMetadata;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
import org.springframework.boot.jdbc.metadata.HikariDataSourcePoolMetadata;
@ -44,9 +45,10 @@ public class DataSourcePoolMetadataProvidersConfiguration {
@Bean
public DataSourcePoolMetadataProvider tomcatPoolDataSourceMetadataProvider() {
return (dataSource) -> {
if (dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource) {
return new TomcatDataSourcePoolMetadata(
(org.apache.tomcat.jdbc.pool.DataSource) dataSource);
org.apache.tomcat.jdbc.pool.DataSource tomcatDataSource = DataSourceUnwrapper
.unwrap(dataSource, org.apache.tomcat.jdbc.pool.DataSource.class);
if (tomcatDataSource != null) {
return new TomcatDataSourcePoolMetadata(tomcatDataSource);
}
return null;
};
@ -61,9 +63,10 @@ public class DataSourcePoolMetadataProvidersConfiguration {
@Bean
public DataSourcePoolMetadataProvider hikariPoolDataSourceMetadataProvider() {
return (dataSource) -> {
if (dataSource instanceof HikariDataSource) {
return new HikariDataSourcePoolMetadata(
(HikariDataSource) dataSource);
HikariDataSource hikariDataSource = DataSourceUnwrapper.unwrap(dataSource,
HikariDataSource.class);
if (hikariDataSource != null) {
return new HikariDataSourcePoolMetadata(hikariDataSource);
}
return null;
};
@ -78,9 +81,10 @@ public class DataSourcePoolMetadataProvidersConfiguration {
@Bean
public DataSourcePoolMetadataProvider commonsDbcp2PoolDataSourceMetadataProvider() {
return (dataSource) -> {
if (dataSource instanceof BasicDataSource) {
return new CommonsDbcp2DataSourcePoolMetadata(
(BasicDataSource) dataSource);
BasicDataSource dbcpDataSource = DataSourceUnwrapper.unwrap(dataSource,
BasicDataSource.class);
if (dbcpDataSource != null) {
return new CommonsDbcp2DataSourcePoolMetadata(dbcpDataSource);
}
return null;
};

Loading…
Cancel
Save