Changes
parent
0e3489c7ca
commit
5c8f4db2dc
|
@ -1,85 +1,90 @@
|
||||||
//package com.muyu.rule.compile;
|
package com.muyu.rule.compile;
|
||||||
//
|
|
||||||
//import com.muyu.rule.scan.JavaCodeScan;
|
|
||||||
//import io.jsonwebtoken.io.IOException;
|
import com.muyu.rule.scan.JavaCodeScan;
|
||||||
//
|
import io.jsonwebtoken.io.IOException;
|
||||||
//import javax.tools.JavaCompiler;
|
|
||||||
//import javax.tools.JavaFileObject;
|
import javax.tools.JavaCompiler;
|
||||||
//import javax.tools.StandardJavaFileManager;
|
import javax.tools.JavaFileObject;
|
||||||
//import javax.tools.ToolProvider;
|
import javax.tools.StandardJavaFileManager;
|
||||||
//import java.io.File;
|
import javax.tools.ToolProvider;
|
||||||
//import java.io.FileNotFoundException;
|
import java.io.File;
|
||||||
//import java.nio.file.Files;
|
import java.io.FileNotFoundException;
|
||||||
//
|
import java.nio.file.Files;
|
||||||
///**
|
|
||||||
// * java源码编译工具
|
/**
|
||||||
// */
|
* java源码编译工具
|
||||||
//public class SourceCodeCompiler {
|
*/
|
||||||
//
|
public class SourceCodeCompiler extends ClassLoader{
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 1.获取系统java编译器
|
private static JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||||
// */
|
|
||||||
// private static JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
private static StandardJavaFileManager fileManager = compiler.getStandardFileManager(null,null,null);
|
||||||
// /**
|
|
||||||
// * 获取JAVA文件管理器
|
/**
|
||||||
// */
|
* 输入编译的文件夹路径
|
||||||
// private static StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
|
* @param path 文件夹路径
|
||||||
//
|
*/
|
||||||
// /**
|
|
||||||
// * 输入编译的文件夹路径
|
public static void javaCompilerPath(String path){
|
||||||
// *
|
File[] files = JavaCodeScan.javaSourceScanByPath(path);
|
||||||
// * @param path 文件夹路径
|
|
||||||
// */
|
javaCompiler(files);
|
||||||
// public static void javaCompilerPath(String path) {
|
}
|
||||||
// File[] files = JavaCodeScan.javaSourceScanByPath(path);
|
|
||||||
// //进行编译
|
/**
|
||||||
// javaCompiler(files);
|
* 输入编译的文件路径
|
||||||
// }
|
* @param filePath
|
||||||
//
|
*/
|
||||||
// /**
|
|
||||||
// * 输入编译的文件夹路径
|
public static void javaCompilerFile(String... filePath){
|
||||||
// *
|
int filePathLength = filePath.length;
|
||||||
// * @param filePath
|
File[] files = new File[filePathLength];
|
||||||
// */
|
|
||||||
// public static void javaCompilerFile(String... filePath) {
|
for (int i = 0; i < filePathLength; i++) {
|
||||||
// int filesPathLength = filePath.length;
|
files[i] = new File(filePath[i]);
|
||||||
// File[] files = new File[filesPathLength];
|
|
||||||
// //定义要编译的源文件
|
|
||||||
// for (int i = 0; i < filesPathLength; i++) {
|
}
|
||||||
// files[i] = new File(filePath[i]);
|
|
||||||
// }
|
|
||||||
// }
|
}
|
||||||
// /**
|
|
||||||
// *可同时编译多个java源文件
|
|
||||||
// * @param file
|
/**
|
||||||
// */
|
* 可同时编译多个java源文件
|
||||||
// public static void javaCompiler(File... file) {
|
* @param file
|
||||||
// //通过源文件获取到要编译的java类源码迭代器,包括所有内部类,其中每个类都是一个 javaFileObject,也被称为一个汇编单元
|
*/
|
||||||
// Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file);
|
|
||||||
// //生成编译任务
|
|
||||||
// JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
|
public static void javaCompiler(File... file){
|
||||||
// //执行编译任务
|
//过源文件获取到要编译的Java类源码选代器,包括所有内部类,其中每个类都是一个 JavaFile0bject,也被称为一个汇编单元
|
||||||
// task.call();
|
Iterable<? extends JavaFileObject> comilationUnits = fileManager.getJavaFileObjects(file);
|
||||||
// }
|
|
||||||
//
|
JavaCompiler.CompilationTask task= compiler.getTask(null,fileManager,null,null,null,comilationUnits);
|
||||||
//
|
|
||||||
//}
|
task.call();
|
||||||
//
|
|
||||||
//
|
}
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
}
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,74 +1,97 @@
|
||||||
//package com.muyu.rule.load;
|
package com.muyu.rule.load;
|
||||||
//
|
|
||||||
//import com.muyu.rule.scan.JavaCodeScan;
|
import com.muyu.rule.scan.JavaCodeScan;
|
||||||
//import io.jsonwebtoken.io.IOException;
|
import io.jsonwebtoken.io.IOException;
|
||||||
//import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
//import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
//
|
|
||||||
//import java.io.File;
|
import java.io.File;
|
||||||
//import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
//import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
//import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
//import java.util.Map;
|
import java.util.Map;
|
||||||
//
|
|
||||||
///**
|
/**
|
||||||
// * @ClassName Java java字节码加载器
|
* @ClassName Java java字节码加载器
|
||||||
// * @Description 加载
|
* @Description 加载
|
||||||
// * @Author Chen
|
* @Author Chen
|
||||||
// * @Date 2024/8/23 09:55
|
* @Date 2024/8/23 09:55
|
||||||
// */
|
*/
|
||||||
//public class JavaBinaryClassLoader extends ClassLoader {
|
public class JavaBinaryClassLoader extends ClassLoader {
|
||||||
// private static JavaBinaryClassLoader loader = new JavaBinaryClassLoader();
|
private static JavaBinaryClassLoader loader =new JavaBinaryClassLoader();
|
||||||
//
|
|
||||||
// private static final Logger log = LoggerFactory.getLogger(JavaBinaryClassLoader.class);
|
private static final Logger log = LoggerFactory.getLogger(JavaBinaryClassLoader.class);
|
||||||
//
|
|
||||||
//
|
|
||||||
// /**
|
/**
|
||||||
// * @param name class 类的文件名
|
* @param name class 类的文件名
|
||||||
// * @param pack 类所在的包名
|
* @param pack 类所在的包名eg:com.ecoogy.etl.engine.
|
||||||
// * @param location 类文件的路径
|
* @param location 类文件的路径,设定目录: 设定目录时,需要在最后带上"/"
|
||||||
// * @return
|
*/
|
||||||
// */
|
|
||||||
// private static Class<?> loadClassByNameAndLocation(String name, String pack, File location) {
|
|
||||||
// byte[] datas = null;
|
|
||||||
// try {
|
private static Class<?> loadClassByNameAndLocation(String name, String pack, File location){
|
||||||
// datas = loader.loadClassData(name, location);
|
|
||||||
// } catch (Exception e) {
|
try {
|
||||||
// throw new RuntimeException(e);
|
//将class文件的数据读入到byte数组中 转成二进制文件
|
||||||
// }
|
byte[] datas = loader.loadClassData(name,location);
|
||||||
// pack = loader.convert(pack);
|
pack= loader.convert(pack);
|
||||||
// Class<?> aClass = loader.defineClass(pack + name, datas, 0, datas.length);
|
//通过byte数组加载Class对象
|
||||||
// log.info("成功加载规则引擎->{}", aClass.getName());
|
String cc = pack+name;
|
||||||
// return aClass;
|
|
||||||
// }
|
Class<?> aClass = loader.defineClass(cc, datas, 0, datas.length);
|
||||||
//
|
log.info("成功加载规则引擎-> {} ",aClass.getName());
|
||||||
// /**
|
return aClass;
|
||||||
// * @param pack 类所在的包名 后面需要 "."
|
} catch (IOException e) {
|
||||||
// * @param location 包所在的路径
|
log.error("从文件中加载类失败 {}", name, location, e);
|
||||||
// * @return
|
return null;
|
||||||
// */
|
}
|
||||||
// public static Map<String, Class<?>> loadClassesByLocation(String pack, String location) {
|
|
||||||
// File[] childFiles = JavaCodeScan.javaBinaryByPath(location);
|
}
|
||||||
// HashMap<String, Class<?>> map = new HashMap<>();
|
/**
|
||||||
// for (File f : childFiles) {
|
* @param pack 类所在的包名 eg:com.szelink.test. 后面需要带个"."
|
||||||
// String name = f.getName().substring(0, f.getName().indexOf(".class"));
|
* @param location 包所在的路径
|
||||||
// Class<?> c = loadClassByNameAndLocation(name, pack, new File(location));
|
* @return
|
||||||
// map.put(name, c);
|
*/
|
||||||
// }
|
public static Map<String ,Class<?>> loadClassesByLocation(String pack, String location){
|
||||||
// return map;
|
int count = 0;
|
||||||
// }
|
File[] childFiles = JavaCodeScan.javaBinaryByPath(location);
|
||||||
//
|
HashMap<String, Class<?>> map = new HashMap<>();
|
||||||
//
|
for (File f : childFiles) {
|
||||||
// public byte[] loadClassData(String name, File location) throws Exception {
|
String name = f.getName().substring(0, f.getName().indexOf(".class"));
|
||||||
// File classFile = new File(location, name + ".class");
|
|
||||||
// if (!classFile.exists()) {
|
Class<?> c = loadClassByNameAndLocation(name,pack,new File(location));
|
||||||
// throw new FileNotFoundException("找不到类文件: " + classFile.getAbsolutePath());
|
System.out.println(++count);
|
||||||
// }
|
map.put(name,c);
|
||||||
// return Files.readAllBytes(classFile.toPath());
|
}
|
||||||
// }
|
return map;
|
||||||
//
|
}
|
||||||
// public String convert(String pack) {
|
|
||||||
// return pack;
|
/**
|
||||||
// }
|
* 修改文件的格式
|
||||||
//
|
* @param pack
|
||||||
//}
|
* @return
|
||||||
|
*/
|
||||||
|
private String convert(String pack) {
|
||||||
|
return pack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] loadClassData(String name, File location)throws IOException {
|
||||||
|
File classFile = new File(location, name + ".class");
|
||||||
|
if (!classFile.exists()) {
|
||||||
|
try {
|
||||||
|
throw new FileNotFoundException("找不到类文件: " + classFile.getAbsolutePath());
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//FileNotFoundException
|
||||||
|
try {
|
||||||
|
return Files.readAllBytes(classFile.toPath());
|
||||||
|
} catch (java.io.IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -1,65 +1,87 @@
|
||||||
//package com.muyu.rule.scan;
|
package com.muyu.rule.scan;
|
||||||
//
|
|
||||||
//import org.slf4j.Logger;
|
import com.muyu.rule.load.JavaBinaryClassLoader;
|
||||||
//import org.slf4j.LoggerFactory;
|
import org.slf4j.Logger;
|
||||||
//
|
import org.slf4j.LoggerFactory;
|
||||||
//import java.io.File;
|
|
||||||
//import java.io.FilenameFilter;
|
import java.io.File;
|
||||||
//
|
import java.io.FilenameFilter;
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @ClassName JavaCodeScan
|
/**
|
||||||
// * @Description 扫描文件器
|
* @ClassName JavaCodeScan
|
||||||
// * @Author Chen
|
* @Description 扫描文件器
|
||||||
// * @Date 2024/8/23 09:56
|
* @Author Chen
|
||||||
// */
|
* @Date 2024/8/23 09:56
|
||||||
//public class JavaCodeScan {
|
*/
|
||||||
// private static final Logger log = LoggerFactory.getLogger(JavaCodeScan.class);
|
public class JavaCodeScan {
|
||||||
// private static final String JAVA_SOURCE_SUF = ".java";
|
private static final Logger log = LoggerFactory.getLogger(JavaBinaryClassLoader.class);
|
||||||
// private static final String JAVA_BINARY_SUF = ".class";
|
|
||||||
//
|
private static final String JAVA_SOURCE_SUF = ".java";
|
||||||
// /**
|
private static final String JAVA_BINARY_SUF = ".class";
|
||||||
// * 扫描本路径下java所有源文件 并创建为file文件
|
|
||||||
// */
|
/**
|
||||||
// public static File[] javaSourceScanByPath(String filePath) {
|
* 扫描本路径下java所有源文件 并创建为file文件
|
||||||
// return filterFileBySuf(filePath, JAVA_SOURCE_SUF);
|
* @param filePath 文件路径
|
||||||
// }
|
* @return
|
||||||
//
|
*/
|
||||||
// /**
|
|
||||||
// * 扫描本路径下java所有源文件 并创建为file文件
|
public static File[] javaSourceScanByPath(String filePath){
|
||||||
// */
|
|
||||||
// public static File[] javaBinaryByPath(String filePath) {
|
return filterFileBySuf(filePath,JAVA_SOURCE_SUF);
|
||||||
// return filterFileBySuf(filePath, JAVA_BINARY_SUF);
|
}
|
||||||
// }
|
|
||||||
//
|
/**
|
||||||
// public static File isJavaSourceScanByPath(String filePath) {
|
* 扫描本路径下java所有源文件 并创建为file文件
|
||||||
// File file = new File(filePath);
|
* @param filePath 文件路径
|
||||||
// if (file.isFile()) {
|
* @return
|
||||||
// return file;
|
*/
|
||||||
// }
|
public static File[] javaBinaryByPath(String filePath){
|
||||||
// return null;
|
|
||||||
// }
|
return filterFileBySuf(filePath,JAVA_BINARY_SUF);
|
||||||
//
|
}
|
||||||
// /**
|
|
||||||
// * @param filePath 文件路径
|
/**
|
||||||
// * @param suf 扫描后缀
|
* 校验是否为java文件 是 返回File 否 抛出异常
|
||||||
// * @return
|
* @param filePath
|
||||||
// */
|
* @return
|
||||||
// public static File[] filterFileBySuf(String filePath, String suf) {
|
*/
|
||||||
// //创建需要扫描的文件夹
|
public static File isJavaSourceScanByPath(String filePath){
|
||||||
// File file = new File(filePath);
|
File file = new File(filePath);
|
||||||
// //列出所有的筛选suf结尾的文件集
|
if (file.isFile()){
|
||||||
// File[] childFiles = file.listFiles(new FilenameFilter() {
|
return file;
|
||||||
// @Override
|
}
|
||||||
// public boolean accept(File dir, String name) {
|
return null;
|
||||||
// if (name.indexOf(suf) <= -1) {
|
}
|
||||||
// return false;
|
|
||||||
// } else {
|
|
||||||
// log.info("扫描到{}文件->{}", suf, name);
|
|
||||||
// return true;
|
|
||||||
// }
|
/**
|
||||||
// }
|
*
|
||||||
// });
|
* @param filePath 文件路径
|
||||||
// return childFiles;
|
* @param suf 扫描后缀
|
||||||
// }
|
* @return
|
||||||
//}
|
*/
|
||||||
|
public static File[] filterFileBySuf(String filePath,String suf){
|
||||||
|
//创建需要扫描的文件夹
|
||||||
|
File file = new File(filePath);
|
||||||
|
//列出所有的筛选suf结尾的文件集
|
||||||
|
File[] childFiles = file.listFiles(new FilenameFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File dir, String name) {
|
||||||
|
if (name.indexOf(suf) <= -1) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
log.info("扫描到{}文件->{}", suf, name);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return childFiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -2,55 +2,13 @@ package com.muyu.rule.test;
|
||||||
|
|
||||||
import com.aliyun.oss.OSSClient;
|
import com.aliyun.oss.OSSClient;
|
||||||
import com.aliyun.oss.model.GetObjectRequest;
|
import com.aliyun.oss.model.GetObjectRequest;
|
||||||
import com.aliyun.oss.model.OSSObject;
|
|
||||||
import com.aliyun.oss.model.ObjectMetadata;
|
import com.aliyun.oss.model.ObjectMetadata;
|
||||||
import com.muyu.common.core.domain.Result;
|
import com.muyu.common.core.domain.Result;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
|
|
||||||
public class OSSFileDownload {
|
public class OSSFileDownload {
|
||||||
|
|
||||||
// // 阿里云 endpoint
|
|
||||||
// private static final String endpoint = "oss-cn-shanghai.aliyuncs.com";
|
|
||||||
// // 阿里云 accessKeyId
|
|
||||||
// private static final String accessKeyId = "LTAI5t6ytyyHWWB693bMEo53";
|
|
||||||
// // 阿里云 accessKeySecret
|
|
||||||
// private static final String accessKeySecret = "CSubcq72LLVttBlo2XO1JEMEmw894z";
|
|
||||||
// // bucket
|
|
||||||
// private static final String bucketName = "oss-tai";
|
|
||||||
// // 文件路径
|
|
||||||
// private static final String fileNamePath = "oss/";
|
|
||||||
// // 本地文件下载路径
|
|
||||||
//// private static final String localpath = "D:\\oss\\邮箱_SDFGHJ.java";
|
|
||||||
// private static final String localpath = "D:\\oss\\";
|
|
||||||
//
|
|
||||||
// public static Result<Object> streamingDownload(String fileName) {
|
|
||||||
// String source = null;
|
|
||||||
// // 创建 OSSClient 实例
|
|
||||||
// OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
|
||||||
// // 判断 bucket 是否存在
|
|
||||||
// Boolean flag_bucket = ossClient.doesBucketExist(bucketName);
|
|
||||||
// if (!flag_bucket) {
|
|
||||||
// System.out.println("bucket不存在");
|
|
||||||
// return Result.error("bucket不存在");
|
|
||||||
// }
|
|
||||||
// String objectName = fileName + ".java";
|
|
||||||
// Boolean flag_file = ossClient.doesObjectExist(bucketName, localpath + objectName);
|
|
||||||
// if (!flag_file) {
|
|
||||||
// System.out.println("预下载文件不存在");
|
|
||||||
// return Result.error("预下载文件不存在");
|
|
||||||
// }
|
|
||||||
// // 本地文件下载路径
|
|
||||||
// String localPath = "home/" + objectName;
|
|
||||||
// ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, localpath + objectName), new File(localPath));
|
|
||||||
// System.out.println(object);
|
|
||||||
// ossClient.shutdown();
|
|
||||||
// return Result.success(source);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 阿里云 endpoint
|
// 阿里云 endpoint
|
||||||
private static final String endpoint = "oss-cn-shanghai.aliyuncs.com";
|
private static final String endpoint = "oss-cn-shanghai.aliyuncs.com";
|
||||||
// 阿里云 accessKeyId
|
// 阿里云 accessKeyId
|
||||||
|
@ -62,7 +20,9 @@ public class OSSFileDownload {
|
||||||
// OSS文件路径
|
// OSS文件路径
|
||||||
private static final String filePath = "oss/";
|
private static final String filePath = "oss/";
|
||||||
|
|
||||||
|
|
||||||
public static Result<Object> streamingDownload(String fileName) {
|
public static Result<Object> streamingDownload(String fileName) {
|
||||||
|
|
||||||
String source = null;
|
String source = null;
|
||||||
// 创建 OSSClient 实例
|
// 创建 OSSClient 实例
|
||||||
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
||||||
|
@ -85,5 +45,4 @@ public class OSSFileDownload {
|
||||||
ossClient.shutdown();
|
ossClient.shutdown();
|
||||||
return Result.success(source);
|
return Result.success(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue