Java tutorial
/* * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved. * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * Created on 2014-3-25 */ package ext.sns.config; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.apache.commons.lang3.ArrayUtils; import play.libs.Yaml; import ext.sns.auth.AuthResponse; /** * * * @ClassName: ConfigManager * @Description: SNS?? * @date 2014-3-25 ?7:32:25 * @author ShenTeng * */ public class ConfigManager { /** SNS? */ private static SNSConfig snsConfig; /** Map<????,???> */ private static Map<String, ProviderConfig> providerConfigMap; public static final String TYPE_KEY = "!type!"; /** * ? */ public static void loadConfig() { snsConfig = (SNSConfig) Yaml.load("snsConfig.yml"); // ?? providerConfigMap = new HashMap<String, ProviderConfig>(); for (ProviderConfig providerConfig : snsConfig.getProviderConfigList()) { providerConfigMap.put(providerConfig.getName(), providerConfig); } } /** * ????? * * @param typeList ??? * * @return ????? */ public static List<String> getProviderNameByTypes(List<String> typeList) { return getProviderNameByTypes(typeList.toArray(new String[0])); } /** * ????? * * @param providerName ???? * @return */ public static boolean checkProviderName(String providerName) { return providerConfigMap.containsKey(providerName); } /** * ????? * * @param types ??? * * @return ????? */ public static List<String> getProviderNameByTypes(String... types) { Set<String> keySet = new HashSet<>(); for (Entry<String, ProviderConfig> e : providerConfigMap.entrySet()) { for (String type : e.getValue().getTypes()) { if (ArrayUtils.contains(types, type)) { keySet.add(e.getKey()); break; } } } return new ArrayList<>(keySet); } /** * SNS? * * @return SNS? */ public static SNSConfig getSNSConfig() { return snsConfig; } /** * ????????? * * @param providerName ???? * @return ??? */ public static ProviderConfig getProviderConfigByName(String providerName) { return providerConfigMap.get(providerName); } /** * ??????? * * @param authResponse ?? * @return ???null - ?? */ public static ProviderConfig getProviderConfigByAuthResponse(AuthResponse authResponse) { for (ProviderConfig cfg : providerConfigMap.values()) { if (cfg.isMatchAuthResponse(authResponse)) { return cfg; } } return null; } }