Skip to content

Commit 934d70a

Browse files
committed
Polish "Perform best effort to retrieve DataSourceProxy"
Closes spring-projectsgh-15206
1 parent 7691386 commit 934d70a

File tree

4 files changed

+150
-120
lines changed

4 files changed

+150
-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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 org.springframework.aop.framework.AopProxyUtils;
22+
import org.springframework.aop.support.AopUtils;
23+
import org.springframework.jdbc.datasource.DelegatingDataSource;
24+
25+
/**
26+
* Unwraps a {@link DataSource} that may have been proxied or wrapped in a
27+
* {@link DelegatingDataSource}.
28+
*
29+
* @author Tadaya Tsuyukubo
30+
* @author Stephane Nicoll
31+
* @since 2.0.7
32+
*/
33+
public final class DataSourceUnwrapper {
34+
35+
private DataSourceUnwrapper() {
36+
}
37+
38+
public static <T> T unwrap(DataSource dataSource, java.lang.Class<T> target) {
39+
if (target.isInstance(dataSource)) {
40+
return target.cast(dataSource);
41+
}
42+
if (dataSource instanceof DelegatingDataSource) {
43+
return unwrap(((DelegatingDataSource) dataSource).getTargetDataSource(),
44+
target);
45+
}
46+
else if (AopUtils.isAopProxy(dataSource)) {
47+
Object proxyTarget = AopProxyUtils.getSingletonTarget(dataSource);
48+
if (proxyTarget instanceof DataSource) {
49+
return unwrap((DataSource) proxyTarget, target);
50+
}
51+
}
52+
return null;
53+
}
54+
55+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.junit.Test;
23+
24+
import org.springframework.aop.framework.ProxyFactory;
25+
import org.springframework.jdbc.datasource.DelegatingDataSource;
26+
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link DataSourceUnwrapper}.
32+
*
33+
* @author Stephane Nicoll
34+
*/
35+
public class DataSourceUnwrapperTests {
36+
37+
@Test
38+
public void unwrapWithTarget() {
39+
DataSource dataSource = new HikariDataSource();
40+
assertThat(DataSourceUnwrapper.unwrap(dataSource, HikariDataSource.class))
41+
.isSameAs(dataSource);
42+
}
43+
44+
@Test
45+
public void unwrapWithWrongTarget() {
46+
DataSource dataSource = new HikariDataSource();
47+
assertThat(
48+
DataSourceUnwrapper.unwrap(dataSource, SingleConnectionDataSource.class))
49+
.isNull();
50+
}
51+
52+
@Test
53+
public void unwrapWithDelegate() {
54+
DataSource dataSource = new HikariDataSource();
55+
DataSource actual = wrapInDelegate(wrapInDelegate(dataSource));
56+
assertThat(DataSourceUnwrapper.unwrap(actual, HikariDataSource.class))
57+
.isSameAs(dataSource);
58+
}
59+
60+
@Test
61+
public void unwrapWithProxy() {
62+
DataSource dataSource = new HikariDataSource();
63+
DataSource actual = wrapInProxy(wrapInProxy(dataSource));
64+
assertThat(DataSourceUnwrapper.unwrap(actual, HikariDataSource.class))
65+
.isSameAs(dataSource);
66+
}
67+
68+
@Test
69+
public void unwrapWithProxyAndDelegate() {
70+
DataSource dataSource = new HikariDataSource();
71+
DataSource actual = wrapInProxy(wrapInDelegate(dataSource));
72+
assertThat(DataSourceUnwrapper.unwrap(actual, HikariDataSource.class))
73+
.isSameAs(dataSource);
74+
}
75+
76+
private DataSource wrapInProxy(DataSource dataSource) {
77+
return (DataSource) new ProxyFactory(dataSource).getProxy();
78+
}
79+
80+
private DataSource wrapInDelegate(DataSource dataSource) {
81+
return new DelegatingDataSource(dataSource);
82+
}
83+
84+
}

0 commit comments

Comments
 (0)