zhubaomin
2025-04-07 1a2b07f01ba4616fd9e894dddf474b56d020158c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.dy.common.mw.protocol;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class ProtocolCache {
 
    //本系统中,Driver在单线程中运行,所以只用一个实例
    private static final HashMap<String, Driver> drivers = new HashMap<>() ;
    //注解与处理类的映射
    private static final HashMap<String, AnnotationDriverVo> driverMap = new HashMap<>() ;
    private static final HashMap<String, AnnotationPrefixedDataAvailableVo> prefixedDataAvailableMap = new HashMap<>() ;
    private static final HashMap<String, AnnotationOnLineVo> onLineMap = new HashMap<>() ;
 
 
    /**
     * 在单线程环境中运行
     * 通过协议驱动的类名,得到类单例
     * @param protocolName 协议名称
     * @param protocolVersion 协议版本号
     * @return 驱动
     * @throws Exception 异常
     */
    public static Driver getDriver(String protocolName, short protocolVersion) throws Exception{
        Driver dri = drivers.get(protocolName + protocolVersion) ;
        if(dri == null){
            AnnotationDriverVo vo = driverMap.get(protocolName + protocolVersion) ;
            if(vo != null && vo.clazz != null){
                dri = (Driver)vo.clazz.getDeclaredConstructor().newInstance();
                drivers.put(protocolName + protocolVersion, dri) ;
            }
        }
        return dri ;
    }
    /**
     * 在单线程环境中运行
     * 通过协议驱动的类名,得到类单例
     * @param protocolNameVersion 协议名称和版本号
     * @return 驱动
     * @throws Exception 异常
     */
    public static Driver getDriver(String protocolNameVersion) throws Exception{
        Driver dri = drivers.get(protocolNameVersion) ;
        if(dri == null){
            AnnotationDriverVo vo = driverMap.get(protocolNameVersion) ;
            if(vo != null && vo.clazz != null){
                dri = (Driver)vo.clazz.getDeclaredConstructor().newInstance();
                drivers.put(protocolNameVersion, dri) ;
            }
        }
        return dri ;
    }
 
    /**
     * 得到驱动的数量,即中间件支持的协议数量
     * @return 驱动总数
     */
    public static int driverCount(){
        if(drivers.size() == 0){
            return 0 ;
        }else{
            return drivers.size() ;
        }
    }
 
    /**
     * 得到第一个驱动
     * @return 驱动
     */
    public static Driver getFirstDriver(){
        Driver dri = null ;
        if(drivers.size() > 0){
            Map.Entry<String, Driver> ent = drivers.entrySet().iterator().next() ;
            dri = ent.getValue();
        }
        return dri ;
    }
 
    /**
     * 得到所有协议名称
     * @return 协议名称集合
     */
    @SuppressWarnings("unused")
    public static List<String> getProtocolList() {
        return new ArrayList<>(driverMap.keySet()) ;
    }
 
    protected static HashMap<String, AnnotationDriverVo> getDriverMap() {
        return driverMap;
    }
    protected static HashMap<String, AnnotationPrefixedDataAvailableVo> getPrefixedDataAvailableMap() {
        return prefixedDataAvailableMap;
    }
    protected static HashMap<String, AnnotationOnLineVo> getOnLineMap() {
        return onLineMap;
    }
 
    protected AnnotationDriverVo getAnnotationDriver(String protocolName, Short protocolVersion){
        return driverMap.get(protocolName + "" + protocolVersion) ;
    }
 
    protected static AnnotationPrefixedDataAvailableVo getAnnotationPrefixedDataAvailable(String protocolName, Short protocolVersion){
        return prefixedDataAvailableMap.get(protocolName + "" + protocolVersion) ;
    }
 
    protected static AnnotationOnLineVo getAnnotationOnLine(String protocolName, Short protocolVersion){
        return onLineMap.get(protocolName + "" + protocolVersion) ;
    }
 
    /*
    public static void main(String[] args){
        HashMap<String, Integer> mp = new HashMap<>() ;
        mp.put("a1", 1) ;
        mp.put("a2", 2) ;
        List<String> list = new ArrayList<>(mp.keySet()) ;
        System.out.println(list);
    }
    */
}