fix: 规则引擎完善
parent
b92a8cef22
commit
6b69b29567
|
@ -11,44 +11,8 @@ import java.net.URLClassLoader;
|
|||
* @Author Xin.Yao
|
||||
* @Date 2024/5/1 19:37
|
||||
*/
|
||||
public class CustomClassLoader extends URLClassLoader {
|
||||
public CustomClassLoader(ClassLoader parent, String classPath) throws MalformedURLException {
|
||||
super(new URL[]{new URL("file:" + classPath)},parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
try {
|
||||
byte[] classBytes = loadClassData(name);
|
||||
return defineClass(name, classBytes, 0, classBytes.length);
|
||||
} catch (IOException e) {
|
||||
throw new ClassNotFoundException(name, e);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] loadClassData(String className) throws IOException {
|
||||
String fileName = className.replace('.', File.separatorChar) + ".class";
|
||||
File classFile = new File(getPathForResource(fileName));
|
||||
if (!classFile.exists()) {
|
||||
throw new FileNotFoundException("Class file not found: " + classFile.getAbsolutePath());
|
||||
}
|
||||
|
||||
try (InputStream in = new FileInputStream(classFile)) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[4096];
|
||||
int n;
|
||||
while ((n = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, n);
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
private String getPathForResource(String resource) {
|
||||
URL url = super.getResource(resource);
|
||||
if (url == null) {
|
||||
throw new IllegalArgumentException("Resource not found: " + resource);
|
||||
}
|
||||
return url.getPath();
|
||||
public class CustomClassLoader extends ClassLoader {
|
||||
public Class<?> defineClassFromBytes(String name, byte[] data) {
|
||||
return defineClass(name, data, 0, data.length);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,5 +18,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
public class MuYuRuleEngineApplication {
|
||||
public static void main (String[] args) {
|
||||
SpringApplication.run(MuYuRuleEngineApplication.class, args);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,38 +17,48 @@ public class EngineMaintenance extends BaseEntity {
|
|||
/** $column.columnComment */
|
||||
private Long id;
|
||||
|
||||
|
||||
/** 规则引擎名称 */
|
||||
@Excel(name = "规则引擎名称")
|
||||
private String name;
|
||||
|
||||
|
||||
/** 规则引擎类型 */
|
||||
@Excel(name = "规则引擎类型")
|
||||
private String type;
|
||||
|
||||
|
||||
/** 规则引擎激活状态 */
|
||||
@Excel(name = "规则引擎激活状态")
|
||||
private String isActivate;
|
||||
|
||||
|
||||
/** 规则引擎状态 */
|
||||
@Excel(name = "规则引擎状态")
|
||||
private String status;
|
||||
|
||||
|
||||
/** 规则引擎描述 */
|
||||
@Excel(name = "规则引擎描述")
|
||||
private String description;
|
||||
|
||||
|
||||
|
||||
/** 规则引擎编码 */
|
||||
@Excel(name = "规则引擎编码")
|
||||
private String code;
|
||||
|
||||
|
||||
/** 规则引擎级别 */
|
||||
@Excel(name = "规则引擎级别")
|
||||
private String level;
|
||||
|
||||
|
||||
/** 编辑代码文本 */
|
||||
@Excel(name = "编辑代码文本")
|
||||
private String codeText;
|
||||
|
||||
|
||||
public String getCodeText() {
|
||||
return codeText;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package com.muyu.rule.engine.service.impl;
|
|||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -120,13 +122,14 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
|
|||
//创建源文件
|
||||
String className = "Rule"+Character.toUpperCase(engineMaintenance.getCode().charAt(0)) + engineMaintenance.getCode().substring(1)+"Class";
|
||||
// 源文件路径和名称
|
||||
String javaPath = "D:\\work\\ruoyi-cloud-server\\muyu-modules\\muyu-rule-engine\\src\\main\\java\\com\\muyu\\rule\\engine\\domain\\";
|
||||
String classPath = "D:\\work\\ruoyi-cloud-server\\muyu-modules\\muyu-rule-engine\\target\\classes\\";
|
||||
String javaPath = "D:\\idea\\ruoyi-could-server-muyu\\muyu-modules\\muyu-rule-engine\\src\\main\\java\\com\\muyu\\rule\\engine\\domain\\";
|
||||
String classPath = "D:\\idea\\ruoyi-could-server-muyu\\muyu-modules\\muyu-rule-engine\\target\\classes\\";
|
||||
String filename = javaPath+className+".java";
|
||||
File file = new File(filename);
|
||||
// 确保源文件所在的目录存在
|
||||
File fileParent = file.getParentFile();
|
||||
|
||||
|
||||
if (!fileParent.exists()) {
|
||||
fileParent.mkdir();
|
||||
}
|
||||
|
@ -167,13 +170,13 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
|
|||
|
||||
@Override
|
||||
public Result testMethod(String code) {
|
||||
String className = "Rule"+Character.toUpperCase(code.charAt(0)) + code.substring(1)+"Class";
|
||||
String classPath = "D:\\work\\ruoyi-cloud-server\\muyu-modules\\muyu-rule-engine\\target\\classes\\";
|
||||
String className = "com.muyu.rule.engine.domain.Rule"+Character.toUpperCase(code.charAt(0)) + code.substring(1)+"Class";
|
||||
String classPath = "D:\\idea\\ruoyi-could-server-muyu\\muyu-modules\\muyu-rule-engine\\target\\classes"+className.replace(".", "\\")+".class";
|
||||
try {
|
||||
// 使用CustomClassLoader加载class到内存
|
||||
CustomClassLoader customClassLoader = new CustomClassLoader(ClassLoader.getSystemClassLoader(), classPath);
|
||||
// 加载类
|
||||
Class<?> aClass = customClassLoader.loadClass("com.muyu.rule.engine.domain."+className);
|
||||
// 读取类文件
|
||||
byte[] classData = Files.readAllBytes(Paths.get(classPath));
|
||||
CustomClassLoader customClassLoader = new CustomClassLoader();
|
||||
Class<?> aClass = customClassLoader.defineClassFromBytes(className,classData);
|
||||
// 实例化类
|
||||
Object obj = aClass.newInstance();
|
||||
//获取类中所有方法
|
||||
|
@ -189,4 +192,29 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
|
|||
|
||||
return Result.success("测试成功");
|
||||
}
|
||||
|
||||
public String getTypeCodeText(EngineMaintenance engineMaintenance) {
|
||||
String className =Character.toUpperCase(engineMaintenance.getCode().charAt(0)) + engineMaintenance.getCode().substring(1);
|
||||
switch (engineMaintenance.getType()) {
|
||||
case "data-set":
|
||||
className = className+"DataSetContext";
|
||||
return "package com.muyu.rule.engine.domain;\n" +
|
||||
"\n" +
|
||||
"public class "+className+" {\n" +
|
||||
"\n" +
|
||||
" private final RecordContext recordContext;\n" +
|
||||
"\n" +
|
||||
" public DataSetContext (RecordContext recordContext) {\n" +
|
||||
" this.recordContext = recordContext;\n" +
|
||||
" }\n" +
|
||||
"}\n";
|
||||
case "data-record":
|
||||
return "记录";
|
||||
default:
|
||||
return "数据字段";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue