simplify premain logic

This commit is contained in:
金戟 2021-04-16 12:52:24 +08:00
parent 42dd58584b
commit b8771f0dac
3 changed files with 56 additions and 48 deletions

View File

@ -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

View File

@ -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);
}
}
}
}
}

View File

@ -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();