|  | @ -3,6 +3,7 @@
 | 
	
		
			
				|  |  | package com.yihu.hos.common.compiler;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.yihu.hos.core.file.FileUtil;
 | 
	
		
			
				|  |  | import org.apache.commons.io.FileUtils;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import javax.tools.*;
 | 
	
		
			
				|  |  | import java.io.File;
 | 
	
	
		
			
				|  | @ -32,14 +33,15 @@ public class CamelCompiler {
 | 
	
		
			
				|  |  |      * @param newCron         新cron表达式
 | 
	
		
			
				|  |  |      * @throws IOException
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static void compiler(String packageName,String oldClassName,String newClassName,String newCron) throws IOException {
 | 
	
		
			
				|  |  |     public static String compiler(String filePath,String packageName,String oldClassName,String newClassName,String newCron) throws IOException {
 | 
	
		
			
				|  |  |         String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
 | 
	
		
			
				|  |  |         classPath = classPath.substring(1);
 | 
	
		
			
				|  |  |         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
 | 
	
		
			
				|  |  |         // 建立DiagnosticCollector对象
 | 
	
		
			
				|  |  |         DiagnosticCollector diagnostics = new DiagnosticCollector();
 | 
	
		
			
				|  |  |         StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
 | 
	
		
			
				|  |  |         // 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
 | 
	
		
			
				|  |  |         File file = genNewJava(packageName, oldClassName,newClassName, newCron);
 | 
	
		
			
				|  |  |         File file = genNewJava(filePath,packageName, oldClassName,newClassName, newCron);
 | 
	
		
			
				|  |  |         if (file!=null){
 | 
	
		
			
				|  |  |             Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file.getAbsolutePath());
 | 
	
		
			
				|  |  |             // options命令行选项
 | 
	
	
		
			
				|  | @ -48,19 +50,66 @@ public class CamelCompiler {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             // 编译源程序
 | 
	
		
			
				|  |  |             boolean success = task.call();
 | 
	
		
			
				|  |  |             fileManager.close();
 | 
	
		
			
				|  |  |             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;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             fileManager.close();
 | 
	
		
			
				|  |  |             System.out.println((success) ? "编译成功" : "编译失败");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  |     public static String copyProcess (String filePath,String packageName,String oldClassName) throws IOException {
 | 
	
		
			
				|  |  |         String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
 | 
	
		
			
				|  |  |         classPath = classPath.substring(1);
 | 
	
		
			
				|  |  |         String newPath = String.format(classPathTemplate, packageName, oldClassName);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
 | 
	
		
			
				|  |  |         // 建立DiagnosticCollector对象
 | 
	
		
			
				|  |  |         DiagnosticCollector diagnostics = new DiagnosticCollector();
 | 
	
		
			
				|  |  |         StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
 | 
	
		
			
				|  |  |         // 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
 | 
	
		
			
				|  |  |         File file = new File(filePath);
 | 
	
		
			
				|  |  |         File toFIle = new File(newPath);
 | 
	
		
			
				|  |  |         if (file.exists()){
 | 
	
		
			
				|  |  |             if (!toFIle.getParentFile().exists()) toFIle.getParentFile().mkdirs();
 | 
	
		
			
				|  |  |             //复制
 | 
	
		
			
				|  |  |             FileUtils.copyFile(file, toFIle);
 | 
	
		
			
				|  |  |             if (toFIle!=null){
 | 
	
		
			
				|  |  |                 Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(toFIle.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();
 | 
	
		
			
				|  |  |                 fileManager.close();
 | 
	
		
			
				|  |  |                 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 resultPath = toFIle.getName().replace(".java", ".class");
 | 
	
		
			
				|  |  |                     return classPath + packageName  + resultPath;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }else {
 | 
	
		
			
				|  |  |                 return null;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      *  修改cron表达式,生成新java文件
 | 
	
	
		
			
				|  | @ -69,12 +118,12 @@ public class CamelCompiler {
 | 
	
		
			
				|  |  |      * @param newClassName  新类名
 | 
	
		
			
				|  |  |      * @param newContent    新cron表达式
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static File genNewJava(String packageName, String oldClassName,String newClassName, String newContent) {
 | 
	
		
			
				|  |  |     public static File genNewJava(String filePath,String packageName, String oldClassName,String newClassName, String newContent) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             String oldPath = String.format(classPathTemplate, packageName, oldClassName);
 | 
	
		
			
				|  |  | //            String oldPath = String.format(classPathTemplate, packageName, oldClassName);
 | 
	
		
			
				|  |  |             String newPath = String.format(classPathTemplate, packageName, newClassName);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             String text = FileUtil.readFileText(new File(oldPath));
 | 
	
		
			
				|  |  |             String text = FileUtil.readFileText(new File(filePath));
 | 
	
		
			
				|  |  |             if (text.contains("?cron=")){
 | 
	
		
			
				|  |  |                 String oldStr = text.substring(text.indexOf("?cron=")+6);
 | 
	
		
			
				|  |  |                 String cron = oldStr.substring(0,oldStr.indexOf("\""));
 | 
	
	
		
			
				|  | @ -85,7 +134,11 @@ public class CamelCompiler {
 | 
	
		
			
				|  |  |                 text = text.replace(oldClassName,newClassName);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             File fPath = new File(packagePathTemplate+packageName);
 | 
	
		
			
				|  |  |             if (!fPath.exists()) fPath.mkdirs();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             File f = new File(newPath);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             FileWriter fw = new FileWriter(f);
 | 
	
		
			
				|  |  |             fw.write(text);
 | 
	
		
			
				|  |  |             fw.flush();
 | 
	
	
		
			
				|  | @ -125,13 +178,81 @@ public class CamelCompiler {
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //    public static void main(String[] args) {
 | 
	
		
			
				|  |  | //        try {
 | 
	
		
			
				|  |  | //            compiler("/crawler/route", "QuartzRoute","QuartzRoute001","xx000xx");
 | 
	
		
			
				|  |  | //        } catch (IOException e) {
 | 
	
		
			
				|  |  | //            e.printStackTrace();
 | 
	
		
			
				|  |  | //        }
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //    }
 | 
	
		
			
				|  |  |     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();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |