Added DriverClassNameProvider used for providing jdbc driver class based on jdbc url
Uses known databases from org.flywaydb.core.internal.util.jdbc.DriverDataSource Fixes gh-824, fixes gh-809
This commit is contained in:
parent
a5543f18b9
commit
b33eb95dd2
|
@ -20,12 +20,15 @@ import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for configuration of a database pool.
|
* Base class for configuration of a database pool.
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
|
* @author Maciej Walkowiak
|
||||||
* @since 1.1.0
|
* @since 1.1.0
|
||||||
*/
|
*/
|
||||||
@ConfigurationProperties(prefix = DataSourceAutoConfiguration.CONFIGURATION_PREFIX)
|
@ConfigurationProperties(prefix = DataSourceAutoConfiguration.CONFIGURATION_PREFIX)
|
||||||
|
@ -53,6 +56,8 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
|
||||||
|
|
||||||
private EmbeddedDatabaseConnection embeddedDatabaseConnection = EmbeddedDatabaseConnection.NONE;
|
private EmbeddedDatabaseConnection embeddedDatabaseConnection = EmbeddedDatabaseConnection.NONE;
|
||||||
|
|
||||||
|
private DriverClassNameProvider driverClassNameProvider = new DriverClassNameProvider();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||||
this.classLoader = classLoader;
|
this.classLoader = classLoader;
|
||||||
|
@ -64,11 +69,22 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
|
||||||
.get(this.classLoader);
|
.get(this.classLoader);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDriverClassName() {
|
protected String getDriverClassName() {
|
||||||
if (StringUtils.hasText(this.driverClassName)) {
|
if (StringUtils.hasText(this.driverClassName)) {
|
||||||
|
Assert.state(ClassUtils.isPresent(this.driverClassName, null),
|
||||||
|
"Cannot load driver class: " + this.driverClassName);
|
||||||
return this.driverClassName;
|
return this.driverClassName;
|
||||||
}
|
}
|
||||||
String driverClassName = this.embeddedDatabaseConnection.getDriverClassName();
|
String driverClassName = null;
|
||||||
|
|
||||||
|
if (StringUtils.hasText(this.url)) {
|
||||||
|
driverClassName = this.driverClassNameProvider.getDriverClassName(this.url);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!StringUtils.hasText(driverClassName)) {
|
||||||
|
driverClassName = this.embeddedDatabaseConnection.getDriverClassName();
|
||||||
|
}
|
||||||
|
|
||||||
if (!StringUtils.hasText(driverClassName)) {
|
if (!StringUtils.hasText(driverClassName)) {
|
||||||
throw new BeanCreationException(
|
throw new BeanCreationException(
|
||||||
"Cannot determine embedded database driver class for database type "
|
"Cannot determine embedded database driver class for database type "
|
||||||
|
@ -173,5 +189,4 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
|
||||||
public ClassLoader getClassLoader() {
|
public ClassLoader getClassLoader() {
|
||||||
return this.classLoader;
|
return this.classLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2014 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.springframework.boot.autoconfigure.jdbc;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides JDBC driver class name for given JDBC URL.
|
||||||
|
*
|
||||||
|
* @author Maciej Walkowiak
|
||||||
|
* @since 1.1.0
|
||||||
|
*/
|
||||||
|
class DriverClassNameProvider {
|
||||||
|
|
||||||
|
private static final String JDBC_URL_PREFIX = "jdbc";
|
||||||
|
|
||||||
|
private static final Map<String, String> driverMap = new HashMap<String, String>() {
|
||||||
|
{
|
||||||
|
put("db2", "com.ibm.db2.jcc.DB2Driver");
|
||||||
|
put("derby", "org.apache.derby.jdbc.EmbeddedDriver");
|
||||||
|
put("h2", "org.h2.Driver");
|
||||||
|
put("hsqldb", "org.hsqldb.jdbcDriver");
|
||||||
|
put("sqlite", "org.sqlite.JDBC");
|
||||||
|
put("mysql", "com.mysql.jdbc.Driver");
|
||||||
|
put("mariadb", "org.mariadb.jdbc.Driver");
|
||||||
|
put("google", "com.google.appengine.api.rdbms.AppEngineDriver");
|
||||||
|
put("oracle", "oracle.jdbc.OracleDriver");
|
||||||
|
put("postgresql", "org.postgresql.Driver");
|
||||||
|
put("jtds", "net.sourceforge.jtds.jdbc.Driver");
|
||||||
|
put("sqlserver", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to find JDBC driver class name based on given JDBC URL
|
||||||
|
*
|
||||||
|
* @param jdbcUrl JDBC URL
|
||||||
|
* @return driver class name or null if not found
|
||||||
|
*/
|
||||||
|
String getDriverClassName(final String jdbcUrl) {
|
||||||
|
Assert.notNull(jdbcUrl, "JDBC URL cannot be null");
|
||||||
|
|
||||||
|
if (!jdbcUrl.startsWith(JDBC_URL_PREFIX)) {
|
||||||
|
throw new IllegalArgumentException("JDBC URL should start with '"
|
||||||
|
+ JDBC_URL_PREFIX + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
String urlWithoutPrefix = jdbcUrl.substring(JDBC_URL_PREFIX.length());
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
for (Map.Entry<String, String> driver : driverMap.entrySet()) {
|
||||||
|
if (urlWithoutPrefix.startsWith(":" + driver.getKey() + ":")) {
|
||||||
|
result = driver.getValue();
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -95,6 +95,8 @@ public class DataSourceAutoConfigurationTests {
|
||||||
|
|
||||||
@Test(expected = BeanCreationException.class)
|
@Test(expected = BeanCreationException.class)
|
||||||
public void testBadUrl() throws Exception {
|
public void testBadUrl() throws Exception {
|
||||||
|
EnvironmentTestUtils.addEnvironment(this.context,
|
||||||
|
"spring.datasource.url:jdbc:not-going-to-work");
|
||||||
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
|
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
|
||||||
this.context.register(DataSourceAutoConfiguration.class,
|
this.context.register(DataSourceAutoConfiguration.class,
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
PropertyPlaceholderAutoConfiguration.class);
|
||||||
|
@ -104,6 +106,9 @@ public class DataSourceAutoConfigurationTests {
|
||||||
|
|
||||||
@Test(expected = BeanCreationException.class)
|
@Test(expected = BeanCreationException.class)
|
||||||
public void testBadDriverClass() throws Exception {
|
public void testBadDriverClass() throws Exception {
|
||||||
|
EnvironmentTestUtils.addEnvironment(this.context,
|
||||||
|
"spring.datasource.driverClassName:org.none.jdbcDriver",
|
||||||
|
"spring.datasource.url:jdbc:hsqldb:mem:testdb");
|
||||||
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
|
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
|
||||||
this.context.register(DataSourceAutoConfiguration.class,
|
this.context.register(DataSourceAutoConfiguration.class,
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
PropertyPlaceholderAutoConfiguration.class);
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package org.springframework.boot.autoconfigure.jdbc;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link DataSourceProperties}.
|
||||||
|
*
|
||||||
|
* @author Maciej Walkowiak
|
||||||
|
*/
|
||||||
|
public class DataSourcePropertiesTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void correctDriverClassNameFromJdbcUrlWhenDriverClassNameNotDefined() {
|
||||||
|
DataSourceProperties configuration = new DataSourceProperties();
|
||||||
|
configuration.setUrl("jdbc:mysql://mydb");
|
||||||
|
String driverClassName = configuration.getDriverClassName();
|
||||||
|
assertEquals(driverClassName, "com.mysql.jdbc.Driver");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void driverClassNameFromDriverClassNamePropertyWhenDefined() {
|
||||||
|
DataSourceProperties configuration = new DataSourceProperties();
|
||||||
|
configuration.setUrl("jdbc:mysql://mydb");
|
||||||
|
configuration.setDriverClassName("my.driver.ClassName");
|
||||||
|
String driverClassName = configuration.getDriverClassName();
|
||||||
|
assertEquals(driverClassName, "my.driver.ClassName");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2014 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.springframework.boot.autoconfigure.jdbc;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link DriverClassNameProvider}.
|
||||||
|
*
|
||||||
|
* @author Maciej Walkowiak
|
||||||
|
*/
|
||||||
|
public class DriverClassNameProviderTest {
|
||||||
|
private DriverClassNameProvider driverClassNameProvider = new DriverClassNameProvider();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGettingClassNameForKnownDatabase() {
|
||||||
|
String driverClassName = driverClassNameProvider.getDriverClassName("jdbc:postgresql://hostname/dbname");
|
||||||
|
|
||||||
|
assertEquals("org.postgresql.Driver", driverClassName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReturnsNullForUnknownDatabase() {
|
||||||
|
String driverClassName = driverClassNameProvider.getDriverClassName("jdbc:unknowndb://hostname/dbname");
|
||||||
|
|
||||||
|
assertNull(driverClassName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testFailureOnNullJdbcUrl() {
|
||||||
|
driverClassNameProvider.getDriverClassName(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testFailureOnMalformedJdbcUrl() {
|
||||||
|
driverClassNameProvider.getDriverClassName("malformed:url");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue