From 5c8f4db2dc9c858a93308cb049c70db2c5001b6e Mon Sep 17 00:00:00 2001 From: chentaisen <14615430+chentaisen@user.noreply.gitee.com> Date: Mon, 2 Sep 2024 21:12:19 +0800 Subject: [PATCH] Changes --- .../muyu/rule/compile/SourceCodeCompiler.java | 175 +++++++++--------- .../muyu/rule/load/JavaBinaryClassLoader.java | 171 +++++++++-------- .../java/com/muyu/rule/scan/JavaCodeScan.java | 152 ++++++++------- .../com/muyu/rule/test/OSSFileDownload.java | 49 +---- 4 files changed, 278 insertions(+), 269 deletions(-) diff --git a/muyu-rule-server/src/main/java/com/muyu/rule/compile/SourceCodeCompiler.java b/muyu-rule-server/src/main/java/com/muyu/rule/compile/SourceCodeCompiler.java index ac79e04..3bd1d45 100644 --- a/muyu-rule-server/src/main/java/com/muyu/rule/compile/SourceCodeCompiler.java +++ b/muyu-rule-server/src/main/java/com/muyu/rule/compile/SourceCodeCompiler.java @@ -1,85 +1,90 @@ -//package com.muyu.rule.compile; -// -//import com.muyu.rule.scan.JavaCodeScan; -//import io.jsonwebtoken.io.IOException; -// -//import javax.tools.JavaCompiler; -//import javax.tools.JavaFileObject; -//import javax.tools.StandardJavaFileManager; -//import javax.tools.ToolProvider; -//import java.io.File; -//import java.io.FileNotFoundException; -//import java.nio.file.Files; -// -///** -// * java源码编译工具 -// */ -//public class SourceCodeCompiler { -// -// -// /** -// * 1.获取系统java编译器 -// */ -// private static JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); -// /** -// * 获取JAVA文件管理器 -// */ -// private static StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); -// -// /** -// * 输入编译的文件夹路径 -// * -// * @param path 文件夹路径 -// */ -// public static void javaCompilerPath(String path) { -// File[] files = JavaCodeScan.javaSourceScanByPath(path); -// //进行编译 -// javaCompiler(files); -// } -// -// /** -// * 输入编译的文件夹路径 -// * -// * @param filePath -// */ -// public static void javaCompilerFile(String... filePath) { -// int filesPathLength = filePath.length; -// File[] files = new File[filesPathLength]; -// //定义要编译的源文件 -// for (int i = 0; i < filesPathLength; i++) { -// files[i] = new File(filePath[i]); -// } -// } -// /** -// *可同时编译多个java源文件 -// * @param file -// */ -// public static void javaCompiler(File... file) { -// //通过源文件获取到要编译的java类源码迭代器,包括所有内部类,其中每个类都是一个 javaFileObject,也被称为一个汇编单元 -// Iterable compilationUnits = fileManager.getJavaFileObjects(file); -// //生成编译任务 -// JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits); -// //执行编译任务 -// task.call(); -// } -// -// -//} -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// -// +package com.muyu.rule.compile; + + +import com.muyu.rule.scan.JavaCodeScan; +import io.jsonwebtoken.io.IOException; + +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.file.Files; + +/** + * java源码编译工具 + */ +public class SourceCodeCompiler extends ClassLoader{ + + + private static JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + + private static StandardJavaFileManager fileManager = compiler.getStandardFileManager(null,null,null); + + /** + * 输入编译的文件夹路径 + * @param path 文件夹路径 + */ + + 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; + File[] files = new File[filePathLength]; + + for (int i = 0; i < filePathLength; i++) { + files[i] = new File(filePath[i]); + + + } + + + } + + + /** + * 可同时编译多个java源文件 + * @param file + */ + + + public static void javaCompiler(File... file){ + //过源文件获取到要编译的Java类源码选代器,包括所有内部类,其中每个类都是一个 JavaFile0bject,也被称为一个汇编单元 + Iterable comilationUnits = fileManager.getJavaFileObjects(file); + + JavaCompiler.CompilationTask task= compiler.getTask(null,fileManager,null,null,null,comilationUnits); + + task.call(); + + } + + +} + + + + + + + + + + + + + + + + + + diff --git a/muyu-rule-server/src/main/java/com/muyu/rule/load/JavaBinaryClassLoader.java b/muyu-rule-server/src/main/java/com/muyu/rule/load/JavaBinaryClassLoader.java index 28e79c5..188a045 100644 --- a/muyu-rule-server/src/main/java/com/muyu/rule/load/JavaBinaryClassLoader.java +++ b/muyu-rule-server/src/main/java/com/muyu/rule/load/JavaBinaryClassLoader.java @@ -1,74 +1,97 @@ -//package com.muyu.rule.load; -// -//import com.muyu.rule.scan.JavaCodeScan; -//import io.jsonwebtoken.io.IOException; -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -// -//import java.io.File; -//import java.io.FileNotFoundException; -//import java.nio.file.Files; -//import java.util.HashMap; -//import java.util.Map; -// -///** -// * @ClassName Java java字节码加载器 -// * @Description 加载 -// * @Author Chen -// * @Date 2024/8/23 09:55 -// */ -//public class JavaBinaryClassLoader extends ClassLoader { -// private static JavaBinaryClassLoader loader = new JavaBinaryClassLoader(); -// -// private static final Logger log = LoggerFactory.getLogger(JavaBinaryClassLoader.class); -// -// -// /** -// * @param name class 类的文件名 -// * @param pack 类所在的包名 -// * @param location 类文件的路径 -// * @return -// */ -// private static Class loadClassByNameAndLocation(String name, String pack, File location) { -// byte[] datas = null; -// try { -// datas = loader.loadClassData(name, location); -// } catch (Exception e) { -// throw new RuntimeException(e); -// } -// pack = loader.convert(pack); -// Class aClass = loader.defineClass(pack + name, datas, 0, datas.length); -// log.info("成功加载规则引擎->{}", aClass.getName()); -// return aClass; -// } -// -// /** -// * @param pack 类所在的包名 后面需要 "." -// * @param location 包所在的路径 -// * @return -// */ -// public static Map> loadClassesByLocation(String pack, String location) { -// File[] childFiles = JavaCodeScan.javaBinaryByPath(location); -// HashMap> map = new HashMap<>(); -// for (File f : childFiles) { -// String name = f.getName().substring(0, f.getName().indexOf(".class")); -// Class c = loadClassByNameAndLocation(name, pack, new File(location)); -// map.put(name, c); -// } -// return map; -// } -// -// -// public byte[] loadClassData(String name, File location) throws Exception { -// File classFile = new File(location, name + ".class"); -// if (!classFile.exists()) { -// throw new FileNotFoundException("找不到类文件: " + classFile.getAbsolutePath()); -// } -// return Files.readAllBytes(classFile.toPath()); -// } -// -// public String convert(String pack) { -// return pack; -// } -// -//} +package com.muyu.rule.load; + +import com.muyu.rule.scan.JavaCodeScan; +import io.jsonwebtoken.io.IOException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.file.Files; +import java.util.HashMap; +import java.util.Map; + +/** + * @ClassName Java java字节码加载器 + * @Description 加载 + * @Author Chen + * @Date 2024/8/23 09:55 + */ +public class JavaBinaryClassLoader extends ClassLoader { + private static JavaBinaryClassLoader loader =new JavaBinaryClassLoader(); + + private static final Logger log = LoggerFactory.getLogger(JavaBinaryClassLoader.class); + + + /** + * @param name class 类的文件名 + * @param pack 类所在的包名eg:com.ecoogy.etl.engine. + * @param location 类文件的路径,设定目录: 设定目录时,需要在最后带上"/" + */ + + + + private static Class loadClassByNameAndLocation(String name, String pack, File location){ + + try { + //将class文件的数据读入到byte数组中 转成二进制文件 + byte[] datas = loader.loadClassData(name,location); + pack= loader.convert(pack); + //通过byte数组加载Class对象 + String cc = pack+name; + + Class aClass = loader.defineClass(cc, datas, 0, datas.length); + log.info("成功加载规则引擎-> {} ",aClass.getName()); + return aClass; + } catch (IOException e) { + log.error("从文件中加载类失败 {}", name, location, e); + return null; + } + + } + /** + * @param pack 类所在的包名 eg:com.szelink.test. 后面需要带个"." + * @param location 包所在的路径 + * @return + */ + public static Map> loadClassesByLocation(String pack, String location){ + int count = 0; + File[] childFiles = JavaCodeScan.javaBinaryByPath(location); + HashMap> map = new HashMap<>(); + for (File f : childFiles) { + String name = f.getName().substring(0, f.getName().indexOf(".class")); + + Class c = loadClassByNameAndLocation(name,pack,new File(location)); + System.out.println(++count); + map.put(name,c); + } + return map; + } + + /** + * 修改文件的格式 + * @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); + } + } + +} diff --git a/muyu-rule-server/src/main/java/com/muyu/rule/scan/JavaCodeScan.java b/muyu-rule-server/src/main/java/com/muyu/rule/scan/JavaCodeScan.java index 4926d03..20ca3c1 100644 --- a/muyu-rule-server/src/main/java/com/muyu/rule/scan/JavaCodeScan.java +++ b/muyu-rule-server/src/main/java/com/muyu/rule/scan/JavaCodeScan.java @@ -1,65 +1,87 @@ -//package com.muyu.rule.scan; -// -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -// -//import java.io.File; -//import java.io.FilenameFilter; -// -// -///** -// * @ClassName JavaCodeScan -// * @Description 扫描文件器 -// * @Author Chen -// * @Date 2024/8/23 09:56 -// */ -//public class JavaCodeScan { -// private static final Logger log = LoggerFactory.getLogger(JavaCodeScan.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) { -// return filterFileBySuf(filePath, JAVA_SOURCE_SUF); -// } -// -// /** -// * 扫描本路径下java所有源文件 并创建为file文件 -// */ -// public static File[] javaBinaryByPath(String filePath) { -// return filterFileBySuf(filePath, JAVA_BINARY_SUF); -// } -// -// public static File isJavaSourceScanByPath(String filePath) { -// File file = new File(filePath); -// if (file.isFile()) { -// return file; -// } -// return null; -// } -// -// /** -// * @param filePath 文件路径 -// * @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; -// } -//} +package com.muyu.rule.scan; + +import com.muyu.rule.load.JavaBinaryClassLoader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FilenameFilter; + + +/** + * @ClassName JavaCodeScan + * @Description 扫描文件器 + * @Author Chen + * @Date 2024/8/23 09:56 + */ +public class JavaCodeScan { + private static final Logger log = LoggerFactory.getLogger(JavaBinaryClassLoader.class); + + private static final String JAVA_SOURCE_SUF = ".java"; + private static final String JAVA_BINARY_SUF = ".class"; + + /** + * 扫描本路径下java所有源文件 并创建为file文件 + * @param filePath 文件路径 + * @return + */ + + public static File[] javaSourceScanByPath(String filePath){ + + return filterFileBySuf(filePath,JAVA_SOURCE_SUF); + } + + /** + * 扫描本路径下java所有源文件 并创建为file文件 + * @param filePath 文件路径 + * @return + */ + public static File[] javaBinaryByPath(String filePath){ + + return filterFileBySuf(filePath,JAVA_BINARY_SUF); + } + + /** + * 校验是否为java文件 是 返回File 否 抛出异常 + * @param filePath + * @return + */ + public static File isJavaSourceScanByPath(String filePath){ + File file = new File(filePath); + if (file.isFile()){ + return file; + } + return null; + } + + + + + /** + * + * @param filePath 文件路径 + * @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; + } + +} diff --git a/muyu-rule-server/src/main/java/com/muyu/rule/test/OSSFileDownload.java b/muyu-rule-server/src/main/java/com/muyu/rule/test/OSSFileDownload.java index b61c6d6..7544012 100644 --- a/muyu-rule-server/src/main/java/com/muyu/rule/test/OSSFileDownload.java +++ b/muyu-rule-server/src/main/java/com/muyu/rule/test/OSSFileDownload.java @@ -2,55 +2,13 @@ package com.muyu.rule.test; import com.aliyun.oss.OSSClient; import com.aliyun.oss.model.GetObjectRequest; -import com.aliyun.oss.model.OSSObject; import com.aliyun.oss.model.ObjectMetadata; import com.muyu.common.core.domain.Result; -import java.io.BufferedReader; import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; 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 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 private static final String endpoint = "oss-cn-shanghai.aliyuncs.com"; // 阿里云 accessKeyId @@ -62,7 +20,9 @@ public class OSSFileDownload { // OSS文件路径 private static final String filePath = "oss/"; + public static Result streamingDownload(String fileName) { + String source = null; // 创建 OSSClient 实例 OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); @@ -73,17 +33,16 @@ public class OSSFileDownload { return Result.error("bucket不存在"); } String objectName = fileName + ".java"; - Boolean flag_file = ossClient.doesObjectExist(bucketName, filePath+objectName); + Boolean flag_file = ossClient.doesObjectExist(bucketName, filePath + objectName); if (!flag_file) { System.out.println("预下载文件不存在"); return Result.error("预下载文件不存在"); } // 本地文件下载路径 - String localPath = "home/"+objectName; + String localPath = "home/" + objectName; ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, filePath + objectName), new File(localPath)); System.out.println(object); ossClient.shutdown(); return Result.success(source); } - }