View Javadoc
1   package com.zone.weixin4j.service.context;
2   
3   import com.zone.weixin4j.dispatcher.WeixinMessageMatcher;
4   import com.zone.weixin4j.service.WeiXin4jContextAware;
5   import com.zone.weixin4j.socket.WeixinMessageTransfer;
6   import com.zone.weixin4j.spring.TokenGenerater;
7   import com.zone.weixin4j.util.AesToken;
8   import org.springframework.beans.BeansException;
9   import org.springframework.context.ApplicationContext;
10  import org.springframework.context.ApplicationContextAware;
11  import org.springframework.core.NamedThreadLocal;
12  import org.springframework.util.StringUtils;
13  
14  import java.util.HashMap;
15  import java.util.List;
16  import java.util.Map;
17  import java.util.concurrent.ConcurrentHashMap;
18  
19  /**
20   * Created by Yz on 2017/3/15.
21   * WeiXin4j上下文
22   */
23  public class WeiXin4jContextAwareImpl implements ApplicationContextAware, WeiXin4jContextAware {
24  
25      private static final ThreadLocal<WeixinMessageTransfer> weixinMessageTransfer = new NamedThreadLocal<WeixinMessageTransfer>("WeixinMessageTransfer");
26  
27      private ApplicationContext applicationContext;
28  
29      private boolean openAlwaysResponse;
30      private boolean useDebugMessageHandler;
31  
32      private List<AesToken> aesTokens;
33      private TokenGenerater tokenGenerater;
34  
35      private WeixinMessageMatcher weixinMessageMatcher;
36  
37      private Map<String, AesToken> aesTokenMap = new HashMap<String, AesToken>();
38  
39      @Override
40      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
41          this.applicationContext = applicationContext;
42  
43      }
44  
45      public boolean isOpenAlwaysResponse() {
46          return openAlwaysResponse;
47      }
48  
49      public void setOpenAlwaysResponse(boolean openAlwaysResponse) {
50          this.openAlwaysResponse = openAlwaysResponse;
51      }
52  
53      public boolean isUseDebugMessageHandler() {
54          return useDebugMessageHandler;
55      }
56  
57      public void setUseDebugMessageHandler(boolean useDebugMessageHandler) {
58          this.useDebugMessageHandler = useDebugMessageHandler;
59      }
60  
61      public List<AesToken> getAesTokens() {
62          return aesTokens;
63      }
64  
65      public void setAesTokens(List<AesToken> aesTokens) {
66          this.aesTokens = aesTokens;
67      }
68  
69      public void setTokenGenerater(TokenGenerater tokenGenerater) {
70          this.tokenGenerater = tokenGenerater;
71      }
72  
73      public ApplicationContext getApplicationContext() {
74          return applicationContext;
75      }
76  
77      public void init() {
78          this.aesTokens = tokenGenerater.getAesTokens();
79          for(AesToken aesToken : this.aesTokens){
80              this.aesTokenMap.put(StringUtils.isEmpty(aesToken.getWeixinId()) ? "" : aesToken.getWeixinId(), aesToken);
81          }
82      }
83  
84      public void destroy() {
85          this.applicationContext = null;
86      }
87  
88      public static ThreadLocal<WeixinMessageTransfer> getWeixinMessageTransfer() {
89          return weixinMessageTransfer;
90      }
91  
92      public Map<String, AesToken> getAesTokenMap() {
93          return aesTokenMap;
94      }
95  
96      public void setAesTokenMap(Map<String, AesToken> aesTokenMap) {
97          this.aesTokenMap = aesTokenMap;
98      }
99  
100     public WeixinMessageMatcher getWeixinMessageMatcher() {
101         return weixinMessageMatcher;
102     }
103 
104     public void setWeixinMessageMatcher(WeixinMessageMatcher weixinMessageMatcher) {
105         this.weixinMessageMatcher = weixinMessageMatcher;
106     }
107 }