zhubaomin
2024-08-22 81636c3be211bdd6c32b60df5b89d2026911f24e
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/AnnotationScan.java
@@ -1,13 +1,13 @@
package com.dy.common.mw.protocol;
import java.net.URI;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import com.dy.common.util.ClassScan;
import com.dy.common.util.NumUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.reflections.Reflections;
/**
 * 扫描:
@@ -20,7 +20,9 @@
@SuppressWarnings("unused")
public class AnnotationScan {
   
   private static String scanRootPackage = "com.dy.common.mw.protocol." ;
   private static final Logger log = LogManager.getLogger(AnnotationScan.class);
   private static String scanRootPackage = "com.dy.common.mw.protocol" ;
   private static AnnotationScan instance ;
   
@@ -47,19 +49,9 @@
    * @throws Exception 异常
    */
   private void scanEndpointAnnotations() throws Exception{
      ClassLoader load = this.getClass().getClassLoader() ;
      if(!scanRootPackage.endsWith(".")){
         scanRootPackage += "." ;
      }
      URL url = AnnotationScan.class.getResource("/" + scanRootPackage.replace('.', '/'));
      if(url != null){
         String urlStr = url.toString() ;
         if(urlStr != null){
            URI uri = new URI(url.toString());
            List<String> classNames = new ClassScan().searchClassFromUrl(new URI[]{uri}, scanRootPackage) ;
            if(classNames != null && classNames.size() > 0){
      Reflections reflections = new Reflections(scanRootPackage); // 指定包名
      Set<Class<?>> driverClasses = reflections.getTypesAnnotatedWith(AnnotationDriver.class);
      if (driverClasses != null && driverClasses.size() > 0) {
               String protocolName ;
               int priority;
               int onLineDataMinLength;
@@ -69,17 +61,14 @@
               HashMap<String, AnnotationPrefixedDataAvailableVo> prefixedDataAvailableMap = ProtocolCache.getPrefixedDataAvailableMap() ;
               HashMap<String, AnnotationOnLineVo> onLineMap = ProtocolCache.getOnLineMap() ;
               for(String cName : classNames){
                  String className = cName.substring(0,  cName.lastIndexOf('.')) ;
                  Class<?> clazz = load.loadClass(className) ;
                  if(clazz.isAnnotationPresent(AnnotationDriver.class)){
         for (Class<?> clazz : driverClasses) {
                     AnnotationDriver ann = clazz.getAnnotation(AnnotationDriver.class) ;
                     if(ann != null){
                        if(ann.enable()){
                           protocolName = ann.name() ;
                           if(!(protocolName.trim().equals(""))){
                              if(driverMap.containsKey(protocolName)){
                                 throw new Exception("严重错误,协议驱动名称" + protocolName + "出现了重复注解!") ;
                        throw new Exception("严重错误,协议驱动(名称)" + protocolName + "出现了重复注解!");
                              }
                              driverMap.put(protocolName, new AnnotationDriverVo(clazz)) ;
                           }else{
@@ -89,8 +78,8 @@
                     }
                  }
                  if(clazz.isAnnotationPresent(AnnotationPrefixedDataAvailable.class)){
         Set<Class<?>> prefixedClass = reflections.getTypesAnnotatedWith(AnnotationPrefixedDataAvailable.class);
         for (Class<?> clazz : prefixedClass) {
                     AnnotationPrefixedDataAvailable ann = clazz.getAnnotation(AnnotationPrefixedDataAvailable.class) ;
                     if(ann != null){
                        if(ann.enable()){
@@ -128,7 +117,8 @@
                     }
                  }
                  if(clazz.isAnnotationPresent(AnnotationOnLine.class)){
         Set<Class<?>> onLineClass = reflections.getTypesAnnotatedWith(AnnotationOnLine.class);
         for (Class<?> clazz : onLineClass) {
                     AnnotationOnLine ann = clazz.getAnnotation(AnnotationOnLine.class) ;
                     if(ann != null){
                        if(ann.enable()){
@@ -153,8 +143,8 @@
                        }
                     }
                  }
               }
         //进行验证
               String error = this.hasErrorInDriver(driverMap) ;
               if(error != null){
                  throw new Exception(error) ;
@@ -173,8 +163,7 @@
               throw new Exception("严重错误, 扫描协议相关注解所得到类型集合为空,扫描根路径是" + scanRootPackage) ;
            }
         }
      }
   }
   /**
    * 检查优先级重复
    * @param priority 优先级