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

com.dahuatech.icc.oauth.handle.InitVersionProcessor Maven / Gradle / Ivy

There is a newer version: 1.0.13.7
Show newest version
package com.dahuatech.icc.oauth.handle;

import com.dahuatech.hutool.core.util.StrUtil;
import com.dahuatech.hutool.http.HttpRequest;
import com.dahuatech.hutool.http.HttpResponse;
import com.dahuatech.hutool.http.HttpStatus;
import com.dahuatech.hutool.log.Log;
import com.dahuatech.hutool.log.LogFactory;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.IccHttpHttpRequest;
import com.dahuatech.icc.oauth.http.IccResponse;
import com.dahuatech.icc.oauth.http.Versions;
import com.dahuatech.icc.oauth.model.v202010.HttpConfigInfo;
import com.dahuatech.icc.util.BeanUtil;
import com.dahuatech.icc.util.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class InitVersionProcessor {

    /** 各个子系统的版本 */
    public static volatile Map systemVersionMap = new ConcurrentHashMap<>();
    public static Map hostMap = new ConcurrentHashMap<>();

    private final Log logger = LogFactory.get();

    /** 2步初始化资源系统版本信息 每隔一天获取下版本信息*/
    public void initSystemVersion(HttpConfigInfo httpConfigInfo) {
        if(StringUtils.isEmpty(httpConfigInfo.getHost())){
            logger.warn("Init version number fail, reason: host is null.");
            return;
        }
        if(hostMap.get(httpConfigInfo.getHost()) != null
                && hostMap.get(httpConfigInfo.getHost()).after(new Date())){
            logger.debug("Init version number:" + httpConfigInfo.getHost() + " not expired");
            return;
        }
        logger.debug("Init version number:" + httpConfigInfo.getHost() + "start");
        // 第一步-先获取evo-brm版本
        String baseVersion = null;
        String BRM_BASE_VERSION_URL = "/evo-apigw/evo-brm/version";
        try {
            HttpRequest request = HttpRequest.get(httpConfigInfo.getPrefixUrl() + BRM_BASE_VERSION_URL);
            request.timeout(80000);
            request.setReadTimeout(80000);
            HttpResponse evoVersion = request.execute();
            if (evoVersion.getStatus() == HttpStatus.HTTP_OK) {
                String versionBody = evoVersion.body();
                Versions versions = BeanUtil.toBean(versionBody, Versions.class);
                if (versions.isSuccess()) {
                    baseVersion = versions.getData().getVersion();
                }
            }
        } catch (Exception e) {
            logger.error(
                    "init icc subsystem version error [{}],error [{}]", httpConfigInfo.getPrefixUrl() + BRM_BASE_VERSION_URL, e);
            e.printStackTrace();
        }
        try {
            // 第二步,根据evo-brm获取各个子系统的版本
            if (StrUtil.isNotBlank(baseVersion)) {
                String BRM_VERSION_URL = "/evo-apigw/evo-brm/%S/config/get-version";
                IccHttpHttpRequest subSystemRequest =
                        new IccHttpHttpRequest(String.format(httpConfigInfo.getPrefixUrl() + BRM_VERSION_URL, baseVersion));
                HttpResponse evoVersions = subSystemRequest.executeResponse();
                if (evoVersions.getStatus() == HttpStatus.HTTP_OK) {
                    String evoVersionBody = evoVersions.body();
                    SystemVersions systemVersions = BeanUtil.toBean(evoVersionBody, SystemVersions.class);
                    if (systemVersions.isSuccess()
                            && systemVersions.getData() != null
                            && systemVersions.getData().getVersionInfo().size() > 0) {
                        List versionInfos = systemVersions.getData().getVersionInfo();
                        for (VersionInfo vi : versionInfos) {
                            // 版本为空,不给与设置
                            if (StrUtil.isBlank(vi.getVersion())) {
                                continue;
                            }
                            systemVersionMap.put(httpConfigInfo.getHost() + vi.getSystemName(), vi.getVersion());
                        }
                    }
                }
            }
        } catch (ClientException e) {
            logger.error("fetch subSystem version error [{}]", e);
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,1);
        hostMap.put(httpConfigInfo.getHost(),calendar.getTime());
        logger.debug("Init version number over, version number:{}.", httpConfigInfo.getHost());
    }

    public static synchronized InitVersionProcessor getInstance() {
        return InitVersionProcessor.SingleHolder.INSTANCE;
    }

    static class VersionInfo {

        private String systemName;
        private String version;

        public String getSystemName() {
            return systemName;
        }

        public void setSystemName(String systemName) {
            this.systemName = systemName;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        @Override
        public String toString() {
            return "VersionInfo{"
                    + "systemName='"
                    + systemName
                    + '\''
                    + ", version='"
                    + version
                    + '\''
                    + '}';
        }
    }

    static class SystemVersions extends IccResponse {

        private VersionInfoDO data;

        public VersionInfoDO getData() {
            return data;
        }

        public void setData(VersionInfoDO data) {
            this.data = data;
        }

        @Override
        public String toString() {
            return "SystemVersions{" + "data=" + data + '}';
        }

        public class VersionInfoDO {

            private List versionInfo;

            public List getVersionInfo() {
                return versionInfo;
            }

            public void setVersionInfo(List versionInfo) {
                this.versionInfo = versionInfo;
            }
        }
    }

    private static class SingleHolder{
        public final static InitVersionProcessor INSTANCE = new InitVersionProcessor();
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy