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 CLIENT_HOLDERS = new ConcurrentHashMap<>(); private static final Map 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 list = null; try { @SuppressWarnings("unchecked") List obj = (List) 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; } }