From 1abb6e89b9380f6b373482dfd7397e6618d34d77 Mon Sep 17 00:00:00 2001 From: Saisai Liu <1374434128@qq.com> Date: Sat, 4 May 2024 17:43:45 +0800 Subject: [PATCH] =?UTF-8?q?feat():=E8=A7=84=E5=88=99=E5=BC=95=E6=93=8E?= =?UTF-8?q?=E7=AE=80=E6=98=93=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../engine/controller/ScopeController.java | 8 +- .../com/muyu/engine/service/ScopeService.java | 4 +- .../engine/service/impl/ScopeServiceImpl.java | 98 ++++++++++++++----- 3 files changed, 83 insertions(+), 27 deletions(-) diff --git a/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/controller/ScopeController.java b/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/controller/ScopeController.java index 9dc88ed..a39328a 100644 --- a/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/controller/ScopeController.java +++ b/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/controller/ScopeController.java @@ -111,11 +111,11 @@ public class ScopeController extends BaseController /** * 测试 */ - @RequiresPermissions("engine:scope:test") + @RequiresPermissions("engine:scope:testEngine") @Log(title = "规则引擎", businessType = BusinessType.DELETE) - @GetMapping("test/{id}") - public Result test(@PathVariable Long id) throws ServletException { - return toAjax(scopeService.test(id)); + @GetMapping("testEngine/{id}") + public Result test(@PathVariable Long id) throws Exception { + return toAjax(scopeService.testEngine(id)); } diff --git a/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/service/ScopeService.java b/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/service/ScopeService.java index 623f421..72c3061 100644 --- a/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/service/ScopeService.java +++ b/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/service/ScopeService.java @@ -1,5 +1,7 @@ package com.muyu.engine.service; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.util.List; import com.baomidou.mybatisplus.extension.service.IService; @@ -72,5 +74,5 @@ public interface ScopeService extends IService void deleteScopeByRuleIds(Long[] ids); - boolean test(Long id) throws ServletException; + boolean testEngine(Long id) throws Exception; } diff --git a/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/service/impl/ScopeServiceImpl.java b/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/service/impl/ScopeServiceImpl.java index dddf0cb..9851907 100644 --- a/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/service/impl/ScopeServiceImpl.java +++ b/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/com/muyu/engine/service/impl/ScopeServiceImpl.java @@ -1,5 +1,8 @@ package com.muyu.engine.service.impl; +import java.io.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -18,6 +21,7 @@ import org.springframework.stereotype.Service; import com.muyu.engine.mapper.ScopeMapper; import com.muyu.engine.domain.Scope; import com.muyu.engine.service.ScopeService; +import org.springframework.util.ReflectionUtils; import javax.servlet.ServletException; @@ -29,8 +33,7 @@ import javax.servlet.ServletException; */ @Service @Log4j2 -public class ScopeServiceImpl extends ServiceImpl implements ScopeService -{ +public class ScopeServiceImpl extends ServiceImpl implements ScopeService { @Autowired private ScopeMapper scopeMapper; @Autowired @@ -43,8 +46,7 @@ public class ScopeServiceImpl extends ServiceImpl implements * @return 规则引擎代码 */ @Override - public Scope selectScopeById(Long id) - { + public Scope selectScopeById(Long id) { return scopeMapper.selectScopeById(id); } @@ -55,8 +57,7 @@ public class ScopeServiceImpl extends ServiceImpl implements * @return 规则引擎代码 */ @Override - public List selectScopeList(Scope scope) - { + public List selectScopeList(Scope scope) { return scopeMapper.selectScopeList(scope); } @@ -67,8 +68,7 @@ public class ScopeServiceImpl extends ServiceImpl implements * @return 结果 */ @Override - public int insertScope(Scope scope) - { + public int insertScope(Scope scope) { scope.setCreateTime(DateUtils.getNowDate()); return scopeMapper.insertScope(scope); } @@ -80,8 +80,7 @@ public class ScopeServiceImpl extends ServiceImpl implements * @return 结果 */ @Override - public int updateScope(Scope scope) - { + public int updateScope(Scope scope) { scope.setUpdateTime(DateUtils.getNowDate()); Scope one = this.getOne(new LambdaQueryWrapper<>() {{ eq(Scope::getValue, scope.getValue()); @@ -98,8 +97,7 @@ public class ScopeServiceImpl extends ServiceImpl implements * @return 结果 */ @Override - public int deleteScopeByIds(Long[] ids) - { + public int deleteScopeByIds(Long[] ids) { return scopeMapper.deleteScopeByIds(ids); } @@ -110,8 +108,7 @@ public class ScopeServiceImpl extends ServiceImpl implements * @return 结果 */ @Override - public int deleteScopeById(Long id) - { + public int deleteScopeById(Long id) { return scopeMapper.deleteScopeById(id); } @@ -124,31 +121,66 @@ public class ScopeServiceImpl extends ServiceImpl implements }}).stream().map(Scope::getId).toList(); longs.addAll(list); } - if (!longs.isEmpty()){ + if (!longs.isEmpty()) { this.deleteScopeByIds(longs.toArray(Long[]::new)); } } @Override - public boolean test(Long id) throws ServletException { + public boolean testEngine(Long id) throws Exception { + + RuleEngine ruleEngine = ruleEngineService.getById(id); + if (ruleEngine.getIsActivate().contains("no")) throw new ServletException("未激活"); + if (ruleEngine.getStatus().contains("1")) throw new ServletException("已停用"); + Scope scope = this.getOne(new LambdaQueryWrapper() {{ + eq(Scope::getRuleEngineId, ruleEngine.getId()); + eq(Scope::getValue, "taskContext"); + }}); + String code = scope.getCode(); + String path = code.substring(code.indexOf("com"), code.indexOf(";")).replaceAll("/.", "/").trim(); + String fileName = code.substring(code.indexOf("class") + 6, code.indexOf("{")).trim(); + String name = path+"."+fileName; + String javaPackageName = name.replace(".",File.separator)+".java"; + String javaAbsolutePath = "D:/ruoyi/FinallyTest/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/" +javaPackageName; + String jarAbsolutePath = "D:/ruoyi/FinallyTest/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/target/classes/com/muyu/engine/controller"; + Process process = Runtime.getRuntime().exec("javac -classpath "+ jarAbsolutePath+ " " + javaAbsolutePath); + ClassLoader classLoader = ReflectionUtils.class.getClassLoader(); +// Class aClass = Class.forName(fileName); + Class aClass = classLoader.loadClass(name); + Object o = aClass.newInstance(); + Method test = aClass.getMethod("test",null); + test.invoke(o,null); + try { + InputStream errorStream = process.getErrorStream(); + InputStreamReader inputStreamReader = new InputStreamReader(errorStream); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + String line = null; + while ((line=bufferedReader.readLine()) != null){ + System.out.println(line); + } + int exitVal = process.waitFor(); + System.out.println("Process exitValue: " + exitVal); + } catch (Exception e) { + e.printStackTrace(); + } return false; } /** * 引擎流程并执行测试 + * * @param id * @return */ @Override public boolean init(Long id) throws ServletException { RuleEngine ruleEngine = ruleEngineService.getById(id); - if (ruleEngine.getIsActivate().contains("no"))throw new ServletException("未激活"); - if (ruleEngine.getStatus().contains("1"))throw new ServletException("已停用"); + if (ruleEngine.getIsActivate().contains("no")) throw new ServletException("未激活"); + if (ruleEngine.getStatus().contains("1")) throw new ServletException("已停用"); Supplier booleanSupplier = () -> { try { return testEngine(ruleEngine); - } catch (ServletException e) { - log.error(e.getMessage()); + } catch (Exception e) { throw new RuntimeException(e); } }; @@ -156,10 +188,32 @@ public class ScopeServiceImpl extends ServiceImpl implements return booleanSupplier.get(); } - private Boolean testEngine(RuleEngine ruleEngine) throws ServletException { + private Boolean testEngine(RuleEngine ruleEngine) throws ServletException, IOException { System.out.println("初始化"); System.out.println(ruleEngine); - if(false)throw new ServletException("读取异常"); + Scope scope = this.getOne(new LambdaQueryWrapper() {{ + eq(Scope::getRuleEngineId, ruleEngine.getId()); + eq(Scope::getValue, "taskContext"); + }}); + String code = scope.getCode(); + String path = code.substring(code.indexOf("com"), code.indexOf(";")).replaceAll("\\.", "/").trim(); + String fileName = code.substring(code.indexOf("class") + 6, code.indexOf("{")).trim(); + File file = new File("D:/ruoyi/FinallyTest/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/" + path + "/" + fileName + ".java"); + if (!file.exists()) { + file.createNewFile(); + } + String[] split = code.split("/n"); + //OutputStreamWriter对象将字符转换为字节流,指定了文件输出流和编码方式。 + OutputStreamWriter outputStreamWriter = + new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"); + //BufferedWriter对象,用于缓冲写入数据,提高写入效率。 + BufferedWriter bw = new BufferedWriter(outputStreamWriter); + for (String str : split) { + bw.write(str); + bw.newLine(); + } + bw.close(); + outputStreamWriter.close(); return true; } }