From a90f25668be5a25775284931c18ae1f50083a84c Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 10 Sep 2012 10:41:36 +0200 Subject: [PATCH] Spring-backed DataSources consistently implement JDBC 4.0's Wrapper interface Issue: SPR-9770 --- .../jdbc/datasource/AbstractDataSource.java | 17 +++++++---------- .../jdbc/datasource/DelegatingDataSource.java | 9 ++++++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/AbstractDataSource.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/AbstractDataSource.java index 89ebe1218c..d677b5fd77 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/AbstractDataSource.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/AbstractDataSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -24,8 +24,6 @@ import javax.sql.DataSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.util.Assert; - /** * Abstract base class for Spring's {@link javax.sql.DataSource} * implementations, taking care of the padding. @@ -78,17 +76,16 @@ public abstract class AbstractDataSource implements DataSource { //--------------------------------------------------------------------- @SuppressWarnings("unchecked") - public T unwrap(Class iface) throws SQLException { - Assert.notNull(iface, "Interface argument must not be null"); - if (!DataSource.class.equals(iface)) { - throw new SQLException("DataSource of type [" + getClass().getName() + - "] can only be unwrapped as [javax.sql.DataSource], not as [" + iface.getName()); + public T unwrap(Class iface) throws SQLException { + if (iface.isInstance(this)) { + return (T) this; } - return (T) this; + throw new SQLException("DataSource of type [" + getClass().getName() + + "] cannot be unwrapped as [" + iface.getName() + "]"); } public boolean isWrapperFor(Class iface) throws SQLException { - return DataSource.class.equals(iface); + return iface.isInstance(this); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DelegatingDataSource.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DelegatingDataSource.java index 5d6a21a33a..a4c162dde8 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DelegatingDataSource.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DelegatingDataSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -110,12 +110,15 @@ public class DelegatingDataSource implements DataSource, InitializingBean { //--------------------------------------------------------------------- @SuppressWarnings("unchecked") - public T unwrap(Class iface) throws SQLException { + public T unwrap(Class iface) throws SQLException { + if (iface.isInstance(this)) { + return (T) this; + } return getTargetDataSource().unwrap(iface); } public boolean isWrapperFor(Class iface) throws SQLException { - return getTargetDataSource().isWrapperFor(iface); + return (iface.isInstance(this) || getTargetDataSource().isWrapperFor(iface)); }