From b8771f0dac72e7f12730390c35d9cb4522e33310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Fri, 16 Apr 2021 12:52:24 +0800 Subject: [PATCH] simplify premain logic --- .../com/alibaba/testable/agent/PreMain.java | 49 ++---------------- .../testable/agent/config/ArgumentParser.java | 51 +++++++++++++++++++ .../agent/config/PropertiesParser.java | 4 +- 3 files changed, 56 insertions(+), 48 deletions(-) create mode 100644 testable-agent/src/main/java/com/alibaba/testable/agent/config/ArgumentParser.java diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java index e261f7e..9a16447 100755 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java @@ -1,9 +1,9 @@ package com.alibaba.testable.agent; +import com.alibaba.testable.agent.config.ArgumentParser; import com.alibaba.testable.agent.config.PropertiesParser; import com.alibaba.testable.agent.transformer.TestableClassTransformer; import com.alibaba.testable.agent.util.GlobalConfig; -import com.alibaba.testable.core.model.MockScope; import com.alibaba.testable.core.util.LogUtil; import com.alibaba.ttl.threadpool.agent.TtlAgent; @@ -15,20 +15,9 @@ import java.lang.instrument.Instrumentation; */ public class PreMain { - private static final String AND = "&"; - private static final String USE_THREAD_POOL = "useThreadPool"; - private static final String LOG_LEVEL = "logLevel"; - private static final String LOG_FILE = "logFile"; - private static final String DUMP_PATH = "dumpPath"; - private static final String PKG_PREFIX = "pkgPrefix"; - private static final String MOCK_SCOPE = "mockScope"; - private static final String CONFIG_FILE = "configFile"; - private static final String EQUAL = "="; - private static String configFilePath = null; - public static void premain(String agentArgs, Instrumentation inst) { - parseArgs(agentArgs); - new PropertiesParser().parseFile(configFilePath); + ArgumentParser.parseArgs(agentArgs); + PropertiesParser.parseFile(ArgumentParser.configFilePath); GlobalConfig.setupLogRootPath(); if (GlobalConfig.isEnhanceThreadLocal()) { // add transmittable thread local transformer @@ -39,38 +28,6 @@ public class PreMain { cleanup(); } - private static void parseArgs(String args) { - if (args == null) { - return; - } - for (String a : args.split(AND)) { - int i = a.indexOf(EQUAL); - if (i > 0) { - // parameter with key = value - String k = a.substring(0, i); - String v = a.substring(i + 1); - if (k.equals(LOG_LEVEL)) { - GlobalConfig.setLogLevel(v); - } else if (k.equals(LOG_FILE)) { - GlobalConfig.setLogFile(v); - } else if (k.equals(DUMP_PATH)) { - GlobalConfig.setDumpPath(v); - } else if (k.equals(PKG_PREFIX)) { - GlobalConfig.setPkgPrefix(v); - } else if (k.equals(MOCK_SCOPE)) { - GlobalConfig.setDefaultMockScope(MockScope.of(v)); - } else if (k.equals(CONFIG_FILE)) { - configFilePath = v; - } - } else { - // parameter with single value - if (a.equals(USE_THREAD_POOL)) { - GlobalConfig.setEnhanceThreadLocal(true); - } - } - } - } - private static void cleanup() { Runtime.getRuntime().addShutdownHook(new Thread() { @Override diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/config/ArgumentParser.java b/testable-agent/src/main/java/com/alibaba/testable/agent/config/ArgumentParser.java new file mode 100644 index 0000000..08a070c --- /dev/null +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/config/ArgumentParser.java @@ -0,0 +1,51 @@ +package com.alibaba.testable.agent.config; + +import com.alibaba.testable.agent.util.GlobalConfig; +import com.alibaba.testable.core.model.MockScope; + +public class ArgumentParser { + + private static final String AND = "&"; + private static final String USE_THREAD_POOL = "useThreadPool"; + private static final String LOG_LEVEL = "logLevel"; + private static final String LOG_FILE = "logFile"; + private static final String DUMP_PATH = "dumpPath"; + private static final String PKG_PREFIX = "pkgPrefix"; + private static final String MOCK_SCOPE = "mockScope"; + private static final String CONFIG_FILE = "configFile"; + private static final String EQUAL = "="; + public static String configFilePath = null; + + public static void parseArgs(String args) { + if (args == null) { + return; + } + for (String a : args.split(AND)) { + int i = a.indexOf(EQUAL); + if (i > 0) { + // parameter with key = value + String k = a.substring(0, i); + String v = a.substring(i + 1); + if (k.equals(LOG_LEVEL)) { + GlobalConfig.setLogLevel(v); + } else if (k.equals(LOG_FILE)) { + GlobalConfig.setLogFile(v); + } else if (k.equals(DUMP_PATH)) { + GlobalConfig.setDumpPath(v); + } else if (k.equals(PKG_PREFIX)) { + GlobalConfig.setPkgPrefix(v); + } else if (k.equals(MOCK_SCOPE)) { + GlobalConfig.setDefaultMockScope(MockScope.of(v)); + } else if (k.equals(CONFIG_FILE)) { + configFilePath = v; + } + } else { + // parameter with single value + if (a.equals(USE_THREAD_POOL)) { + GlobalConfig.setEnhanceThreadLocal(true); + } + } + } + } + +} diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/config/PropertiesParser.java b/testable-agent/src/main/java/com/alibaba/testable/agent/config/PropertiesParser.java index 70a6f58..2c58e9c 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/config/PropertiesParser.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/config/PropertiesParser.java @@ -25,7 +25,7 @@ public class PropertiesParser { private static final String ENABLE_THREAD_POOL = "thread.pool.enhance.enable"; private static final String ENABLE_OMNI_INJECT = "omni.constructor.enhance.enable"; - public void parseFile(String configFilePath) { + public static void parseFile(String configFilePath) { String path = (configFilePath == null) ? DEFAULT_CONFIG_FILE : configFilePath; String fullPath = PathUtil.join(System.getProperty(PROPERTY_USER_DIR), path); Properties pps = new Properties(); @@ -44,7 +44,7 @@ public class PropertiesParser { parsePropertiesContent(pps); } - private void parsePropertiesContent(Properties pps) { + private static void parsePropertiesContent(Properties pps) { Enumeration en = pps.propertyNames(); while(en.hasMoreElements()) { String k = (String)en.nextElement();