mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-03-30 22:20:34 +08:00
simplify premain logic
This commit is contained in:
parent
42dd58584b
commit
b8771f0dac
@ -1,9 +1,9 @@
|
|||||||
package com.alibaba.testable.agent;
|
package com.alibaba.testable.agent;
|
||||||
|
|
||||||
|
import com.alibaba.testable.agent.config.ArgumentParser;
|
||||||
import com.alibaba.testable.agent.config.PropertiesParser;
|
import com.alibaba.testable.agent.config.PropertiesParser;
|
||||||
import com.alibaba.testable.agent.transformer.TestableClassTransformer;
|
import com.alibaba.testable.agent.transformer.TestableClassTransformer;
|
||||||
import com.alibaba.testable.agent.util.GlobalConfig;
|
import com.alibaba.testable.agent.util.GlobalConfig;
|
||||||
import com.alibaba.testable.core.model.MockScope;
|
|
||||||
import com.alibaba.testable.core.util.LogUtil;
|
import com.alibaba.testable.core.util.LogUtil;
|
||||||
import com.alibaba.ttl.threadpool.agent.TtlAgent;
|
import com.alibaba.ttl.threadpool.agent.TtlAgent;
|
||||||
|
|
||||||
@ -15,20 +15,9 @@ import java.lang.instrument.Instrumentation;
|
|||||||
*/
|
*/
|
||||||
public class PreMain {
|
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) {
|
public static void premain(String agentArgs, Instrumentation inst) {
|
||||||
parseArgs(agentArgs);
|
ArgumentParser.parseArgs(agentArgs);
|
||||||
new PropertiesParser().parseFile(configFilePath);
|
PropertiesParser.parseFile(ArgumentParser.configFilePath);
|
||||||
GlobalConfig.setupLogRootPath();
|
GlobalConfig.setupLogRootPath();
|
||||||
if (GlobalConfig.isEnhanceThreadLocal()) {
|
if (GlobalConfig.isEnhanceThreadLocal()) {
|
||||||
// add transmittable thread local transformer
|
// add transmittable thread local transformer
|
||||||
@ -39,38 +28,6 @@ public class PreMain {
|
|||||||
cleanup();
|
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() {
|
private static void cleanup() {
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -25,7 +25,7 @@ public class PropertiesParser {
|
|||||||
private static final String ENABLE_THREAD_POOL = "thread.pool.enhance.enable";
|
private static final String ENABLE_THREAD_POOL = "thread.pool.enhance.enable";
|
||||||
private static final String ENABLE_OMNI_INJECT = "omni.constructor.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 path = (configFilePath == null) ? DEFAULT_CONFIG_FILE : configFilePath;
|
||||||
String fullPath = PathUtil.join(System.getProperty(PROPERTY_USER_DIR), path);
|
String fullPath = PathUtil.join(System.getProperty(PROPERTY_USER_DIR), path);
|
||||||
Properties pps = new Properties();
|
Properties pps = new Properties();
|
||||||
@ -44,7 +44,7 @@ public class PropertiesParser {
|
|||||||
parsePropertiesContent(pps);
|
parsePropertiesContent(pps);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parsePropertiesContent(Properties pps) {
|
private static void parsePropertiesContent(Properties pps) {
|
||||||
Enumeration<?> en = pps.propertyNames();
|
Enumeration<?> en = pps.propertyNames();
|
||||||
while(en.hasMoreElements()) {
|
while(en.hasMoreElements()) {
|
||||||
String k = (String)en.nextElement();
|
String k = (String)en.nextElement();
|
||||||
|
Loading…
Reference in New Issue
Block a user