<commit_before>package com.yeepay.g3.sdk.yop.config;

import com.yeepay.g3.sdk.yop.YopServiceException;
import com.yeepay.g3.sdk.yop.utils.JsonUtils;
import com.yeepay.g3.sdk.yop.utils.ValidateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * title: sdk配置（老版本）<br>
 * description:描述<br>
 * Copyright: Copyright (c)2011<br>
 * Company: 易宝支付(YeePay)<br>
 *
 * @author dreambt
 * @version 1.0.0
 * @since 2016/12/26 下午3:50
 */
public final class SDKConfigSupport {

    private static final Logger LOGGER = Logger.getLogger(SDKConfigSupport.class);

    private static final String SDK_CONFIG_FILE_PROPERTY_KEY = "yop.sdk.config.file";

    private static final String SDK_CONFIG_FILE_SEPARATOR = ",";

    private static final String SDK_CONFIG_DIR = "config";

    private static final String DEFAULT_SDK_CONFIG_FILE_PATH = "config/yop_sdk_config_default.json";

    private static final String DEFAULT_SDK_CONFIG_KEY = "default";

    private static final Pattern SDK_CONFIG_FILE_NAME_PATTERN = Pattern.compile("^yop_sdk_config_(.+).json$");

    private static final ConcurrentMap<String, SDKConfig> CONFIGS = new ConcurrentHashMap<String, SDKConfig>();

    private static SDKConfig defaultSDKConfig;

    private static boolean customDefault;

    private static volatile boolean inited = false;

    public static SDKConfig getDefaultSDKConfig() {
        initSDKConfig();
        return defaultSDKConfig;
    }

    public static Map<String, SDKConfig> getSDKConfigs() {
        initSDKConfig();
        return Collections.unmodifiableMap(CONFIGS);
    }

    public static boolean isCustomDefault() {
        initSDKConfig();
        return customDefault;
    }

    private static void initSDKConfig() {
        if (inited) {
            return;
        }
        synchronized (SDKConfigSupport.class) {
            if (inited) {
                return;
            }
            String configFileProperty = System.getProperty(SDK_CONFIG_FILE_PROPERTY_KEY);
            if (StringUtils.isNotEmpty(configFileProperty)) {
                String[] configFiles = StringUtils.split(configFileProperty, SDK_CONFIG_FILE_SEPARATOR);
                for (String absolutePath : configFiles) {
                    String fileName = StringUtils.substringAfterLast(absolutePath, File.separator);
                    Matcher matcher = SDK_CONFIG_FILE_NAME_PATTERN.matcher(fileName);
                    if (matcher.matches()) {
                        SDKConfig sdkConfig = loadConfig(absolutePath);
                        ValidateUtils.checkCustomSDKConfig(sdkConfig);
                        String fileNameSuffix = matcher.group(1);
                        if (StringUtils.equals(fileNameSuffix, DEFAULT_SDK_CONFIG_KEY)) {
                            defaultSDKConfig = sdkConfig;
                            CONFIGS.put(StringUtils.replace(sdkConfig.getAppKey(), ":", ""), sdkConfig);
                        } else {
                            CONFIGS.put(fileNameSuffix, sdkConfig);
                        }
                    } else {
                        LOGGER.warn("Illegal SDkConfig File Name:" + fileName);
                    }
                }
            } else {
                List<String> fileNames = loadConfigFilesFromClassPath();
                if (CollectionUtils.isNotEmpty(fileNames)) {
                    for (String fileName : fileNames) {
                        Matcher matcher = SDK_CONFIG_FILE_NAME_PATTERN.matcher(fileName);
                        if (matcher.matches()) {
                            SDKConfig sdkConfig = loadConfig(SDK_CONFIG_DIR + File.separator + fileName);
                            ValidateUtils.checkCustomSDKConfig(sdkConfig);
                            String fileNameSuffix = matcher.group(1);
                            if (StringUtils.equals(fileNameSuffix, DEFAULT_SDK_CONFIG_KEY)) {
                                defaultSDKConfig = sdkConfig;
                                CONFIGS.put(StringUtils.replace(sdkConfig.getAppKey(), ":", ""), sdkConfig);
                            } else {
                                CONFIGS.put(fileNameSuffix, sdkConfig);
                            }
                        } else {
                            LOGGER.warn("Illegal SDkConfig File Name:" + fileName);
                        }
                    }
                }
            }
            if (defaultSDKConfig == null) {
                if (CONFIGS.size() == 1) {
                    defaultSDKConfig = CONFIGS.values().iterator().next();
                    customDefault = true;
                } else {
                    defaultSDKConfig = loadConfig(DEFAULT_SDK_CONFIG_FILE_PATH);
                    customDefault = false;
                }
            } else {
                customDefault = true;
            }
            inited = true;
        }
    }

    private static List<String> loadConfigFilesFromClassPath() {
        List<String> filenames = new ArrayList<String>();
        try {
            //无法读取jar包的目录
            InputStream in = ConfigUtils.getContextClassLoader().getResourceAsStream(SDK_CONFIG_DIR);
            if (in != null) {
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String resource;
                while ((resource = br.readLine()) != null) {
                    filenames.add(resource);
                }
            }
        } catch (Exception ex) {
            LOGGER.debug("Unable to read sdk config dir.");<commit_msg>refactor: 优化sdk配置文件读取代码<commit_after>package com.yeepay.g3.sdk.yop.config;

import com.yeepay.g3.sdk.yop.YopServiceException;
import com.yeepay.g3.sdk.yop.utils.JsonUtils;
import com.yeepay.g3.sdk.yop.utils.ValidateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * title: sdk配置（老版本）<br>
 * description:描述<br>
 * Copyright: Copyright (c)2011<br>
 * Company: 易宝支付(YeePay)<br>
 *
 * @author dreambt
 * @version 1.0.0
 * @since 2016/12/26 下午3:50
 */
public final class SDKConfigSupport {

    private static final Logger LOGGER = Logger.getLogger(SDKConfigSupport.class);

    private static final String SDK_CONFIG_FILE_PROPERTY_KEY = "yop.sdk.config.file";

    private static final String SDK_CONFIG_FILE_SEPARATOR = ",";

    private static final String SDK_CONFIG_DIR = "config";

    private static final String DEFAULT_SDK_CONFIG_FILE_PATH = "config/yop_sdk_config_default.json";

    private static final String DEFAULT_SDK_CONFIG_KEY = "default";

    private static final Pattern SDK_CONFIG_FILE_NAME_PATTERN = Pattern.compile("^yop_sdk_config_(.+).json$");

    private static final ConcurrentMap<String, SDKConfig> CONFIGS = new ConcurrentHashMap<String, SDKConfig>();

    private static SDKConfig defaultSDKConfig;

    private static boolean customDefault;

    private static volatile boolean inited = false;

    public static SDKConfig getDefaultSDKConfig() {
        initSDKConfig();
        return defaultSDKConfig;
    }

    public static Map<String, SDKConfig> getSDKConfigs() {
        initSDKConfig();
        return Collections.unmodifiableMap(CONFIGS);
    }

    public static boolean isCustomDefault() {
        initSDKConfig();
        return customDefault;
    }

    private static void initSDKConfig() {
        if (inited) {
            return;
        }
        synchronized (SDKConfigSupport.class) {
            if (inited) {
                return;
            }
            String configFileProperty = System.getProperty(SDK_CONFIG_FILE_PROPERTY_KEY);
            if (StringUtils.isNotEmpty(configFileProperty)) {
                String[] configFiles = StringUtils.split(configFileProperty, SDK_CONFIG_FILE_SEPARATOR);
                for (String absolutePath : configFiles) {
                    String fileName = StringUtils.substringAfterLast(absolutePath, File.separator);
                    storeSDKConfig(fileName, absolutePath);
                }
            } else {
                List<String> fileNames = loadConfigFilesFromClassPath();
                if (CollectionUtils.isNotEmpty(fileNames)) {
                    for (String fileName : fileNames) {
                        String filePath = SDK_CONFIG_DIR + File.separator + fileName;
                        storeSDKConfig(fileName, filePath);
                    }
                }
            }
            if (defaultSDKConfig == null) {
                if (CONFIGS.size() == 1) {
                    defaultSDKConfig = CONFIGS.values().iterator().next();
                    customDefault = true;
                } else {
                    defaultSDKConfig = loadConfig(DEFAULT_SDK_CONFIG_FILE_PATH);
                    customDefault = false;
                }
            } else {
                customDefault = true;
            }
            inited = true;
        }
    }

    private static void storeSDKConfig(String fileName, String filePath) {
        Matcher matcher = SDK_CONFIG_FILE_NAME_PATTERN.matcher(fileName);
        if (matcher.matches()) {
            SDKConfig sdkConfig = loadConfig(filePath);
            ValidateUtils.checkCustomSDKConfig(sdkConfig);
            if (StringUtils.equals(matcher.group(1), DEFAULT_SDK_CONFIG_KEY)) {
                defaultSDKConfig = sdkConfig;
            }
            CONFIGS.put(StringUtils.replace(sdkConfig.getAppKey(), ":", ""), sdkConfig);
        } else {
            LOGGER.warn("Illegal SDkConfig File Name:" + fileName);
        }
    }

    private static List<String> loadConfigFilesFromClassPath() {
        List<String> filenames = new ArrayList<String>();
        try {
            //无法读取jar包的目录
            InputStream in = ConfigUtils.getContextClassLoader().getResourceAsStream(SDK_CONFIG_DIR);
            if (in != null) {
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String resource;
                while ((resource = br.readLine()) != null) {
                    filenames.add(resource);
                }
            }
        } catch (Exception ex) {
            LOGGER.debug("Unable to read sdk config dir.");
{'fraction_non_alphanumeric': 0.09132124352331607, 'fraction_numerical': 0.0050888230940044415, 'mean_word_length': 2.0786100825975504, 'pattern_counts': {'":': 3, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3871899', 'n_tokens_mistral': 3008, 'n_tokens_neox': 2860, 'n_words': 562}