|
@ -2,7 +2,10 @@
|
|
|
|
|
|
package com.yihu.hos.common.compiler;
|
|
package com.yihu.hos.common.compiler;
|
|
|
|
|
|
|
|
import com.yihu.hos.common.constants.BrokerConstant;
|
|
import com.yihu.hos.core.file.FileUtil;
|
|
import com.yihu.hos.core.file.FileUtil;
|
|
|
|
import com.yihu.hos.models.SystemClassMapping;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
|
|
import javax.tools.*;
|
|
import javax.tools.*;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
@ -27,20 +30,18 @@ public class CamelCompiler {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 编译java文件
|
|
* 编译java文件
|
|
* @param packageName java包路径
|
|
|
|
* @param oldClassName 旧java文件名
|
|
|
|
* @param newClassName 新java文件名
|
|
|
|
* @param newCron 新cron表达式
|
|
|
|
|
|
* @param params java包路径
|
|
* @throws IOException
|
|
* @throws IOException
|
|
*/
|
|
*/
|
|
public static String compiler(String filePath,String packageName,String oldClassName,String newClassName,String newCron) throws IOException {
|
|
|
|
|
|
public static String compiler(ClassParams params) throws IOException {
|
|
String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
|
|
String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
|
|
|
|
String toClassPath = params.getFilePath().replace(".java",".class");
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
// 建立DiagnosticCollector对象
|
|
// 建立DiagnosticCollector对象
|
|
DiagnosticCollector diagnostics = new DiagnosticCollector();
|
|
DiagnosticCollector diagnostics = new DiagnosticCollector();
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, Charset.forName("UTF-8"));
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, Charset.forName("UTF-8"));
|
|
// 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
|
|
// 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
|
|
File file = genNewJava(filePath,packageName, oldClassName,newClassName, newCron);
|
|
|
|
|
|
File file = genNewJava(params);
|
|
if (file!=null){
|
|
if (file!=null){
|
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file.getAbsolutePath());
|
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file.getAbsolutePath());
|
|
// options命令行选项
|
|
// options命令行选项
|
|
@ -52,95 +53,96 @@ public class CamelCompiler {
|
|
fileManager.close();
|
|
fileManager.close();
|
|
System.out.println((success) ? "编译成功" : "编译失败");
|
|
System.out.println((success) ? "编译成功" : "编译失败");
|
|
if (!success){
|
|
if (!success){
|
|
|
|
//错误信息打印
|
|
List diagnostics1 = diagnostics.getDiagnostics();
|
|
List diagnostics1 = diagnostics.getDiagnostics();
|
|
for (int i=0;i<diagnostics1.size();i++){
|
|
for (int i=0;i<diagnostics1.size();i++){
|
|
System.out.println(diagnostics1.get(i).toString());
|
|
System.out.println(diagnostics1.get(i).toString());
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}else {
|
|
}else {
|
|
|
|
//添加加载类
|
|
|
|
String dotPackageName = params.getPackageName().replace("/",".");
|
|
|
|
SystemClassMapping.getSystemClassNameMapping().put(params.getRouteId() + BrokerConstant.ROUTE + params.getNewClassName(),dotPackageName + params.getNewClassName());
|
|
String className = file.getName().replace(".java", ".class");
|
|
String className = file.getName().replace(".java", ".class");
|
|
classPath = classPath.substring(1);
|
|
classPath = classPath.substring(1);
|
|
return classPath + packageName + className;
|
|
|
|
|
|
String oldClassPath = classPath + params.getPackageName() + className;
|
|
|
|
File oldClassFile = new File(oldClassPath);
|
|
|
|
FileUtils.copyFile(oldClassFile,new File(toClassPath));
|
|
|
|
return toClassPath;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String copyProcess (String filePath,String packageName,String oldClassName) throws IOException {
|
|
|
|
|
|
public static String copyProcess (String routeId ,String filePath,String packageName,String oldClassName) throws IOException {
|
|
String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
|
|
String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
|
|
String newPath = String.format(classPathTemplate, packageName, oldClassName);
|
|
|
|
|
|
|
|
|
|
String toClassPath = filePath.replace(".java",".class");
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
// 建立DiagnosticCollector对象
|
|
// 建立DiagnosticCollector对象
|
|
DiagnosticCollector diagnostics = new DiagnosticCollector();
|
|
DiagnosticCollector diagnostics = new DiagnosticCollector();
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, Charset.forName("UTF-8"));
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, Charset.forName("UTF-8"));
|
|
// 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
|
|
// 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
|
|
// File file = new File(filePath);
|
|
|
|
// File toFIle = new File(newPath);
|
|
|
|
|
|
|
|
String text = FileUtil.readFileText(new File(filePath));
|
|
|
|
File fPath = new File(packagePathTemplate+packageName);
|
|
File fPath = new File(packagePathTemplate+packageName);
|
|
if (!fPath.exists()) fPath.mkdirs();
|
|
if (!fPath.exists()) fPath.mkdirs();
|
|
|
|
|
|
File toFIle = genProcessor(filePath,packageName,oldClassName);
|
|
File toFIle = genProcessor(filePath,packageName,oldClassName);
|
|
if (toFIle.exists()){
|
|
if (toFIle.exists()){
|
|
if (!toFIle.getParentFile().exists()) toFIle.getParentFile().mkdirs();
|
|
|
|
//复制
|
|
|
|
// FileUtils.copyFile(file, toFIle);
|
|
|
|
if (toFIle!=null){
|
|
|
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(toFIle.getAbsolutePath());
|
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(toFIle.getAbsolutePath());
|
|
// options命令行选项
|
|
// options命令行选项
|
|
Iterable<String> options = Arrays.asList("-d",classPath,"-sourcepath", classPath);// 指定的路径一定要存在,javac不会自己创建文件夹
|
|
Iterable<String> options = Arrays.asList("-d",classPath,"-sourcepath", classPath);// 指定的路径一定要存在,javac不会自己创建文件夹
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
|
|
|
|
|
|
// 编译源程序
|
|
// 编译源程序
|
|
boolean success = task.call();
|
|
boolean success = task.call();
|
|
fileManager.close();
|
|
|
|
System.out.println((success) ? "编译成功" : "编译失败");
|
|
|
|
if (!success){
|
|
|
|
|
|
System.out.println((success) ? oldClassName+"编译成功" : "编译失败");
|
|
|
|
fileManager.close();
|
|
|
|
if (!success){
|
|
List diagnostics1 = diagnostics.getDiagnostics();
|
|
List diagnostics1 = diagnostics.getDiagnostics();
|
|
for (int i=0;i<diagnostics1.size();i++){
|
|
for (int i=0;i<diagnostics1.size();i++){
|
|
System.out.println(diagnostics1.get(i).toString());
|
|
System.out.println(diagnostics1.get(i).toString());
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}else {
|
|
}else {
|
|
|
|
String dotPackageName = packageName.replace("/", ".");
|
|
|
|
SystemClassMapping.getSystemClassNameMapping().put(routeId+ BrokerConstant.PROCESSOR + oldClassName,dotPackageName + oldClassName);
|
|
String resultPath = toFIle.getName().replace(".java", ".class");
|
|
String resultPath = toFIle.getName().replace(".java", ".class");
|
|
classPath = classPath.substring(1);
|
|
classPath = classPath.substring(1);
|
|
return classPath + packageName + resultPath;
|
|
|
|
|
|
String oldClassPath = classPath + packageName + resultPath;
|
|
|
|
File classFile = new File(oldClassPath);
|
|
|
|
FileUtils.copyFile(classFile,new File(toClassPath));
|
|
|
|
return toClassPath;
|
|
}
|
|
}
|
|
}else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
System.out.println("生成processor的java文件失败");
|
|
return null;
|
|
return null;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 修改cron表达式,生成新java文件
|
|
* 修改cron表达式,生成新java文件
|
|
* @param packageName 包名
|
|
|
|
* @param oldClassName 旧类名
|
|
|
|
* @param newClassName 新类名
|
|
|
|
* @param newContent 新cron表达式
|
|
|
|
|
|
* @param params 生成camel的参数
|
|
*/
|
|
*/
|
|
public static File genNewJava(String filePath,String packageName, String oldClassName,String newClassName, String newContent) {
|
|
|
|
|
|
public static File genNewJava(ClassParams params) {
|
|
try {
|
|
try {
|
|
// String oldPath = String.format(classPathTemplate, packageName, oldClassName);
|
|
// String oldPath = String.format(classPathTemplate, packageName, oldClassName);
|
|
String newPath = String.format(classPathTemplate, packageName, newClassName);
|
|
|
|
|
|
String newPath = String.format(classPathTemplate, params.getPackageName(), params.getOldClassName()+params.getRouteId());
|
|
|
|
|
|
String text = FileUtil.readFileText(new File(filePath));
|
|
|
|
|
|
String text = FileUtil.readFileText(new File(params.getFilePath()));
|
|
if (text.contains("?cron=")){
|
|
if (text.contains("?cron=")){
|
|
String oldStr = text.substring(text.indexOf("?cron=")+6);
|
|
String oldStr = text.substring(text.indexOf("?cron=")+6);
|
|
String cron = oldStr.substring(0,oldStr.indexOf("\""));
|
|
String cron = oldStr.substring(0,oldStr.indexOf("\""));
|
|
text = text.replace(cron,newContent);
|
|
|
|
|
|
text = text.replace(cron,params.getCron());
|
|
}
|
|
}
|
|
|
|
|
|
if (text.contains(oldClassName)){
|
|
|
|
text = text.replace(oldClassName,newClassName);
|
|
|
|
|
|
if (text.contains(params.getOldClassName())){
|
|
|
|
text = text.replace(params.getOldClassName(), params.getNewClassName());
|
|
}
|
|
}
|
|
|
|
//修改routeId;模板规则 routeId("routeId")
|
|
|
|
text = text.replace("routeId(\"routeId\")","routeId(\""+params.getRouteId() +"\")" );
|
|
|
|
|
|
File fPath = new File(packagePathTemplate+packageName);
|
|
|
|
|
|
File fPath = new File(packagePathTemplate+params.getPackageName());
|
|
if (!fPath.exists()) fPath.mkdirs();
|
|
if (!fPath.exists()) fPath.mkdirs();
|
|
|
|
|
|
File f = new File(newPath);
|
|
File f = new File(newPath);
|
|
@ -160,9 +162,7 @@ public class CamelCompiler {
|
|
|
|
|
|
public static File genProcessor(String filePath,String packageName,String newClassName) {
|
|
public static File genProcessor(String filePath,String packageName,String newClassName) {
|
|
try {
|
|
try {
|
|
// String oldPath = String.format(classPathTemplate, packageName, oldClassName);
|
|
|
|
String newPath = String.format(classPathTemplate, packageName, newClassName);
|
|
String newPath = String.format(classPathTemplate, packageName, newClassName);
|
|
|
|
|
|
String text = FileUtil.readFileText(new File(filePath));
|
|
String text = FileUtil.readFileText(new File(filePath));
|
|
|
|
|
|
File fPath = new File(packagePathTemplate+packageName);
|
|
File fPath = new File(packagePathTemplate+packageName);
|
|
@ -183,110 +183,4 @@ public class CamelCompiler {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void compiler2(String packageName,String className) throws IOException {
|
|
|
|
String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
|
|
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
|
// 建立DiagnosticCollector对象
|
|
|
|
DiagnosticCollector diagnostics = new DiagnosticCollector();
|
|
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
|
|
|
|
// 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
|
|
|
|
Iterable compilationUnits = fileManager.getJavaFileObjects(String.format(classPathTemplate, packageName, className));
|
|
|
|
// options命令行选项
|
|
|
|
// 指定的路径一定要存在,javac不会自己创建文件夹
|
|
|
|
Iterable<String> options = Arrays.asList( "-d", classPath);
|
|
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
|
|
|
|
// 编译源程序
|
|
|
|
boolean success = task.call();
|
|
|
|
if (!success){
|
|
|
|
List diagnostics1 = diagnostics.getDiagnostics();
|
|
|
|
for (int i=0;i<diagnostics1.size();i++){
|
|
|
|
System.out.println(diagnostics1.get(i).toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fileManager.close();
|
|
|
|
System.out.println((success) ? "编译成功" : "编译失败");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String compiler3(String filePath,String packageName,String oldClassName,String newClassName,String newCron) throws IOException {
|
|
|
|
String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
|
|
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
|
// 建立DiagnosticCollector对象
|
|
|
|
DiagnosticCollector diagnostics = new DiagnosticCollector();
|
|
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
|
|
|
|
// 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
|
|
|
|
File file = genNewJava(filePath,packageName, oldClassName,newClassName, newCron);
|
|
|
|
if (file!=null){
|
|
|
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file.getAbsolutePath());
|
|
|
|
// options命令行选项
|
|
|
|
Iterable<String> options = Arrays.asList("-d",classPath);// 指定的路径一定要存在,javac不会自己创建文件夹
|
|
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
|
|
|
|
|
|
|
|
fileManager.close();
|
|
|
|
// 编译源程序
|
|
|
|
boolean success = task.call();
|
|
|
|
System.out.println((success) ? "编译成功" : "编译失败");
|
|
|
|
if (!success){
|
|
|
|
List diagnostics1 = diagnostics.getDiagnostics();
|
|
|
|
for (int i=0;i<diagnostics1.size();i++){
|
|
|
|
System.out.println(diagnostics1.get(i).toString());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}else {
|
|
|
|
String className = file.getName().replace(".java", ".class");
|
|
|
|
return classPath + packageName + className;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void copyCompiler (String filePath,String packageName,String oldClassName) throws IOException {
|
|
|
|
String classPath = "D:\\" ;
|
|
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
|
// 建立DiagnosticCollector对象
|
|
|
|
DiagnosticCollector diagnostics = new DiagnosticCollector();
|
|
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
|
|
|
|
// 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
|
|
|
|
File file = new File(filePath);
|
|
|
|
if (file.exists()){
|
|
|
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file.getAbsolutePath());
|
|
|
|
// options命令行选项
|
|
|
|
Iterable<String> options = Arrays.asList("-d",classPath);// 指定的路径一定要存在,javac不会自己创建文件夹
|
|
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
|
|
|
|
// 编译源程序
|
|
|
|
boolean success = task.call();
|
|
|
|
if (!success){
|
|
|
|
List diagnostics1 = diagnostics.getDiagnostics();
|
|
|
|
for (int i=0;i<diagnostics1.size();i++){
|
|
|
|
System.out.println(diagnostics1.get(i).toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fileManager.close();
|
|
|
|
System.out.println((success) ? "编译成功" : "编译失败");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
try {
|
|
|
|
// copyCompiler("E:\\crawler\\processor\\Processor0.java", "/crawler/processor", "Processor0");
|
|
|
|
// copyCompiler("E:\\crawler\\route\\QuartzRoute.java","/crawler/route", "QuartzRoute");
|
|
|
|
// copyProcess("E:\\crawler\\processor\\Processor0.java", "/crawler/processor", "Processor0");
|
|
|
|
|
|
|
|
String classPath= compiler3("E:\\crawler\\route\\QuartzRoute.java","/crawler/route", "QuartzRoute","QuartzRoute1479","textxtxt");
|
|
|
|
String text = FileUtil.readFileText(new File(classPath));
|
|
|
|
System.out.println(text);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|