shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
package com.walker.tcp;
 
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 集中存放系统定义的所有<code>ProtocolResolver</code>对象。</p>
 * 注意:一定要把默认的回车换行的ProtocolResolver配置在最后面,因为有可能其他特殊前缀的报文也会使用回车换行结尾,
 * 因此如果出现这种情况,也能确保用户自定义的ProtocolResolver会优先被调用。
 * @author 时克英
 * @date 2018-11-27
 *
 */
public class ActionCallablePostProcessor implements BeanPostProcessor {
 
    private final transient Logger logger = LoggerFactory.getLogger(getClass());
 
    private static Map<String, ActionCallable> reference = new ConcurrentHashMap<>();
 
//    private static List<ProtocolResolver> cacheList =null;
 
//    private final InnerComparator comparator = new InnerComparator();
 
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        if(ActionCallable.class.isAssignableFrom(bean.getClass())){
            ActionCallable pr = (ActionCallable)bean;
            if(StringUtils.isEmpty(pr.getRequestProtocol())){
                logger.error(pr.getSummary() + "," + pr.getClassName() + ", " + pr.getClass().getName());
                throw new IllegalArgumentException("ActionCallable未实现getRequestProtocol()方法!");
            }
            reference.put(pr.getRequestProtocol(), pr);
            logger.info("找到了一个ActionCallable:" + pr.getClass().getName());
        }
 
        return bean;
    }
 
    /**
     * 根据数据协议编号,查找要处理该业务的action实例。</p>
     *
     * @param protocolNum
     * @return
     */
    public static final ActionCallable getAction(String protocolNum){
        if(reference.size() == 0){
            throw new IllegalArgumentException(ActionCallable.ERR_NOT_FOUND);
        }
 
        ActionCallable action = reference.get(protocolNum);
        if(action == null){
            throw new IllegalArgumentException("未找到定义的ActionCallable,protocolNum = " + protocolNum);
        }
        return action;
    }
 
    public static final List<ActionCallable> getActionList(){
        List<ActionCallable> result = new ArrayList<>();
        for(ActionCallable action : reference.values()){
            result.add(action);
        }
        return result;
    }
 
    /**
     * 测试方法。
     * @param map
     */
    public static final void setTestActions(Map<String, ActionCallable> map){
        if(ActionCallablePostProcessor.reference.size() > 0){
            System.out.println("++++++++++ " + reference);
            throw new IllegalStateException("reference已经存在数据,不能覆盖。该方法只能在测试时使用!");
        }
        ActionCallablePostProcessor.reference = map;
    }
}