优化PropertiesAgent接口

This commit is contained in:
Redkale
2022-11-25 20:27:41 +08:00
parent d6b626fd47
commit b24eb1ce9e
2 changed files with 56 additions and 49 deletions

View File

@@ -111,13 +111,6 @@ public final class Application {
*/ */
public static final String RESNAME_APP_CLIENT_ASYNCGROUP = "APP_CLIENT_ASYNCGROUP"; public static final String RESNAME_APP_CLIENT_ASYNCGROUP = "APP_CLIENT_ASYNCGROUP";
/**
* 环境变量, 类型Environment
*
* @since 2.7.0
*/
public static final String RESNAME_APP_ENV = "APP_ENV";
/** /**
* 当前Service所属的SNCP Server的地址 类型: SocketAddress、InetSocketAddress、String <br> * 当前Service所属的SNCP Server的地址 类型: SocketAddress、InetSocketAddress、String <br>
*/ */
@@ -306,7 +299,7 @@ public final class Application {
this.resourceFactory.register(RESNAME_APP_CONF_DIR, File.class, confFile); this.resourceFactory.register(RESNAME_APP_CONF_DIR, File.class, confFile);
this.resourceFactory.register(RESNAME_APP_CONF_DIR, Path.class, confFile.toPath()); this.resourceFactory.register(RESNAME_APP_CONF_DIR, Path.class, confFile.toPath());
} }
this.resourceFactory.register(RESNAME_APP_ENV, Environment.class, appEnvironment); this.resourceFactory.register(Environment.class, appEnvironment);
{ {
int nid = config.getIntValue("nodeid", 0); int nid = config.getIntValue("nodeid", 0);
this.nodeid = nid; this.nodeid = nid;
@@ -749,23 +742,24 @@ 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;
appProperties.put(key, value); putResourceProperties(key, value, null);
value = replaceValue(value); // appProperties.put(key, value);
if (key.startsWith("redkale.datasource.") || key.startsWith("redkale.datasource[") // value = replaceValue(value);
|| key.startsWith("redkale.cachesource.") || key.startsWith("redkale.cachesource[")) { // if (key.startsWith("redkale.datasource.") || key.startsWith("redkale.datasource[")
sourceProperties.put(key, value); // || key.startsWith("redkale.cachesource.") || key.startsWith("redkale.cachesource[")) {
} else if (key.startsWith("system.property.")) { // sourceProperties.put(key, value);
String propName = key.substring("system.property.".length()); // } else if (key.startsWith("system.property.")) {
if (System.getProperty(propName) == null) { //命令行传参数优先级高 // String propName = key.substring("system.property.".length());
System.setProperty(propName, value); // 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("mimetype.property.")) {
} else if (key.startsWith("property.")) { // MimeType.add(key.substring("mimetype.property.".length()), value);
resourceFactory.register(key, value); // } else if (key.startsWith("property.")) {
} else { // resourceFactory.register(key, value);
resourceFactory.register("property." + key, value); // } else {
} // resourceFactory.register("property." + key, value);
// }
} }
String dfloads = propertiesConf.getValue("load"); String dfloads = propertiesConf.getValue("load");
if (dfloads != null) { if (dfloads != null) {
@@ -779,14 +773,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());
appProperties.putAll(ps);
ps.forEach((x, y) -> { ps.forEach((x, y) -> {
if (x.toString().startsWith("redkale.datasource.") || x.toString().startsWith("redkale.datasource[") putResourceProperties(x.toString(), y, null);
|| x.toString().startsWith("redkale.cachesource.") || x.toString().startsWith("redkale.cachesource[")) {
sourceProperties.put(x, replaceValue(y.toString()));
} else {
resourceFactory.register("property." + x, replaceValue(y.toString()));
}
}); });
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.WARNING, "load properties(" + dfload + ") error", e); logger.log(Level.WARNING, "load properties(" + dfload + ") error", e);
@@ -813,7 +801,7 @@ public final class Application {
if (compileMode) { if (compileMode) {
this.propertiesAgent.compile(propertiesConf); this.propertiesAgent.compile(propertiesConf);
} else { } else {
this.propertiesAgent.init(this, resourceFactory, propertiesConf); this.propertiesAgent.init(this, propertiesConf);
} }
logger.info("PropertiesAgent (type = " + this.propertiesAgent.getClass().getSimpleName() + ") init in " + (System.currentTimeMillis() - s) + " ms"); logger.info("PropertiesAgent (type = " + this.propertiesAgent.getClass().getSimpleName() + ") init in " + (System.currentTimeMillis() - s) + " ms");
break; break;
@@ -1700,6 +1688,35 @@ 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) {
if (key == null || value == null) return;
appProperties.put(key, value);
value = replaceValue(value.toString());
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, String.valueOf(value));
}
} else if (key.startsWith("mimetype.property.")) {
MimeType.add(key.substring("mimetype.property.".length()), String.valueOf(value));
} else if (key.startsWith("property.")) {
if (cache == null) {
resourceFactory.register(key, value);
} else {
cache.put(key, value);
}
} else {
if (cache == null) {
resourceFactory.register("property." + key, value);
} else {
cache.put("property." + key, value);
}
}
}
private static String generateHelp() { private static String generateHelp() {
return "" return ""
+ "Usage: redkale [command] [arguments]\r\n" + "Usage: redkale [command] [arguments]\r\n"

View File

@@ -41,10 +41,9 @@ public abstract class PropertiesAgent {
* 初始化配置源配置项需要写入appProperties并监听配置项的变化 * 初始化配置源配置项需要写入appProperties并监听配置项的变化
* *
* @param application Application * @param application Application
* @param factory 依赖注入资源工厂
* @param conf 节点配置 * @param conf 节点配置
*/ */
public abstract void init(Application application, ResourceFactory factory, AnyValue conf); public abstract void init(Application application, AnyValue conf);
/** /**
* 销毁动作 * 销毁动作
@@ -53,22 +52,13 @@ public abstract class PropertiesAgent {
*/ */
public abstract void destroy(AnyValue conf); public abstract void destroy(AnyValue conf);
protected String getKeyResourceName(String key) { protected void putResourceProperties(Application application, Properties props) {
return key.startsWith("redkale.") Properties cache = new Properties();
|| key.startsWith("property.") props.forEach((k, v) -> application.putResourceProperties(k.toString(), v, cache));
|| key.startsWith("system.property.") application.resourceFactory.register(cache);
|| key.startsWith("mimetype.property.") ? key : ("property." + key);
}
protected void putResourceProperties(Application application, Properties newProps) {
newProps.forEach((k, v) -> putResourceProperties(application, k.toString(), v));
} }
protected void putResourceProperties(Application application, String key, Object value) { protected void putResourceProperties(Application application, String key, Object value) {
if (key.startsWith("redkale.datasource.") || key.startsWith("redkale.datasource[") application.putResourceProperties(key, value, null);
|| key.startsWith("redkale.cachesource.") || key.startsWith("redkale.cachesource[")) {
application.sourceProperties.put(key, value);
}
application.appProperties.put(key, value);
} }
} }