PropertiesAgent优化

This commit is contained in:
Redkale
2022-12-07 13:26:52 +08:00
parent 4560c231ce
commit 07ed7163ea
5 changed files with 17 additions and 9 deletions

View File

@@ -1714,13 +1714,15 @@ public final class Application {
//key只会是system.property.、mimetype.property.、redkale.cachesource(.|[)、redkale.datasource(.|[)和其他非redkale.开头的配置项 //key只会是system.property.、mimetype.property.、redkale.cachesource(.|[)、redkale.datasource(.|[)和其他非redkale.开头的配置项
void updateEnvironmentProperty(String key, Object value, Properties envChangeCache, Properties sourceChangeCache) { void updateEnvironmentProperty(String key, Object value, Properties envChangeCache, Properties sourceChangeCache) {
if (key == null || value == null) return; if (key == null || value == null) return;
String val = replaceValue(value.toString()); String val = replaceValue(value.toString()).trim();
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[")) {
if (sourceChangeCache == null) { if (!Objects.equals(val, sourceProperties.getProperty(key))) {
sourceProperties.put(key, val); if (sourceChangeCache == null) {
} else { sourceProperties.put(key, val);
sourceChangeCache.put(key, val); } else {
sourceChangeCache.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());

View File

@@ -53,9 +53,10 @@ public abstract class PropertiesAgent {
public abstract void destroy(AnyValue conf); public abstract void destroy(AnyValue conf);
protected void updateEnvironmentProperties(Application application, Properties props) { protected void updateEnvironmentProperties(Application application, Properties props) {
if (props.isEmpty()) return;
Properties envChangeCache = new Properties(); Properties envChangeCache = new Properties();
Properties sourceChangeCache = new Properties(); Properties sourceChangeCache = new Properties();
props.forEach((k, v) -> application.updateEnvironmentProperty(k.toString(), v, envChangeCache, sourceChangeCache)); props.forEach((k, v) -> application.updateEnvironmentProperty(k.toString(), v.toString().trim(), envChangeCache, sourceChangeCache));
if (!envChangeCache.isEmpty()) { if (!envChangeCache.isEmpty()) {
application.resourceFactory.register(envChangeCache, "", Environment.class); application.resourceFactory.register(envChangeCache, "", Environment.class);
} }

View File

@@ -1152,7 +1152,7 @@ public class DataJdbcSource extends DataSqlSource {
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
clientInfo.put("version", urlVersion); clientInfo.put("version", String.valueOf(urlVersion));
} }
@ResourceListener @ResourceListener
@@ -1179,7 +1179,7 @@ public class DataJdbcSource extends DataSqlSource {
|| !Objects.equals(newPassword, this.connectAttrs.get("password")) || !Objects.equals(newUrl, url)) { || !Objects.equals(newPassword, this.connectAttrs.get("password")) || !Objects.equals(newUrl, url)) {
this.urlVersion++; this.urlVersion++;
Properties newClientInfo = new Properties(); Properties newClientInfo = new Properties();
newClientInfo.put("version", urlVersion); newClientInfo.put("version", String.valueOf(urlVersion));
this.clientInfo = newClientInfo; this.clientInfo = newClientInfo;
} }
this.url = newUrl; this.url = newUrl;

View File

@@ -54,6 +54,7 @@ public interface ResourceEvent<T> {
static <T> String cover(T val) { static <T> String cover(T val) {
if (val == null) return null; if (val == null) return null;
String str = val.toString(); String str = val.toString();
if ("false".equalsIgnoreCase(str)) return str;
if (str.length() <= 4) return str; if (str.length() <= 4) return str;
if (numRegx.test(str)) return str; if (numRegx.test(str)) return str;
return str.substring(0, 2) + "***" + str.substring(str.length() - 2); return str.substring(0, 2) + "***" + str.substring(str.length() - 2);

View File

@@ -447,7 +447,11 @@ public final class ResourceFactory {
properties.forEach((k, v) -> { properties.forEach((k, v) -> {
Object old = register(true, k.toString(), String.class, v, wrappers); Object old = register(true, k.toString(), String.class, v, wrappers);
if (!Objects.equals(v, old)) { if (!Objects.equals(v, old)) {
environmentEventList.add(ResourceEvent.create(k.toString(), v, old)); String key = k.toString();
if (key.startsWith("property.")) {
key = key.substring("property.".length());
}
environmentEventList.add(ResourceEvent.create(key, v, old));
} }
}); });
Map<Object, Method> envListenMap = new LinkedHashMap<>(); Map<Object, Method> envListenMap = new LinkedHashMap<>();