feat(): 引擎维护,规则维护,初始化,测试连接
parent
0f4b839193
commit
4b62c4c7ab
|
@ -73,4 +73,10 @@ public class EngineRule extends BaseEntity {
|
|||
*/
|
||||
@Excel(name = "描述")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 编辑代码文本
|
||||
*/
|
||||
@Excel(name = "编辑代码文本")
|
||||
private String codeText;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package com.etl.data.rule.classLoading;
|
||||
|
||||
/**
|
||||
* 类加载器
|
||||
*
|
||||
* @author Chao
|
||||
* @ClassName: CustomClassLoader 类加载器
|
||||
* @CreateTime: 2024/5/4 下午2:10
|
||||
*/
|
||||
public class CustomClassLoader extends ClassLoader {
|
||||
public Class<?> defineClassFromBytes(String name, byte[] data) {
|
||||
return defineClass(name, data, 0, data.length);
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ import java.util.List;
|
|||
@RestController
|
||||
@RequestMapping("/engine")
|
||||
public class EngineRuleController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private IEngineRuleService engineRuleService;
|
||||
|
||||
|
@ -88,4 +89,24 @@ public class EngineRuleController extends BaseController {
|
|||
public Result remove(@PathVariable Long[] ids) {
|
||||
return toAjax(engineRuleService.deleteEngineRuleByIds(ids));
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化规则引擎类
|
||||
* @param engineRule
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("initializeRuleEngine")
|
||||
public Result initializeRuleEngine(@RequestBody EngineRule engineRule) {
|
||||
return engineRuleService.initializeRuleMaintenance(engineRule);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试方法
|
||||
* @param encoding
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/testMethod/{encoding}")
|
||||
public Result testMethod(@PathVariable("encoding") String encoding) {
|
||||
return engineRuleService.testMethod(encoding);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.etl.data.rule.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.etl.common.core.domain.Result;
|
||||
import com.etl.data.rule.domain.EngineRule;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -59,4 +60,8 @@ public interface IEngineRuleService extends IService<EngineRule> {
|
|||
* @return 结果
|
||||
*/
|
||||
public int deleteEngineRuleById(Long id);
|
||||
|
||||
Result initializeRuleMaintenance(EngineRule engineRule);
|
||||
|
||||
Result testMethod(String code);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.etl.data.rule.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.etl.common.core.domain.Result;
|
||||
import com.etl.common.core.utils.DateUtils;
|
||||
import com.etl.data.rule.domain.EngineRule;
|
||||
import com.etl.data.rule.mapper.EngineRuleMapper;
|
||||
|
@ -8,6 +9,17 @@ import com.etl.data.rule.service.IEngineRuleService;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.StandardJavaFileManager;
|
||||
import javax.tools.ToolProvider;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -52,6 +64,10 @@ public class EngineRuleServiceImpl extends ServiceImpl<EngineRuleMapper, EngineR
|
|||
@Override
|
||||
public int insertEngineRule(EngineRule engineRule) {
|
||||
engineRule.setCreateTime(DateUtils.getNowDate());
|
||||
String className = "Rule" + Character.toUpperCase(engineRule.getEncoding().charAt(0)) + engineRule.getEncoding().substring(1) + "Class";
|
||||
engineRule.setCodeText("package com.etl.data.rule.domain;\n\n\n" +
|
||||
"public class " + className + "{\n" +
|
||||
"}");
|
||||
return engineRuleMapper.insertEngineRule(engineRule);
|
||||
}
|
||||
|
||||
|
@ -88,4 +104,108 @@ public class EngineRuleServiceImpl extends ServiceImpl<EngineRuleMapper, EngineR
|
|||
public int deleteEngineRuleById(Long id) {
|
||||
return engineRuleMapper.deleteEngineRuleById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result initializeRuleMaintenance(EngineRule engineRule) {
|
||||
|
||||
try {
|
||||
String className = "Rule" + Character.toUpperCase(engineRule.getEncoding().charAt(0)) + engineRule.getEncoding().substring(1) + "Class";
|
||||
String javaPath = "D:\\Java\\etl\\dataProcess\\ETL-Cloud\\etl-modules\\etl-modules-data-source\\etl-modules-data-source-common\\src\\main\\java\\com\\etl\\data\\rule\\domain\\";
|
||||
String classPath = "D:\\Java\\etl\\dataProcess\\ETL-Cloud\\etl-modules\\etl-modules-data-source\\etl-modules-data-source-common\\target\\classes";
|
||||
String fileName = javaPath + className + ".java";
|
||||
File file = new File(fileName);
|
||||
File parentFile = file.getParentFile();
|
||||
if (!parentFile.exists()) {
|
||||
parentFile.mkdirs();
|
||||
}
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
file.createNewFile();
|
||||
|
||||
FileWriter fileWriter = new FileWriter(file);
|
||||
fileWriter.write(engineRule.getCodeText());
|
||||
fileWriter.flush();
|
||||
fileWriter.close();
|
||||
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
|
||||
StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, null, null);
|
||||
Iterable fileObjects = fileManager.getJavaFileObjects(fileName);
|
||||
List<String> options = Arrays.asList("-d", classPath);
|
||||
JavaCompiler.CompilationTask cTask = javaCompiler.getTask(null, fileManager, null, options, null, fileObjects);
|
||||
Boolean call = cTask.call();
|
||||
fileManager.close();
|
||||
if (call) {
|
||||
return Result.success(null, "初始化成功");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage());
|
||||
Result.error("初始化失败");
|
||||
}
|
||||
return Result.error(null, "初始化失败");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result testMethod(String encoding) {
|
||||
|
||||
|
||||
String className = "Rule" + Character.toUpperCase(encoding.charAt(0)) + encoding.substring(1) + "Class";
|
||||
String classPath = "etl-modules/etl-modules-data-source/etl-modules-data-source-common/target/classes/com/etl/data/rule/domain";
|
||||
// String classPath = "D:\\Java\\etl\\dataProcess\\ETL-Cloud\\etl-modules\\etl-modules-data-source\\etl-modules-data-source-common\\target\\classes\\" + className.replace(".", "\\") + ".class";
|
||||
try {
|
||||
|
||||
|
||||
// 将路径转换为URL
|
||||
URL classUrl = Paths.get(classPath).toUri().toURL();
|
||||
|
||||
// 创建一个只包含我们自定义路径的URLClassLoader
|
||||
URLClassLoader classLoader = new URLClassLoader(new URL[]{classUrl});
|
||||
// 使用自定义的ClassLoader加载类
|
||||
Class<?> clazz = Class.forName("com.etl.data.rule.domain." + className, true, classLoader); // 替换YourClassName为你的类名
|
||||
|
||||
Object testInstance = clazz.newInstance(); // 或者使用getDeclaredConstructor().newInstance();
|
||||
|
||||
// byte[] classData = Files.readAllBytes(Paths.get(classPath));
|
||||
// CustomClassLoader customClassLoader = new CustomClassLoader();
|
||||
// Class<?> aClass = customClassLoader.defineClassFromBytes(className, classData);
|
||||
// Object o = aClass.newInstance();
|
||||
// Method[] methods = clazz.getMethods();
|
||||
Method method = clazz.getMethod("main", String[].class);
|
||||
method.invoke(null, new Object[]{null});
|
||||
// 遍历所有方法,寻找第一个无参方法并执行
|
||||
// for (Method method : methods) {
|
||||
// if (method.getParameterTypes().length == 0) { // 检查方法是否有参数
|
||||
// System.out.println("调用无参方法: " + method.getName());
|
||||
// method.invoke(testInstance); // 调用找到的无参方法
|
||||
// break; // 如果只需要调用第一个无参方法,则可以跳出循环
|
||||
// }
|
||||
// }
|
||||
// customClassLoader = null;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
return Result.error("测试失败");
|
||||
}
|
||||
return Result.success(null, "测试成功");
|
||||
}
|
||||
|
||||
public String getTypeCodeText(EngineRule engineRule) {
|
||||
String className = Character.toUpperCase(engineRule.getEncoding().charAt(0)) + engineRule.getEncoding().substring(1);
|
||||
switch (engineRule.getType()) {
|
||||
case "data-set":
|
||||
className = className + "DataSetContext";
|
||||
return "package com.etl.data.rule.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 "数据字段";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<result property="status" column="status" />
|
||||
<result property="description" column="description" />
|
||||
<result property="remark" column="remark" />
|
||||
<result property="codeText" column="code_text" />
|
||||
<result property="createBy" column="create_by" />
|
||||
<result property="createTime" column="create_time" />
|
||||
<result property="updateBy" column="update_by" />
|
||||
|
@ -21,7 +22,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
</resultMap>
|
||||
|
||||
<sql id="selectEngineRuleVo">
|
||||
select id, name, type, scope, encoding, activated_or_not, status, description, remark, create_by, create_time, update_by, update_time from engine_rule
|
||||
select id, name, type, scope, encoding, activated_or_not, status, description, remark, code_text, create_by, create_time, update_by, update_time from engine_rule
|
||||
</sql>
|
||||
|
||||
<select id="selectEngineRuleList" parameterType="com.etl.data.rule.domain.EngineRule" resultMap="EngineRuleResult">
|
||||
|
@ -50,6 +51,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<if test="activatedOrNot != null">activated_or_not,</if>
|
||||
<if test="status != null">status,</if>
|
||||
<if test="description != null">description,</if>
|
||||
<if test="codeText != null">code_text,</if>
|
||||
<if test="remark != null">remark,</if>
|
||||
<if test="createBy != null">create_by,</if>
|
||||
<if test="createTime != null">create_time,</if>
|
||||
|
@ -64,6 +66,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<if test="activatedOrNot != null">#{activatedOrNot},</if>
|
||||
<if test="status != null">#{status},</if>
|
||||
<if test="description != null">#{description},</if>
|
||||
<if test="codeText != null">#{codeText},</if>
|
||||
<if test="remark != null">#{remark},</if>
|
||||
<if test="createBy != null">#{createBy},</if>
|
||||
<if test="createTime != null">#{createTime},</if>
|
||||
|
@ -82,6 +85,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<if test="activatedOrNot != null">activated_or_not = #{activatedOrNot},</if>
|
||||
<if test="status != null">status = #{status},</if>
|
||||
<if test="description != null">description = #{description},</if>
|
||||
<if test="codeText != null">code_text = #{codeText},</if>
|
||||
<if test="remark != null">remark = #{remark},</if>
|
||||
<if test="createBy != null">create_by = #{createBy},</if>
|
||||
<if test="createTime != null">create_time = #{createTime},</if>
|
||||
|
|
Loading…
Reference in New Issue