SDKConfig.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * Licensed Property to Sand Co., Ltd.
  3. * <p>
  4. * (C) Copyright of Sand Co., Ltd. 2010
  5. * All Rights Reserved.
  6. * <p>
  7. * <p>
  8. * Modification History:
  9. * =============================================================================
  10. * Author Date Description
  11. * ------------ ---------- ---------------------------------------------------
  12. * 企业产品团队 2016-10-12 基本参数工具类.
  13. * =============================================================================
  14. */
  15. package cn.com.sandpay.cashier.sdk;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.Properties;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. /**
  26. * @version 2.0.0
  27. * @ClassName: SDKConfig
  28. * @Description:
  29. */
  30. public class SDKConfig {
  31. public static Logger logger = LoggerFactory.getLogger(SDKConfig.class);
  32. public static final String FILE_NAME = "sand_sdk.properties";
  33. /**
  34. * 通讯地址.
  35. */
  36. private String url;
  37. /**
  38. * 商户号.
  39. */
  40. private String mid;
  41. /**
  42. * 平台商户号.
  43. */
  44. private String plMid;
  45. /**
  46. * 商户私钥证书路径.
  47. */
  48. private String signCertPath;
  49. /**
  50. * 商户私钥证书密码.
  51. */
  52. private String signCertPwd;
  53. /**
  54. * 杉德证书路径.
  55. */
  56. private String sandCertPath;
  57. /**
  58. * 配置文件中的通讯地址常量.
  59. */
  60. public static final String SDK_URL = "sandsdk.url";
  61. /**
  62. * 配置文件中的商户号常量.
  63. */
  64. public static final String SDK_MID = "sandsdk.mid";
  65. /**
  66. * 配置文件中的平台商户号常量.
  67. */
  68. public static final String SDK_PL_MID = "sandsdk.plMid";
  69. /**
  70. * 配置文件中的商户私钥证书路径常量.
  71. */
  72. public static final String SDK_SIGN_CERT_PATH = "sandsdk.signCert.path";
  73. /**
  74. * 配置文件中的商户私钥证书密码常量.
  75. */
  76. public static final String SDK_SIGN_CERT_PWD = "sandsdk.signCert.pwd";
  77. /**
  78. * 配置文件中的杉德证书路径常量.
  79. */
  80. public static final String SDK_SNAD_CERT_PATH = "sandsdk.sandCert.path";
  81. /**
  82. * 操作对象.
  83. */
  84. private static SDKConfig config = new SDKConfig();
  85. /**
  86. * 属性文件对象.
  87. */
  88. private Properties properties;
  89. private SDKConfig() {
  90. super();
  91. }
  92. /**
  93. * 获取config对象.
  94. *
  95. * @return
  96. */
  97. public static SDKConfig getConfig() {
  98. return config;
  99. }
  100. /**
  101. * 从properties文件加载
  102. *
  103. * @param rootPath 不包含文件名的目录.
  104. */
  105. public void loadPropertiesFromPath(String rootPath) {
  106. if (StringUtils.isNotBlank(rootPath)) {
  107. logger.info("从路径读取配置文件: " + rootPath + File.separator + FILE_NAME);
  108. File file = new File(rootPath + File.separator + FILE_NAME);
  109. InputStream in = null;
  110. if (file.exists()) {
  111. try {
  112. in = new FileInputStream(file);
  113. properties = new Properties();
  114. properties.load(in);
  115. loadProperties(properties);
  116. } catch (FileNotFoundException e) {
  117. logger.error(e.getMessage(), e);
  118. } catch (IOException e) {
  119. logger.error(e.getMessage(), e);
  120. } finally {
  121. if (null != in) {
  122. try {
  123. in.close();
  124. } catch (IOException e) {
  125. logger.error(e.getMessage(), e);
  126. }
  127. }
  128. }
  129. } else {
  130. // 由于此时可能还没有完成LOG的加载,因此采用标准输出来打印日志信息
  131. logger.error(rootPath + FILE_NAME + "不存在,加载参数失败");
  132. }
  133. } else {
  134. loadPropertiesFromSrc();
  135. }
  136. }
  137. /**
  138. * 从classpath路径下加载配置参数
  139. */
  140. public void loadPropertiesFromSrc() {
  141. InputStream in = null;
  142. try {
  143. logger.info("从classpath: " + SDKConfig.class.getClassLoader().getResource("").getPath() + " 获取属性文件" + FILE_NAME);
  144. in = SDKConfig.class.getClassLoader().getResourceAsStream(FILE_NAME);
  145. if (null != in) {
  146. properties = new Properties();
  147. try {
  148. properties.load(in);
  149. } catch (IOException e) {
  150. throw e;
  151. }
  152. } else {
  153. logger.error(FILE_NAME + "属性文件未能在classpath指定的目录下 " + SDKConfig.class.getClassLoader().getResource("").getPath() + " 找到!");
  154. return;
  155. }
  156. loadProperties(properties);
  157. } catch (IOException e) {
  158. logger.error(e.getMessage(), e);
  159. } finally {
  160. if (null != in) {
  161. try {
  162. in.close();
  163. } catch (IOException e) {
  164. logger.error(e.getMessage(), e);
  165. }
  166. }
  167. }
  168. }
  169. /**
  170. * 根据传入的 {@link #load(java.util.Properties)}对象设置配置参数
  171. *
  172. * @param pro
  173. */
  174. public void loadProperties(Properties pro) {
  175. logger.info("开始从属性文件中加载配置项");
  176. String value = null;
  177. value = pro.getProperty(SDK_URL);
  178. if (!StringUtils.isEmpty(value)) {
  179. this.url = value.trim();
  180. logger.info("配置项:通讯地址==>" + SDK_URL + "==>" + value + " 已加载");
  181. }
  182. value = pro.getProperty(SDK_MID);
  183. if (!StringUtils.isEmpty(value)) {
  184. this.mid = value.trim();
  185. logger.info("配置项:商户号==>" + SDK_MID + "==>" + value + " 已加载");
  186. }
  187. value = pro.getProperty(SDK_PL_MID);
  188. if (!StringUtils.isEmpty(value)) {
  189. this.plMid = value.trim();
  190. logger.info("配置项:平台商户号==>" + SDK_PL_MID + "==>" + value + " 已加载");
  191. }
  192. value = pro.getProperty(SDK_SIGN_CERT_PATH);
  193. if (!StringUtils.isEmpty(value)) {
  194. this.signCertPath = value.trim();
  195. logger.info("配置项:商户私钥证书路径==>" + SDK_SIGN_CERT_PATH + "==>" + value + " 已加载");
  196. }
  197. value = pro.getProperty(SDK_SIGN_CERT_PWD);
  198. if (!StringUtils.isEmpty(value)) {
  199. this.signCertPwd = value.trim();
  200. logger.info("配置项:商户私钥证书密码==>" + SDK_SIGN_CERT_PWD + "==>" + value + " 已加载");
  201. }
  202. value = pro.getProperty(SDK_SNAD_CERT_PATH);
  203. if (!StringUtils.isEmpty(value)) {
  204. this.sandCertPath = value.trim();
  205. logger.info("配置项:杉德公钥证书路径==>" + SDK_SNAD_CERT_PATH + "==>" + value + " 已加载");
  206. }
  207. }
  208. public String getUrl() {
  209. return url;
  210. }
  211. public void setUrl(String url) {
  212. this.url = url;
  213. }
  214. public String getMid() {
  215. return mid;
  216. }
  217. public void setMid(String mid) {
  218. this.mid = mid;
  219. }
  220. public String getPlMid() {
  221. return plMid;
  222. }
  223. public void setPlMid(String plMid) {
  224. this.plMid = plMid;
  225. }
  226. public String getSignCertPath() {
  227. return signCertPath;
  228. }
  229. public void setSignCertPath(String signCertPath) {
  230. this.signCertPath = signCertPath;
  231. }
  232. public String getSignCertPwd() {
  233. return signCertPwd;
  234. }
  235. public void setSignCertPwd(String signCertPwd) {
  236. this.signCertPwd = signCertPwd;
  237. }
  238. public String getSandCertPath() {
  239. return sandCertPath;
  240. }
  241. public void setSandCertPath(String sandCertPath) {
  242. this.sandCertPath = sandCertPath;
  243. }
  244. public Properties getProperties() {
  245. return properties;
  246. }
  247. public void setProperties(Properties properties) {
  248. this.properties = properties;
  249. }
  250. }