ren.hankai.cordwood.core.Preferences.java Source code

Java tutorial

Introduction

Here is the source code for ren.hankai.cordwood.core.Preferences.java

Source

/*******************************************************************************
 * Copyright (C) 2018 hankai
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 ******************************************************************************/

package ren.hankai.cordwood.core;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.core.io.FileSystemResource;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ???
 *
 * @author hankai
 * @version 1.0.0
 * @since Jun 21, 2016 12:54:36 PM
 */
public final class Preferences {

    private static final Logger logger = LoggerFactory.getLogger(Preferences.class);

    /**
     *  ???? @profile(Bootstrap.PROFILE_TEST) ?
     */
    public static final String PROFILE_TEST = "test";

    /**
     * ???? @profile(Bootstrap.PROFILE_PRODUCTION) ?
     */
    public static final String PROFILE_PRODUCTION = "prod";

    /**
     * ? HSQL ?
     */
    public static final String PROFILE_HSQL = "hsql";

    /**
     * ? MySQL ?
     */
    public static final String PROFILE_MYSQL = "mysql";

    /**
     * .? Oracle ?
     */
    public static final String PROFILE_ORACLE = "oracle";

    /**
     * ? Redis ??
     */
    public static final String PROFILE_REDIS_SESSION = "redis-session";

    /**
     * ? redis ??
     */
    public static final String PROFILE_REDIS_CACHE = "redis-cache";

    /**
     * ???
     */
    public static final String ENV_APP_HOME_DIR = "app.home";

    /**
     * ????hello,apple,etc? 
     */
    public static final String DATA_SEPARATOR = ",";

    /**
     * ??
     */
    public static final String PLUGIN_RESOURCE_BASE = "resources";

    // ?????????
    private static String appHome = null;

    // ??
    private static Map<String, Object> parameters = null;

    private Preferences() {
    }

    /**
     * ???
     *
     * @author hankai
     * @since Jun 27, 2016 10:09:35 PM
     */
    private static Map<String, Object> getSystemPrefs() {
        if (parameters == null) {
            parameters = new HashMap<>();
            final YamlMapFactoryBean bean = new YamlMapFactoryBean();
            bean.setResources(new FileSystemResource(Preferences.getConfigDir() + "/system.yml"));
            parameters.putAll(bean.getObject());
        }
        return parameters;
    }

    /**
     * ???
     *
     * @return ?
     *
     * @author hankai
     * @since Jul 28, 2015 10:51:49 AM
     */
    public static String getHomeDir() {
        if (appHome == null) {
            //  JVM ??
            appHome = System.getProperty(ENV_APP_HOME_DIR);
            if (StringUtils.isEmpty(appHome)) {
                // ??
                appHome = System.getenv(ENV_APP_HOME_DIR);
            }
            // 
            if ((appHome != null) && appHome.endsWith(File.separator)) {
                appHome = appHome.substring(0, appHome.length());
            }
            if (StringUtils.isEmpty(appHome)) {
                // ??home??? home-not-set ????
                appHome = System.getProperty("user.dir") + File.separator + "home-not-set";
            }
            try {
                appHome = new File(appHome).getCanonicalPath();
            } catch (final IOException ex) {
                logger.error("Failed to ge canonical path of app.home", ex);
                throw new RuntimeException(ex);
            }
            System.setProperty(ENV_APP_HOME_DIR, appHome);
        }
        return appHome;
    }

    /**
     * ??
     *
     * @return 
     *
     * @author hankai
     * @since Jul 28, 2015 10:52:19 AM
     */
    public static String getCacheDir() {
        final String dir = getHomeDir() + File.separator + "cache";
        System.setProperty("app.cache", dir);
        return dir;
    }

    /**
     * ???
     *
     * @return ?
     * @author hankai
     * @since Jul 28, 2015 10:52:44 AM
     */
    public static String getConfigDir() {
        final String dir = getHomeDir() + File.separator + "config";
        System.setProperty("app.config", dir);
        return dir;
    }

    /**
     * ??
     *
     * @param configFile ???
     * @return 
     * @author hankai
     * @since Oct 21, 2016 1:13:13 PM
     */
    public static String getConfigFilePath(String configFile) {
        return getConfigDir() + File.separator + configFile;
    }

    /**
     * ?
     *
     * @return ?
     * @author hankai
     * @since Aug 10, 2017 9:11:48 PM
     */
    public static String getTemplatesDir() {
        final String dir = getConfigDir() + File.separator + "templates";
        System.setProperty("app.templates", dir);
        return dir;
    }

    /**
     * ???
     *
     * @return ?
     * @author hankai
     * @since Jul 28, 2015 10:53:05 AM
     */
    public static String getDataDir() {
        final String dir = getHomeDir() + File.separator + "data";
        System.setProperty("app.data", dir);
        return dir;
    }

    /**
     * ?
     *
     * @return ?
     * @author hankai
     * @since Sep 30, 2016 3:39:19 PM
     */
    public static String getDbDir() {
        return getHomeDir() + File.separator + "data" + File.separator + "database";
    }

    /**
     * ???
     *
     * @return ?
     * @author hankai
     * @since Jun 21, 2016 11:21:50 AM
     */
    public static String getDbConfigFile() {
        return getDbConfigFile(null);
    }

    /**
     * ???
     *
     * @param fileName ????
     * @return ?
     * @author hankai
     * @since Jun 21, 2016 11:21:15 AM
     */
    public static String getDbConfigFile(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            fileName = "database.properties";
        }
        return getConfigDir() + File.separator + fileName;
    }

    /**
     * ??
     *
     * @return 
     * @author hankai
     * @since Jul 28, 2015 10:53:49 AM
     */
    public static String getLogDir() {
        final String dir = getHomeDir() + File.separator + "logs";
        System.setProperty("app.log", dir);
        return dir;
    }

    /**
     * ???
     *
     * @return 
     * @author hankai
     * @since Jul 28, 2015 10:54:02 AM
     */
    public static String getTempDir() {
        final String dir = getHomeDir() + File.separator + "temp";
        System.setProperty("app.temp", dir);
        return dir;
    }

    /**
     * ??
     *
     * @return 
     * @author hankai
     * @since Jul 28, 2015 10:54:16 AM
     */
    public static String getAttachmentDir() {
        final String dir = getDataDir() + File.separator + "attachment";
        System.setProperty("app.attachment", dir);
        return dir;
    }

    /**
     * ???
     *
     * @return 
     * @author hankai
     * @since Aug 18, 2016 5:09:38 PM
     */
    public static String getBackupDir() {
        final String dir = getDataDir() + File.separator + "backups";
        System.setProperty("app.backup", dir);
        return dir;
    }

    /**
     * ?
     *
     * @return ?
     * @author hankai
     * @since Sep 30, 2016 3:37:19 PM
     */
    public static String getPluginsDir() {
        final String dir = getDataDir() + File.separator + "plugins";
        System.setProperty("app.plugins", dir);
        return dir;
    }

    /**
     * ?
     *
     * @return ?
     * @author hankai
     * @since Oct 18, 2016 2:45:39 PM
     */
    public static String getLibsDir() {
        final String dir = getHomeDir() + File.separator + "libs";
        System.setProperty("app.libs", dir);
        return dir;
    }

    /**
     * ???
     *
     * @param extraUrls ?? URL ? URL ?
     * @return ? URL ?
     * @author hankai
     * @since Oct 18, 2016 3:09:42 PM
     */
    public static URL[] getLibUrls(URL... extraUrls) {
        final List<URL> list = new ArrayList<>();
        if ((extraUrls != null) && (extraUrls.length > 0)) {
            list.addAll(Arrays.asList(extraUrls));
        }
        final File file = new File(getLibsDir());
        final File[] files = file.listFiles();
        if (files != null) {
            for (final File libFile : files) {
                if (FilenameUtils.isExtension(libFile.getName(), "jar")) {
                    try {
                        list.add(libFile.toURI().toURL());
                    } catch (final MalformedURLException ex) {
                        throw new RuntimeException(
                                String.format("Failed to get url from lib path: %s", libFile.getAbsolutePath()),
                                ex);
                    }
                }
            }
        }
        if (list.size() > 0) {
            final URL[] urls = new URL[list.size()];
            list.toArray(urls);
            return urls;
        }
        return null;
    }

    /**
     * ?
     *
     * @return 
     * @author hankai
     * @since Jun 28, 2016 1:27:31 PM
     */
    public static String getSystemSk() {
        final Object obj = getSystemPrefs().get("systemSk");
        if (obj != null) {
            return obj.toString();
        }
        return null;
    }

    /**
     * ??
     *
     * @return ??
     * @author hankai
     * @since Jun 28, 2016 1:49:49 PM
     */
    public static String getTransferKey() {
        final Object obj = getSystemPrefs().get("transferKey");
        if (obj != null) {
            return obj.toString();
        }
        return null;
    }

    /**
     * ? API ??
     *
     * @return 
     * @author hankai
     * @since Jun 29, 2016 9:48:43 PM
     */
    public static Integer getApiAccessTokenExpiry() {
        final Object obj = getSystemPrefs().get("apiAccessTokenExpiry");
        if (obj != null) {
            return Integer.parseInt(obj.toString());
        }
        return null;
    }

    /**
     * ?????IP
     *
     * @return ????IP
     * @author hankai
     * @since Feb 28, 2017 7:21:46 PM
     */
    public static String getProxyName() {
        final Object obj = getSystemPrefs().get("proxyName");
        if (obj != null) {
            return obj.toString();
        }
        return null;
    }

    /**
     * ????
     *
     * @return ???
     * @author hankai
     * @since Feb 28, 2017 7:22:13 PM
     */
    public static Integer getProxyPort() {
        final Object obj = getSystemPrefs().get("proxyPort");
        if (obj != null) {
            return Integer.parseInt(obj.toString());
        }
        return null;
    }

    /**
     * ?????
     *
     * @return ????
     * @author hankai
     * @since Feb 28, 2017 7:22:34 PM
     */
    public static String getProxyScheme() {
        final Object obj = getSystemPrefs().get("proxyScheme");
        if (obj != null) {
            return obj.toString();
        }
        return null;
    }

    /**
     * ??
     *
     * @param key ??
     * @return 
     * @author hankai
     * @since Mar 8, 2017 11:08:01 PM
     */
    public static String getCustomConfig(String key) {
        final Object obj = getSystemPrefs().get(key);
        if (obj != null) {
            return obj.toString();
        }
        return null;
    }
}