feat():测试
parent
4ec95e574c
commit
87118e5521
|
@ -0,0 +1,64 @@
|
|||
package com.muyu.compile;
|
||||
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.constant.MethodSuffix;
|
||||
|
||||
import javax.tools.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
* @Author:蓬叁
|
||||
* @Package:com.muyu.compile
|
||||
* @Project:cloud-rule
|
||||
* @name:OSSFileCompile
|
||||
* @Date:2024/8/31 下午7:08
|
||||
*/
|
||||
public class JavaFileCompile {
|
||||
|
||||
public static Result<Object> compile(String fileName){
|
||||
|
||||
try {
|
||||
|
||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
String[] strings = {"-classpath","/home/lib","-verbose","-d", "/home/lib",fileName + MethodSuffix.JAVASUFFIX };
|
||||
// 执行编译任务
|
||||
int result = compiler.run(null, null, null, strings);
|
||||
// 检查编译结果
|
||||
if (result == 0) {
|
||||
System.out.println("编译成功,生成的.class文件位于源代码同目录");
|
||||
} else {
|
||||
System.out.println("编译失败");
|
||||
}
|
||||
|
||||
// class文件路径
|
||||
String externalClassFilePath = "/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX;
|
||||
|
||||
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);
|
||||
|
||||
// 加载类
|
||||
// 注意类名必须是完全限定名(包括包名)
|
||||
Class<?> clazz = externalClassLoader.loadClassFromPath(path, "/home/lib/com/muyu/generate/"+fileName);
|
||||
System.out.println(clazz);
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
package com.muyu.compile;
|
||||
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import org.apache.catalina.startup.EngineConfig;
|
||||
|
||||
import javax.tools.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author:蓬叁
|
||||
* @Package:com.muyu.compile
|
||||
* @Project:cloud-rule
|
||||
* @name:OSSFileCompile
|
||||
* @Date:2024/8/31 下午7:08
|
||||
*/
|
||||
public class OSSFileCompile{
|
||||
|
||||
public static Result<Object> compile(String source){
|
||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
|
||||
String[] strings = {"-classpath","/home/lib","-verbose","-d", "/home/lib",source };
|
||||
// 执行编译任务
|
||||
int result = compiler.run(null, null, null, strings);
|
||||
// 检查编译结果
|
||||
if (result == 0) {
|
||||
System.out.println("编译成功,生成的.class文件位于源代码同目录");
|
||||
} else {
|
||||
System.out.println("编译失败");
|
||||
}
|
||||
|
||||
String externalClassFilePath = "/home";
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
// public static Result<Object> compile(String source){
|
||||
// JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
// StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
|
||||
// // 执行编译任务
|
||||
// Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList(source));
|
||||
// Boolean result = compiler.getTask(null, fileManager, null, Arrays.asList("-cp", "/home/lib"), null, compilationUnits).call();
|
||||
//// int result = compiler.run(null, null, null, source);
|
||||
// // 检查编译结果
|
||||
// if (result) {
|
||||
// System.out.println("编译成功,生成的.class文件位于源代码同目录");
|
||||
// return Result.success("编译成功,生成的.class文件位于源代码同目录");
|
||||
// } else {
|
||||
// System.out.println("编译失败");
|
||||
// return Result.error("编译失败");
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.muyu.constant;
|
||||
|
||||
/**
|
||||
* @Author:蓬叁
|
||||
* @Package:com.muyu.constant
|
||||
* @Project:cloud-rule
|
||||
* @name:MethodSuffix
|
||||
* @Date:2024/9/5 下午2:44
|
||||
*/
|
||||
public class MethodSuffix {
|
||||
public static final String JAVASUFFIX = ".java";
|
||||
public static final String CLASSSUFFIX = ".class";
|
||||
}
|
|
@ -2,7 +2,6 @@ package com.muyu.load;
|
|||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.compile.OSSFileCompile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
|
|
|
@ -1,19 +1,11 @@
|
|||
package com.muyu.load;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.GetObjectRequest;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.compile.OSSFileCompile;
|
||||
import com.muyu.util.SourceCodeCompiler;
|
||||
import com.muyu.compile.JavaFileCompile;
|
||||
import com.muyu.constant.MethodSuffix;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author:蓬叁
|
||||
|
@ -44,20 +36,20 @@ public class OSSFileLoad {
|
|||
System.out.println("bucket不存在");
|
||||
return Result.error("bucket不存在");
|
||||
}
|
||||
Boolean flag_file = ossClient.doesObjectExist(bucketName, filePath+fileName);
|
||||
Boolean flag_file = ossClient.doesObjectExist(bucketName, filePath+fileName+ MethodSuffix.JAVASUFFIX);
|
||||
if (!flag_file) {
|
||||
System.out.println("预下载文件不存在");
|
||||
return Result.error("预下载文件不存在");
|
||||
}
|
||||
// 本地文件下载路径
|
||||
String localPath = "home/"+fileName;
|
||||
String localPath = "home/" + fileName + MethodSuffix.JAVASUFFIX;
|
||||
// ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, filePath + fileName), new File(localPath));
|
||||
// System.out.println(object);
|
||||
//
|
||||
// ossClient.shutdown();
|
||||
try {
|
||||
// 从OSS下载文件
|
||||
InputStream inputStream = ossClient.getObject(bucketName, filePath+fileName).getObjectContent();
|
||||
InputStream inputStream = ossClient.getObject(bucketName, filePath + fileName + MethodSuffix.JAVASUFFIX).getObjectContent();
|
||||
File downloadFile = new File(localPath);
|
||||
FileOutputStream outputStream = new FileOutputStream(downloadFile);
|
||||
|
||||
|
@ -80,7 +72,7 @@ public class OSSFileLoad {
|
|||
}
|
||||
//对路径里的.java文件进行编译
|
||||
System.out.println("第一步");
|
||||
OSSFileCompile.compile("home/"+fileName);
|
||||
JavaFileCompile.compile("home/"+fileName);
|
||||
// SourceCodeCompiler.javaCompilerPath("home/");
|
||||
// System.out.println("第二步");
|
||||
// File outputDir = new File("home/"); // 或者是你指定的其他输出目录
|
||||
|
|
|
@ -64,7 +64,7 @@ public class RuleVersionController {
|
|||
ruleVersionService.save(RuleVersion.addBuild(ruleVersionAddReq));
|
||||
Result<Object> objectResult = ALiYunUpload.uploadFiles(ruleVersionAddReq.getRuleVersionText(), ruleVersionAddReq.getRuleVersionType());
|
||||
System.out.println(objectResult.getMsg());
|
||||
OSSFileLoad.streamingDownload(ruleVersionAddReq.getRuleVersionType() + ".java");
|
||||
OSSFileLoad.streamingDownload(ruleVersionAddReq.getRuleVersionType());
|
||||
return Result.success(null,"操作成功");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue