如何动态获取客户端版本号

package com.xxx.arch.mw.nbp.client.util;

import com.xxx.arch.mw.nbp.client.Client;
import com.xxx.arch.mw.nbp.client.logger.LoggerInit;
import com.xxx.arch.mw.nbp.common.constant.CommonConstants;
import com.xxx.middleware.logger.Logger;

import java.net.URL;
import java.security.CodeSource;

/**
 * @created 2022-10-09 7:29 PM
 * @description:
 */
public class VersionUtils {

    private static final Logger LOGGER = LoggerInit.LOGGER;

    public static final String VERSION = getVersion(Client.class);

    public static String getVersion(Class cls) {
        return getVersion(cls, CommonConstants.EMPTY);
    }

    public static String getVersion(Class cls, String defaultVersion) {
        try {
            // find version info from MANIFEST.MF first
            Package pkg = cls.getPackage();
            String version = null;
            if (pkg != null) {
                version = pkg.getImplementationVersion();
                if (version != null && version.length() > 0) {
                    return version;
                }

                version = pkg.getSpecificationVersion();
                if (version != null && version.length() > 0) {
                    return version;
                }
            }

            // guess version from jar file name if nothing's found from MANIFEST.MF
            CodeSource codeSource = cls.getProtectionDomain().getCodeSource();
            if (codeSource == null) {
                LOGGER.info("No codeSource for class " + cls.getName() + " when getVersion, use default version " + defaultVersion);
                return defaultVersion;
            }

            URL location = codeSource.getLocation();
            if (location == null) {
                LOGGER.info("No location for class " + cls.getName() + " when getVersion, use default version " + defaultVersion);
                return defaultVersion;
            }
            String file = location.getFile();
            if (file != null && file.length() > 0 && file.endsWith(".jar")) {
                version = getFromFile(file);
            }

            // return default version if no version info is found
            return version == null || version.length() == 0 ? defaultVersion : version;
        } catch (Throwable e) {
            // return default version when any exception is thrown
            LOGGER.error("NBP-CLIENT UTIL", "return default version, ignore exception ", e.getMessage(), e);
            return defaultVersion;
        }
    }

    /**
     * get version from file: path/to/group-module-x.y.z.jar, returns x.y.z
     */
    private static String getFromFile(String file) {
        // remove suffix ".jar": "path/to/group-module-x.y.z"
        file = file.substring(0, file.length() - 4);

        // remove path: "group-module-x.y.z"
        int i = file.lastIndexOf('/');
        if (i >= 0) {
            file = file.substring(i + 1);
        }

        // remove group: "module-x.y.z"
        i = file.indexOf("-");
        if (i >= 0) {
            file = file.substring(i + 1);
        }

        // remove module: "x.y.z"
        while (file.length() > 0 && !Character.isDigit(file.charAt(0))) {
            i = file.indexOf("-");
            if (i >= 0) {
                file = file.substring(i + 1);
            } else {
                break;
            }
        }
        return file;
    }
}

在做客户端时,我们经常需要获取当前客户端当前的版本号,为了能够动态获取当前客户端的版本号,可以参考上述工具类

你可能感兴趣的:(python,pycharm,开发语言)