diff --git a/HelloWorld.class b/HelloWorld.class new file mode 100644 index 0000000..63615f6 Binary files /dev/null and b/HelloWorld.class differ diff --git a/HelloWorld.java b/HelloWorld.java new file mode 100644 index 0000000..7769f07 --- /dev/null +++ b/HelloWorld.java @@ -0,0 +1 @@ +public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } } \ No newline at end of file diff --git a/cloud-etl-common/pom.xml b/cloud-etl-common/pom.xml index 8ac9eb7..2f97eba 100644 --- a/cloud-etl-common/pom.xml +++ b/cloud-etl-common/pom.xml @@ -26,6 +26,12 @@ com.muyu cloud-common-security + + com.aliyun.oss + aliyun-sdk-oss + 3.17.4 + compile + diff --git a/cloud-etl-common/src/main/java/com/muyu/utils/OssUpload.java b/cloud-etl-common/src/main/java/com/muyu/utils/OssUpload.java new file mode 100644 index 0000000..2cbc69d --- /dev/null +++ b/cloud-etl-common/src/main/java/com/muyu/utils/OssUpload.java @@ -0,0 +1,56 @@ +package com.muyu.utils; + + +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.model.PutObjectRequest; +import com.muyu.common.core.domain.Result; + +import java.io.ByteArrayInputStream; + +import java.nio.charset.StandardCharsets; + +public class OssUpload { + + private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com"; + + private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ"; + + private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x"; + + private static final String bucketName = "011811"; + + public static Result uploadFiles(String content, String fileName) { + + OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); + + String objectName = "build/rule/version/" + fileName + ".java"; + + try { + ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)); + + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream); + + ossClient.putObject(putObjectRequest); + + return Result.success(null, "文件" + fileName + ".java" + "存储成功"); + + } catch (Exception e) { + + e.printStackTrace(); + + return Result.error(null, "文件" + fileName + ".java" + "存储失败"); + + } finally { + + if (ossClient != null) { + + ossClient.shutdown(); + + } + + } + + } + +} diff --git a/cloud-etl-common/src/main/java/com/muyu/utils/OssUtil.java b/cloud-etl-common/src/main/java/com/muyu/utils/OssUtil.java new file mode 100644 index 0000000..409d122 --- /dev/null +++ b/cloud-etl-common/src/main/java/com/muyu/utils/OssUtil.java @@ -0,0 +1,164 @@ +//package com.muyu.utils; +// +//import lombok.extern.log4j.Log4j2; +//import org.springframework.web.multipart.MultipartFile; +// +//import java.io.*; +//import java.time.LocalDateTime; +//import java.util.UUID; +// +///** +// * Oss服务调用 +// */ +//@Log4j2 +//public class OssUtil { +// +// /** +// * Endpoint 存储对象概述 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限 访问路径前缀 存储对象概述 +// */ +// private static String endPoint = "oss-cn-beijing.aliyuncs.com"; +// private static String accessKeyId = " LTAI5tFNfBpzEbLkntksCgwC"; +// private static String accessKeySecret = "XmYwUEea6BpgssdLKXMlpdzCI42Pk7"; +// private static String accessPre = "https://qdm123.oss-cn-beijing.aliyuncs.com/"; +// +// /** +// * bucket名称 +// * +// * @return +// */ +// private static String bucketName = "qdm123"; +// +// private static Oss ossClient; +// +// static { +// ossClient = new OSSClientBuilder().build( +// endPoint, +// accessKeyId, +// accessKeySecret); +// log.info("oss服务连接成功!"); +// } +// +// /** +// * 默认路径上传本地文件 +// * +// * @param filePath +// */ +// public static String uploadFile(String filePath) { +// return uploadFileForBucket(bucketName, getOssFilePath(filePath), filePath); +// } +// +// /** +// * 默认路径上传multipartFile文件 +// * +// * @param multipartFile +// */ +// public static String uploadMultipartFile(MultipartFile multipartFile) { +// return uploadMultipartFile(bucketName, getOssFilePath(multipartFile.getOriginalFilename()), multipartFile); +// } +// +// /** +// * 上传 multipartFile 类型文件 +// * +// * @param bucketName +// * @param ossPath +// * @param multipartFile +// */ +// public static String uploadMultipartFile(String bucketName, String ossPath, MultipartFile multipartFile) { +// InputStream inputStream = null; +// try { +// inputStream = multipartFile.getInputStream(); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// uploadFileInputStreamForBucket(bucketName, ossPath, inputStream); +// return accessPre + ossPath; +// } +// +// /** +// * 使用File上传PutObject上传文件 ** 程序默认使用次方法上传 +// * +// * @param bucketName 实例名称 +// * @param ossPath oss存储路径 +// * @param filePath 本地文件路径 +// */ +// public static String uploadFileForBucket(String bucketName, String ossPath, String filePath) { +// // 创建PutObjectRequest对象。 +// PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath, new File(filePath)); +// +// // 上传 +// ossClient.putObject(putObjectRequest); +// return accessPre + ossPath; +// } +// +// /** +// * 使用文件流上传到指定的bucket实例 +// * +// * @param bucketName 实例名称 +// * @param ossPath oss存储路径 +// * @param filePath 本地文件路径 +// */ +// public static String uploadFileInputStreamForBucket(String bucketName, String ossPath, String filePath) { +// +// // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。 +// InputStream inputStream = null; +// try { +// inputStream = new FileInputStream(filePath); +// } catch (FileNotFoundException e) { +// e.printStackTrace(); +// } +// // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。 +// uploadFileInputStreamForBucket(bucketName, ossPath, inputStream); +// return accessPre + ossPath; +// } +// +// public static void uploadFileInputStreamForBucket(String bucketName, String ossPath, InputStream inputStream) { +// ossClient.putObject(bucketName, ossPath, inputStream); +// } +// +// /** +// * 下载 +// * +// * @param ossFilePath +// * @param filePath +// */ +// public static void downloadFile(String ossFilePath, String filePath) { +// downloadFileForBucket(bucketName, ossFilePath, filePath); +// } +// +// /** +// * 下载 +// * +// * @param bucketName 实例名称 +// * @param ossFilePath oss存储路径 +// * @param filePath 本地文件路径 +// */ +// public static void downloadFileForBucket(String bucketName, String ossFilePath, String filePath) { +// ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(filePath)); +// } +// +// /** +// * @return +// */ +// public static String getOssDefaultPath() { +// LocalDateTime now = LocalDateTime.now(); +// String url = +// now.getYear() + "/" + +// now.getMonth() + "/" + +// now.getDayOfMonth() + "/" + +// now.getHour() + "/" + +// now.getMinute() + "/"; +// return url; +// } +// +// public static String getOssFilePath(String filePath) { +// String fileSuf = filePath.substring(filePath.indexOf(".") + 1); +// return getOssDefaultPath() + UUID.randomUUID().toString() + "." + fileSuf; +// } +// +//} +// +// +// +// +// +// diff --git a/cloud-etl-server/pom.xml b/cloud-etl-server/pom.xml index 8343b75..cbdc405 100644 --- a/cloud-etl-server/pom.xml +++ b/cloud-etl-server/pom.xml @@ -44,6 +44,31 @@ spring-boot-starter-actuator + + + + com.aliyun.oss + aliyun-sdk-oss + 3.17.4 + + + + javax.xml.bind + jaxb-api + 2.3.1 + + + javax.activation + activation + 1.1.1 + + + + org.glassfish.jaxb + jaxb-runtime + 2.3.3 + + com.mysql @@ -90,6 +115,7 @@ + diff --git a/cloud-etl-server/src/main/java/com/muyu/controller/EngIneController.java b/cloud-etl-server/src/main/java/com/muyu/controller/EngIneController.java index b5e9713..42c3af8 100644 --- a/cloud-etl-server/src/main/java/com/muyu/controller/EngIneController.java +++ b/cloud-etl-server/src/main/java/com/muyu/controller/EngIneController.java @@ -210,8 +210,7 @@ public class EngIneController extends BaseController { */ @PostMapping("/generate") public EngineVersion generate(@RequestBody EngineVersion engineVersion) { - EngineVersion byId1 = engineVersionService.getById(engineVersion.getId()); - EngineMaintenance byId = engIneService.selectById(byId1.getId()); + EngineMaintenance byId = engIneService.selectById(engineVersion.getId()); byId.setName("generate" + "_" + byId.getEngineCode() + "_" + engineVersion.getVersionCode()); engineVersion.setRuleContent(GenerateConstant.generateConstant(byId, engineVersion)); return engineVersion; diff --git a/cloud-etl-server/src/main/java/com/muyu/input/Demo.java b/cloud-etl-server/src/main/java/com/muyu/input/Demo.java new file mode 100644 index 0000000..882605a --- /dev/null +++ b/cloud-etl-server/src/main/java/com/muyu/input/Demo.java @@ -0,0 +1,55 @@ +package com.muyu.input; + +import com.aliyun.oss.ClientException; +import com.aliyun.oss.OSS; +import com.aliyun.oss.common.auth.*; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.OSSException; +import com.aliyun.oss.model.PutObjectRequest; +import com.aliyun.oss.model.PutObjectResult; +import java.io.FileInputStream; +import java.io.InputStream; + +public class Demo { + + public static void main(String[] args) throws Exception { + // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。 + String endpoint = "https://oss-cn-shanghai.aliyuncs.com"; + // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 + EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); + // 填写Bucket名称,例如examplebucket。 + String bucketName = "011811"; + // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。 + String objectName = "localpath\\examplefile.txt"; + // 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。 + // 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。 + String filePath= "C:\\Users\\qdm\\Desktop\\Project\\CLOUD-ETL-UI-ENGINE\\cloud-etl-engine\\cloud-etl-server\\target\\"; + + // 创建OSSClient实例。 + OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider); + + try { + InputStream inputStream = new FileInputStream(filePath); + // 创建PutObjectRequest对象。 + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream); + // 创建PutObject请求。 + PutObjectResult result = ossClient.putObject(putObjectRequest); + } catch (OSSException oe) { + System.out.println("Caught an OSSException, which means your request made it to OSS, " + + "but was rejected with an error response for some reason."); + System.out.println("Error Message:" + oe.getErrorMessage()); + System.out.println("Error Code:" + oe.getErrorCode()); + System.out.println("Request ID:" + oe.getRequestId()); + System.out.println("Host ID:" + oe.getHostId()); + } catch (ClientException ce) { + System.out.println("Caught an ClientException, which means the client encountered " + + "a serious internal problem while trying to communicate with OSS, " + + "such as not being able to access the network."); + System.out.println("Error Message:" + ce.getMessage()); + } finally { + if (ossClient != null) { + ossClient.shutdown(); + } + } + } +} diff --git a/cloud-etl-server/src/main/java/com/muyu/javacomplier/JavaCompilerDemo.java b/cloud-etl-server/src/main/java/com/muyu/javacomplier/JavaCompilerDemo.java new file mode 100644 index 0000000..3861201 --- /dev/null +++ b/cloud-etl-server/src/main/java/com/muyu/javacomplier/JavaCompilerDemo.java @@ -0,0 +1,53 @@ +package com.muyu.javacomplier; + +import lombok.extern.log4j.Log4j2; + +import javax.tools.*; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Arrays; +@Log4j2 +public class JavaCompilerDemo { + + public static void main(String[] args) { + String sourceCode = "public class HelloWorld { public static void main(String[] args) { System.out.println(\"Hello, world!\"); } }"; + String fileName = "HelloWorld.java"; + try { + // 将源代码写入文件 + writeToFile(fileName, sourceCode); + + // 获取Java编译器实例 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + + // 设置编译参数 + DiagnosticCollector diagnostics = new DiagnosticCollector<>(); + StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); + Iterable compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList(fileName)); + + // 执行编译 + JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits); + log.info("数据是:{}"+task); + boolean success = task.call(); + + // 处理编译结果 + if (success) { + System.out.println("编译成功"); + } else { + System.out.println("编译失败"); + diagnostics.getDiagnostics().forEach(d -> System.out.format("Line %d, Column %d: %s%n", d.getLineNumber(), d.getColumnNumber(), d.getMessage(null))); + } + + // 释放资源 + fileManager.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void writeToFile(String fileName, String sourceCode) throws IOException { + try (FileWriter writer = new FileWriter(fileName)) { + writer.write(sourceCode); + } + } +} diff --git a/cloud-etl-server/src/main/java/com/muyu/javacomplier/JavaStringCompiler.java b/cloud-etl-server/src/main/java/com/muyu/javacomplier/JavaStringCompiler.java new file mode 100644 index 0000000..c3fb494 --- /dev/null +++ b/cloud-etl-server/src/main/java/com/muyu/javacomplier/JavaStringCompiler.java @@ -0,0 +1,70 @@ +package com.itranswarp.compiler; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Map; + +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; +import javax.tools.JavaCompiler.CompilationTask; + +/** + * In-memory compile Java source code as String. + * + * @author michael + */ +public class JavaStringCompiler { + + JavaCompiler compiler; + StandardJavaFileManager stdManager; + + public JavaStringCompiler() { + this.compiler = ToolProvider.getSystemJavaCompiler(); + this.stdManager = compiler.getStandardFileManager(null, null, null); + } + + /** + * Compile a Java source file in memory. + * + * @param fileName + * Java file name, e.g. "Test.java" + * @param source + * The source code as String. + * @return The compiled results as Map that contains class name as key, + * class binary as value. + * @throws IOException + * If compile error. + */ + public Map compile(String fileName, String source) throws IOException { + try (MemoryJavaFileManager manager = new MemoryJavaFileManager(stdManager)) { + JavaFileObject javaFileObject = manager.makeStringSource(fileName, source); + CompilationTask task = compiler.getTask(null, manager, null, null, null, Arrays.asList(javaFileObject)); + Boolean result = task.call(); + if (result == null || !result.booleanValue()) { + throw new RuntimeException("Compilation failed."); + } + return manager.getClassBytes(); + } + } + + /** + * Load class from compiled classes. + * + * @param name + * Full class name. + * @param classBytes + * Compiled results as a Map. + * @return The Class instance. + * @throws ClassNotFoundException + * If class not found. + * @throws IOException + * If load error. + */ + public Class loadClass(String name, Map classBytes) throws ClassNotFoundException, IOException { + try (MemoryClassLoader classLoader = new MemoryClassLoader(classBytes)) { + return classLoader.loadClass(name); + } + } +} diff --git a/cloud-etl-server/src/main/java/com/muyu/javacomplier/MemoryClassLoader.java b/cloud-etl-server/src/main/java/com/muyu/javacomplier/MemoryClassLoader.java new file mode 100644 index 0000000..5ba63a0 --- /dev/null +++ b/cloud-etl-server/src/main/java/com/muyu/javacomplier/MemoryClassLoader.java @@ -0,0 +1,33 @@ +package com.muyu.javacomplier; + +import java.net.URL; +import java.net.URLClassLoader; +import java.util.HashMap; +import java.util.Map; + +/** + * Load class from byte[] which is compiled in memory. + * + * @author michael + */ +class MemoryClassLoader extends URLClassLoader { + + // class name to class bytes: + Map classBytes = new HashMap(); + + public MemoryClassLoader(Map classBytes) { + super(new URL[0], MemoryClassLoader.class.getClassLoader()); + this.classBytes.putAll(classBytes); + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + byte[] buf = classBytes.get(name); + if (buf == null) { + return super.findClass(name); + } + classBytes.remove(name); + return defineClass(name, buf, 0, buf.length); + } + +} diff --git a/cloud-etl-server/src/main/java/com/muyu/javacomplier/MemoryJavaFileManager.java b/cloud-etl-server/src/main/java/com/muyu/javacomplier/MemoryJavaFileManager.java new file mode 100644 index 0000000..107d4ff --- /dev/null +++ b/cloud-etl-server/src/main/java/com/muyu/javacomplier/MemoryJavaFileManager.java @@ -0,0 +1,96 @@ +package com.itranswarp.compiler; + +import java.io.ByteArrayOutputStream; +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.URI; +import java.nio.CharBuffer; +import java.util.HashMap; +import java.util.Map; + +import javax.tools.FileObject; +import javax.tools.ForwardingJavaFileManager; +import javax.tools.JavaFileManager; +import javax.tools.JavaFileObject; +import javax.tools.JavaFileObject.Kind; +import javax.tools.SimpleJavaFileObject; + +/** + * In-memory java file manager. + * + * @author michael + */ +class MemoryJavaFileManager extends ForwardingJavaFileManager { + + // compiled classes in bytes: + final Map classBytes = new HashMap(); + + MemoryJavaFileManager(JavaFileManager fileManager) { + super(fileManager); + } + + public Map getClassBytes() { + return new HashMap(this.classBytes); + } + + @Override + public void flush() throws IOException { + } + + @Override + public void close() throws IOException { + classBytes.clear(); + } + + @Override + public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, Kind kind, + FileObject sibling) throws IOException { + if (kind == Kind.CLASS) { + return new MemoryOutputJavaFileObject(className); + } else { + return super.getJavaFileForOutput(location, className, kind, sibling); + } + } + + JavaFileObject makeStringSource(String name, String code) { + return new MemoryInputJavaFileObject(name, code); + } + + static class MemoryInputJavaFileObject extends SimpleJavaFileObject { + + final String code; + + MemoryInputJavaFileObject(String name, String code) { + super(URI.create("string:///" + name), Kind.SOURCE); + this.code = code; + } + + @Override + public CharBuffer getCharContent(boolean ignoreEncodingErrors) { + return CharBuffer.wrap(code); + } + } + + class MemoryOutputJavaFileObject extends SimpleJavaFileObject { + final String name; + + MemoryOutputJavaFileObject(String name) { + super(URI.create("string:///" + name), Kind.CLASS); + this.name = name; + } + + @Override + public OutputStream openOutputStream() { + return new FilterOutputStream(new ByteArrayOutputStream()) { + @Override + public void close() throws IOException { + out.close(); + ByteArrayOutputStream bos = (ByteArrayOutputStream) out; + classBytes.put(name, bos.toByteArray()); + } + }; + } + + } +} diff --git a/cloud-etl-server/src/main/java/com/muyu/javacomplier/OssDownload.java b/cloud-etl-server/src/main/java/com/muyu/javacomplier/OssDownload.java new file mode 100644 index 0000000..89557d3 --- /dev/null +++ b/cloud-etl-server/src/main/java/com/muyu/javacomplier/OssDownload.java @@ -0,0 +1,48 @@ +package com.muyu.cloud.rule.utils.upload; + +import com.aliyun.oss.OSSClient; +import com.aliyun.oss.model.GetObjectRequest; +import com.aliyun.oss.model.ObjectMetadata; +import com.muyu.common.core.domain.Result; + +import java.io.File; + + +public class OssDownload { + + // 阿里云 endpoint + private static final String endpoint = "oss-cn-wulanchabu.aliyuncs.com"; + // 阿里云 accessKeyId + private static final String accessKeyId = "LTAI5t8mk6aWqDq5w6WA49et"; + // 阿里云 accessKeySecret   + private static final String accessKeySecret = "SuLwzfEOruGOs1RSCq4vA1Bcf1849R"; + // bucket + private static final String bucketName = "oss-1209"; + // OSS文件路径 + private static final String filePath = "build/rule/version/"; + + 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, filePath+objectName); + if (!flag_file) { + System.out.println("预下载文件不存在"); + return Result.error("预下载文件不存在"); + } + // 本地文件下载路径 + 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); + } + +} diff --git a/pom.xml b/pom.xml index 66816b4..3c57c66 100644 --- a/pom.xml +++ b/pom.xml @@ -22,8 +22,8 @@ cloud-etl-server - 1.8 - 1.8 + 17 + 17 true