|  | @ -0,0 +1,165 @@
 | 
	
		
			
				|  |  | 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 org.apache.logging.log4j.LogManager;
 | 
	
		
			
				|  |  | import org.apache.logging.log4j.Logger;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.net.URL;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | import java.util.concurrent.SynchronousQueue;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by lingfeng on 2016/8/4.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service("ESBCamelService")
 | 
	
		
			
				|  |  | public class ESBCamelService {
 | 
	
		
			
				|  |  |     public static final String BEAN_ID = "ESBCamelService";
 | 
	
		
			
				|  |  |     private static Logger logger = LogManager.getLogger();
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 当外界组件通知一个新的processor处理器被定义时,该事件被触发。
 | 
	
		
			
				|  |  |      * @param serviceFlow 本次processor处理器变化,所涉及的业务系统唯一标识。
 | 
	
		
			
				|  |  |      * @param packageName processor处理器定义涉及的class包名
 | 
	
		
			
				|  |  |      * @param className processor处理器定义涉及的class类名
 | 
	
		
			
				|  |  |      * @param path processor处理器定义涉及的class对应路径
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Result onProcessorAdded(String serviceFlow, String packageName, String className, String path) {
 | 
	
		
			
				|  |  |  /*
 | 
	
		
			
				|  |  |          * 当一个处理器进行添加时,要做以下处理
 | 
	
		
			
				|  |  |          * 1、首先根据serviceFlow的信息,在ESB-Broker Server的映射容器中寻
 | 
	
		
			
				|  |  |          * 找个业务系统定义的各种Server Class 文件在ESB-Broker Server节点本地存储的根路径
 | 
	
		
			
				|  |  |          * 2、然后按照packageName、className、contents的信息将这个class文件写入到正确的位置
 | 
	
		
			
				|  |  |          *
 | 
	
		
			
				|  |  |          * 注意:由于此时只是完成了class了文件的写入,所以这个class文件还没有被classloader进行初始化。
 | 
	
		
			
				|  |  |          * 另外由于,CamelContext并没有提供单独进行processor处理器加载的功能,而是随着routes实例的加载而加载
 | 
	
		
			
				|  |  |          * 而这个工作将在onRouteDefineChanged事件中完成,所以在完成processor-class文件的写入后,就不需要再做其它事情了。
 | 
	
		
			
				|  |  |          * */
 | 
	
		
			
				|  |  |         if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
 | 
	
		
			
				|  |  |                 || StringUtil.isEmpty(className) || StringUtil.isEmpty(path)) {
 | 
	
		
			
				|  |  |             logger.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |             return Result.error("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         this.createClassfile(serviceFlow, packageName, className, path);
 | 
	
		
			
				|  |  |         return Result.success("新增处理器成功!");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 当外界组件通知一个已有的processor处理器data部分发生变化时,该事件被触发。
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Result onProcessorDataChanged(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("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         this.updateClassfile(serviceFlow, packageName, className, path);
 | 
	
		
			
				|  |  |         return Result.success("修改处理器成功!");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 当外界组件通知一个新的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);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         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("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         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("必要的入参数据不正确,请检查!");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         this.deleteClassfile(serviceFlow, packageName, className);
 | 
	
		
			
				|  |  |         return Result.success("删除路由成功!");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private void createClassfile(String serviceFlow, String packageName, String className, String path) {
 | 
	
		
			
				|  |  |         // 1、============
 | 
	
		
			
				|  |  |         Map<String, URL> systemPathMapping = SystemPathMapping.getSystemRootPathMapping();
 | 
	
		
			
				|  |  |         URL systemRootURL = systemPathMapping.get(serviceFlow);
 | 
	
		
			
				|  |  |         if(systemRootURL == null) {
 | 
	
		
			
				|  |  |             return;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 2、============开始写入class文件
 | 
	
		
			
				|  |  |         ClassFileUtil.createClassfile(systemRootURL, packageName, className, path);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 完成
 | 
	
		
			
				|  |  |         logger.info("===================" + packageName + "." + className + ".class 生成过程结束");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private void updateClassfile(String serviceFlow, String packageName, String className, String path) {
 | 
	
		
			
				|  |  |         // 1、============
 | 
	
		
			
				|  |  |         Map<String, URL> systemPathMapping = SystemPathMapping.getSystemRootPathMapping();
 | 
	
		
			
				|  |  |         URL systemRootURL = systemPathMapping.get(serviceFlow);
 | 
	
		
			
				|  |  |         if(systemRootURL == null) {
 | 
	
		
			
				|  |  |             return;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 2、============开始写入class文件
 | 
	
		
			
				|  |  |         ClassFileUtil.updateClassfile(systemRootURL, packageName, className, path);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 完成
 | 
	
		
			
				|  |  |         logger.info("===================" + packageName + "." + className + ".class 修改过程结束");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private void deleteClassfile(String serviceFlow, String packageName, String className) {
 | 
	
		
			
				|  |  |         // 1、============
 | 
	
		
			
				|  |  |         Map<String, URL> systemPathMapping = SystemPathMapping.getSystemRootPathMapping();
 | 
	
		
			
				|  |  |         URL systemRootURL = systemPathMapping.get(serviceFlow);
 | 
	
		
			
				|  |  |         if(systemRootURL == null) {
 | 
	
		
			
				|  |  |             return;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 2、============开始写入class文件
 | 
	
		
			
				|  |  |         ClassFileUtil.deleteClassfile(systemRootURL, packageName, className);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 完成
 | 
	
		
			
				|  |  |         logger.info("===================" + packageName + "." + className + ".class 删除过程结束");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |