View Javadoc
1   package com.foxinmy.weixin4j.util;
2   
3   import java.util.MissingResourceException;
4   import java.util.ResourceBundle;
5   import java.util.logging.Level;
6   
7   import com.alibaba.fastjson.JSON;
8   import com.foxinmy.weixin4j.model.WeixinAccount;
9   
10  /**
11   * 公众号配置信息 class路径下weixin4j.properties文件
12   *
13   * @className Weixin4jConfigUtil
14   * @author jinyu(foxinmy@gmail.com)
15   * @date 2014年10月31日
16   * @since JDK 1.6
17   * @see
18   */
19  public class Weixin4jConfigUtil {
20      private final static String CLASSPATH_PREFIX = "classpath:";
21      private final static String CLASSPATH_VALUE;
22      public final static ClassLoader CLASSLOADER;
23      private static ResourceBundle weixinBundle;
24      static {
25          CLASSLOADER = Thread.currentThread().getContextClassLoader();
26          CLASSPATH_VALUE = CLASSLOADER.getResource("").getPath();
27          try {
28              weixinBundle = ResourceBundle.getBundle(Consts.WEIXIN4J);
29          } catch (MissingResourceException e) {
30              ;
31          }
32      }
33  
34      private final static String WEIXIN4J_PREFIX = "weixin4j";
35  
36      private static String wrapKeyName(String key) {
37          if (!key.startsWith(WEIXIN4J_PREFIX)) {
38              return String.format("%s.%s", WEIXIN4J_PREFIX, key);
39          }
40          return key;
41      }
42  
43      /**
44       * 获取weixin4j.properties文件中的key值
45       *
46       * @param key
47       * @return
48       */
49      public static String getValue(String key) {
50          String wrapKey = wrapKeyName(key);
51          return System.getProperty(wrapKey, weixinBundle.getString(wrapKey));
52      }
53  
54      /**
55       * key不存在时则返回传入的默认值
56       *
57       * @param key
58       * @param defaultValue
59       * @return
60       */
61      public static String getValue(String key, String defaultValue) {
62          String value = defaultValue;
63          try {
64              value = getValue(key);
65              if (StringUtil.isBlank(value)) {
66                  value = defaultValue;
67              }
68          } catch (MissingResourceException e) {
69              ;
70          } catch (NullPointerException e) {
71              ;
72          }
73          return value;
74      }
75  
76      /**
77       * 判断属性是否存在[classpath:]如果存在则拼接项目路径后返回 一般用于文件的绝对路径获取
78       *
79       * @param key
80       * @return
81       */
82      public static String getClassPathValue(String key) {
83          return replaceClassPathValue(getValue(key));
84      }
85  
86      /**
87       *
88       * @param key
89       * @param defaultValue
90       * @return
91       */
92      public static String getClassPathValue(String key, String defaultValue) {
93          return replaceClassPathValue(getValue(key, defaultValue));
94      }
95  
96      public static String replaceClassPathValue(String value) {
97          return value.replaceFirst(CLASSPATH_PREFIX, CLASSPATH_VALUE);
98      }
99  
100     /**
101      * 获取微信账号信息
102      *
103      * @return 微信账号信息
104      */
105     public static WeixinAccount getWeixinAccount() {
106         WeixinAccount account = null;
107         try {
108             account = JSON.parseObject(getValue("account"), WeixinAccount.class);
109         } catch (NullPointerException e) {
110             System.err.println("'weixin4j.account' key not found in weixin4j.properties.");
111         } catch (MissingResourceException e) {
112             System.err.println("'weixin4j.account' key not found in weixin4j.properties.");
113         }
114         return account;
115     }
116 
117     public static Level getJdkLoggerLevel(){
118         try {
119             Level level = Level.parse(getValue("jdkLogger.level", "OFF"));
120             return level;
121         }catch (IllegalArgumentException ex){
122             return Level.OFF;
123         }
124     }
125 }