优化PropertiesAgent
This commit is contained in:
@@ -149,7 +149,7 @@ public final class Application {
|
|||||||
//@since 2.3.0
|
//@since 2.3.0
|
||||||
final ExecutorService workExecutor;
|
final ExecutorService workExecutor;
|
||||||
|
|
||||||
//Source 原始的配置资源
|
//Source 原始的配置资源, 只会存在redkale.datasource(.|[) redkale.cachesource(.|[)开头的配置项
|
||||||
final Properties sourceProperties = new Properties();
|
final Properties sourceProperties = new Properties();
|
||||||
|
|
||||||
//CacheSource 配置信息
|
//CacheSource 配置信息
|
||||||
@@ -177,7 +177,7 @@ public final class Application {
|
|||||||
//@since 2.7.0
|
//@since 2.7.0
|
||||||
private PropertiesAgent propertiesAgent;
|
private PropertiesAgent propertiesAgent;
|
||||||
|
|
||||||
//所有的配置信息都在里面,包含 redkale.port, redkale.lib等
|
//只存放system.property.、mimetype.property.、redkale.cachesource(.|[)、redkale.datasource(.|[)和其他非redkale.开头的配置项
|
||||||
final Properties appProperties = new Properties();
|
final Properties appProperties = new Properties();
|
||||||
|
|
||||||
//配置信息,只读版Properties
|
//配置信息,只读版Properties
|
||||||
@@ -718,8 +718,15 @@ public final class Application {
|
|||||||
File sourceFile = new File(new File(confPath), "source.properties");
|
File sourceFile = new File(new File(confPath), "source.properties");
|
||||||
if (sourceFile.isFile() && sourceFile.canRead()) {
|
if (sourceFile.isFile() && sourceFile.canRead()) {
|
||||||
InputStream in = new FileInputStream(sourceFile);
|
InputStream in = new FileInputStream(sourceFile);
|
||||||
sourceProperties.load(in);
|
Properties props = new Properties();
|
||||||
|
props.load(in);
|
||||||
in.close();
|
in.close();
|
||||||
|
props.forEach((key, val) -> {
|
||||||
|
if (key.toString().startsWith("redkale.datasource.") || key.toString().startsWith("redkale.datasource[")
|
||||||
|
|| key.toString().startsWith("redkale.cachesource.") || key.toString().startsWith("redkale.cachesource[")) {
|
||||||
|
sourceProperties.put(key, val);
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
//兼容 persistence.xml 【已废弃】
|
//兼容 persistence.xml 【已废弃】
|
||||||
File persist = new File(new File(confPath), "persistence.xml");
|
File persist = new File(new File(confPath), "persistence.xml");
|
||||||
@@ -734,8 +741,15 @@ public final class Application {
|
|||||||
try {
|
try {
|
||||||
final URI sourceURI = RedkaleClassLoader.getConfResourceAsURI(configFromCache ? null : confDir, "source.properties");
|
final URI sourceURI = RedkaleClassLoader.getConfResourceAsURI(configFromCache ? null : confDir, "source.properties");
|
||||||
InputStream in = sourceURI.toURL().openStream();
|
InputStream in = sourceURI.toURL().openStream();
|
||||||
sourceProperties.load(in);
|
Properties props = new Properties();
|
||||||
|
props.load(in);
|
||||||
in.close();
|
in.close();
|
||||||
|
props.forEach((key, val) -> {
|
||||||
|
if (key.toString().startsWith("redkale.datasource.") || key.toString().startsWith("redkale.datasource[")
|
||||||
|
|| key.toString().startsWith("redkale.cachesource.") || key.toString().startsWith("redkale.cachesource[")) {
|
||||||
|
sourceProperties.put(key, val);
|
||||||
|
}
|
||||||
|
});
|
||||||
} catch (Exception e) { //没有文件 跳过
|
} catch (Exception e) { //没有文件 跳过
|
||||||
}
|
}
|
||||||
//兼容 persistence.xml 【已废弃】
|
//兼容 persistence.xml 【已废弃】
|
||||||
@@ -759,24 +773,7 @@ public final class Application {
|
|||||||
String key = prop.getValue("name");
|
String key = prop.getValue("name");
|
||||||
String value = prop.getValue("value");
|
String value = prop.getValue("value");
|
||||||
if (key == null || value == null) continue;
|
if (key == null || value == null) continue;
|
||||||
putResourceProperties(key, value, null);
|
putEnvironmentProperties(key, value, null);
|
||||||
// appProperties.put(key, value);
|
|
||||||
// value = replaceValue(value);
|
|
||||||
// if (key.startsWith("redkale.datasource.") || key.startsWith("redkale.datasource[")
|
|
||||||
// || key.startsWith("redkale.cachesource.") || key.startsWith("redkale.cachesource[")) {
|
|
||||||
// sourceProperties.put(key, value);
|
|
||||||
// } else if (key.startsWith("system.property.")) {
|
|
||||||
// String propName = key.substring("system.property.".length());
|
|
||||||
// if (System.getProperty(propName) == null) { //命令行传参数优先级高
|
|
||||||
// System.setProperty(propName, value);
|
|
||||||
// }
|
|
||||||
// } else if (key.startsWith("mimetype.property.")) {
|
|
||||||
// MimeType.add(key.substring("mimetype.property.".length()), value);
|
|
||||||
// } else if (key.startsWith("property.")) {
|
|
||||||
// resourceFactory.register(key, value);
|
|
||||||
// } else {
|
|
||||||
// resourceFactory.register("property." + key, value);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
String dfloads = propertiesConf.getValue("load");
|
String dfloads = propertiesConf.getValue("load");
|
||||||
if (dfloads != null) {
|
if (dfloads != null) {
|
||||||
@@ -790,8 +787,8 @@ public final class Application {
|
|||||||
ps.load(in);
|
ps.load(in);
|
||||||
in.close();
|
in.close();
|
||||||
if (logger.isLoggable(Level.FINEST)) logger.log(Level.FINEST, "load properties(" + dfload + ") size = " + ps.size());
|
if (logger.isLoggable(Level.FINEST)) logger.log(Level.FINEST, "load properties(" + dfload + ") size = " + ps.size());
|
||||||
ps.forEach((x, y) -> {
|
ps.forEach((x, y) -> { //load中的配置项除了redkale.cachesource.和redkale.datasource.开头,不应该有其他redkale.开头配置项
|
||||||
putResourceProperties(x.toString(), y, null);
|
putEnvironmentProperties(x.toString(), y, null);
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.WARNING, "load properties(" + dfload + ") error", e);
|
logger.log(Level.WARNING, "load properties(" + dfload + ") error", e);
|
||||||
@@ -835,8 +832,8 @@ public final class Application {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!sourceProperties.isEmpty()) {
|
if (!sourceProperties.isEmpty()) {
|
||||||
AnyValue propConf = AnyValue.loadFromProperties(sourceProperties);
|
AnyValue sourceConf = AnyValue.loadFromProperties(sourceProperties);
|
||||||
AnyValue redNode = propConf.getAnyValue("redkale");
|
AnyValue redNode = sourceConf.getAnyValue("redkale");
|
||||||
if (redNode != null) {
|
if (redNode != null) {
|
||||||
AnyValue cacheNode = redNode.getAnyValue("cachesource");
|
AnyValue cacheNode = redNode.getAnyValue("cachesource");
|
||||||
if (cacheNode != null) cacheNode.forEach(null, (k, v) -> {
|
if (cacheNode != null) cacheNode.forEach(null, (k, v) -> {
|
||||||
@@ -1705,31 +1702,38 @@ public final class Application {
|
|||||||
return value == null ? value : value.replace("${APP_HOME}", homePath).replace("${APP_NAME}", name);
|
return value == null ? value : value.replace("${APP_HOME}", homePath).replace("${APP_NAME}", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void putResourceProperties(String key, Object value, Properties cache) {
|
//初始化加载时:notifyCache=null
|
||||||
|
//配置项动态变更时 notifyCache!=null, 由调用方统一执行ResourceFactory.register(notifyCache)
|
||||||
|
//key只会是system.property.、mimetype.property.、redkale.cachesource(.|[)、redkale.datasource(.|[)和其他非redkale.开头的配置项
|
||||||
|
void putEnvironmentProperties(String key, Object value, Properties notifyCache) {
|
||||||
if (key == null || value == null) return;
|
if (key == null || value == null) return;
|
||||||
appProperties.put(key, value);
|
String val = replaceValue(value.toString());
|
||||||
value = replaceValue(value.toString());
|
|
||||||
if (key.startsWith("redkale.datasource.") || key.startsWith("redkale.datasource[")
|
if (key.startsWith("redkale.datasource.") || key.startsWith("redkale.datasource[")
|
||||||
|| key.startsWith("redkale.cachesource.") || key.startsWith("redkale.cachesource[")) {
|
|| key.startsWith("redkale.cachesource.") || key.startsWith("redkale.cachesource[")) {
|
||||||
sourceProperties.put(key, value);
|
appProperties.put(key, val);
|
||||||
|
sourceProperties.put(key, val);
|
||||||
} else if (key.startsWith("system.property.")) {
|
} else if (key.startsWith("system.property.")) {
|
||||||
String propName = key.substring("system.property.".length());
|
String propName = key.substring("system.property.".length());
|
||||||
if (System.getProperty(propName) == null) { //命令行传参数优先级高
|
if (notifyCache != null || System.getProperty(propName) == null) { //命令行传参数优先级高
|
||||||
System.setProperty(propName, String.valueOf(value));
|
System.setProperty(propName, val);
|
||||||
}
|
}
|
||||||
} else if (key.startsWith("mimetype.property.")) {
|
} else if (key.startsWith("mimetype.property.")) {
|
||||||
MimeType.add(key.substring("mimetype.property.".length()), String.valueOf(value));
|
MimeType.add(key.substring("mimetype.property.".length()), val);
|
||||||
} else if (key.startsWith("property.")) {
|
} else if (key.startsWith("property.")) {
|
||||||
if (cache == null) {
|
if (notifyCache == null) {
|
||||||
resourceFactory.register(key, value);
|
resourceFactory.register(key, val);
|
||||||
} else {
|
} else {
|
||||||
cache.put(key, value);
|
notifyCache.put(key, val);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (cache == null) {
|
if (key.startsWith("redkale.")) {
|
||||||
resourceFactory.register("property." + key, value);
|
throw new RuntimeException("property " + key + " cannot redkale. startsWith");
|
||||||
|
}
|
||||||
|
appProperties.put(key, val);
|
||||||
|
if (notifyCache == null) {
|
||||||
|
resourceFactory.register("property." + key, val);
|
||||||
} else {
|
} else {
|
||||||
cache.put("property." + key, value);
|
notifyCache.put("property." + key, val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,14 +52,14 @@ public abstract class PropertiesAgent {
|
|||||||
*/
|
*/
|
||||||
public abstract void destroy(AnyValue conf);
|
public abstract void destroy(AnyValue conf);
|
||||||
|
|
||||||
protected void putResourceProperties(Application application, Properties props) {
|
protected void putEnvironmentProperties(Application application, Properties props) {
|
||||||
Properties cache = new Properties();
|
Properties notifyCache = new Properties();
|
||||||
props.forEach((k, v) -> application.putResourceProperties(k.toString(), v, cache));
|
props.forEach((k, v) -> application.putEnvironmentProperties(k.toString(), v, notifyCache));
|
||||||
application.resourceFactory.register(cache);
|
application.resourceFactory.register(notifyCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void putResourceProperties(Application application, String key, Object value) {
|
protected void putEnvironmentProperties(Application application, String key, Object value) {
|
||||||
application.putResourceProperties(key, value, null);
|
application.putEnvironmentProperties(key, value, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void reconfigLogging(Application application, Properties loggingProperties) {
|
protected void reconfigLogging(Application application, Properties loggingProperties) {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import java.lang.reflect.Type;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
|
import org.redkale.util.Resourcable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redkale中缓存数据源的核心类。 主要供业务开发者使用, 技术开发者提供CacheSource的实现。<br>
|
* Redkale中缓存数据源的核心类。 主要供业务开发者使用, 技术开发者提供CacheSource的实现。<br>
|
||||||
@@ -25,7 +26,7 @@ import org.redkale.convert.*;
|
|||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
*/
|
*/
|
||||||
public interface CacheSource {
|
public interface CacheSource extends Resourcable {
|
||||||
|
|
||||||
public String getType();
|
public String getType();
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ import org.redkale.util.*;
|
|||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public interface DataSource {
|
public interface DataSource extends Resourcable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取数据源类型
|
* 获取数据源类型
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import java.util.function.BiConsumer;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 环境变量, 只读版Properties
|
* 环境变量, 只读版Properties
|
||||||
|
* 只存放system.property.、mimetype.property.、redkale.cachesource(.|[)、redkale.datasource(.|[)和其他非redkale.开头的配置项
|
||||||
*
|
*
|
||||||
* 详情见: https://redkale.org
|
* 详情见: https://redkale.org
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user