zhenglingfeng 8 лет назад
Родитель
Сommit
af4f2a451a

+ 1 - 0
hos-broker/src/main/java/com/yihu/hos/common/listener/ApplicationStartListener.java

@ -155,6 +155,7 @@ public class ApplicationStartListener implements ApplicationListener<ContextRefr
            URLs.add(systemClassFlowPath.toURI().toURL());
        }
        ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
        ClassLoader camelESBClassLoader = new URLClassLoader(URLs.toArray(new URL[]{}), currentClassLoader);
        Thread.currentThread().setContextClassLoader(camelESBClassLoader);
        SystemCamelContext.getDefaultCamelContext().setApplicationContextClassLoader(camelESBClassLoader);

+ 13 - 0
hos-broker/src/main/java/com/yihu/hos/controllers/ESBCamelController.java

@ -59,6 +59,19 @@ public class ESBCamelController {
        return esbCamelService.onProcessorDataChanged(serviceFlow, packageName, className, path);
    }
    @RequestMapping(value = "/processor", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "删除Processor处理器", produces = "application/json", notes = "当外界组件通知一个已有的processor处理器data部分发生删除时,该事件被触发")
    public Result onProcessorDataDelete(
            @ApiParam(name = "serviceFlow", value = "服务名称", required = true)
            @RequestParam(value = "serviceFlow") String serviceFlow,
            @ApiParam(name = "packageName", value = "包名", required = true)
            @RequestParam(value = "packageName") String packageName,
            @ApiParam(name = "className", value = "类名", required = true)
            @RequestParam(value = "className") String className) {
        return esbCamelService.onProcessorDataDelete(serviceFlow, packageName, className);
    }
    @RequestMapping(value = "/route", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增Route路由", produces = "application/json", notes = "当外界组件通知一个新的RouteDefine路由被定义时,该事件被触发")

+ 33 - 6
hos-broker/src/main/java/com/yihu/hos/services/ESBCamelService.java

@ -14,6 +14,7 @@ import com.yihu.hos.models.SystemCamelContext;
import com.yihu.hos.models.SystemClassMapping;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.GridFSUtil;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
@ -24,6 +25,8 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
/**
@ -54,6 +57,11 @@ public class ESBCamelService {
            String fileName = DES.decrypt(path, DES.COMMON_PASSWORD);
            MongoDatabase db = mongoConfig.mongoClient().getDatabase(dbName);
            if (GridFSUtil.readFile(db, out, fileName)) {
                File packageFile = new File(serviceFlow + StringUtil.replaceStrAll(packageName, ".", "/"));
                ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
                ClassLoader camelESBClassLoader = new URLClassLoader(new URL[]{packageFile.toURI().toURL()}, currentClassLoader);
                Thread.currentThread().setContextClassLoader(camelESBClassLoader);
                return Result.success("新增处理器成功!");
            } else {
                return Result.error("新增处理器失败!");
@ -98,10 +106,30 @@ public class ESBCamelService {
        }
    }
    /**
     * 当外界组件通知一个已有的Processor路由定义被删除时,该事件被触发。
     */
    public Result onProcessorDataDelete(String serviceFlow, String packageName, String className) {
        try {
            if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
                    || StringUtil.isEmpty(className)) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            SystemCamelContext.getDefaultCamelContext().stopRoute(serviceFlow);
            SystemCamelContext.getDefaultCamelContext().removeRoute(serviceFlow);
            this.deleteClassfile(serviceFlow, packageName, className, BrokerConstant.PROCESSOR);
            return Result.success("删除路由成功!");
        } catch (Exception e) {
            return Result.error("删除路由失败!");
        }
    }
    /**
     * 当外界组件通知一个新的RouteDefine路由被定义时,该事件被触发
     */
    public Result onRouteDefineAdded(String serviceFlow , String packageName , String className , String path) {
    public Result onRouteDefineAdded(String serviceFlow, String packageName, String className, String path) {
        try {
            if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
                    || StringUtil.isEmpty(className) || StringUtil.isEmpty(path)) {
@ -174,7 +202,7 @@ public class ESBCamelService {
            SystemCamelContext.getDefaultCamelContext().stopRoute(serviceFlow);
            SystemCamelContext.getDefaultCamelContext().removeRoute(serviceFlow);
            this.deleteClassfile(serviceFlow, packageName, className, BrokerConstant.PROCESSOR);
            this.deleteClassfile(serviceFlow, packageName, className, BrokerConstant.ROUTE);
            return Result.success("删除路由成功!");
        } catch (Exception e) {
            return Result.error("删除路由失败!");
@ -231,17 +259,16 @@ public class ESBCamelService {
        return out;
    }
    private FileOutputStream updateClassfile(String serviceFlow, String packageName, String className, String type) {
    private FileOutputStream updateClassfile(String serviceFlow, String packageName, String className, String type) throws MalformedURLException {
        // 1、============
        Map<String, String> systemClassNameMapping = SystemClassMapping.getSystemClassNameMapping();
        String systemClassName = systemClassNameMapping.get(serviceFlow + type + className);
        if(StringUtil.isEmpty(systemClassName)) {
            return null;
        }
        String packagePath = StringUtil.replaceStrAll(packageName, ".", "/");
        String classPath = this.getClass().getProtectionDomain().getClassLoader().getResource("").getPath() + packagePath + "/" + className + ".class";
        File systemClassFlowPath = new File(this.getClass().getProtectionDomain().getClassLoader().getResource("").getPath());
        // 2、============开始写入class文件
        FileOutputStream out = ClassFileUtil.updateClassfile(classPath);
        FileOutputStream out = ClassFileUtil.updateClassfile(systemClassFlowPath.toURI().toURL(), packageName, className);
        // 完成
        logger.info("===================" + packageName + CoreConstant.DOT + className + ".class 修改过程结束");
        return out;

+ 6 - 4
hos-core/src/main/java/com/yihu/hos/core/datatype/ClassFileUtil.java

@ -108,12 +108,12 @@ public class ClassFileUtil {
            if (!systemFlowFile.exists()) {
                systemFlowFile.mkdirs();
            }
            packageName = StringUtil.replaceStrAll(packageName, ".", "/");
            File packageFile = new File(systemFlowURL.getPath() + "/" + packageName);
            String packagePath = StringUtil.replaceStrAll(packageName, ".", "/");
            File packageFile = new File(systemFlowURL.getPath() + "/" + packagePath);
            if (!packageFile.exists()) {
                packageFile.mkdirs();
            }
            String classPath = packageFile + "/" + className + ".class";
            String classPath = packageFile.getPath() + "/" + className + ".class";
            File file = new File(classPath);
            if (file.isFile() && file.exists()) {
                file.delete();
@ -127,7 +127,9 @@ public class ClassFileUtil {
        }
    }
    public static FileOutputStream updateClassfile(String classPath) {
    public static FileOutputStream updateClassfile(URL systemFlowURL, String packageName, String className) {
        String packagePath = StringUtil.replaceStrAll(packageName, ".", "/");
        String classPath = systemFlowURL.getPath() + "/" + packagePath + "/" + className + ".class";
        // 开始输出文件内容
        try {
            File file = new File(classPath);

+ 2 - 2
src/main/java/com/yihu/hos/services/ServiceFlowEventService.java

@ -42,8 +42,8 @@ public class ServiceFlowEventService {
        this.sendMsg("processorDataChanged", serviceFlow, packageName, className, path);
    }
    public void processorDataDeleted(String serviceFlow, String packageName, String className, String path) {
        this.sendMsg("processorDataDeleted", serviceFlow, packageName, className, path);
    public void processorDataDeleted(String serviceFlow, String packageName, String className) {
        this.sendMsg("processorDataDeleted", serviceFlow, packageName, className, null);
    }
    /**

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

@ -284,7 +284,7 @@ public class FlowManager implements IFlowManage {
            //processor
            switch (operate){
                case "add" : serviceFlowEventService.processorAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
                case "delete" : serviceFlowEventService.processorDataDeleted(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
                case "delete" : serviceFlowEventService.processorDataDeleted(flowCode, flowClass.getPackageName(), flowClass.getClassName()); break;
                case "update" : serviceFlowEventService.processorDataChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
                default : break;
            }