support configure file

This commit is contained in:
金戟 2021-04-07 09:36:47 +08:00
parent f6b5bab250
commit 74fbc5b53f
6 changed files with 100 additions and 4 deletions

View File

@ -0,0 +1 @@
omni.constructor.enhance.enable = true

View File

@ -0,0 +1 @@
omni.constructor.enhance.enable = true

View File

@ -1,5 +1,6 @@
package com.alibaba.testable.agent;
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;
@ -21,13 +22,15 @@ public class PreMain {
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 boolean enhanceThreadLocal = false;
private static String configFilePath = null;
public static void premain(String agentArgs, Instrumentation inst) {
parseArgs(agentArgs);
new PropertiesParser().parseFile(configFilePath);
GlobalConfig.setupLogRootPath();
if (enhanceThreadLocal) {
if (GlobalConfig.isEnhanceThreadLocal()) {
// add transmittable thread local transformer
TtlAgent.premain(agentArgs, inst);
}
@ -56,11 +59,13 @@ public class PreMain {
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)) {
enhanceThreadLocal = true;
GlobalConfig.setEnhanceThreadLocal(true);
}
}
}

View File

@ -0,0 +1,70 @@
package com.alibaba.testable.agent.config;
import com.alibaba.testable.agent.util.GlobalConfig;
import com.alibaba.testable.agent.util.PathUtil;
import com.alibaba.testable.core.model.MockScope;
import com.alibaba.testable.core.util.LogUtil;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import static com.alibaba.testable.agent.constant.ConstPool.PROPERTY_USER_DIR;
public class PropertiesParser {
private static final String DEFAULT_CONFIG_FILE = "src/test/resources/testable.properties";
private static final String LOG_LEVEL = "log.level";
private static final String LOG_FILE = "log.file";
private static final String DUMP_PATH = "dump.path";
private static final String PKG_PREFIX_WHITELIST = "custom.pkgPrefix.whiteList";
private static final String DEFAULT_MOCK_SCOPE = "mock.scope.default";
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) {
String path = (configFilePath == null) ? DEFAULT_CONFIG_FILE : configFilePath;
String fullPath = PathUtil.join(System.getProperty(PROPERTY_USER_DIR), path);
Properties pps = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(fullPath));
pps.load(in);
LogUtil.verbose("Loaded configure file %s", fullPath);
} catch (IOException e) {
if (configFilePath == null) {
LogUtil.verbose("No configure file found, skip.");
} else {
LogUtil.warn("No configure file found at %s", fullPath);
}
return;
}
parsePropertiesContent(pps);
}
private void parsePropertiesContent(Properties pps) {
Enumeration<?> en = pps.propertyNames();
while(en.hasMoreElements()) {
String k = (String)en.nextElement();
String v = pps.getProperty(k);
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_WHITELIST)) {
GlobalConfig.setPkgPrefix(v);
} else if (k.equals(DEFAULT_MOCK_SCOPE)) {
GlobalConfig.setDefaultMockScope(MockScope.of(v));
} else if (k.equals(ENABLE_THREAD_POOL)) {
GlobalConfig.setEnhanceThreadLocal(Boolean.parseBoolean(v));
} else if (k.equals(ENABLE_OMNI_INJECT)) {
GlobalConfig.setEnhanceOmniConstructor(Boolean.parseBoolean(v));
}
}
}
}

View File

@ -52,7 +52,8 @@ public class TestableClassTransformer implements ClassFileTransformer {
return null;
}
LogUtil.verbose("Handle class: " + className);
byte[] bytes = new OmniClassHandler().getBytes(classFileBuffer);
byte[] bytes = GlobalConfig.isEnhanceOmniConstructor() ?
new OmniClassHandler().getBytes(classFileBuffer) : classFileBuffer;
ClassNode cn = ClassUtil.getClassNode(className);
if (cn != null) {
return transformMock(bytes, cn);

View File

@ -25,6 +25,8 @@ public class GlobalConfig {
private static String dumpPath = null;
private static String pkgPrefix = null;
private static MockScope defaultMockScope = MockScope.GLOBAL;
private static boolean enhanceThreadLocal = false;
private static boolean enhanceOmniConstructor = false;
public static void setLogLevel(String level) {
if (level.equals(MUTE)) {
@ -78,4 +80,20 @@ public class GlobalConfig {
LogUtil.setGlobalLogPath(PathUtil.join(System.getProperty(PROPERTY_USER_DIR), logFile));
}
}
public static void setEnhanceThreadLocal(boolean enabled) {
enhanceThreadLocal = enabled;
}
public static boolean isEnhanceThreadLocal() {
return enhanceThreadLocal;
}
public static void setEnhanceOmniConstructor(boolean enabled) {
enhanceOmniConstructor = enabled;
}
public static boolean isEnhanceOmniConstructor() {
return enhanceOmniConstructor;
}
}