All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.linksfield.cube.partnersdk.rest.EndpointManager Maven / Gradle / Ivy

package net.linksfield.cube.partnersdk.rest;

import lombok.Getter;
import net.linksfield.cube.partnersdk.configuration.EndpointPropertiesProxy;

import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;

/**
 * @ClassName EndpointManager
 * @Description 所有的API端口配置管理
 * @Author James.hu
 * @Date 2023/3/15
 **/
public class EndpointManager {
    @Getter
    private EndpointPropertiesProxy propertiesProxy;

    public EndpointManager() {
        this.propertiesProxy = loadEndpointFromProperties();
    }

    private static final String DEFAULT_ENDPOINTS_FILE = "endpoints.properties";

    /**
     * 从classpath加载对应的API配置文件, 默认情况下读取endpoint-prod.properties
     * 可以通过配置java系统变量 -Dlinksfield.partner.env 调整读取的配置文件
     * 可选系统变量 -Dlinksfield.partner.endpoint.path 自定义读取外部配置文件
     * @return
     */
    public EndpointPropertiesProxy loadEndpointFromProperties() {
        // 首选从公共Url文件读取所有endpoint信息
        EndpointPropertiesProxy properties = new EndpointPropertiesProxy();
        try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(DEFAULT_ENDPOINTS_FILE)) {
            properties.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 再单独加载指定的环境使用的properties, 覆盖公共端点配置
        String env = System.getProperty("linksfield.cube.partner.env", "prod");
        String customConfigPath = System.getProperty("linksfield.cube.partner.endpoint.path");
        String filename = Optional.ofNullable(customConfigPath).orElse("endpoint-" + env.toLowerCase() + ".properties");
        Properties envProperties = new Properties();
        try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filename)) {
            envProperties.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 覆盖配置文件
        envProperties.entrySet().forEach(entry -> {
            properties.put(entry.getKey(), entry.getValue());
        });

        properties.bindingField();

        return properties;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy