[agent] [init] - 优化启动信息处理

This commit is contained in:
zeaslity
2023-02-14 15:54:42 +08:00
parent d15ca087ac
commit 145bbac7ea

View File

@@ -8,14 +8,13 @@ import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.Environment;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
@@ -23,18 +22,12 @@ import java.util.Properties;
@Slf4j
public class CollectSystemInfo implements ApplicationContextAware {
public AgentServerInfo agentServerInfo;
@Resource
Environment environment;
private ApplicationContext context;
@Resource
OctopusAgentInitService octopusAgentInitService;
public AgentServerInfo agentServerInfo;
private ApplicationContext context;
@PostConstruct
private void getInjectServerInfo() {
@@ -43,6 +36,44 @@ public class CollectSystemInfo implements ApplicationContextAware {
agentServerInfo = (AgentServerInfo) context.getBean("agentServerInfo");
// 进行归一化处理,去除掉属性中所有多余的 "
log.debug("开始归一化处理 AgentInfoVO");
Class<AgentServerInfo> infoClass = AgentServerInfo.class;
Arrays
.stream(infoClass.getDeclaredFields())
.forEach(
field -> {
try {
log.debug(
"开始处理初始化AgentInfoVO中的字段 => {}",
field.getName()
);
if (!field
.getType()
.equals(String.class)) {
return;
}
// must do this
field.setAccessible(true);
Object o = field.get(agentServerInfo);
if (null == o) {
return;
}
String value = (String) o;
field.set(
agentServerInfo,
value.replace("\"",
"")
);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
);
if (ObjectUtils.isEmpty(agentServerInfo)) {
throw new MyRuntimeException(" Collect server info error !");
}