|
|
|
@ -39,11 +39,12 @@ import org.springframework.util.ClassUtils; |
|
|
|
* inject additional properties into the result you can downcast it, or use |
|
|
|
* inject additional properties into the result you can downcast it, or use |
|
|
|
* {@code @ConfigurationProperties}. |
|
|
|
* {@code @ConfigurationProperties}. |
|
|
|
* |
|
|
|
* |
|
|
|
|
|
|
|
* @param <T> type of DataSource produced by the builder |
|
|
|
* @author Dave Syer |
|
|
|
* @author Dave Syer |
|
|
|
* @author Madhura Bhave |
|
|
|
* @author Madhura Bhave |
|
|
|
* @since 2.0.0 |
|
|
|
* @since 2.0.0 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class DataSourceBuilder { |
|
|
|
public final class DataSourceBuilder<T extends DataSource> { |
|
|
|
|
|
|
|
|
|
|
|
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] { |
|
|
|
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] { |
|
|
|
"com.zaxxer.hikari.HikariDataSource", |
|
|
|
"com.zaxxer.hikari.HikariDataSource", |
|
|
|
@ -56,24 +57,25 @@ public class DataSourceBuilder { |
|
|
|
|
|
|
|
|
|
|
|
private Map<String, String> properties = new HashMap<>(); |
|
|
|
private Map<String, String> properties = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
public static DataSourceBuilder create() { |
|
|
|
public static DataSourceBuilder<?> create() { |
|
|
|
return new DataSourceBuilder(null); |
|
|
|
return new DataSourceBuilder<DataSource>(null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static DataSourceBuilder create(ClassLoader classLoader) { |
|
|
|
public static DataSourceBuilder<?> create(ClassLoader classLoader) { |
|
|
|
return new DataSourceBuilder(classLoader); |
|
|
|
return new DataSourceBuilder<DataSource>(classLoader); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public DataSourceBuilder(ClassLoader classLoader) { |
|
|
|
private DataSourceBuilder(ClassLoader classLoader) { |
|
|
|
this.classLoader = classLoader; |
|
|
|
this.classLoader = classLoader; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public DataSource build() { |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
|
|
|
public T build() { |
|
|
|
Class<? extends DataSource> type = getType(); |
|
|
|
Class<? extends DataSource> type = getType(); |
|
|
|
DataSource result = BeanUtils.instantiateClass(type); |
|
|
|
DataSource result = BeanUtils.instantiateClass(type); |
|
|
|
maybeGetDriverClassName(); |
|
|
|
maybeGetDriverClassName(); |
|
|
|
bind(result); |
|
|
|
bind(result); |
|
|
|
return result; |
|
|
|
return (T) result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void maybeGetDriverClassName() { |
|
|
|
private void maybeGetDriverClassName() { |
|
|
|
@ -95,40 +97,38 @@ public class DataSourceBuilder { |
|
|
|
binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result)); |
|
|
|
binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public DataSourceBuilder type(Class<? extends DataSource> type) { |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
|
|
|
public <D extends DataSource> DataSourceBuilder<D> type(Class<D> type) { |
|
|
|
this.type = type; |
|
|
|
this.type = type; |
|
|
|
return this; |
|
|
|
return (DataSourceBuilder<D>) this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public DataSourceBuilder url(String url) { |
|
|
|
public DataSourceBuilder<T> url(String url) { |
|
|
|
this.properties.put("url", url); |
|
|
|
this.properties.put("url", url); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public DataSourceBuilder driverClassName(String driverClassName) { |
|
|
|
public DataSourceBuilder<T> driverClassName(String driverClassName) { |
|
|
|
this.properties.put("driverClassName", driverClassName); |
|
|
|
this.properties.put("driverClassName", driverClassName); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public DataSourceBuilder username(String username) { |
|
|
|
public DataSourceBuilder<T> username(String username) { |
|
|
|
this.properties.put("username", username); |
|
|
|
this.properties.put("username", username); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public DataSourceBuilder password(String password) { |
|
|
|
public DataSourceBuilder<T> password(String password) { |
|
|
|
this.properties.put("password", password); |
|
|
|
this.properties.put("password", password); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public Class<? extends DataSource> findType() { |
|
|
|
public static Class<? extends DataSource> findType(ClassLoader classLoader) { |
|
|
|
if (this.type != null) { |
|
|
|
|
|
|
|
return this.type; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (String name : DATA_SOURCE_TYPE_NAMES) { |
|
|
|
for (String name : DATA_SOURCE_TYPE_NAMES) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
return (Class<? extends DataSource>) ClassUtils.forName(name, |
|
|
|
return (Class<? extends DataSource>) ClassUtils.forName(name, |
|
|
|
this.classLoader); |
|
|
|
classLoader); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) { |
|
|
|
catch (Exception ex) { |
|
|
|
// Swallow and continue
|
|
|
|
// Swallow and continue
|
|
|
|
@ -138,7 +138,8 @@ public class DataSourceBuilder { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Class<? extends DataSource> getType() { |
|
|
|
private Class<? extends DataSource> getType() { |
|
|
|
Class<? extends DataSource> type = findType(); |
|
|
|
Class<? extends DataSource> type = this.type != null ? this.type |
|
|
|
|
|
|
|
: findType(this.classLoader); |
|
|
|
if (type != null) { |
|
|
|
if (type != null) { |
|
|
|
return type; |
|
|
|
return type; |
|
|
|
} |
|
|
|
} |
|
|
|
|