master
zhang chengzhi 2024-09-04 20:18:49 +08:00
parent 42c281bf26
commit 0d0147dcdd
1 changed files with 40 additions and 8 deletions

View File

@ -13,6 +13,7 @@ import com.muyu.rule.common.utils.OssUtil;
import com.muyu.rule.server.EngineConfig;
import com.muyu.rule.server.basic.BasicEngine;
import com.muyu.rule.server.complie.SourceCodeComplier;
import com.muyu.rule.server.load.ExternalClassLoader;
import com.muyu.rule.server.load.JavaBinaryClassLoader;
import com.muyu.rule.server.mapper.RuleEngineVersionMapper;
import com.muyu.rule.server.scan.JavaCodeScan;
@ -22,10 +23,10 @@ import org.springframework.stereotype.Service;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -35,6 +36,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static com.muyu.rule.server.constant.SuffixClass.Suffix_CLASS;
import static com.muyu.rule.server.constant.SuffixClass.Suffix_JAVA;
/**
@ -104,16 +106,24 @@ public class RuleEngineServiceImpl extends ServiceImpl<RuleEngineVersionMapper,
return string;
}
public static final String engineWorkSourcePath = "home/source/";
public static final String engineWorkClassPath = "home/class/";
//
//
public static final String className_prefix = "com.muyu.rule.server.basic.engine.value.";
@Override
public void HotLoadClass(String className, String versionClazz) {
try {
String ossFilePath = className + Suffix_JAVA;
EngineConfig engineConfig = new EngineConfig();
// String filePath ="home/"+ossFilePath;
String filePath = engineConfig.getSourcePath() + ossFilePath;
//下载容器
//下载文件到指定服务器目录下
OssUtil.downloadFileForBucket(bucketName, ossFilePath, filePath);
//编译
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
@ -122,7 +132,7 @@ public class RuleEngineServiceImpl extends ServiceImpl<RuleEngineVersionMapper,
OutputStream second = null; // 程序的输出 null 用 system.out
OutputStream third = null; // 程序的错误输出 .,null 用 system.err
// 程序编译参数 注意 我们编译目录是我们的项目目录
String[] strings = {"-classpath",engineConfig.getSourcePath(),"-verbose","-d", engineConfig.getSourcePath(), engineConfig.getSourcePath() +"/"+className+Suffix_JAVA};
String[] strings = {"-classpath",engineWorkSourcePath,"-verbose","-d", engineWorkSourcePath, engineWorkSourcePath +"\\"+className_prefix+className+Suffix_JAVA};
// 0 表示成功, 其他表示出现了错误
System.out.println(Arrays.toString(strings));
@ -135,8 +145,30 @@ public class RuleEngineServiceImpl extends ServiceImpl<RuleEngineVersionMapper,
}
// 假设这是你的外部类文件路径
String externalClassFilePath =
engineConfig.getSourcePath() + "\\com\\muyu\\data\\engine\\service\\"+className+Suffix_JAVA;
engineWorkClassPath + "\\com\\muyu\\data\\engine\\service\\"+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 = externalClassLoader.loadClassFromPath(classFilePath,className_prefix+className+Suffix_CLASS);
//创建类的实例
Object instance = clazz.getDeclaredConstructor().newInstance();
} catch (IOException | NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
throw new RuntimeException(e);
}
//对class文件进行自定义类加载规则引擎
// Map<String, Class<?>> stringClassMap = JavaBinaryClassLoader.loadClassesByLocation(engineConfig.getServerPack(), engineConfig.getClassPath());