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

org.zodiac.sdk.log.LogCode2Description Maven / Gradle / Ivy

The newest version!
package org.zodiac.sdk.log;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.zodiac.sdk.log.util.ReportUtil;
import org.zodiac.sdk.toolkit.space.SpaceId;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

public class LogCode2Description {

    private static final Map LOG_CODE_2_DESCRIPTION_MAP = new ConcurrentHashMap<>();

    public static String convert(String spaceName, String code) {
        return convert(SpaceId.withSpaceName(spaceName), code);
    }

    public static String convert(SpaceId spaceId, String code) {
        LogCode2Description logCode2Description = null;
        if (isCodeSpaceInitialized(spaceId)) {
            logCode2Description = LOG_CODE_2_DESCRIPTION_MAP.get(spaceId);
        } else {
            logCode2Description = create(spaceId);
        }

        return logCode2Description.convert(code);
    }

    public static LogCode2Description create(String spaceName) {
        return create(SpaceId.withSpaceName(spaceName));
    }

    public static LogCode2Description create(SpaceId spaceId) {
        if (isCodeSpaceInitialized(spaceId)) {
            ReportUtil.reportWarn("Code space: \"" + spaceId.getSpaceName()
                                  + "\" is already initialized!");
            return LOG_CODE_2_DESCRIPTION_MAP.get(spaceId);
        }

        synchronized (spaceId) {
            if (isCodeSpaceInitialized(spaceId)) {
                ReportUtil.reportWarn("Code space: \"" + spaceId.getSpaceName()
                                      + "\" is already initialized!");
                return LOG_CODE_2_DESCRIPTION_MAP.get(spaceId);
            }
            LogCode2Description logCode2Description = doCreate(spaceId);
            ReportUtil.reportInfo("Code Space: \"" + spaceId.getSpaceName() + "\" init ok");
            return logCode2Description;
        }
    }

    private static LogCode2Description doCreate(SpaceId spaceId) {
        LogCode2Description logCode2Description = new LogCode2Description(spaceId);
        LOG_CODE_2_DESCRIPTION_MAP.put(spaceId, logCode2Description);
        return logCode2Description;
    }

    private static boolean isCodeSpaceInitialized(SpaceId spaceId) {
        return LOG_CODE_2_DESCRIPTION_MAP.containsKey(spaceId);
    }

    public static void removeCodeSpace(String spaceName) {
        removeCodeSpace(SpaceId.withSpaceName(spaceName));
    }

    public static void removeCodeSpace(SpaceId spaceId) {
        if (spaceId == null || !isCodeSpaceInitialized(spaceId)) {
            return;
        }

        LOG_CODE_2_DESCRIPTION_MAP.remove(spaceId);
    }

    private String     logFormat;
    private Map codeMap = new ConcurrentHashMap<>();

    private LogCode2Description(SpaceId spaceId) {
        logFormat = spaceId.getSpaceName().toUpperCase() + "-%s: %s";
        String prefix = spaceId.getSpaceName().replace(".", "/") + "/log-codes";
        String encoding = Locale.getDefault().toString();
        if (StrUtil.isEmpty(encoding)) {
            encoding = Locale.ENGLISH.toString();
        }
        String fileName = prefix + "_" + encoding + ".properties";
        if (this.getClass().getClassLoader().getResource(fileName) == null) {
            fileName = prefix + ".properties";
        }

        List configUrls = getResources(this.getClass().getClassLoader(), fileName);
        if (configUrls != null) {
            for (URL configUrl : configUrls) {
                try (InputStream in = configUrl.openStream()) {
                    Properties properties = new Properties();

                    if (in == null) {
                        ReportUtil.reportError(String.format("Code file for CodeSpace \"%s\" doesn't exist!", spaceId.getSpaceName()));
                    } else {
                        InputStreamReader reader = new InputStreamReader(in);
                        properties.load(reader);
                    }
                    for (Map.Entry entry: properties.entrySet()) {
                        String key = (String) entry.getKey();
                        codeMap.put(key, String.format(logFormat, key, entry.getValue()));
                    }
                } catch (Throwable e) {
                    ReportUtil.reportError(String.format("Code space \"%s\" initializing failed!", spaceId.getSpaceName()), e);
                }
            }
        }
    }

    public String convert(String code) {
        return codeMap.computeIfAbsent(code, k -> String.format(logFormat, k, "Unknown Code"));
    }

    public String convert(String code, Object... args) {
        return String.format(convert(code), args);
    }


    private List getResources(ClassLoader classLoader, String path) {
        List rtn = new ArrayList<>();
        try {
            Enumeration allUrls = classLoader.getResources(path);
            if (allUrls != null) {
                while (allUrls.hasMoreElements()) {
                    rtn.add(allUrls.nextElement());
                }
            }
        } catch (IOException e) {
            ReportUtil.reportWarn("Fail to get resource of " + path + " from classpath", e);
            return null;
        }
        return rtn;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy