Skip to content

Commit e424dfb

Browse files
committed
Polish "Perform best effort to retrieve DataSourceProxy"
Closes gh-15206
1 parent 26f9a92 commit e424dfb

File tree

5 files changed

+271
-120
lines changed

5 files changed

+271
-120
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfiguration.java

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,20 @@
2626
import org.apache.commons.logging.LogFactory;
2727
import org.apache.tomcat.jdbc.pool.DataSourceProxy;
2828

29-
import org.springframework.aop.framework.AopProxyUtils;
30-
import org.springframework.aop.support.AopUtils;
3129
import org.springframework.beans.factory.ObjectProvider;
3230
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3331
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3432
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3533
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
34+
import org.springframework.boot.jdbc.DataSourceUnwrapper;
3635
import org.springframework.context.annotation.Bean;
3736
import org.springframework.context.annotation.Configuration;
38-
import org.springframework.jdbc.datasource.DelegatingDataSource;
3937
import org.springframework.jmx.export.MBeanExporter;
4038

4139
/**
4240
* Configures DataSource related MBeans.
4341
*
4442
* @author Stephane Nicoll
45-
* @author Tadaya Tsuyukubo
4643
*/
4744
@Configuration
4845
@ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true)
@@ -93,7 +90,8 @@ static class TomcatDataSourceJmxConfiguration {
9390
@Bean
9491
@ConditionalOnMissingBean(name = "dataSourceMBean")
9592
public Object dataSourceMBean(DataSource dataSource) {
96-
DataSourceProxy dataSourceProxy = extractDataSourceProxy(dataSource);
93+
DataSourceProxy dataSourceProxy = DataSourceUnwrapper.unwrap(dataSource,
94+
DataSourceProxy.class);
9795
if (dataSourceProxy != null) {
9896
try {
9997
return dataSourceProxy.createPool().getJmxPool();
@@ -105,36 +103,6 @@ public Object dataSourceMBean(DataSource dataSource) {
105103
return null;
106104
}
107105

108-
/**
109-
* Perform best effort to retrieve tomcat's {@link DataSourceProxy}.
110-
*
111-
* Since {@link DataSourceProxy#unwrap(Class)} always return {@code null}, it
112-
* cannot directly retrieve {@link DataSourceProxy}. This method tries best effort
113-
* to find {@link DataSourceProxy} if the given {@link DataSource} is wrapped or
114-
* proxied by spring.
115-
* @param dataSource candidate datasource
116-
* @return found DataSourceProxy or null
117-
*/
118-
private DataSourceProxy extractDataSourceProxy(DataSource dataSource) {
119-
if (dataSource instanceof DataSourceProxy) {
120-
return (DataSourceProxy) dataSource; // found
121-
}
122-
else if (dataSource instanceof DelegatingDataSource) {
123-
// check delegating target
124-
return extractDataSourceProxy(
125-
((DelegatingDataSource) dataSource).getTargetDataSource());
126-
}
127-
else if (AopUtils.isAopProxy(dataSource)) {
128-
// for proxy by spring, try target(advised) instance
129-
Object target = AopProxyUtils.getSingletonTarget(dataSource);
130-
if (target instanceof DataSource) {
131-
return extractDataSourceProxy((DataSource) target);
132-
}
133-
}
134-
135-
return null;
136-
}
137-
138106
}
139107

140108
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java

Lines changed: 8 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
import org.apache.tomcat.jdbc.pool.jmx.ConnectionPool;
3232
import org.junit.Test;
3333

34-
import org.springframework.aop.framework.AopProxyUtils;
3534
import org.springframework.aop.framework.ProxyFactory;
36-
import org.springframework.aop.support.AopUtils;
3735
import org.springframework.beans.factory.config.BeanPostProcessor;
3836
import org.springframework.boot.autoconfigure.AutoConfigurations;
3937
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
@@ -159,6 +157,7 @@ public void tomcatAutoConfiguredCanExposeMBeanPool() {
159157
this.contextRunner.withPropertyValues(
160158
"spring.datasource.type=" + DataSource.class.getName(),
161159
"spring.datasource.jmx-enabled=true").run((context) -> {
160+
assertThat(context).hasBean("dataSourceMBean");
162161
assertThat(context).hasSingleBean(ConnectionPool.class);
163162
assertThat(context.getBean(DataSourceProxy.class).createPool()
164163
.getJmxPool())
@@ -173,12 +172,9 @@ public void tomcatProxiedCanExposeMBeanPool() {
173172
"spring.datasource.type=" + DataSource.class.getName(),
174173
"spring.datasource.jmx-enabled=true")
175174
.run((context) -> {
176-
assertThat(context).hasSingleBean(ConnectionPool.class);
177-
DataSourceProxy dataSourceProxy = (DataSourceProxy) AopProxyUtils
178-
.getSingletonTarget(
179-
context.getBean(javax.sql.DataSource.class));
180-
assertThat(dataSourceProxy.createPool().getJmxPool())
181-
.isSameAs(context.getBean(ConnectionPool.class));
175+
assertThat(context).hasBean("dataSourceMBean");
176+
assertThat(context).getBean("dataSourceMBean")
177+
.isInstanceOf(ConnectionPool.class);
182178
});
183179
}
184180

@@ -189,55 +185,12 @@ public void tomcatDelegateCanExposeMBeanPool() {
189185
"spring.datasource.type=" + DataSource.class.getName(),
190186
"spring.datasource.jmx-enabled=true")
191187
.run((context) -> {
192-
assertThat(context).hasSingleBean(ConnectionPool.class);
193-
DataSourceProxy dataSourceProxy = (DataSourceProxy) context
194-
.getBean(DelegatingDataSource.class).getTargetDataSource();
195-
assertThat(dataSourceProxy.createPool().getJmxPool())
196-
.isSameAs(context.getBean(ConnectionPool.class));
197-
});
198-
}
199-
200-
@Test
201-
public void tomcatProxyAndDelegateCanExposeMBeanPool() {
202-
this.contextRunner
203-
.withUserConfiguration(DataSourceMixWrapAndProxyConfiguration.class)
204-
.withPropertyValues(
205-
"spring.datasource.type=" + DataSource.class.getName(),
206-
"spring.datasource.jmx-enabled=true")
207-
.run((context) -> {
208-
assertThat(context).hasSingleBean(ConnectionPool.class);
209-
DataSourceProxy dataSourceProxy = extractTomcatDataSource(
210-
context.getBean(javax.sql.DataSource.class));
211-
assertThat(dataSourceProxy.createPool().getJmxPool())
212-
.isSameAs(context.getBean(ConnectionPool.class));
188+
assertThat(context).hasBean("dataSourceMBean");
189+
assertThat(context).getBean("dataSourceMBean")
190+
.isInstanceOf(ConnectionPool.class);
213191
});
214192
}
215193

216-
private static javax.sql.DataSource wrap(javax.sql.DataSource dataSource) {
217-
return (javax.sql.DataSource) new ProxyFactory(dataSource).getProxy();
218-
}
219-
220-
private static javax.sql.DataSource delegate(javax.sql.DataSource dataSource) {
221-
return new DelegatingDataSource(dataSource);
222-
}
223-
224-
private static DataSource extractTomcatDataSource(javax.sql.DataSource dataSource) {
225-
if (dataSource instanceof DataSource) {
226-
return (DataSource) dataSource;
227-
}
228-
else if (dataSource instanceof DelegatingDataSource) {
229-
return extractTomcatDataSource(
230-
((DelegatingDataSource) dataSource).getTargetDataSource());
231-
}
232-
else if (AopUtils.isAopProxy(dataSource)) {
233-
return extractTomcatDataSource(
234-
(javax.sql.DataSource) AopProxyUtils.getSingletonTarget(dataSource));
235-
}
236-
237-
throw new RuntimeException(
238-
"Not proxied or delegated tomcat DataSource: " + dataSource);
239-
}
240-
241194
@Configuration
242195
static class DataSourceProxyConfiguration {
243196

@@ -253,7 +206,7 @@ private static class DataSourceBeanPostProcessor implements BeanPostProcessor {
253206
@Override
254207
public Object postProcessAfterInitialization(Object bean, String beanName) {
255208
if (bean instanceof javax.sql.DataSource) {
256-
return wrap((javax.sql.DataSource) bean);
209+
return new ProxyFactory(bean).getProxy();
257210
}
258211
return bean;
259212
}
@@ -279,34 +232,4 @@ public Object postProcessAfterInitialization(Object bean,
279232

280233
}
281234

282-
@Configuration
283-
static class DataSourceMixWrapAndProxyConfiguration {
284-
285-
@Bean
286-
public static DataSourceBeanPostProcessor dataSourceBeanPostProcessor() {
287-
return new DataSourceBeanPostProcessor() {
288-
@Override
289-
public Object postProcessAfterInitialization(Object bean,
290-
String beanName) {
291-
if (bean instanceof javax.sql.DataSource) {
292-
javax.sql.DataSource dataSource = (javax.sql.DataSource) bean;
293-
// delegate/wrap multiple times
294-
for (int i = 0; i < 10; i++) {
295-
if (i % 2 == 0) {
296-
dataSource = wrap(dataSource);
297-
}
298-
else {
299-
dataSource = delegate(dataSource);
300-
}
301-
}
302-
303-
return dataSource;
304-
}
305-
return bean;
306-
}
307-
};
308-
}
309-
310-
}
311-
312235
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.jdbc;
18+
19+
import java.sql.Wrapper;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.springframework.aop.framework.AopProxyUtils;
24+
import org.springframework.aop.support.AopUtils;
25+
import org.springframework.jdbc.datasource.DelegatingDataSource;
26+
import org.springframework.util.ClassUtils;
27+
28+
/**
29+
* Unwraps a {@link DataSource} that may have been proxied or wrapped in a custom
30+
* {@link Wrapper} such as {@link DelegatingDataSource}.
31+
*
32+
* @author Tadaya Tsuyukubo
33+
* @author Stephane Nicoll
34+
* @since 2.0.7
35+
*/
36+
public final class DataSourceUnwrapper {
37+
38+
private static final boolean DELEGATING_DATA_SOURCE_PRESENT = ClassUtils.isPresent(
39+
"org.springframework.jdbc.datasource.DelegatingDataSource",
40+
DataSourceUnwrapper.class.getClassLoader());
41+
42+
private DataSourceUnwrapper() {
43+
}
44+
45+
/**
46+
* Return an object that implements the given {@code target} type, unwrapping delegate
47+
* or proxy if necessary.
48+
* @param dataSource the datasource to handle
49+
* @param target the type that the result must implement
50+
* @param <T> the target type
51+
* @return an object that implements the target type or {@code null}
52+
*/
53+
public static <T> T unwrap(DataSource dataSource, Class<T> target) {
54+
if (target.isInstance(dataSource)) {
55+
return target.cast(dataSource);
56+
}
57+
T unwrapped = safeUnwrap(dataSource, target);
58+
if (unwrapped != null) {
59+
return unwrapped;
60+
}
61+
if (DELEGATING_DATA_SOURCE_PRESENT) {
62+
DataSource targetDataSource = DelegatingDataSourceUnwrapper
63+
.getTargetDataSource(dataSource);
64+
if (targetDataSource != null) {
65+
return unwrap(targetDataSource, target);
66+
}
67+
}
68+
if (AopUtils.isAopProxy(dataSource)) {
69+
Object proxyTarget = AopProxyUtils.getSingletonTarget(dataSource);
70+
if (proxyTarget instanceof DataSource) {
71+
return unwrap((DataSource) proxyTarget, target);
72+
}
73+
}
74+
return null;
75+
}
76+
77+
private static <S> S safeUnwrap(Wrapper wrapper, Class<S> target) {
78+
try {
79+
return wrapper.unwrap(target);
80+
}
81+
catch (Exception ex) {
82+
return null;
83+
}
84+
}
85+
86+
private static class DelegatingDataSourceUnwrapper {
87+
88+
private static DataSource getTargetDataSource(DataSource dataSource) {
89+
if (dataSource instanceof DelegatingDataSource) {
90+
return ((DelegatingDataSource) dataSource).getTargetDataSource();
91+
}
92+
return null;
93+
}
94+
95+
}
96+
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.jdbc;
18+
19+
import javax.sql.DataSource;
20+
21+
import com.zaxxer.hikari.HikariDataSource;
22+
import org.apache.tomcat.jdbc.pool.DataSourceProxy;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
26+
import org.springframework.aop.framework.ProxyFactory;
27+
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
28+
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Integration tests for {@link DataSourceUnwrapper} when spring-jdbc is not available.
34+
*
35+
* @author Stephane Nicoll
36+
*/
37+
@RunWith(ModifiedClassPathRunner.class)
38+
@ClassPathExclusions("spring-jdbc-*.jar")
39+
public class DataSourceUnwrapperNoSpringJdbcTests {
40+
41+
@Test
42+
public void unwrapWithProxy() {
43+
DataSource dataSource = new HikariDataSource();
44+
DataSource actual = wrapInProxy(wrapInProxy(dataSource));
45+
assertThat(DataSourceUnwrapper.unwrap(actual, HikariDataSource.class))
46+
.isSameAs(dataSource);
47+
}
48+
49+
@Test
50+
public void unwrapDataSourceProxy() {
51+
org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
52+
DataSource actual = wrapInProxy(wrapInProxy(dataSource));
53+
assertThat(DataSourceUnwrapper.unwrap(actual, DataSourceProxy.class))
54+
.isSameAs(dataSource);
55+
}
56+
57+
private DataSource wrapInProxy(DataSource dataSource) {
58+
return (DataSource) new ProxyFactory(dataSource).getProxy();
59+
}
60+
61+
}

0 commit comments

Comments
 (0)