Skip to content

Commit 78a140a

Browse files
committed
Polish "Use DataSource.unwrap to get routing data source"
See gh-42313
1 parent 3f9f049 commit 78a140a

File tree

2 files changed

+20
-81
lines changed

2 files changed

+20
-81
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthContributorAutoConfiguration.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public HealthContributor dbHealthContributor(Map<String, DataSource> dataSources
8989
if (dataSourceHealthIndicatorProperties.isIgnoreRoutingDataSources()) {
9090
Map<String, DataSource> filteredDatasources = dataSources.entrySet()
9191
.stream()
92-
.filter((e) -> !isAbstractRoutingDataSource(e.getValue()))
92+
.filter((e) -> !isRoutingDataSource(e.getValue()))
9393
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
9494
return createContributor(filteredDatasources);
9595
}
@@ -105,9 +105,8 @@ private HealthContributor createContributor(Map<String, DataSource> beans) {
105105
}
106106

107107
private HealthContributor createContributor(DataSource source) {
108-
if (isAbstractRoutingDataSource(source)) {
109-
return new RoutingDataSourceHealthContributor(unwrapAbstractRoutingDataSource(source),
110-
this::createContributor);
108+
if (isRoutingDataSource(source)) {
109+
return new RoutingDataSourceHealthContributor(extractRoutingDataSource(source), this::createContributor);
111110
}
112111
return new DataSourceHealthIndicator(source, getValidationQuery(source));
113112
}
@@ -117,7 +116,7 @@ private String getValidationQuery(DataSource source) {
117116
return (poolMetadata != null) ? poolMetadata.getValidationQuery() : null;
118117
}
119118

120-
private static boolean isAbstractRoutingDataSource(DataSource dataSource) {
119+
private static boolean isRoutingDataSource(DataSource dataSource) {
121120
if (dataSource instanceof AbstractRoutingDataSource) {
122121
return true;
123122
}
@@ -129,16 +128,15 @@ private static boolean isAbstractRoutingDataSource(DataSource dataSource) {
129128
}
130129
}
131130

132-
private static AbstractRoutingDataSource unwrapAbstractRoutingDataSource(DataSource dataSource) {
131+
private static AbstractRoutingDataSource extractRoutingDataSource(DataSource dataSource) {
133132
if (dataSource instanceof AbstractRoutingDataSource routingDataSource) {
134133
return routingDataSource;
135134
}
136135
try {
137136
return dataSource.unwrap(AbstractRoutingDataSource.class);
138137
}
139138
catch (SQLException ex) {
140-
throw new IllegalStateException(
141-
"DataSource '%s' failed to unwrap '%s'".formatted(dataSource, AbstractRoutingDataSource.class), ex);
139+
throw new IllegalStateException("Failed to unwrap AbstractRoutingDataSource from " + dataSource, ex);
142140
}
143141
}
144142

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthContributorAutoConfigurationTests.java

Lines changed: 14 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.jdbc;
1818

19-
import java.io.PrintWriter;
20-
import java.sql.Connection;
21-
import java.sql.ConnectionBuilder;
2219
import java.sql.SQLException;
23-
import java.sql.SQLFeatureNotSupportedException;
24-
import java.sql.ShardingKeyBuilder;
2520
import java.util.HashMap;
2621
import java.util.Map;
27-
import java.util.logging.Logger;
2822

2923
import javax.sql.DataSource;
3024

@@ -256,11 +250,24 @@ static class ProxyDataSourceBeanPostProcessor implements BeanPostProcessor {
256250
@Override
257251
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
258252
if (bean instanceof DataSource dataSource) {
259-
return new ProxyDataSource(dataSource);
253+
return proxyDataSource(dataSource);
260254
}
261255
return bean;
262256
}
263257

258+
private static DataSource proxyDataSource(DataSource dataSource) {
259+
try {
260+
DataSource mock = mock(DataSource.class);
261+
given(mock.isWrapperFor(AbstractRoutingDataSource.class))
262+
.willReturn(dataSource instanceof AbstractRoutingDataSource);
263+
given(mock.unwrap(AbstractRoutingDataSource.class)).willAnswer((invocation) -> dataSource);
264+
return mock;
265+
}
266+
catch (SQLException ex) {
267+
throw new IllegalStateException(ex);
268+
}
269+
}
270+
264271
}
265272

266273
@Configuration(proxyBeanMethods = false)
@@ -280,70 +287,4 @@ AbstractRoutingDataSource routingDataSource() throws Exception {
280287

281288
}
282289

283-
static class ProxyDataSource implements DataSource {
284-
285-
private final DataSource dataSource;
286-
287-
ProxyDataSource(DataSource dataSource) {
288-
this.dataSource = dataSource;
289-
}
290-
291-
@Override
292-
public void setLogWriter(PrintWriter out) throws SQLException {
293-
this.dataSource.setLogWriter(out);
294-
}
295-
296-
@Override
297-
public Connection getConnection() throws SQLException {
298-
return this.dataSource.getConnection();
299-
}
300-
301-
@Override
302-
public Connection getConnection(String username, String password) throws SQLException {
303-
return this.dataSource.getConnection(username, password);
304-
}
305-
306-
@Override
307-
public PrintWriter getLogWriter() throws SQLException {
308-
return this.dataSource.getLogWriter();
309-
}
310-
311-
@Override
312-
public void setLoginTimeout(int seconds) throws SQLException {
313-
this.dataSource.setLoginTimeout(seconds);
314-
}
315-
316-
@Override
317-
public int getLoginTimeout() throws SQLException {
318-
return this.dataSource.getLoginTimeout();
319-
}
320-
321-
@Override
322-
public ConnectionBuilder createConnectionBuilder() throws SQLException {
323-
return this.dataSource.createConnectionBuilder();
324-
}
325-
326-
@Override
327-
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
328-
return this.dataSource.getParentLogger();
329-
}
330-
331-
@Override
332-
public ShardingKeyBuilder createShardingKeyBuilder() throws SQLException {
333-
return this.dataSource.createShardingKeyBuilder();
334-
}
335-
336-
@Override
337-
@SuppressWarnings("unchecked")
338-
public <T> T unwrap(Class<T> iface) throws SQLException {
339-
return iface.isInstance(this) ? (T) this : this.dataSource.unwrap(iface);
340-
}
341-
342-
@Override
343-
public boolean isWrapperFor(Class<?> iface) throws SQLException {
344-
return (iface.isInstance(this) || this.dataSource.isWrapperFor(iface));
345-
}
346-
347-
}
348-
349290
}

0 commit comments

Comments
 (0)