| | |
| | | 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; |
| | | |
| | | /** |
| | | * 扫描: |
| | |
| | | @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 ; |
| | | |
| | |
| | | * @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; |
| | |
| | | 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{ |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | 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()){ |
| | |
| | | } |
| | | } |
| | | |
| | | 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()){ |
| | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | //进行验证 |
| | | String error = this.hasErrorInDriver(driverMap) ; |
| | | if(error != null){ |
| | | throw new Exception(error) ; |
| | |
| | | throw new Exception("严重错误, 扫描协议相关注解所得到类型集合为空,扫描根路径是" + scanRootPackage) ; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 检查优先级重复 |
| | | * @param priority 优先级 |