From 3db22f60619e1d2456d2d8a6ecceb2c2ce61801f Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Tue, 22 Feb 2022 18:15:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20Mo?= =?UTF-8?q?ck=E6=9C=9F=E6=9C=9B=E7=9A=84=E5=90=8E=E7=BD=AE=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E4=B8=AD=E5=A2=9E=E5=8A=A0=E5=AF=B9=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9jar=E5=8C=85=E7=9A=84=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mock期望的后置脚本中增加对第三方jar包的调用 --- .../api/mock/utils/MockScriptEngineUtils.java | 60 +++++++++++++++++-- .../Components/MockCombinationCondition.vue | 4 +- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/mock/utils/MockScriptEngineUtils.java b/backend/src/main/java/io/metersphere/api/mock/utils/MockScriptEngineUtils.java index 25e0d12db5..a34e2fa09a 100644 --- a/backend/src/main/java/io/metersphere/api/mock/utils/MockScriptEngineUtils.java +++ b/backend/src/main/java/io/metersphere/api/mock/utils/MockScriptEngineUtils.java @@ -2,11 +2,18 @@ package io.metersphere.api.mock.utils; import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.mock.RequestMockParams; +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.lang3.StringUtils; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -36,23 +43,66 @@ public class MockScriptEngineUtils { } } + public void loadJar(String jarPath) throws Exception { + File jarFile = new File(jarPath); + // 从URLClassLoader类中获取类所在文件夹的方法,jar也可以认为是一个文件夹 + Method method = null; + try { + method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); + } catch (NoSuchMethodException | SecurityException e1) { + e1.printStackTrace(); + } + // 获取方法的访问权限以便写回 + boolean accessible = method.isAccessible(); + try { + method.setAccessible(true); + // 获取系统类加载器 + URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); + URL url = jarFile.toURI().toURL(); + method.invoke(classLoader, url); + } catch (Exception e) { + e.printStackTrace(); + } finally { + method.setAccessible(accessible); + } + } + + /** + * 加载jar包 + */ + private void loadJars() { + JarConfigService jarConfigService = CommonBeanFactory.getBean(JarConfigService.class); + if (jarConfigService != null) { + List jars = jarConfigService.list(); + jars.forEach(jarConfig -> { + try { + this.loadJar(jarConfig.getPath()); + } catch (Exception e) { + e.printStackTrace(); + LogUtil.error(e.getMessage(), e); + } + }); + } + } + public ScriptEngine getBaseScriptEngine(String scriptLanguage, String url, Map headerMap, RequestMockParams requestMockParams) { ScriptEngine engine = null; try { if (StringUtils.isEmpty(scriptLanguage)) { return null; } + String preScript = ""; if (StringUtils.equalsIgnoreCase(scriptLanguage, "beanshell")) { ScriptEngineManager scriptEngineFactory = new ScriptEngineManager(); - engine = scriptEngineFactory.getEngineByName(scriptLanguage); - String preScript = this.genBeanshellPreScript(url, headerMap, requestMockParams); - engine.eval(preScript); + engine = scriptEngineFactory.getEngineByName("beanshell"); + preScript = this.genBeanshellPreScript(url, headerMap, requestMockParams); } else if (StringUtils.equalsIgnoreCase(scriptLanguage, "python")) { ScriptEngineManager scriptEngineFactory = new ScriptEngineManager(); engine = scriptEngineFactory.getEngineByName(scriptLanguage); - String preScript = this.genPythonPreScript(url, headerMap, requestMockParams); - engine.eval(preScript); + preScript = this.genPythonPreScript(url, headerMap, requestMockParams); } + this.loadJars(); + engine.eval(preScript); } catch (Exception e) { LogUtil.error(e); } diff --git a/frontend/src/business/components/api/definition/components/mock/Components/MockCombinationCondition.vue b/frontend/src/business/components/api/definition/components/mock/Components/MockCombinationCondition.vue index ac503e0cdc..ce94296445 100644 --- a/frontend/src/business/components/api/definition/components/mock/Components/MockCombinationCondition.vue +++ b/frontend/src/business/components/api/definition/components/mock/Components/MockCombinationCondition.vue @@ -38,8 +38,8 @@ export default { lineDivMarginTopHeight: 0, lineDivBottomHeight: 0, filterTypes: [ - {id: 'And', label: 'And'}, - {id: 'Or', label: 'Or'}, + {id: 'And', label: 'AND'}, + {id: 'Or', label: 'OR'}, ], } },