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.开头的配置项
void updateEnvironmentProperty(String key, Object value, Properties envChangeCache, Properties sourceChangeCache) {
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[")
|| key.startsWith("redkale.cachesource.") || key.startsWith("redkale.cachesource[")) {
if (sourceChangeCache == null) {
sourceProperties.put(key, val);
} else {
sourceChangeCache.put(key, val);
if (!Objects.equals(val, sourceProperties.getProperty(key))) {
if (sourceChangeCache == null) {
sourceProperties.put(key, val);
} else {
sourceChangeCache.put(key, val);
}
}
} else if (key.startsWith("system.property.")) {
String propName = key.substring("system.property.".length());

View File

@@ -53,9 +53,10 @@ public abstract class PropertiesAgent {
public abstract void destroy(AnyValue conf);
protected void updateEnvironmentProperties(Application application, Properties props) {
if (props.isEmpty()) return;
Properties envChangeCache = 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()) {
application.resourceFactory.register(envChangeCache, "", Environment.class);
}

View File

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

View File

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

View File

@@ -447,7 +447,11 @@ public final class ResourceFactory {
properties.forEach((k, v) -> {
Object old = register(true, k.toString(), String.class, v, wrappers);
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<>();