174 lines
6.2 KiB
Java
174 lines
6.2 KiB
Java
package com.muyu.service.serviceImpl;
|
||
|
||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||
import com.muyu.common.core.domain.Result;
|
||
import com.muyu.common.core.exception.ServiceException;
|
||
import com.muyu.common.core.utils.StringUtils;
|
||
import com.muyu.domain.EngineMaintenance;
|
||
import com.muyu.domain.EngineVersion;
|
||
import com.muyu.domain.constants.ConfigCodeConstants;
|
||
import com.muyu.domain.constants.RuleOperationConstants;
|
||
import com.muyu.domain.model.TestData;
|
||
import com.muyu.dynamicLoad.DynamicLoader;
|
||
import com.muyu.mapper.EngineVersionMapper;
|
||
import com.muyu.req.VersionClassCreateReq;
|
||
import com.muyu.resp.EngineConfigScopeResp;
|
||
import com.muyu.resp.VersionClassCreateResp;
|
||
import com.muyu.service.EngineVersionService;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Service;
|
||
|
||
import java.io.IOException;
|
||
import java.lang.reflect.Method;
|
||
import java.nio.file.Files;
|
||
import java.nio.file.Paths;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* @Author:qdm
|
||
* @Package:com.muyu.service.serviceImpl
|
||
* @Project:cloud-etl-engine
|
||
* @name:EngineVersionServiceImpl
|
||
* @Date:2024/8/22 22:17
|
||
*/
|
||
@Service
|
||
public class EngineVersionServiceImpl extends ServiceImpl<EngineVersionMapper, EngineVersion> implements EngineVersionService {
|
||
|
||
@Autowired
|
||
EngineVersionMapper engineVersionMapper;
|
||
|
||
@Override
|
||
public List<EngineVersion> list(EngineVersion engineConfig) {
|
||
LambdaQueryWrapper<EngineVersion> queryWrapper = new LambdaQueryWrapper<>();
|
||
|
||
if (StringUtils.isNotNull(engineConfig.getVersionCode())) {
|
||
queryWrapper.eq(EngineVersion::getVersionCode, engineConfig.getVersionCode());
|
||
}
|
||
|
||
if (StringUtils.isNotNull(engineConfig.getRuleContent())) {
|
||
queryWrapper.eq(EngineVersion::getRuleContent, engineConfig.getRuleContent());
|
||
}
|
||
|
||
if (StringUtils.isNotNull(engineConfig.getEngineMaintenanceId())) {
|
||
queryWrapper.eq(EngineVersion::getEngineMaintenanceId, engineConfig.getEngineMaintenanceId());
|
||
}
|
||
|
||
return list(queryWrapper);
|
||
}
|
||
|
||
|
||
@Override
|
||
public EngineVersion updateMsg(EngineVersion engineVersion) {
|
||
boolean b = updateById(engineVersion);
|
||
if (b) {
|
||
return null;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
@Override
|
||
public Result<EngineVersion> updateVersion(EngineVersion engineVersion) {
|
||
return null;
|
||
}
|
||
|
||
@Override
|
||
public List<EngineConfigScopeResp> getScopeList() {
|
||
List<EngineConfigScopeResp> list = new ArrayList<>();
|
||
try {
|
||
int i = 0;
|
||
for (String scope : ConfigCodeConstants.CONFIG_FILE_NAME_ARRAY) {
|
||
if (i == 0) {
|
||
i++;
|
||
continue;
|
||
}
|
||
String path = ConfigCodeConstants.BASE_FILE_PATH + scope;
|
||
String code = Files.readString(Paths.get(path));
|
||
String type = ConfigCodeConstants.CONFIG_FILE_TYPE_ARRAY[i++];
|
||
list.add(EngineConfigScopeResp.builder()
|
||
.type(type)
|
||
.name(scope)
|
||
.code(code)
|
||
.build());
|
||
}
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
return list;
|
||
}
|
||
|
||
@Override
|
||
public EngineConfigScopeResp getScopeInfoById(Integer id) {
|
||
String scope = ConfigCodeConstants.CONFIG_FILE_NAME_ARRAY[id];
|
||
String type = ConfigCodeConstants.CONFIG_FILE_TYPE_ARRAY[id];
|
||
String path = ConfigCodeConstants.BASE_FILE_PATH + scope;
|
||
String code = null;
|
||
try {
|
||
code = Files.readString(Paths.get(path));
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
return EngineConfigScopeResp.builder()
|
||
.type(type)
|
||
.name(scope)
|
||
.code(code)
|
||
.build();
|
||
}
|
||
|
||
@Override
|
||
public Object ruleTest(TestData testData) {
|
||
Object result = null;
|
||
try {
|
||
EngineVersion engineVersion = this.getById(testData.getId());
|
||
String content = engineVersion.getRuleContent().replaceAll("\r\n", "");
|
||
// 对source进行编译生成class文件存放在Map中,这里用bytecode接收
|
||
Map<String, byte[]> bytecode = DynamicLoader.compile(RuleOperationConstants.CLASS_NAME +
|
||
RuleOperationConstants.FILE_SUFFIX, content);
|
||
|
||
// 加载class文件到虚拟机中,然后通过反射执行
|
||
@SuppressWarnings("resource")
|
||
DynamicLoader.MemoryClassLoader classLoader = new DynamicLoader.MemoryClassLoader(
|
||
bytecode);
|
||
Class<?> clazz = classLoader.loadClass(RuleOperationConstants.CLASS_NAME);
|
||
|
||
// 调用ruleTest方法
|
||
Method mainMethod = clazz.getDeclaredMethod("ruleTest", List.class);
|
||
result = mainMethod.invoke(null, testData.getList());
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
throw new ServiceException("测试失败");
|
||
}
|
||
return result;
|
||
}
|
||
|
||
@Override
|
||
public VersionClassCreateResp createVersionClass(VersionClassCreateReq req) {
|
||
String versionClass = "engine_custom" + "_" + req.getEngineCode() + "_" + req.getVersionCode();
|
||
EngineConfigScopeResp scopeResp = this.getScopeInfoById(0);
|
||
String code = scopeResp.getCode().replaceAll("name", versionClass)
|
||
.replaceAll("engineScope", EngineConfigScopeResp.ENGINE[req.getScope()])
|
||
.replaceAll("engineCode", req.getEngineCode());
|
||
return VersionClassCreateResp.builder()
|
||
.versionClass(versionClass)
|
||
.ruleContent(code)
|
||
.build();
|
||
}
|
||
|
||
@Override
|
||
public Result<EngineVersion> add(EngineVersion engineVersion) {
|
||
Integer res = engineVersionMapper.add(engineVersion);
|
||
if (res == 1) {
|
||
return Result.success(engineVersion);
|
||
}
|
||
return Result.error();
|
||
}
|
||
|
||
@Override
|
||
public List<EngineMaintenance> getByIds(Integer versionId) {
|
||
List<EngineMaintenance> list = engineVersionMapper.getByIds(versionId);
|
||
return list;
|
||
}
|
||
}
|