|
@@ -0,0 +1,109 @@
|
|
|
+package cn.com.qmth.examcloud.exchange.inner.service.upyun;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+import com.thoughtworks.xstream.XStream;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.commons.base.helpers.XStreamBuilder;
|
|
|
+import cn.com.qmth.examcloud.commons.base.util.PathUtil;
|
|
|
+import cn.com.qmth.examcloud.commons.base.util.PropertiesUtil;
|
|
|
+
|
|
|
+/**
|
|
|
+ * upyun site manager
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @date 2018年11月21日
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
+ */
|
|
|
+public class UpyunSiteManager {
|
|
|
+
|
|
|
+ private static final Map<String, UpYunClient> CLIENT_HOLDERS = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ private static final Map<String, UpyunSite> SITE_HOLDERS = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ public static void init() {
|
|
|
+ String resoucePath = PathUtil.getResoucePath("upyun.xml");
|
|
|
+ File file = new File(resoucePath);
|
|
|
+
|
|
|
+ XStream xStream = XStreamBuilder.newInstance().build();
|
|
|
+ xStream.allowTypes(new Class[]{UpyunSite.class, List.class});
|
|
|
+ xStream.alias("sites", List.class);
|
|
|
+ xStream.alias("site", UpyunSite.class);
|
|
|
+
|
|
|
+ List<UpyunSite> list = null;
|
|
|
+ try {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ List<UpyunSite> obj = (List<UpyunSite>) xStream.fromXML(file);
|
|
|
+ list = obj;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("EX-520001", "upyun.xml is wrong", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (UpyunSite upyunSite : list) {
|
|
|
+
|
|
|
+ SITE_HOLDERS.put(upyunSite.getId(), upyunSite);
|
|
|
+
|
|
|
+ String upyunId = upyunSite.getUpyunId();
|
|
|
+ String bucketName = PropertiesUtil.getString("$upyun.site." + upyunId + ".bucketName");
|
|
|
+ String userName = PropertiesUtil.getString("$upyun.site." + upyunId + ".userName");
|
|
|
+ String password = PropertiesUtil.getString("$upyun.site." + upyunId + ".password");
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(bucketName)) {
|
|
|
+ throw new StatusException("EX-520002",
|
|
|
+ "bucketName is not configured. upyunId=" + upyunId);
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(userName)) {
|
|
|
+ throw new StatusException("EX-520003",
|
|
|
+ "userName is not configured. upyunId=" + upyunId);
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(password)) {
|
|
|
+ throw new StatusException("EX-520004",
|
|
|
+ "password is not configured. upyunId=" + upyunId);
|
|
|
+ }
|
|
|
+
|
|
|
+ UpYunClient upYunClient = new UpYunClient(bucketName, userName, password);
|
|
|
+
|
|
|
+ CLIENT_HOLDERS.put(upyunSite.getId(), upYunClient);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法注释
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param siteId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static UpYunClient getUpYunClient(String siteId) {
|
|
|
+ UpYunClient upYunClient = CLIENT_HOLDERS.get(siteId);
|
|
|
+
|
|
|
+ if (null == upYunClient) {
|
|
|
+ throw new StatusException("EX-520005", "upYunClient is null");
|
|
|
+ }
|
|
|
+ return upYunClient;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法注释
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param siteId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static UpyunSite getUpyunSite(String siteId) {
|
|
|
+ UpyunSite upyunSite = SITE_HOLDERS.get(siteId);
|
|
|
+
|
|
|
+ if (null == upyunSite) {
|
|
|
+ throw new StatusException("EX-520006", "upyunSite is null");
|
|
|
+ }
|
|
|
+ return upyunSite;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|