From 5f0ac46d785eacffe846fec54232e747dff877c5 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 26 Nov 2018 10:35:10 +0100 Subject: [PATCH] Unwrap DataSource target rather than plain instanceof calls Closes gh-15227 --- .../jdbc/DataSourceJmxConfiguration.java | 12 ++-------- ...rcePoolMetadataProvidersConfiguration.java | 24 +++++++++++-------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfiguration.java index c946d843e0..44ee849305 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfiguration.java @@ -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 diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/metadata/DataSourcePoolMetadataProvidersConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/metadata/DataSourcePoolMetadataProvidersConfiguration.java index 88b1afec01..2ccf48de95 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/metadata/DataSourcePoolMetadataProvidersConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/metadata/DataSourcePoolMetadataProvidersConfiguration.java @@ -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; };