【新增功能】DataSource可以通过application.xml配置
This commit is contained in:
@@ -221,7 +221,25 @@ public abstract class NodeServer {
|
|||||||
try {
|
try {
|
||||||
if (field.getAnnotation(Resource.class) == null) return;
|
if (field.getAnnotation(Resource.class) == null) return;
|
||||||
if ((src instanceof Service) && Sncp.isRemote((Service) src)) return; //远程模式不得注入 DataSource
|
if ((src instanceof Service) && Sncp.isRemote((Service) src)) return; //远程模式不得注入 DataSource
|
||||||
DataSource source = DataSources.createDataSource(resourceName);
|
AnyValue sourceConf = dataResources.get(resourceName);
|
||||||
|
DataSource source = null;
|
||||||
|
boolean needinit = true;
|
||||||
|
if (sourceConf != null) {
|
||||||
|
final Class sourceType = serverClassLoader.loadClass(sourceConf.getValue("value"));
|
||||||
|
if (DataSource.class.isAssignableFrom(sourceType)) { // DataSource
|
||||||
|
final Service srcService = (Service) src;
|
||||||
|
SncpClient client = Sncp.getSncpClient(srcService);
|
||||||
|
final InetSocketAddress sncpAddr = client == null ? null : client.getClientAddress();
|
||||||
|
final Set<String> groups = new HashSet<>();
|
||||||
|
if (client != null && client.getSameGroup() != null) groups.add(client.getSameGroup());
|
||||||
|
if (client != null && client.getDiffGroups() != null) groups.addAll(client.getDiffGroups());
|
||||||
|
source = (DataSource) Sncp.createLocalService(serverClassLoader, resourceName, sourceType, appResFactory, appSncpTranFactory, sncpAddr, groups, Sncp.getConf(srcService));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (source == null) {
|
||||||
|
source = DataSources.createDataSource(resourceName); //从persistence.xml配置中创建
|
||||||
|
needinit = false;
|
||||||
|
}
|
||||||
application.dataSources.add(source);
|
application.dataSources.add(source);
|
||||||
appResFactory.register(resourceName, DataSource.class, source);
|
appResFactory.register(resourceName, DataSource.class, source);
|
||||||
|
|
||||||
@@ -242,7 +260,7 @@ public abstract class NodeServer {
|
|||||||
field.set(src, source);
|
field.set(src, source);
|
||||||
rf.inject(source, self); // 给其可能包含@Resource的字段赋值;
|
rf.inject(source, self); // 给其可能包含@Resource的字段赋值;
|
||||||
//NodeServer.this.watchFactory.inject(src);
|
//NodeServer.this.watchFactory.inject(src);
|
||||||
if (source instanceof Service) ((Service) source).init(null);
|
if (source instanceof Service && needinit) ((Service) source).init(sourceConf);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.SEVERE, "DataSource inject error", e);
|
logger.log(Level.SEVERE, "DataSource inject error", e);
|
||||||
}
|
}
|
||||||
@@ -264,12 +282,8 @@ public abstract class NodeServer {
|
|||||||
AnyValue sourceConf = cacheResource.get(resourceName);
|
AnyValue sourceConf = cacheResource.get(resourceName);
|
||||||
if (sourceConf == null) sourceConf = dataResources.get(resourceName);
|
if (sourceConf == null) sourceConf = dataResources.get(resourceName);
|
||||||
final Class sourceType = sourceConf == null ? CacheMemorySource.class : serverClassLoader.loadClass(sourceConf.getValue("value"));
|
final Class sourceType = sourceConf == null ? CacheMemorySource.class : serverClassLoader.loadClass(sourceConf.getValue("value"));
|
||||||
Object source;
|
Object source = null;
|
||||||
if (DataSource.class.isAssignableFrom(sourceType)) { // DataSource
|
if (CacheSource.class.isAssignableFrom(sourceType)) { // CacheSource
|
||||||
source = (DataSource) Sncp.createLocalService(serverClassLoader, resourceName, sourceType, appResFactory, appSncpTranFactory, sncpAddr, groups, Sncp.getConf(srcService));
|
|
||||||
application.dataSources.add((DataSource) source);
|
|
||||||
appResFactory.register(resourceName, DataSource.class, source);
|
|
||||||
} else { // CacheSource
|
|
||||||
source = (CacheSource) Sncp.createLocalService(serverClassLoader, resourceName, sourceType, appResFactory, appSncpTranFactory, sncpAddr, groups, Sncp.getConf(srcService));
|
source = (CacheSource) Sncp.createLocalService(serverClassLoader, resourceName, sourceType, appResFactory, appSncpTranFactory, sncpAddr, groups, Sncp.getConf(srcService));
|
||||||
Type genericType = field.getGenericType();
|
Type genericType = field.getGenericType();
|
||||||
ParameterizedType pt = (genericType instanceof ParameterizedType) ? (ParameterizedType) genericType : null;
|
ParameterizedType pt = (genericType instanceof ParameterizedType) ? (ParameterizedType) genericType : null;
|
||||||
|
|||||||
@@ -36,15 +36,15 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC
|
|||||||
|
|
||||||
protected final Logger logger = Logger.getLogger(DataJdbcSource.class.getSimpleName());
|
protected final Logger logger = Logger.getLogger(DataJdbcSource.class.getSimpleName());
|
||||||
|
|
||||||
protected final String name;
|
protected String name;
|
||||||
|
|
||||||
protected final URL conf;
|
protected URL conf;
|
||||||
|
|
||||||
protected final boolean cacheForbidden;
|
protected boolean cacheForbidden;
|
||||||
|
|
||||||
protected final PoolJdbcSource readPool;
|
protected PoolJdbcSource readPool;
|
||||||
|
|
||||||
protected final PoolJdbcSource writePool;
|
protected PoolJdbcSource writePool;
|
||||||
|
|
||||||
@Resource(name = "$")
|
@Resource(name = "$")
|
||||||
protected DataCacheListener cacheListener;
|
protected DataCacheListener cacheListener;
|
||||||
@@ -60,6 +60,39 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC
|
|||||||
this.cacheForbidden = "NONE".equalsIgnoreCase(readprop.getProperty(JDBC_CACHE_MODE));
|
this.cacheForbidden = "NONE".equalsIgnoreCase(readprop.getProperty(JDBC_CACHE_MODE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DataJdbcSource() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(AnyValue config) { //通过空构造函数创建的对象需要调用init方法进行初始化
|
||||||
|
String unitName = config.getValue("name");
|
||||||
|
Properties readprop = new Properties();
|
||||||
|
Properties writeprop = new Properties();
|
||||||
|
|
||||||
|
for (AnyValue confs : config.getAnyValues("properties")) {
|
||||||
|
boolean write = confs.getValue("name", "").contains("write");
|
||||||
|
for (AnyValue conf : confs.getAnyValues("property")) {
|
||||||
|
String pn = conf.getValue("name");
|
||||||
|
String pv = conf.getValue("value");
|
||||||
|
if (pn == null || pv == null) continue;
|
||||||
|
(write ? writeprop : readprop).put(pn, pv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (AnyValue conf : config.getAnyValues("property")) {
|
||||||
|
String pn = conf.getValue("name");
|
||||||
|
String pv = conf.getValue("value");
|
||||||
|
if (pn == null || pv == null) continue;
|
||||||
|
readprop.put(pn, pv);
|
||||||
|
}
|
||||||
|
if (writeprop.isEmpty()) writeprop = readprop;
|
||||||
|
this.name = unitName;
|
||||||
|
this.conf = null;
|
||||||
|
this.readPool = new PoolJdbcSource(this, "read", readprop);
|
||||||
|
this.writePool = new PoolJdbcSource(this, "write", writeprop);
|
||||||
|
this.cacheForbidden = "NONE".equalsIgnoreCase(readprop.getProperty(JDBC_CACHE_MODE));
|
||||||
|
}
|
||||||
|
|
||||||
//构造前调用
|
//构造前调用
|
||||||
protected void preConstruct(String unitName, Properties readprop, Properties writeprop) {
|
protected void preConstruct(String unitName, Properties readprop, Properties writeprop) {
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user