|  | @ -3,15 +3,18 @@ package com.yihu.hos.services;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.model.Result;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.util.operator.ClassFileUtil;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.util.operator.StringUtil;
 | 
	
		
			
				|  |  | import com.yihu.hos.model.CamelContextQueue;
 | 
	
		
			
				|  |  | import com.yihu.hos.model.SystemPathMapping;
 | 
	
		
			
				|  |  | import com.yihu.hos.common.classLoader.DynamicClassLoader;
 | 
	
		
			
				|  |  | import com.yihu.hos.common.constants.Constant;
 | 
	
		
			
				|  |  | import com.yihu.hos.models.SystemCamelContext;
 | 
	
		
			
				|  |  | import com.yihu.hos.models.SystemPathMapping;
 | 
	
		
			
				|  |  | import org.apache.camel.builder.RouteBuilder;
 | 
	
		
			
				|  |  | import org.apache.logging.log4j.LogManager;
 | 
	
		
			
				|  |  | import org.apache.logging.log4j.Logger;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.io.File;
 | 
	
		
			
				|  |  | import java.net.URL;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | import java.util.concurrent.SynchronousQueue;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by lingfeng on 2016/8/4.
 | 
	
	
		
			
				|  | @ -64,57 +67,83 @@ public class ESBCamelService {
 | 
	
		
			
				|  |  |      * 当外界组件通知一个新的RouteDefine路由被定义时,该事件被触发
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Result onRouteDefineAdded(String serviceFlow , String packageName , String className , String path) {
 | 
	
		
			
				|  |  | /*
 | 
	
		
			
				|  |  |          * 当一个新的路由定义事件发生时,要做以下几件事情:
 | 
	
		
			
				|  |  |          *
 | 
	
		
			
				|  |  |          * 1、根据serviceFlow的信息,在ESB-Broker Server的映射容器中寻
 | 
	
		
			
				|  |  |          * 找个业务系统定义的各种Server Class 文件在ESB-Broker Server节点本地存储的根路径
 | 
	
		
			
				|  |  |          * 2、然后按照packageName、className、contents的信息将这个class文件写入到正确的位置
 | 
	
		
			
				|  |  |          * 3、不能在本线程操作Apache Camel,只能通过一个同步队列通知Apache Camel主线程
 | 
	
		
			
				|  |  |          * */
 | 
	
		
			
				|  |  |         if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
 | 
	
		
			
				|  |  |                 || StringUtil.isEmpty(className) || StringUtil.isEmpty(path)) {
 | 
	
		
			
				|  |  |             logger.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |             return Result.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         // 第1、2两步处理过程,都是在这里完成
 | 
	
		
			
				|  |  |         this.createClassfile(serviceFlow, packageName, className, path);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 3、===============加载到CamelContext中
 | 
	
		
			
				|  |  |         SynchronousQueue<String> camelContextOperateQueue = CamelContextQueue.getCamelCamelContextQueue();
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             camelContextOperateQueue.put(packageName + "." + className);
 | 
	
		
			
				|  |  |         } catch (InterruptedException e) {
 | 
	
		
			
				|  |  |             logger.error(e.getMessage() , e);
 | 
	
		
			
				|  |  |             if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
 | 
	
		
			
				|  |  |                     || StringUtil.isEmpty(className) || StringUtil.isEmpty(path)) {
 | 
	
		
			
				|  |  |                 logger.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |                 return Result.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             File systemClassRootPath = new File(ClassLoader.getSystemResource("").getPath() + "/" + serviceFlow);
 | 
	
		
			
				|  |  |             if (!systemClassRootPath.exists()) {
 | 
	
		
			
				|  |  |                 systemClassRootPath.mkdirs();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             // 记录到工具类中,以便其它线程需要时进行取用
 | 
	
		
			
				|  |  |             SystemPathMapping.getSystemRootPathMapping().put(serviceFlow, systemClassRootPath.toURI().toURL());
 | 
	
		
			
				|  |  |             // 第1、2两步处理过程,都是在这里完成
 | 
	
		
			
				|  |  |             this.createClassfile(serviceFlow, packageName, className, path);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             // 3、===============加载到CamelContext中
 | 
	
		
			
				|  |  |             ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
 | 
	
		
			
				|  |  |             Class<RouteBuilder> routeBuilderClass = (Class<RouteBuilder>) currentClassLoader.loadClass(serviceFlow + Constant.DOT + packageName + Constant.DOT + className);
 | 
	
		
			
				|  |  |             if(routeBuilderClass != null) {
 | 
	
		
			
				|  |  |                 RouteBuilder routeBuilder = routeBuilderClass.newInstance();
 | 
	
		
			
				|  |  |                 SystemCamelContext.getDefaultCamelContext().addRoutes(routeBuilder);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             return Result.success("新增路由成功!");
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             logger.error(e);
 | 
	
		
			
				|  |  |             return Result.error("新增路由失败!");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return Result.success("新增路由成功!");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 当外界组件通知一个已有的RouteDefine路由定义被改变时,主要就是路由定义内容被改变时,该事件被触发。
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Result onRouteDefineChanged(String serviceFlow, String packageName, String className, String path) {
 | 
	
		
			
				|  |  |         if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
 | 
	
		
			
				|  |  |                 || StringUtil.isEmpty(className) || StringUtil.isEmpty(path)) {
 | 
	
		
			
				|  |  |             logger.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |             return Result.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             if (StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
 | 
	
		
			
				|  |  |                     || StringUtil.isEmpty(className) || StringUtil.isEmpty(path)) {
 | 
	
		
			
				|  |  |                 logger.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |                 return Result.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             SystemCamelContext.getDefaultCamelContext().stopRoute(serviceFlow);
 | 
	
		
			
				|  |  |             SystemCamelContext.getDefaultCamelContext().removeRoute(serviceFlow);
 | 
	
		
			
				|  |  |             this.updateClassfile(serviceFlow, packageName, className, path);
 | 
	
		
			
				|  |  |             // 3、===============加载到CamelContext中
 | 
	
		
			
				|  |  |             DynamicClassLoader classLoader = new DynamicClassLoader(DynamicClassLoader.class.getClassLoader());
 | 
	
		
			
				|  |  |             Class<RouteBuilder> routeBuilderClass = (Class<RouteBuilder>) classLoader.loadClass(ClassLoader.getSystemResource(Constant.EMPTY).getPath(),
 | 
	
		
			
				|  |  |                     serviceFlow + Constant.DOT + packageName + Constant.DOT + className + Constant.DOT + Constant.CLASS);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             if (routeBuilderClass != null) {
 | 
	
		
			
				|  |  |                 RouteBuilder routeBuilder = routeBuilderClass.newInstance();
 | 
	
		
			
				|  |  |                 SystemCamelContext.getDefaultCamelContext().addRoutes(routeBuilder);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             return Result.success("修改路由成功!");
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             return Result.error("修改路由失败!");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         this.updateClassfile(serviceFlow, packageName, className, path);
 | 
	
		
			
				|  |  |         return Result.success("修改路由成功!");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 当外界组件通知一个已有的RouteDefine路由定义被删除时,该事件被触发。
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Result onRouteDefineDelete(String serviceFlow, String packageName, String className) {
 | 
	
		
			
				|  |  |         if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
 | 
	
		
			
				|  |  |                 || StringUtil.isEmpty(className)) {
 | 
	
		
			
				|  |  |             logger.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |             return Result.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |         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);
 | 
	
		
			
				|  |  |             return Result.success("删除路由成功!");
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             return Result.error("删除路由失败!");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         this.deleteClassfile(serviceFlow, packageName, className);
 | 
	
		
			
				|  |  |         return Result.success("删除路由成功!");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
	
		
			
				|  | @ -130,7 +159,7 @@ public class ESBCamelService {
 | 
	
		
			
				|  |  |         ClassFileUtil.createClassfile(systemRootURL, packageName, className, path);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 完成
 | 
	
		
			
				|  |  |         logger.info("===================" + packageName + "." + className + ".class 生成过程结束");
 | 
	
		
			
				|  |  |         logger.info("===================" + packageName + Constant.DOT + className + ".class 生成过程结束");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private void updateClassfile(String serviceFlow, String packageName, String className, String path) {
 | 
	
	
		
			
				|  | @ -145,7 +174,7 @@ public class ESBCamelService {
 | 
	
		
			
				|  |  |         ClassFileUtil.updateClassfile(systemRootURL, packageName, className, path);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 完成
 | 
	
		
			
				|  |  |         logger.info("===================" + packageName + "." + className + ".class 修改过程结束");
 | 
	
		
			
				|  |  |         logger.info("===================" + packageName + Constant.DOT + className + ".class 修改过程结束");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private void deleteClassfile(String serviceFlow, String packageName, String className) {
 | 
	
	
		
			
				|  | @ -160,6 +189,6 @@ public class ESBCamelService {
 | 
	
		
			
				|  |  |         ClassFileUtil.deleteClassfile(systemRootURL, packageName, className);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 完成
 | 
	
		
			
				|  |  |         logger.info("===================" + packageName + "." + className + ".class 删除过程结束");
 | 
	
		
			
				|  |  |         logger.info("===================" + packageName + Constant.DOT + className + ".class 删除过程结束");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |