UpyunSiteManager.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package cn.com.qmth.examcloud.web.upyun;
  2. import java.io.File;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.concurrent.ConcurrentHashMap;
  6. import org.apache.commons.lang3.StringUtils;
  7. import com.thoughtworks.xstream.XStream;
  8. import cn.com.qmth.examcloud.commons.exception.StatusException;
  9. import cn.com.qmth.examcloud.commons.helpers.XStreamBuilder;
  10. import cn.com.qmth.examcloud.commons.util.PathUtil;
  11. import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
  12. /**
  13. * upyun site manager
  14. *
  15. * @author WANGWEI
  16. * @date 2018年11月21日
  17. * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  18. */
  19. public class UpyunSiteManager {
  20. private static final Map<String, UpYunClient> CLIENT_HOLDERS = new ConcurrentHashMap<>();
  21. private static final Map<String, UpyunSite> SITE_HOLDERS = new ConcurrentHashMap<>();
  22. public static void init() {
  23. String resoucePath = PathUtil.getResoucePath("upyun.xml");
  24. File file = new File(resoucePath);
  25. XStream xStream = XStreamBuilder.newInstance().build();
  26. xStream.allowTypes(new Class[]{UpyunSite.class, List.class});
  27. xStream.alias("sites", List.class);
  28. xStream.alias("site", UpyunSite.class);
  29. List<UpyunSite> list = null;
  30. try {
  31. @SuppressWarnings("unchecked")
  32. List<UpyunSite> obj = (List<UpyunSite>) xStream.fromXML(file);
  33. list = obj;
  34. } catch (Exception e) {
  35. throw new StatusException("520001", "upyun.xml is wrong", e);
  36. }
  37. for (UpyunSite upyunSite : list) {
  38. SITE_HOLDERS.put(upyunSite.getId(), upyunSite);
  39. String upyunId = upyunSite.getUpyunId();
  40. String bucketName = PropertyHolder.getString("$upyun.site." + upyunId + ".bucketName");
  41. String userName = PropertyHolder.getString("$upyun.site." + upyunId + ".userName");
  42. String password = PropertyHolder.getString("$upyun.site." + upyunId + ".password");
  43. String domain = PropertyHolder.getString("$upyun.site." + upyunId + ".domain");
  44. if (StringUtils.isBlank(bucketName)) {
  45. throw new StatusException("520002",
  46. "bucketName is not configured. upyunId=" + upyunId);
  47. }
  48. if (StringUtils.isBlank(userName)) {
  49. throw new StatusException("520003",
  50. "userName is not configured. upyunId=" + upyunId);
  51. }
  52. if (StringUtils.isBlank(password)) {
  53. throw new StatusException("520004",
  54. "password is not configured. upyunId=" + upyunId);
  55. }
  56. if (StringUtils.isBlank(domain)) {
  57. throw new StatusException("520004", "domain is not configured. upyunId=" + upyunId);
  58. }
  59. if (null == CLIENT_HOLDERS.get(upyunSite.getId())) {
  60. UpYunClient upYunClient = new UpYunClient(bucketName, userName, password, domain);
  61. CLIENT_HOLDERS.put(upyunSite.getId(), upYunClient);
  62. }
  63. }
  64. }
  65. /**
  66. * 方法注释
  67. *
  68. * @author WANGWEI
  69. * @param siteId
  70. * @return
  71. */
  72. public static UpYunClient getUpYunClient(String siteId) {
  73. UpYunClient upYunClient = CLIENT_HOLDERS.get(siteId);
  74. if (null == upYunClient) {
  75. throw new StatusException("520005", "upYunClient is null");
  76. }
  77. return upYunClient;
  78. }
  79. /**
  80. * 方法注释
  81. *
  82. * @author WANGWEI
  83. * @param siteId
  84. * @return
  85. */
  86. public static UpyunSite getUpyunSite(String siteId) {
  87. UpyunSite upyunSite = SITE_HOLDERS.get(siteId);
  88. if (null == upyunSite) {
  89. throw new StatusException("520006", "upyunSite is null");
  90. }
  91. return upyunSite;
  92. }
  93. }