groovy脚本无法使用jar包
This commit is contained in:
parent
6da82053a1
commit
87221dfda2
|
@ -283,6 +283,13 @@
|
|||
<version>2.0.22</version>
|
||||
</dependency>
|
||||
|
||||
<!-- ApacheJMeter_core scope 是 runtime,这里去掉scope -->
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-jsr223</artifactId>
|
||||
<version>2.5.10</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 执行 js 代码依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.graalvm.sdk</groupId>
|
||||
|
|
|
@ -24,6 +24,7 @@ import java.io.FileReader;
|
|||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
|
@ -34,6 +35,10 @@ import javax.script.ScriptEngine;
|
|||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import io.metersphere.base.domain.JarConfig;
|
||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.service.JarConfigService;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.commons.collections.map.LRUMap;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -45,9 +50,11 @@ import org.apache.jmeter.threads.JMeterContext;
|
|||
import org.apache.jmeter.threads.JMeterContextService;
|
||||
import org.apache.jmeter.threads.JMeterVariables;
|
||||
import org.apache.jorphan.util.JOrphanUtils;
|
||||
import org.codehaus.groovy.jsr223.GroovyScriptEngineImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Base class for JSR223 Test elements
|
||||
*
|
||||
|
@ -166,6 +173,9 @@ public abstract class JSR223TestElement extends ScriptingTestElement
|
|||
*/
|
||||
protected Object processFileOrScript(ScriptEngine scriptEngine, final Bindings pBindings)
|
||||
throws IOException, ScriptException {
|
||||
|
||||
this.loadGroovyJar(scriptEngine);
|
||||
|
||||
Bindings bindings = pBindings;
|
||||
if (bindings == null) {
|
||||
bindings = scriptEngine.createBindings();
|
||||
|
@ -250,6 +260,37 @@ public abstract class JSR223TestElement extends ScriptingTestElement
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* groovy 使用的是自己的类加载器,
|
||||
* 这里再执行脚本前,使用 groovy的加载器加载jar包,
|
||||
* 解决groovy脚本无法使用jar包的问题
|
||||
* @Auth jianxing
|
||||
* @param scriptEngine
|
||||
*/
|
||||
public static void loadGroovyJar(ScriptEngine scriptEngine) {
|
||||
if (scriptEngine instanceof GroovyScriptEngineImpl) {
|
||||
GroovyScriptEngineImpl groovyScriptEngine = (GroovyScriptEngineImpl) scriptEngine;
|
||||
|
||||
JarConfigService jarConfigService = CommonBeanFactory.getBean(JarConfigService.class);
|
||||
List<JarConfig> jars = jarConfigService.list();
|
||||
|
||||
jars.forEach(jarConfig -> {
|
||||
try {
|
||||
String path = jarConfig.getPath();
|
||||
File file = new File(path);
|
||||
if (file.isDirectory() && !path.endsWith("/")) {
|
||||
file = new File(path + "/");
|
||||
}
|
||||
groovyScriptEngine.getClassLoader().addURL(file.toURI().toURL());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean true if element is not compilable or if compilation succeeds
|
||||
* @throws IOException if script is missing
|
||||
|
@ -351,4 +392,4 @@ public abstract class JSR223TestElement extends ScriptingTestElement
|
|||
public void setScriptLanguage(String s) {
|
||||
scriptLanguage = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue