Forráskód Böngészése

tomcat上java动态编译bug修复

huangzhiyong 8 éve
szülő
commit
036038a9f9

+ 24 - 21
hos-broker/src/main/java/com/yihu/hos/broker/services/camel/CamelCompiler.java

@ -4,6 +4,7 @@ import com.yihu.hos.core.log.Logger;
import com.yihu.hos.core.log.LoggerFactory;
import javax.tools.*;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
@ -20,31 +21,33 @@ public class CamelCompiler {
    public static boolean compile(String sourcePath, String targetPath) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        boolean success =false;
        boolean success = false;
        try {
        // 建立DiagnosticCollector对象
        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, Charset.forName("UTF-8"));
        // 建立源文件对象,根据java模板文件生成要加载的java类
        Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(sourcePath);
        Iterable<String> options = Arrays.asList("-d", targetPath, "-sourcepath", targetPath , "-target","1.8");// 指定的路径一定要存在,javac不会自己创建文件夹
        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
        // 编译源程序
         success = task.call();
        fileManager.close();
        if (!success) {
            //错误信息打印
            List diagnosticList = diagnostics.getDiagnostics();
            for (Object aDiagnosticList : diagnosticList) {
                System.out.println(aDiagnosticList.toString());
                logger.error(aDiagnosticList.toString());
            // 建立DiagnosticCollector对象
            DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
            StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, Charset.forName("UTF-8"));
            // 建立源文件对象,根据java模板文件生成要加载的java类
            Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(sourcePath);
            String classPath = CamelCompiler.class.getResource("/").getPath();
            String libPath = new File(classPath).getParent() + File.separator + "lib";//指定需要导入的包路径
            Iterable<String> options = Arrays.asList("-d", targetPath, "-sourcepath", targetPath, "-extdirs", libPath);// 指定的路径一定要存在,javac不会自己创建文件夹
            JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
            // 编译源程序
            success = task.call();
            fileManager.close();
            if (!success) {
                //错误信息打印
                List diagnosticList = diagnostics.getDiagnostics();
                System.out.println("编译失败:lib包路径:"+libPath);
                for (Object aDiagnosticList : diagnosticList) {
                    logger.error(aDiagnosticList.toString());
                }
            } else {
                System.out.println("编译成功" + sourcePath);
            }
        }else {
            System.out.println("编译成功"+sourcePath);
        }
        }catch (Exception e){
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success;

+ 31 - 25
hos-broker/src/main/java/com/yihu/hos/broker/services/camel/ESBCamelService.java

@ -19,7 +19,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
@ -209,15 +208,15 @@ public class ESBCamelService {
            if (serviceFlowValid.is()) return Result.error("必要的入参数据不正确,请检查!");
            ServiceFlow serviceFlow = serviceFlowValid.getServiceFlow();
            String routeCode = serviceFlow.getRouteCode();
            SystemCamelContext.getContext().stopRoute(routeCode);
            boolean b = SystemCamelContext.getContext().removeRoute(routeCode);
            ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlowValid.getHandleFiles();
            if (handleFiles != null) {
                for (ServiceFlow.HandleFile handleFile : handleFiles) {
                    String routeCode = serviceFlow.getRouteCode();
                    SystemCamelContext.getContext().stopRoute(routeCode);
                    SystemCamelContext.getContext().removeRoute(routeCode);
                    this.deleteClassFile(handleFile);
                    this.deleteServiceFlow(handleFile.getRouteCode());//删除mongo中serviceFlow信息
                    //TODO 删除mongodb中文件
                }
            }
            return Result.success("服务流程删除成功!");
@ -252,7 +251,7 @@ public class ESBCamelService {
                if (!created[0]) {
                    System.out.println("processor 生成失败!");
                    return Result.error("服务流程启动失败!");
                }else {
                } else {
                    System.out.println("processor 生成成功!");
                }
@ -267,7 +266,7 @@ public class ESBCamelService {
                if (!created[0]) {
                    System.out.println("route文件 生成失败!");
                    return Result.error("服务流程启动失败!");
                }else {
                } else {
                    System.out.println("route文件 生成成功!");
                }
            } else {
@ -343,7 +342,7 @@ public class ESBCamelService {
        String sourcePath = ClassFileUtil.downFile(downUrl, resource, handleFile.getPackageName(), handleFile.getClassName(), ClassFileUtil.JAVA_FILE);
        if (sourcePath == null) {
            System.out.println("下载java文件失败,downUrl:"+downUrl);
            System.out.println("下载java文件失败,downUrl:" + downUrl);
            return false;
        }
@ -351,29 +350,36 @@ public class ESBCamelService {
        boolean succ = CamelCompiler.compile(sourcePath, resource.getPath());
        if (succ) {
            //编译成功后将class文件上传至mongodb;文件名为类名+ routeCode
            String packagePath = StringUtil.replaceStrAll(handleFile.getPackageName(), ".", "/");
            String classPath = resource.getPath() + packagePath + "/" + handleFile.getClassName() + ClassFileUtil.CLASS_FILE;
            //上传文件
            String fileName = handleFile.getClassName() + ClassFileUtil.CLASS_FILE;
            String uploadUrl = fsUrl + "/" + handleFile.getFilePath();
            ClassFileUtil.uploadFile(uploadUrl, new File(classPath), fileName);
        }else {
            System.out.println("编译失败,sourcePath:"+sourcePath);
//            String packagePath = StringUtil.replaceStrAll(handleFile.getPackageName(), ".", "/");
//            String classPath = this.getClass().getResource("/").getPath() + packagePath + "/" + handleFile.getClassName() + ClassFileUtil.CLASS_FILE;
//
//            //上传文件
//            String fileName = handleFile.getClassName() + ClassFileUtil.CLASS_FILE;
//            ClassFileUtil.upload(fsUrl, classPath);
        } else {
            System.out.println("编译失败,sourcePath:" + sourcePath);
        }
        return succ;
    }
    private void deleteClassFile(ServiceFlow.HandleFile handleFile) throws Exception {
        String packagePath = StringUtil.replaceStrAll(handleFile.getPackageName(), ".", "/");
        String classPath = ClassLoader.getSystemResource("").getPath() + "/" + packagePath + "/" + handleFile.getClassName() + ".class";
        ClassFileUtil.deleteClassfile(classPath);
    private void deleteClassFile(ServiceFlow.HandleFile handleFile) {
        try {
            String packagePath = StringUtil.replaceStrAll(handleFile.getPackageName(), ".", "/");
            String classPath = this.getClass().getResource("/").getPath() + "/" + packagePath + "/" + handleFile.getClassName() + ".class";
            String javaPath = this.getClass().getResource("/").getPath() + "/" + packagePath + "/" + handleFile.getClassName() + ".java";
            Boolean aBoolean = ClassFileUtil.deleteClassfile(javaPath);
            Boolean bBoolean = ClassFileUtil.deleteClassfile(classPath);
            //删除mongodb中文件
            String delJavaUrl = fsUrl + "/" + handleFile.getFilePath();
            ClassFileUtil.deleteFile(delJavaUrl);
        String delJavaUrl = fsUrl + "/" + handleFile.getFilePath();
        ClassFileUtil.deleteFile(delJavaUrl);
            // 完成
            logger.info("===================" + handleFile.getPackageName() + CoreConstant.DOT + handleFile.getClassName() + ".class 删除过程结束");
        // 完成
        logger.info("===================" + handleFile.getPackageName() + CoreConstant.DOT + handleFile.getClassName() + ".class 删除过程结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**

+ 10 - 0
hos-core/src/main/java/com/yihu/hos/core/datatype/ClassFileUtil.java

@ -112,6 +112,7 @@ public class ClassFileUtil {
            }
            return succ;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
@ -269,6 +270,15 @@ public class ClassFileUtil {
        return response.getBody();
    }
    public static String upload(String url, String filePath) {
        HTTPResponse response = HttpClientKit.postFile(url, "file", filePath, "text/plain");
        if (response.getStatusCode() != 200) {
            System.out.println("上传文件失败,status:"+response.getStatusCode()+" body: "+response.getBody());
            return null;
        }
        return response.getBody();
    }
    /**
     * http 上传文件到中心请求
     *

+ 1 - 1
src/main/java/com/yihu/hos/system/service/FlowManager.java

@ -696,7 +696,7 @@ public class FlowManager {
            }
            serviceFlow.setHandleFiles(handleFiles);
            serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
            serviceFlowEventService.serviceFlowAdded(serviceFlow);
            return newFlow.getId();
        }