123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package cn.com.qmth.examcloud.web.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.exception.StatusException;
- import cn.com.qmth.examcloud.commons.helpers.XStreamBuilder;
- import cn.com.qmth.examcloud.commons.util.PathUtil;
- import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
- /**
- * 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("520001", "upyun.xml is wrong", e);
- }
- for (UpyunSite upyunSite : list) {
- SITE_HOLDERS.put(upyunSite.getId(), upyunSite);
- String upyunId = upyunSite.getUpyunId();
- String bucketName = PropertyHolder.getString("$upyun.site." + upyunId + ".bucketName");
- String userName = PropertyHolder.getString("$upyun.site." + upyunId + ".userName");
- String password = PropertyHolder.getString("$upyun.site." + upyunId + ".password");
- String domain = PropertyHolder.getString("$upyun.site." + upyunId + ".domain");
- if (StringUtils.isBlank(bucketName)) {
- throw new StatusException("520002",
- "bucketName is not configured. upyunId=" + upyunId);
- }
- if (StringUtils.isBlank(userName)) {
- throw new StatusException("520003",
- "userName is not configured. upyunId=" + upyunId);
- }
- if (StringUtils.isBlank(password)) {
- throw new StatusException("520004",
- "password is not configured. upyunId=" + upyunId);
- }
- if (StringUtils.isBlank(domain)) {
- throw new StatusException("520004", "domain is not configured. upyunId=" + upyunId);
- }
- if (null == CLIENT_HOLDERS.get(upyunSite.getId())) {
- UpYunClient upYunClient = new UpYunClient(bucketName, userName, password, domain);
- 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("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("520006", "upyunSite is null");
- }
- return upyunSite;
- }
- }
|