|
@ -16,7 +16,8 @@ import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* java编译工具类
|
|
|
* java编译工具类
|
|
|
*
|
|
|
* @author HZY
|
|
|
* @vsrsion 1.0
|
|
|
* Created at 2016/11/17.
|
|
@ -29,90 +30,98 @@ public class CamelCompiler {
|
|
|
+ "/hos-broker/src/main/java/%s/%s.java";
|
|
|
|
|
|
/**
|
|
|
* 编译java文件
|
|
|
* @param params java包路径
|
|
|
* 编译java模板文件,生成class
|
|
|
*
|
|
|
* @param params 参数对象
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public static String compiler(ClassParams params) throws IOException {
|
|
|
String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
|
|
|
String toClassPath = params.getFilePath().replace(".java",".class");
|
|
|
public static String genRouteClass(ClassParams params) throws IOException {
|
|
|
String targetPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath();//项目class根目录
|
|
|
String copyClassPath = params.getFilePath().replace(".java", ".class");//数据库保存的class路径
|
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
// 建立DiagnosticCollector对象
|
|
|
DiagnosticCollector diagnostics = new DiagnosticCollector();
|
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, Charset.forName("UTF-8"));
|
|
|
// 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
|
|
|
File file = genNewJava(params);
|
|
|
if (file!=null){
|
|
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file.getAbsolutePath());
|
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, Charset.forName("UTF-8"));
|
|
|
// 建立源文件对象,根据java模板文件生成要加载的java类
|
|
|
File loadJavaFile = genRouteJavaFile(params);
|
|
|
if (loadJavaFile != null) {
|
|
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(loadJavaFile.getAbsolutePath());
|
|
|
// options命令行选项
|
|
|
Iterable<String> options = Arrays.asList("-d",classPath,"-sourcepath", classPath);// 指定的路径一定要存在,javac不会自己创建文件夹
|
|
|
Iterable<String> options = Arrays.asList("-d", targetPath, "-sourcepath", targetPath);// 指定的路径一定要存在,javac不会自己创建文件夹
|
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
|
|
|
|
|
|
// 编译源程序
|
|
|
boolean success = task.call();
|
|
|
fileManager.close();
|
|
|
System.out.println((success) ? "编译成功" : "编译失败");
|
|
|
if (!success){
|
|
|
if (!success) {
|
|
|
//错误信息打印
|
|
|
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());
|
|
|
}
|
|
|
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");
|
|
|
classPath = classPath.substring(1);
|
|
|
String oldClassPath = classPath + params.getPackageName() + className;
|
|
|
File oldClassFile = new File(oldClassPath);
|
|
|
FileUtils.copyFile(oldClassFile,new File(toClassPath));
|
|
|
return toClassPath;
|
|
|
String dotPackageName = params.getPackageName().replace("/", "."); //将带“/"的包名转为”.";
|
|
|
SystemClassMapping.getSystemClassNameMapping().put(params.getRouteId() + BrokerConstant.ROUTE + params.getNewClassName(), dotPackageName + params.getNewClassName());
|
|
|
String loadClassName = loadJavaFile.getName().replace(".java", ".class");
|
|
|
targetPath = targetPath.substring(1);
|
|
|
String loadClassPath = targetPath + params.getPackageName() + loadClassName;//加载的class路径
|
|
|
FileUtils.copyFile(new File(loadClassPath), new File(copyClassPath));
|
|
|
return copyClassPath;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static String copyProcess (String routeId ,String filePath,String packageName,String oldClassName) throws IOException {
|
|
|
String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
|
|
|
String toClassPath = filePath.replace(".java",".class");
|
|
|
/**
|
|
|
* 根据java模板生成新的class文件
|
|
|
*
|
|
|
* @param routeId
|
|
|
* @param filePath java模板路径
|
|
|
* @param packageName java模板包名
|
|
|
* @param className java模板类名
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public static String genProcessClass(String routeId, String filePath, String packageName, String className) throws IOException {
|
|
|
String targetPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath();//项目class根目录
|
|
|
String copyClassPath = filePath.replace(".java", ".class");//管理端 数据库保存的class路径
|
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
// 建立DiagnosticCollector对象
|
|
|
DiagnosticCollector diagnostics = new DiagnosticCollector();
|
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, Charset.forName("UTF-8"));
|
|
|
// 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
|
|
|
|
|
|
File fPath = new File(packagePathTemplate+packageName);
|
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, Charset.forName("UTF-8"));
|
|
|
// 建立源文件对象,创建父文件夹
|
|
|
File fPath = new File(packagePathTemplate + packageName);
|
|
|
if (!fPath.exists()) fPath.mkdirs();
|
|
|
|
|
|
File toFIle = genProcessor(filePath,packageName,oldClassName);
|
|
|
if (toFIle.exists()){
|
|
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(toFIle.getAbsolutePath());
|
|
|
// options命令行选项
|
|
|
Iterable<String> options = Arrays.asList("-d",classPath,"-sourcepath", classPath);// 指定的路径一定要存在,javac不会自己创建文件夹
|
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
|
|
|
// 编译源程序
|
|
|
boolean success = task.call();
|
|
|
System.out.println((success) ? oldClassName+"编译成功" : "编译失败");
|
|
|
File loadFIle = genProcessorJavaFile(filePath, packageName, className);
|
|
|
if (loadFIle.exists()) {
|
|
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(loadFIle.getAbsolutePath());
|
|
|
// options命令行选项
|
|
|
Iterable<String> options = Arrays.asList("-d", targetPath, "-sourcepath", targetPath);// 指定的路径一定要存在,javac不会自己创建文件夹
|
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
|
|
|
// 编译源程序
|
|
|
boolean success = task.call();
|
|
|
System.out.println((success) ? className + "编译成功" : className + "编译失败");
|
|
|
fileManager.close();
|
|
|
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 dotPackageName = packageName.replace("/", ".");
|
|
|
SystemClassMapping.getSystemClassNameMapping().put(routeId+ BrokerConstant.PROCESSOR + oldClassName,dotPackageName + oldClassName);
|
|
|
String resultPath = toFIle.getName().replace(".java", ".class");
|
|
|
classPath = classPath.substring(1);
|
|
|
String oldClassPath = classPath + packageName + resultPath;
|
|
|
File classFile = new File(oldClassPath);
|
|
|
FileUtils.copyFile(classFile,new File(toClassPath));
|
|
|
return toClassPath;
|
|
|
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 dotPackageName = packageName.replace("/", ".");//将带“/"的包名转为”.";
|
|
|
SystemClassMapping.getSystemClassNameMapping().put(routeId + BrokerConstant.PROCESSOR + className, dotPackageName + className);
|
|
|
String loadPath = loadFIle.getName().replace(".java", ".class");
|
|
|
targetPath = targetPath.substring(1);
|
|
|
String loadClassPath = targetPath + packageName + loadPath;//要加载的class路径
|
|
|
FileUtils.copyFile(new File(loadClassPath), new File(copyClassPath));
|
|
|
return copyClassPath;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
System.out.println("生成processor的java文件失败");
|
|
@ -121,28 +130,30 @@ public class CamelCompiler {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改cron表达式,生成新java文件
|
|
|
* @param params 生成camel的参数
|
|
|
* 修改cron表达式,生成新java文件
|
|
|
*
|
|
|
* @param params 生成camel的参数
|
|
|
*/
|
|
|
public static File genNewJava(ClassParams params) {
|
|
|
public static File genRouteJavaFile(ClassParams params) {
|
|
|
try {
|
|
|
// String oldPath = String.format(classPathTemplate, packageName, oldClassName);
|
|
|
String newPath = String.format(classPathTemplate, params.getPackageName(), params.getOldClassName()+params.getRouteId());
|
|
|
String newPath = String.format(classPathTemplate, params.getPackageName(), params.getOldClassName() + params.getRouteId());
|
|
|
|
|
|
String text = FileUtil.readFileText(new File(params.getFilePath()));
|
|
|
if (text.contains("?cron=")){
|
|
|
String oldStr = text.substring(text.indexOf("?cron=")+6);
|
|
|
String cron = oldStr.substring(0,oldStr.indexOf("\""));
|
|
|
text = text.replace(cron,params.getCron());
|
|
|
if (text.contains("?cron=")) {
|
|
|
String oldStr = text.substring(text.indexOf("?cron=") + 6);
|
|
|
String cron = oldStr.substring(0, oldStr.indexOf("\""));
|
|
|
text = text.replace(cron, params.getCron());
|
|
|
}
|
|
|
|
|
|
if (text.contains(params.getOldClassName())){
|
|
|
//修改java类名
|
|
|
if (text.contains(params.getOldClassName())) {
|
|
|
text = text.replace(params.getOldClassName(), params.getNewClassName());
|
|
|
}
|
|
|
//修改routeId;模板规则 routeId("routeId")
|
|
|
text = text.replace("routeId(\"routeId\")","routeId(\""+params.getRouteId() +"\")" );
|
|
|
text = text.replace("routeId(\"routeId\")", "routeId(\"" + params.getRouteId() + "\")");
|
|
|
|
|
|
File fPath = new File(packagePathTemplate+params.getPackageName());
|
|
|
File fPath = new File(packagePathTemplate + params.getPackageName());
|
|
|
if (!fPath.exists()) fPath.mkdirs();
|
|
|
|
|
|
File f = new File(newPath);
|
|
@ -151,21 +162,20 @@ public class CamelCompiler {
|
|
|
fw.flush();
|
|
|
fw.close();//这里只是产生一个JAVA文件,简单的IO操作
|
|
|
return f;
|
|
|
}
|
|
|
catch (Exception e) {
|
|
|
System.out.println("修改操作出错");
|
|
|
} catch (Exception e) {
|
|
|
System.out.println("修改Route文件操作出错");
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static File genProcessor(String filePath,String packageName,String newClassName) {
|
|
|
public static File genProcessorJavaFile(String filePath, String packageName, String newClassName) {
|
|
|
try {
|
|
|
String newPath = String.format(classPathTemplate, packageName, newClassName);
|
|
|
String text = FileUtil.readFileText(new File(filePath));
|
|
|
|
|
|
File fPath = new File(packagePathTemplate+packageName);
|
|
|
File fPath = new File(packagePathTemplate + packageName);
|
|
|
if (!fPath.exists()) fPath.mkdirs();
|
|
|
|
|
|
File f = new File(newPath);
|
|
@ -174,9 +184,8 @@ public class CamelCompiler {
|
|
|
fw.flush();
|
|
|
fw.close();//这里只是产生一个JAVA文件,简单的IO操作
|
|
|
return f;
|
|
|
}
|
|
|
catch (Exception e) {
|
|
|
System.out.println("复制文件操作出错");
|
|
|
} catch (Exception e) {
|
|
|
System.out.println("撑撑processor文件操作出错");
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null;
|