Skip to content

Commit

Permalink
[ISSUE alibaba#11231]Optimize the handleSpringBinder method in Proper…
Browse files Browse the repository at this point in the history
…tiesUtil.
  • Loading branch information
stone-98 committed Oct 9, 2023
1 parent 7911eb0 commit 11220ca
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 42 deletions.
18 changes: 10 additions & 8 deletions auth/src/main/java/com/alibaba/nacos/auth/config/AuthConfigs.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ private void refreshPluginProperties() {
try {
Map<String, Properties> newProperties = new HashMap<>(1);
Properties properties = PropertiesUtil.getPropertiesWithPrefix(EnvUtil.getEnvironment(), PREFIX);
for (String each : properties.stringPropertyNames()) {
int typeIndex = each.indexOf('.');
String type = each.substring(0, typeIndex);
String subKey = each.substring(typeIndex + 1);
newProperties.computeIfAbsent(type, key -> new Properties())
.setProperty(subKey, properties.getProperty(each));
if (properties != null) {
for (String each : properties.stringPropertyNames()) {
int typeIndex = each.indexOf('.');
String type = each.substring(0, typeIndex);
String subKey = each.substring(typeIndex + 1);
newProperties.computeIfAbsent(type, key -> new Properties())
.setProperty(subKey, properties.getProperty(each));
}
}
authPluginProperties = newProperties;
} catch (Exception e) {
Expand Down Expand Up @@ -177,8 +179,8 @@ public void onEvent(ServerConfigChangeEvent event) {
cachingEnabled = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_CACHING_ENABLED, Boolean.class, true);
serverIdentityKey = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_SERVER_IDENTITY_KEY, "");
serverIdentityValue = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_SERVER_IDENTITY_VALUE, "");
enableUserAgentAuthWhite = EnvUtil
.getProperty(Constants.Auth.NACOS_CORE_AUTH_ENABLE_USER_AGENT_AUTH_WHITE, Boolean.class, false);
enableUserAgentAuthWhite = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_ENABLE_USER_AGENT_AUTH_WHITE,
Boolean.class, false);
nacosAuthSystemType = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_SYSTEM_TYPE, "");
refreshPluginProperties();
ModuleStateHolder.getInstance().getModuleState(AuthModuleStateBuilder.AUTH_MODULE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ private void refreshPluginProperties() {
try {
Map<String, Properties> newProperties = new HashMap<>(3);
Properties properties = PropertiesUtil.getPropertiesWithPrefix(EnvUtil.getEnvironment(), PREFIX);
for (String each : properties.stringPropertyNames()) {
int typeIndex = each.indexOf('.');
String type = each.substring(0, typeIndex);
String subKey = each.substring(typeIndex + 1);
newProperties.computeIfAbsent(type, key -> new Properties())
.setProperty(subKey, properties.getProperty(each));
if (properties != null) {
for (String each : properties.stringPropertyNames()) {
int typeIndex = each.indexOf('.');
String type = each.substring(0, typeIndex);
String subKey = each.substring(typeIndex + 1);
newProperties.computeIfAbsent(type, key -> new Properties())
.setProperty(subKey, properties.getProperty(each));
}
}
configPluginProperties = newProperties;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.PropertiesUtil;

import java.lang.reflect.InvocationTargetException;

/**
* Grpc config.
*
Expand All @@ -41,11 +39,9 @@ public class RpcServerTlsConfig extends TlsConfig {

public static synchronized RpcServerTlsConfig getInstance() {
if (null == instance) {
try {
instance = PropertiesUtil
.handleSpringBinder(EnvUtil.getEnvironment(), PREFIX, RpcServerTlsConfig.class);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
Loggers.REMOTE.warn("TLS config bind failed, use default value", e);
instance = PropertiesUtil.handleSpringBinder(EnvUtil.getEnvironment(), PREFIX, RpcServerTlsConfig.class);
if (instance == null) {
Loggers.REMOTE.debug("TLS configuration is empty, use default value");
instance = new RpcServerTlsConfig();
}
}
Expand Down
22 changes: 6 additions & 16 deletions sys/src/main/java/com/alibaba/nacos/sys/utils/PropertiesUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package com.alibaba.nacos.sys.utils;

import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.Environment;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Properties;

Expand All @@ -30,13 +30,11 @@
*/
public class PropertiesUtil {

public static Properties getPropertiesWithPrefix(Environment environment, String prefix)
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
public static Properties getPropertiesWithPrefix(Environment environment, String prefix) {
return handleSpringBinder(environment, prefix, Properties.class);
}

public static Map<String, Object> getPropertiesWithPrefixForMap(Environment environment, String prefix)
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
public static Map<String, Object> getPropertiesWithPrefixForMap(Environment environment, String prefix) {
return handleSpringBinder(environment, prefix, Map.class);
}

Expand All @@ -49,16 +47,8 @@ public static Map<String, Object> getPropertiesWithPrefixForMap(Environment envi
* @param <T> target class
* @return binder object
*/
@SuppressWarnings("unchecked")
public static <T> T handleSpringBinder(Environment environment, String prefix, Class<T> targetClass)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException {
Class<?> binderClass = Class.forName("org.springframework.boot.context.properties.bind.Binder");
Method getMethod = binderClass.getDeclaredMethod("get", Environment.class);
Method bindMethod = binderClass.getDeclaredMethod("bind", String.class, Class.class);
Object binderObject = getMethod.invoke(null, environment);
public static <T> T handleSpringBinder(Environment environment, String prefix, Class<T> targetClass) {
String prefixParam = prefix.endsWith(".") ? prefix.substring(0, prefix.length() - 1) : prefix;
Object bindResultObject = bindMethod.invoke(binderObject, prefixParam, targetClass);
Method resultGetMethod = bindResultObject.getClass().getDeclaredMethod("get");
return (T) resultGetMethod.invoke(bindResultObject);
return Binder.get(environment).bind(prefixParam, Bindable.of(targetClass)).orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Properties;

Expand All @@ -40,8 +39,7 @@ public class PropertiesUtilTest {

@Test
@SuppressWarnings("unchecked")
public void testGetPropertiesWithPrefixForMap()
throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
public void testGetPropertiesWithPrefixForMap() {
Map<String, Object> actual = PropertiesUtil.getPropertiesWithPrefixForMap(environment, "nacos.prefix");
assertEquals(3, actual.size());
for (Map.Entry<String, Object> entry : actual.entrySet()) {
Expand All @@ -64,9 +62,14 @@ public void testGetPropertiesWithPrefixForMap()
}

@Test
public void testGetPropertiesWithPrefix()
throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
public void testGetPropertiesWithPrefix() {
Properties actual = PropertiesUtil.getPropertiesWithPrefix(environment, "nacos.prefix");
assertEquals(3, actual.size());
}

@Test
public void testHandleSpringBinder() {
Map properties = PropertiesUtil.handleSpringBinder(environment, "nacos.prefix", Map.class);
assertEquals(3, properties.size());
}
}

0 comments on commit 11220ca

Please sign in to comment.