cloud-rule/cloud-rule-engine/src/main/java/com/muyu/compile/FilesCompilerLoad.java

105 lines
3.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.muyu.compile;
import com.muyu.common.core.domain.Result;
import com.muyu.common.domain.DataValue;
import com.muyu.constant.MethodSuffix;
import com.muyu.engine.basic.BasicEngine;
import com.muyu.upload.ALiYunUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.tools.*;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Author蓬叁
* @Packagecom.muyu.compile
* @Projectcloud-rule
* @nameOSSFileCompile
* @Date2024/8/31 下午7:08
*/
public class FilesCompilerLoad {
private static final Logger log = LoggerFactory.getLogger(FilesCompilerLoad.class);
static Map<String , BasicEngine<DataValue>> engineMap = new ConcurrentHashMap<>();
public static Result<Object> javaCompiler(String fileName){
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String[] strings = {"-classpath","/home/lib","-verbose","-d", "/home/lib","home/"+fileName + MethodSuffix.JAVASUFFIX };
// 执行编译任务
int result = compiler.run(null, null, null, strings);
// 检查编译结果
if (result == 0) {
System.out.println("编译成功,生成的.class文件位于源代码同目录");
} else {
System.out.println("编译失败");
}
ALiYunUpload.uploadClassFiles(fileName,"/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX);
return Result.success();
}
public static Result<Object> classLoad(String fileName){
Class<?> clazz ;
try {
// class文件路径
String externalClassFilePath = "/home/lib/com/muyu/generate/"+fileName + MethodSuffix.CLASSSUFFIX;
log.info("class文件路径:[{}]",externalClassFilePath);
Path path = Paths.get(externalClassFilePath);
String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf('/'));
URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
log.info("开始创建加载器...");
// 加载类
// 注意类名必须是完全限定名(包括包名)
clazz = externalClassLoader.loadClassFromPath(path, "com.muyu.generate."+fileName);
log.info("加载成功:[{}]",clazz.getName());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
Object instance = clazz.getDeclaredConstructor().newInstance();
engineMap.put(fileName, (BasicEngine<DataValue>) instance);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
DataValue dataValue = DataValue.builder()
.type("String")
.label("姓名")
.key("name")
.value("张三")
.build();
BasicEngine<DataValue> engine = engineMap.get(fileName);
engine.set(dataValue);
engine.execution();
return Result.success();
}
}