From de6c6076e41b3ccf173e8419ee9b854f3b929aa2 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Wed, 29 Nov 2017 13:56:26 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E3=80=91DataSource=E5=8F=AF=E4=BB=A5=E9=80=9A=E8=BF=87applicat?= =?UTF-8?q?ion.xml=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/boot/NodeServer.java | 30 +++++++++++---- src/org/redkale/source/DataJdbcSource.java | 43 +++++++++++++++++++--- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/src/org/redkale/boot/NodeServer.java b/src/org/redkale/boot/NodeServer.java index d95b52621..1323912cf 100644 --- a/src/org/redkale/boot/NodeServer.java +++ b/src/org/redkale/boot/NodeServer.java @@ -221,7 +221,25 @@ public abstract class NodeServer { try { if (field.getAnnotation(Resource.class) == null) return; 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 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); appResFactory.register(resourceName, DataSource.class, source); @@ -242,7 +260,7 @@ public abstract class NodeServer { field.set(src, source); rf.inject(source, self); // 给其可能包含@Resource的字段赋值; //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) { logger.log(Level.SEVERE, "DataSource inject error", e); } @@ -264,12 +282,8 @@ public abstract class NodeServer { AnyValue sourceConf = cacheResource.get(resourceName); if (sourceConf == null) sourceConf = dataResources.get(resourceName); final Class sourceType = sourceConf == null ? CacheMemorySource.class : serverClassLoader.loadClass(sourceConf.getValue("value")); - Object source; - if (DataSource.class.isAssignableFrom(sourceType)) { // DataSource - 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 + Object source = null; + if (CacheSource.class.isAssignableFrom(sourceType)) { // CacheSource source = (CacheSource) Sncp.createLocalService(serverClassLoader, resourceName, sourceType, appResFactory, appSncpTranFactory, sncpAddr, groups, Sncp.getConf(srcService)); Type genericType = field.getGenericType(); ParameterizedType pt = (genericType instanceof ParameterizedType) ? (ParameterizedType) genericType : null; diff --git a/src/org/redkale/source/DataJdbcSource.java b/src/org/redkale/source/DataJdbcSource.java index 63b4b6799..0c2396120 100644 --- a/src/org/redkale/source/DataJdbcSource.java +++ b/src/org/redkale/source/DataJdbcSource.java @@ -36,15 +36,15 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC 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 = "$") protected DataCacheListener cacheListener; @@ -60,6 +60,39 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC 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) { }