11111
parent
2ac0a1bdd8
commit
003440340d
|
@ -118,6 +118,14 @@ public static final String importClassAndPackPath="home/lib/";
|
|||
*/
|
||||
public static final String className_prefix = "com.muyu.rule.common.engine.value.";
|
||||
|
||||
/**
|
||||
* 服务器存放引擎的class文件的位置
|
||||
* @param className 类名
|
||||
* @param versionClazz 源代码
|
||||
*/
|
||||
|
||||
public static final String serverClassPath = "com/muyu/rule/common/engine/value/";
|
||||
|
||||
|
||||
//todo
|
||||
@Override
|
||||
|
@ -169,39 +177,46 @@ public static final String importClassAndPackPath="home/lib/";
|
|||
*/
|
||||
@Override
|
||||
public void loadEngineClass(String className) {
|
||||
try {
|
||||
// 假设这是你的外部类文件路径
|
||||
String externalClassFilePath =
|
||||
importClassAndPackPath + "com/muyu/rule/common/engine/value/" + 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;
|
||||
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()};
|
||||
|
||||
clazz = externalClassLoader.loadClassFromPath(classFilePath, className_prefix + className);
|
||||
//创建自定义类加载器
|
||||
ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
|
||||
|
||||
//加载类
|
||||
//注意类名必须是完全限定名(包括包名)
|
||||
Class<?> clazz = null;
|
||||
|
||||
clazz = externalClassLoader.loadClassFromPath(classFilePath, className_prefix + className);
|
||||
|
||||
|
||||
//创建类的实例
|
||||
Object instance = clazz.getDeclaredConstructor().newInstance();
|
||||
//创建类的实例
|
||||
Object instance = clazz.getDeclaredConstructor().newInstance();
|
||||
//将加载出来引擎实例存入map集合中
|
||||
engineMap.put(className, (BasicEngine<DataValue>) instance);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
engineMap.put(className, (BasicEngine<DataValue>) instance);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// log.info("测试引擎是否成功类加载");
|
||||
// DataValue dataValue = DataValue.builder()
|
||||
// .type("String")
|
||||
|
@ -215,6 +230,7 @@ public static final String importClassAndPackPath="home/lib/";
|
|||
// engine.execution();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue