From 145bbac7eaa205c56ed376c8215ac729abb06681 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Tue, 14 Feb 2023 15:54:42 +0800 Subject: [PATCH] =?UTF-8?q?[agent]=20[init]=20-=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E4=BF=A1=E6=81=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bootup/CollectSystemInfo.java | 63 ++++++++++++++----- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/agent/src/main/java/io/wdd/agent/initialization/bootup/CollectSystemInfo.java b/agent/src/main/java/io/wdd/agent/initialization/bootup/CollectSystemInfo.java index 52155c8..a76fdaa 100644 --- a/agent/src/main/java/io/wdd/agent/initialization/bootup/CollectSystemInfo.java +++ b/agent/src/main/java/io/wdd/agent/initialization/bootup/CollectSystemInfo.java @@ -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,26 +22,58 @@ 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(){ + private void getInjectServerInfo() { log.info("[ Octopus Agent ]-- Starting getInjectServerInfo"); agentServerInfo = (AgentServerInfo) context.getBean("agentServerInfo"); + // 进行归一化处理,去除掉属性中所有多余的 " + log.debug("开始归一化处理 AgentInfoVO"); + Class 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 !"); } @@ -62,16 +93,16 @@ public class CollectSystemInfo implements ApplicationContextAware { } @Deprecated - public void initialReadingEnvironment(){ + public void initialReadingEnvironment() { // https://zhuanlan.zhihu.com/p/449416472 // https://cloud.tencent.com/developer/article/1919814 // https://blog.51cto.com/binghe001/5244823 - try{ + try { - Properties props =System.getProperties(); + Properties props = System.getProperties(); System.out.println("props = " + props); System.out.println(); @@ -99,8 +130,8 @@ public class CollectSystemInfo implements ApplicationContextAware { String osArch = System.getProperty("os.arch"); System.out.println("当前用户:" + userName); - System.out.println("用户的主目录:"+props.getProperty("user.home")); - System.out.println("用户的当前工作目录:"+props.getProperty("user.dir")); + System.out.println("用户的主目录:" + props.getProperty("user.home")); + System.out.println("用户的当前工作目录:" + props.getProperty("user.dir")); System.out.println("主机名称:" + localName); System.out.println("主机系统:" + osName); System.out.println("系统版本:" + osVersion);