This commit is contained in:
Redkale
2018-05-20 12:34:53 +08:00
parent 8d1022f70f
commit bb82d70f5a
3 changed files with 54 additions and 3 deletions

View File

@@ -102,7 +102,36 @@ public final class DataSources {
if (readprop == null) throw new IOException("Cannot find (resource.name = '" + unitName + "') DataSource");
if (writeprop == null) writeprop = readprop;
String impl = readprop.getProperty(JDBC_DATASOURCE_CLASS, DataJdbcSource.class.getName());
if (DataJdbcSource.class.getName().equals(impl)) return new DataJdbcSource(unitName, persistxml, readprop, writeprop);
if (DataJdbcSource.class.getName().equals(impl)) {
try {
return new DataJdbcSource(unitName, persistxml, readprop, writeprop);
} catch (RuntimeException re) {
if (!(re.getCause() instanceof ClassNotFoundException)) throw re;
String dbtype = null;
{
/* jdbc:mysql:// jdbc:microsoft:sqlserver:// 取://之前的到最后一个:之间的字符串 */
String url = readprop.getProperty(JDBC_URL);
int pos = url.indexOf("://");
if (pos > 0) {
String url0 = url.substring(0, pos);
pos = url0.lastIndexOf(':');
if (pos > 0) dbtype = url0.substring(pos + 1);
}
}
if (dbtype == null) throw re;
Iterator<SourceLoader> it = ServiceLoader.load(SourceLoader.class).iterator();
Class dsClass = null;
while (it.hasNext()) {
SourceLoader loader = it.next();
if (dbtype.equalsIgnoreCase(loader.dbtype())) {
dsClass = loader.dataSourceClass();
if (dsClass != null) break;
}
}
if (dsClass == null) throw re;
impl = dsClass.getName();
}
}
try {
Class ds = Thread.currentThread().getContextClassLoader().loadClass(impl);
for (Constructor d : ds.getConstructors()) {

View File

@@ -76,7 +76,7 @@ public class PoolJdbcSource extends PoolSource<Connection> {
}
}
static ConnectionPoolDataSource createDataSource(Properties property) {
private static ConnectionPoolDataSource createDataSource(Properties property) {
try {
return createDataSource(property.getProperty(JDBC_SOURCE, property.getProperty(JDBC_DRIVER)),
property.getProperty(JDBC_URL), property.getProperty(JDBC_USER), property.getProperty(JDBC_PWD));
@@ -85,7 +85,7 @@ public class PoolJdbcSource extends PoolSource<Connection> {
}
}
static ConnectionPoolDataSource createDataSource(String source0, String url, String user, String password) throws Exception {
private static ConnectionPoolDataSource createDataSource(String source0, String url, String user, String password) throws Exception {
String source = source0;
if (source0 == null || source0.isEmpty()) {
if (url.startsWith("jdbc:mysql:")) {

View File

@@ -0,0 +1,22 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.source;
/**
* 自定义的DataSource加载器
*
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*/
public interface SourceLoader {
public String dbtype();
public Class<? extends DataSource> dataSourceClass();
}