feat():引擎版本,资产接入连接,引擎版本初始化

master
Saisai Liu 2024-05-08 22:42:49 +08:00
parent 86dd8a7060
commit 8750b7d1cb
8 changed files with 150 additions and 101 deletions

View File

@ -89,6 +89,11 @@
<groupId>com.muyu</groupId>
<artifactId>muyu-common-swagger</artifactId>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.1.7</version>
</dependency>
</dependencies>

View File

@ -100,4 +100,29 @@ public class RuleEngineVersionController extends BaseController
{
return toAjax(ruleEngineVersionService.deleteRuleEngineVersionByIds(ids));
}
/**
*
*/
@RequiresPermissions("engine:version:update")
@Log(title = "规则版本", businessType = BusinessType.DELETE)
@PostMapping("/saveCoding")
public Result saveCoding(@RequestBody RuleEngineVersion ruleEngineVersion)
{
return toAjax(ruleEngineVersionService.saveCoding(ruleEngineVersion));
}
/**
*
*/
@RequiresPermissions("engine:version:update")
@Log(title = "规则版本", businessType = BusinessType.DELETE)
@PostMapping("/testEngine")
public Result testEngine(@RequestBody String codeIng) throws Exception {
return toAjax(ruleEngineVersionService.testEngine(codeIng));
}
}

View File

@ -60,4 +60,18 @@ public interface RuleEngineVersionService extends IService<RuleEngineVersion>
* @return
*/
public int deleteRuleEngineVersionById(Long id);
/**
*
* @param ruleEngineVersion
* @return boolean
*/
boolean saveCoding(RuleEngineVersion ruleEngineVersion);
/**
*
* @param codeIng
* @return
*/
public boolean testEngine(String codeIng) throws Exception;
}

View File

@ -1,14 +1,18 @@
package com.muyu.engine.service.impl;
import java.io.*;
import java.lang.reflect.Method;
import java.util.List;
import com.alibaba.nacos.shaded.com.google.common.base.Supplier;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.engine.domain.RuleEngine;
import com.muyu.engine.service.RuleEngineService;
import io.lettuce.core.dynamic.support.ReflectionUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -26,8 +30,7 @@ import javax.servlet.ServletException;
*/
@Service
@Log4j2
public class RuleEngineVersionServiceImpl extends ServiceImpl<RuleEngineVersionMapper,RuleEngineVersion> implements RuleEngineVersionService
{
public class RuleEngineVersionServiceImpl extends ServiceImpl<RuleEngineVersionMapper, RuleEngineVersion> implements RuleEngineVersionService {
@Autowired
private RuleEngineVersionMapper ruleEngineVersionMapper;
@Autowired
@ -40,8 +43,7 @@ public class RuleEngineVersionServiceImpl extends ServiceImpl<RuleEngineVersionM
* @return
*/
@Override
public RuleEngineVersion selectRuleEngineVersionById(Long id)
{
public RuleEngineVersion selectRuleEngineVersionById(Long id) {
return ruleEngineVersionMapper.selectRuleEngineVersionById(id);
}
@ -52,8 +54,7 @@ public class RuleEngineVersionServiceImpl extends ServiceImpl<RuleEngineVersionM
* @return
*/
@Override
public List<RuleEngineVersion> selectRuleEngineVersionList(Long ruleEngineId)
{
public List<RuleEngineVersion> selectRuleEngineVersionList(Long ruleEngineId) {
return list(new LambdaQueryWrapper<RuleEngineVersion>() {{
eq(RuleEngineVersion::getRuleEngineId, ruleEngineId);
}});
@ -66,8 +67,7 @@ public class RuleEngineVersionServiceImpl extends ServiceImpl<RuleEngineVersionM
* @return
*/
@Override
public int insertRuleEngineVersion(RuleEngineVersion ruleEngineVersion)
{
public int insertRuleEngineVersion(RuleEngineVersion ruleEngineVersion) {
ruleEngineVersion.setCreateTime(DateUtils.getNowDate());
return ruleEngineVersionMapper.insertRuleEngineVersion(ruleEngineVersion);
}
@ -79,8 +79,7 @@ public class RuleEngineVersionServiceImpl extends ServiceImpl<RuleEngineVersionM
* @return
*/
@Override
public int updateRuleEngineVersion(RuleEngineVersion ruleEngineVersion)
{
public int updateRuleEngineVersion(RuleEngineVersion ruleEngineVersion) {
ruleEngineVersion.setUpdateTime(DateUtils.getNowDate());
return ruleEngineVersionMapper.updateRuleEngineVersion(ruleEngineVersion);
}
@ -92,8 +91,7 @@ public class RuleEngineVersionServiceImpl extends ServiceImpl<RuleEngineVersionM
* @return
*/
@Override
public int deleteRuleEngineVersionByIds(Long[] ids)
{
public int deleteRuleEngineVersionByIds(Long[] ids) {
return ruleEngineVersionMapper.deleteRuleEngineVersionByIds(ids);
}
@ -104,77 +102,28 @@ public class RuleEngineVersionServiceImpl extends ServiceImpl<RuleEngineVersionM
* @return
*/
@Override
public int deleteRuleEngineVersionById(Long id)
{
public int deleteRuleEngineVersionById(Long id) {
return ruleEngineVersionMapper.deleteRuleEngineVersionById(id);
}
// @Override
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<Scope>() {{
// 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);
// try {
// int exitVal = process.waitFor();
// System.out.println("Process exitValue: " + exitVal);
// ClassLoader classLoader = ReflectionUtils.class.getClassLoader();
// Class aClass = classLoader.loadClass(name);
// Object o = aClass.newInstance();
// Method[] methods = aClass.getMethods();
// for (Method method : methods) {
// method.invoke(o,null);
// }
// } catch (Exception e) {
// log.error(e.getMessage());
// }
return true;
}
/**
*
*
* @param id
* @param ruleEngineVersion
* @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("已停用");
Supplier<Boolean> booleanSupplier = () -> {
@Override
public boolean saveCoding(RuleEngineVersion ruleEngineVersion) {
String codeIng = ruleEngineVersion.getCodeIng();
try {
return testEngine(ruleEngine);
} catch (Exception e) {
throw new RuntimeException(e);
String path = codeIng.substring(codeIng.indexOf("com"), codeIng.indexOf(";")).replaceAll("\\.", "/").trim();
String fileName = codeIng.substring(codeIng.indexOf("class") + 6, codeIng.indexOf("{")).trim();
//去除父级
if (fileName.contains("extends")) {
fileName = fileName.substring(0, fileName.indexOf("extends")).trim();
}
};
return booleanSupplier.get();
//去除实现接口
if (fileName.contains("implements")) {
fileName = fileName.substring(0, fileName.indexOf("implements")).trim();
}
private Boolean testEngine(RuleEngine ruleEngine) throws ServletException, IOException {
System.out.println("初始化");
System.out.println(ruleEngine);
RuleEngineVersion ruleEngineVersion = this.getOne(new LambdaQueryWrapper<RuleEngineVersion>() {{
eq(RuleEngineVersion::getRuleEngineId, ruleEngine.getId());
}});
String code = ruleEngineVersion.getCodeIng();
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()) {
@ -185,7 +134,7 @@ public class RuleEngineVersionServiceImpl extends ServiceImpl<RuleEngineVersionM
file.delete();
file.createNewFile();
}
String[] split = code.split("/n");
String[] split = codeIng.split("/n");
//OutputStreamWriter对象将字符转换为字节流指定了文件输出流和编码方式。
OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8");
@ -197,6 +146,49 @@ public class RuleEngineVersionServiceImpl extends ServiceImpl<RuleEngineVersionM
}
bw.close();
outputStreamWriter.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
log.info("初始化成功");
ruleEngineVersion.setStatus("1");
boolean update = this.update(ruleEngineVersion, new LambdaUpdateWrapper<RuleEngineVersion>() {{
eq(RuleEngineVersion::getId, ruleEngineVersion.getId());
}});
return update;
}
/**
*
*
* @param
* @return
* @throws Exception
*/
@Override
public boolean testEngine(String codeIng) throws Exception {
String path = codeIng.substring(codeIng.indexOf("com"), codeIng.indexOf(";")).replaceAll("/.", "/").trim();
String fileName = codeIng.substring(codeIng.indexOf("class") + 6, codeIng.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/domain/test";
Process process = Runtime.getRuntime().exec("javac -classpath " + jarAbsolutePath + " " + javaAbsolutePath);
try {
int exitVal = process.waitFor();
System.out.println("Process exitValue: " + exitVal);
ClassLoader classLoader = ReflectionUtils.class.getClassLoader();
Class aClass = classLoader.loadClass(name);
Object o = aClass.newInstance();
Method[] methods = aClass.getMethods();
for (Method method : methods) {
method.invoke(o, null);
}
} catch (Exception e) {
log.error(e.getMessage());
}
return true;
}
}

View File

@ -52,6 +52,10 @@ public class BasicConfigInfo extends BaseEntity
@Excel(name = "数据接入类型")
private String databaseType;
/** 是否已测试 */
@Excel(name = "数据接入类型")
private String isTest;
/** 数据库名称 */
@Excel(name = "数据库名称")
private String databaseName;

View File

@ -136,6 +136,10 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMappe
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
basicConfigInfo.setIsTest("1");
this.update(basicConfigInfo, new LambdaUpdateWrapper<BasicConfigInfo>() {{
eq(BasicConfigInfo::getId, basicConfigInfo.getId());
}});
System.out.println("Connected to the MySQL server successfully.");
//同步数据库信息
//树级结构,库,表

View File

@ -10,6 +10,7 @@
<result property="dataSourcesSystemName" column="data_sources_system_name" />
<result property="host" column="host" />
<result property="port" column="port" />
<result property="isTest" column="is_test" />
<result property="databaseType" column="database_type" />
<result property="databaseName" column="database_name" />
<result property="initLinkNum" column="init_link_num" />
@ -21,7 +22,7 @@
</resultMap>
<sql id="selectBasicConfigInfoVo">
select id, data_resource_name,username,password, data_sources_system_name, host, port, database_type, database_name, init_link_num, max_link_num, max_wait_time, max_wait_times,connection_params, remark from basic_config_info
select id, data_resource_name,username,password, data_sources_system_name, host, port,is_test, database_type, database_name, init_link_num, max_link_num, max_wait_time, max_wait_times,connection_params, remark from basic_config_info
</sql>
<select id="selectBasicConfigInfoList" parameterType="com.muyu.etl.domain.BasicConfigInfo" resultMap="BasicConfigInfoResult">
@ -31,6 +32,7 @@
<if test="dataSourcesSystemName != null and dataSourcesSystemName != ''"> and data_sources_system_name like concat('%', #{dataSourcesSystemName}, '%')</if>
<if test="host != null and host != ''"> and host = #{host}</if>
<if test="port != null and port != ''"> and port = #{port}</if>
<if test="isTest != null and isTest != ''"> and is_test = #{isTest}</if>
<if test="databaseType != null and databaseType != ''"> and database_type = #{databaseType}</if>
<if test="databaseName != null and databaseName != ''"> and database_name like concat('%', #{databaseName}, '%')</if>
<if test="username != null "> and username = #{username}</if>
@ -55,6 +57,7 @@
<if test="dataSourcesSystemName != null">data_sources_system_name,</if>
<if test="host != null">host,</if>
<if test="port != null">port,</if>
<if test="isTest != null">is_test,</if>
<if test="databaseType != null">database_type,</if>
<if test="databaseName != null">database_name,</if>
<if test="username != null">username,</if>
@ -72,6 +75,7 @@
<if test="dataSourcesSystemName != null">#{dataSourcesSystemName},</if>
<if test="host != null">#{host},</if>
<if test="port != null">#{port},</if>
<if test="isTest != null">#{is_test},</if>
<if test="databaseType != null">#{databaseType},</if>
<if test="databaseName != null">#{databaseName},</if>
<if test="username != null">#{username},</if>
@ -92,6 +96,7 @@
<if test="dataSourcesSystemName != null">data_sources_system_name = #{dataSourcesSystemName},</if>
<if test="host != null">host = #{host},</if>
<if test="port != null">port = #{port},</if>
<if test="isTest != null">is_test = #{isTest},</if>
<if test="databaseType != null">database_type = #{databaseType},</if>
<if test="databaseName != null">database_name = #{databaseName},</if>
<if test="username != null">username = #{username},</if>

View File

@ -43,7 +43,7 @@
<select id="selectTableInfoByName" parameterType="com.muyu.etl.domain.TableInfo" resultMap="TableInfoResult">
select * from table_info
where basic_id = #{basicId} and (table_name = #{tableName} or parent_id = #{parentId})
where basic_id = #{basicId} and table_name = #{tableName}
</select>