diff --git a/src/org/redkale/source/DataSources.java b/src/org/redkale/source/DataSources.java index ce06a6663..24237aae4 100644 --- a/src/org/redkale/source/DataSources.java +++ b/src/org/redkale/source/DataSources.java @@ -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 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()) { diff --git a/src/org/redkale/source/PoolJdbcSource.java b/src/org/redkale/source/PoolJdbcSource.java index f56f68aec..5be61b432 100644 --- a/src/org/redkale/source/PoolJdbcSource.java +++ b/src/org/redkale/source/PoolJdbcSource.java @@ -76,7 +76,7 @@ public class PoolJdbcSource extends PoolSource { } } - 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 { } } - 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:")) { diff --git a/src/org/redkale/source/SourceLoader.java b/src/org/redkale/source/SourceLoader.java new file mode 100644 index 000000000..90e200c78 --- /dev/null +++ b/src/org/redkale/source/SourceLoader.java @@ -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加载器 + * + * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + */ +public interface SourceLoader { + + public String dbtype(); + + public Class dataSourceClass(); +}