master
parent
a831863939
commit
b01e6bb3ad
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.rule.server.constant;
|
||||
package com.muyu.rule.common.constant;
|
||||
|
||||
/**
|
||||
* @Author:张承志
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.rule.server.load;
|
||||
package com.muyu.rule.common.load;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
|
@ -1,6 +1,7 @@
|
|||
package com.muyu.rule.server.load;
|
||||
package com.muyu.rule.common.load;
|
||||
|
||||
import com.muyu.rule.server.scan.JavaCodeScan;
|
||||
|
||||
import com.muyu.rule.common.scan.JavaCodeScan;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
|
@ -8,7 +8,5 @@ import java.util.Map;
|
|||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class EngineHashMap {
|
||||
public static Map<String, BasicEngine<DataValue>> engineMap = new ConcurrentHashMap<>();
|
||||
public static Map<String, BasicEngine<DataValue[]>> engineRowMap = new ConcurrentHashMap<>();
|
||||
public static Map<String, BasicEngine<DataValue[][]>> engineDataSetMap = new ConcurrentHashMap<>();
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.muyu.rule.server.scan;
|
||||
package com.muyu.rule.common.scan;
|
||||
|
||||
import com.muyu.rule.server.load.JavaBinaryClassLoader;
|
||||
import com.muyu.rule.common.load.JavaBinaryClassLoader;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
|
@ -0,0 +1,153 @@
|
|||
package com.muyu.rule.common.utils;
|
||||
|
||||
import com.muyu.common.domain.DataValue;
|
||||
import com.muyu.rule.common.basic.BasicEngine;
|
||||
import com.muyu.rule.common.load.ExternalClassLoader;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static com.muyu.rule.common.constant.SuffixClass.Suffix_CLASS;
|
||||
|
||||
|
||||
|
||||
|
||||
@Log4j2
|
||||
@Component
|
||||
public class LoadEngineClass {
|
||||
public static Map<String, BasicEngine<DataValue>> engineMap = new ConcurrentHashMap<>();
|
||||
public static Map<String, BasicEngine<DataValue[]>> engineRowMap = new ConcurrentHashMap<>();
|
||||
public static Map<String, BasicEngine<DataValue[][]>> engineDataSetMap = new ConcurrentHashMap<>();
|
||||
/**
|
||||
* 服务器的存放java源代码的位置
|
||||
*/
|
||||
public static final String engineWorkSourcePath = "/home/source/";
|
||||
|
||||
|
||||
/**
|
||||
* 引擎加载需要引入类和包文件的位置
|
||||
*/
|
||||
public static final String importClassAndPackPath = "home/lib/";
|
||||
|
||||
/**
|
||||
* 规则引擎的包的路径
|
||||
*/
|
||||
public static final String className_prefix = "com.muyu.rule.common.engine.";
|
||||
|
||||
/**
|
||||
* 服务器存放引擎的class文件的位置
|
||||
*
|
||||
* @param className 类名
|
||||
* @param versionClazz 源代码
|
||||
*/
|
||||
|
||||
public static final String serverClassPath = "com/muyu/rule/common/engine/";
|
||||
public void loadValueEngineClass(String className) {
|
||||
|
||||
//查询类是否已加载,避免重复加载规则引擎
|
||||
if (!engineMap.containsKey(className)) {
|
||||
log.info("引擎map集合下没有该实例的规则,进行自定义类加载");
|
||||
try {
|
||||
// 假设这是你的外部类文件路径
|
||||
String externalClassFilePath =
|
||||
importClassAndPackPath + serverClassPath + className +Suffix_CLASS;
|
||||
Path classFilePath = Paths.get(externalClassFilePath);
|
||||
String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf('/'));
|
||||
URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
|
||||
|
||||
//创建自定义类加载器
|
||||
ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
|
||||
//加载类
|
||||
//注意类名必须是完全限定名(包括包名)
|
||||
Class<?> clazz = null;
|
||||
clazz = externalClassLoader.loadClassFromPath(classFilePath, className_prefix + className);
|
||||
//创建类的实例
|
||||
Object instance = clazz.getDeclaredConstructor().newInstance();
|
||||
//将加载出来引擎实例存入map集合中
|
||||
|
||||
engineMap.put(className,(BasicEngine<DataValue>) instance);
|
||||
|
||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException |
|
||||
NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void loadRowEngineClass(String className) {
|
||||
|
||||
//查询类是否已加载,避免重复加载规则引擎
|
||||
if (!engineRowMap.containsKey(className)) {
|
||||
log.info("引擎map集合下没有该实例的规则,进行自定义类加载");
|
||||
try {
|
||||
// 假设这是你的外部类文件路径
|
||||
String externalClassFilePath =
|
||||
importClassAndPackPath + serverClassPath + className + Suffix_CLASS;
|
||||
Path classFilePath = Paths.get(externalClassFilePath);
|
||||
String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf('/'));
|
||||
URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
|
||||
|
||||
//创建自定义类加载器
|
||||
ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
|
||||
|
||||
//加载类
|
||||
//注意类名必须是完全限定名(包括包名)
|
||||
Class<?> clazz = null;
|
||||
|
||||
clazz = externalClassLoader.loadClassFromPath(classFilePath, className_prefix + className);
|
||||
//创建类的实例
|
||||
Object instance = clazz.getDeclaredConstructor().newInstance();
|
||||
//将加载出来引擎实例存入map集合中
|
||||
|
||||
engineRowMap.put(className,(BasicEngine<DataValue[]>) instance);
|
||||
|
||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException |
|
||||
NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void loadDataSetEngineClass(String className) {
|
||||
|
||||
//查询类是否已加载,避免重复加载规则引擎
|
||||
if (!engineDataSetMap.containsKey(className)) {
|
||||
log.info("引擎map集合下没有该实例的规则,进行自定义类加载");
|
||||
try {
|
||||
// 设置外部类文件路径
|
||||
String externalClassFilePath =
|
||||
importClassAndPackPath + serverClassPath + className + Suffix_CLASS;
|
||||
Path classFilePath = Paths.get(externalClassFilePath);
|
||||
String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf('/'));
|
||||
URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
|
||||
|
||||
//创建自定义类加载器
|
||||
ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
|
||||
|
||||
//加载类
|
||||
//注意类名必须是完全限定名(包括包名)
|
||||
Class<?> clazz = null;
|
||||
|
||||
clazz = externalClassLoader.loadClassFromPath(classFilePath, className_prefix + className);
|
||||
//创建类的实例
|
||||
Object instance = clazz.getDeclaredConstructor().newInstance();
|
||||
//将加载出来引擎实例存入map集合中
|
||||
|
||||
engineDataSetMap.put(className,(BasicEngine<DataValue[][]>) instance);
|
||||
|
||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException |
|
||||
NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
com.muyu.rule.common.utils.LoadEngineClass
|
|
@ -5,7 +5,7 @@ import com.muyu.common.enums.DataType;
|
|||
import com.muyu.rule.common.utils.OssUtil;
|
||||
import com.muyu.rule.common.basic.BasicEngine;
|
||||
import com.muyu.rule.server.complie.SourceCodeComplier;
|
||||
import com.muyu.rule.server.load.JavaBinaryClassLoader;
|
||||
import com.muyu.rule.common.load.JavaBinaryClassLoader;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Map;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package com.muyu.rule.server.complie;
|
||||
|
||||
import cn.hutool.core.util.DesensitizedUtil;
|
||||
import com.muyu.rule.server.scan.JavaCodeScan;
|
||||
import com.muyu.rule.common.scan.JavaCodeScan;
|
||||
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.JavaFileObject;
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.muyu.common.core.domain.Result;
|
|||
import com.muyu.common.domain.DataValue;
|
||||
import com.muyu.rule.common.domain.RuleEngineVersion;
|
||||
import com.muyu.rule.common.domain.req.VersionAddReq;
|
||||
import com.muyu.rule.common.utils.LoadEngineClass;
|
||||
import com.muyu.rule.server.service.RuleEngineVersionService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
@ -298,12 +299,17 @@ public Result<List<RuleEngineVersion>> selectVersionById(@PathVariable("id") Lon
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Autowired
|
||||
private LoadEngineClass loadEngineClass;
|
||||
|
||||
@PostMapping("/testEngine/{className}")
|
||||
@Operation(summary = "字段规则引擎测试", description = "传入规则引擎的类名,和数据,用规则引擎进行测试数据")
|
||||
public Result testEngine(@PathVariable("className") String className,@RequestBody DataValue dataValue){
|
||||
log.info("数据:" + dataValue);
|
||||
//加载对应的规则引擎
|
||||
versionService.loadValueEngineClass(className);
|
||||
loadEngineClass.loadValueEngineClass(className);
|
||||
//执行对应的规则引擎
|
||||
DataValue value = versionService.testEngine(className, dataValue);
|
||||
|
||||
|
@ -315,7 +321,7 @@ public Result<List<RuleEngineVersion>> selectVersionById(@PathVariable("id") Lon
|
|||
public Result testEngine(@PathVariable("className") String className,@RequestBody DataValue[] dataValue){
|
||||
log.info("数据:" + dataValue);
|
||||
//加载对应的规则引擎
|
||||
versionService.loadRowEngineClass(className);
|
||||
loadEngineClass.loadRowEngineClass(className);
|
||||
//执行对应的规则引擎
|
||||
DataValue[] value = versionService.testEngine(className, dataValue);
|
||||
|
||||
|
@ -325,7 +331,7 @@ public Result<List<RuleEngineVersion>> selectVersionById(@PathVariable("id") Lon
|
|||
@Operation(summary = "数据集的规则引擎测试", description = "传入规则引擎的类名,和数据,用规则引擎进行测试数据")
|
||||
public Result DataSetEngine(@PathVariable("className") String className,@RequestBody DataValue[][] dataValues){
|
||||
|
||||
versionService.loadDataSetEngineClass(className);
|
||||
loadEngineClass.loadDataSetEngineClass(className);
|
||||
|
||||
DataValue[][] testedEngine = versionService.testEngine(className, dataValues);
|
||||
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
package com.muyu.rule.server.execution;
|
||||
|
||||
import com.muyu.rule.common.domain.RuleEngine;
|
||||
import com.muyu.rule.common.domain.RuleRegion;
|
||||
import com.muyu.rule.server.load.JavaBinaryClassLoader;
|
||||
import com.muyu.rule.server.pool.container.EngineContainer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
|
|
@ -83,9 +83,4 @@ public interface RuleEngineVersionService extends IService<RuleEngineVersion> {
|
|||
|
||||
public DataValue[][] testEngine( String className, DataValue[][] dataValues);
|
||||
|
||||
void loadValueEngineClass(String className);
|
||||
|
||||
void loadRowEngineClass(String className);
|
||||
|
||||
void loadDataSetEngineClass(String className);
|
||||
}
|
||||
|
|
|
@ -6,10 +6,10 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.common.core.enums.SystemYesNo;
|
||||
import com.muyu.common.domain.DataValue;
|
||||
import com.muyu.rule.common.domain.RuleEngineVersion;
|
||||
import com.muyu.rule.common.utils.OssUtil;
|
||||
import com.muyu.rule.common.basic.BasicEngine;
|
||||
import com.muyu.rule.server.load.ExternalClassLoader;
|
||||
import com.muyu.rule.common.domain.RuleEngineVersion;
|
||||
import com.muyu.rule.common.load.ExternalClassLoader;
|
||||
import com.muyu.rule.common.utils.OssUtil;
|
||||
import com.muyu.rule.server.mapper.RuleEngineVersionMapper;
|
||||
import com.muyu.rule.server.service.RuleEngineVersionService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
@ -24,12 +24,11 @@ import java.nio.file.Path;
|
|||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static com.muyu.rule.common.map.EngineHashMap.*;
|
||||
import static com.muyu.rule.server.constant.SuffixClass.Suffix_CLASS;
|
||||
import static com.muyu.rule.server.constant.SuffixClass.Suffix_JAVA;
|
||||
import static com.muyu.rule.common.constant.SuffixClass.Suffix_CLASS;
|
||||
import static com.muyu.rule.common.constant.SuffixClass.Suffix_JAVA;
|
||||
import static com.muyu.rule.common.utils.LoadEngineClass.*;
|
||||
|
||||
|
||||
/**
|
||||
* @Author:张承志
|
||||
|
@ -240,111 +239,6 @@ public class RuleEngineVersionServiceImpl extends ServiceImpl<RuleEngineVersionM
|
|||
return dataValues1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadValueEngineClass(String className) {
|
||||
|
||||
//查询类是否已加载,避免重复加载规则引擎
|
||||
if (!engineMap.containsKey(className)) {
|
||||
log.info("引擎map集合下没有该实例的规则,进行自定义类加载");
|
||||
try {
|
||||
// 假设这是你的外部类文件路径
|
||||
String externalClassFilePath =
|
||||
importClassAndPackPath + serverClassPath + className + Suffix_CLASS;
|
||||
Path classFilePath = Paths.get(externalClassFilePath);
|
||||
String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf('/'));
|
||||
URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
|
||||
|
||||
//创建自定义类加载器
|
||||
ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
|
||||
//加载类
|
||||
//注意类名必须是完全限定名(包括包名)
|
||||
Class<?> clazz = null;
|
||||
clazz = externalClassLoader.loadClassFromPath(classFilePath, className_prefix + className);
|
||||
//创建类的实例
|
||||
Object instance = clazz.getDeclaredConstructor().newInstance();
|
||||
//将加载出来引擎实例存入map集合中
|
||||
|
||||
engineMap.put(className,(BasicEngine<DataValue>) instance);
|
||||
|
||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException |
|
||||
NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadRowEngineClass(String className) {
|
||||
|
||||
//查询类是否已加载,避免重复加载规则引擎
|
||||
if (!engineRowMap.containsKey(className)) {
|
||||
log.info("引擎map集合下没有该实例的规则,进行自定义类加载");
|
||||
try {
|
||||
// 假设这是你的外部类文件路径
|
||||
String externalClassFilePath =
|
||||
importClassAndPackPath + serverClassPath + className + Suffix_CLASS;
|
||||
Path classFilePath = Paths.get(externalClassFilePath);
|
||||
String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf('/'));
|
||||
URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
|
||||
|
||||
//创建自定义类加载器
|
||||
ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
|
||||
|
||||
//加载类
|
||||
//注意类名必须是完全限定名(包括包名)
|
||||
Class<?> clazz = null;
|
||||
|
||||
clazz = externalClassLoader.loadClassFromPath(classFilePath, className_prefix + className);
|
||||
//创建类的实例
|
||||
Object instance = clazz.getDeclaredConstructor().newInstance();
|
||||
//将加载出来引擎实例存入map集合中
|
||||
|
||||
engineRowMap.put(className,(BasicEngine<DataValue[]>) instance);
|
||||
|
||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException |
|
||||
NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadDataSetEngineClass(String className) {
|
||||
|
||||
//查询类是否已加载,避免重复加载规则引擎
|
||||
if (!engineDataSetMap.containsKey(className)) {
|
||||
log.info("引擎map集合下没有该实例的规则,进行自定义类加载");
|
||||
try {
|
||||
// 设置外部类文件路径
|
||||
String externalClassFilePath =
|
||||
importClassAndPackPath + serverClassPath + className + Suffix_CLASS;
|
||||
Path classFilePath = Paths.get(externalClassFilePath);
|
||||
String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf('/'));
|
||||
URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
|
||||
|
||||
//创建自定义类加载器
|
||||
ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
|
||||
|
||||
//加载类
|
||||
//注意类名必须是完全限定名(包括包名)
|
||||
Class<?> clazz = null;
|
||||
|
||||
clazz = externalClassLoader.loadClassFromPath(classFilePath, className_prefix + className);
|
||||
//创建类的实例
|
||||
Object instance = clazz.getDeclaredConstructor().newInstance();
|
||||
//将加载出来引擎实例存入map集合中
|
||||
|
||||
engineDataSetMap.put(className,(BasicEngine<DataValue[][]>) instance);
|
||||
|
||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException |
|
||||
NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void execution(String engineKey,DataValue dataValue){
|
||||
|
||||
BasicEngine<DataValue> dataValueBasicEngine = engineMap.get(engineKey);
|
||||
|
|
Loading…
Reference in New Issue