SystemEnvHelper.java 3.2 KB
Newer Older
1
/*
2
 * Copyright (C) 2018-2019 Chatopera Inc, <https://www.chatopera.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.chatopera.cc.util;

import org.apache.commons.lang.StringUtils;
19 20 21 22 23 24
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
25 26

public class SystemEnvHelper {
27 28 29 30 31
    private final static Logger logger = LoggerFactory.getLogger(SystemEnvHelper.class);
    private static Properties props;

    /**
     * 根据类的全名查找是否存在
32
     *
33 34 35 36 37 38 39 40 41 42 43 44
     * @param classFullName
     * @return
     */
    public static boolean isClassExistByFullName(final String classFullName) {
        try {
//             Class<?> uriClass =
            Class.forName(classFullName);
            return true;
        } catch (ClassNotFoundException ex) {
            return false;
        }
    }
45

46

47
    /**
48
     * 获得环境变量的值,如果不存在,返回默认值
49
     *
50 51
     * @param variable
     * @param defaultvalue
52 53
     * @return
     */
54 55 56 57 58 59 60
    public static String getenv(final String variable, final String defaultvalue) {
        final String val = System.getenv(variable);

        if (StringUtils.isBlank(val)) {
            return defaultvalue;
        }
        return val;
61
    }
62

63

64 65 66 67 68 69 70
    /**
     * 加载配置,先检查环境变量,再从application properties加载
     *
     * @param property
     * @return
     */
    public static String parseFromApplicationProps(final String property) {
71
        // 将 property 转化为环境变量
72 73 74 75 76 77 78 79 80 81 82 83 84
        String P = StringUtils.upperCase(property);

        P = StringUtils.replaceChars(P, "-", "_");
        P = StringUtils.replaceChars(P, ".", "_");
        String val = System.getenv(P);

        if (StringUtils.isBlank(val)) {
            val = getProps().getProperty(property);
        }
        return val;
    }

    /**
85 86 87 88 89 90 91 92 93 94 95 96 97
     * Get properties filename
     * @return
     */
    private static String getPropsFileName() {
        String profile = getenv("SPRING_PROFILES_ACTIVE", "");
        if (StringUtils.isNotBlank(profile)) {
            return "application-" + profile + ".properties";
        }
        return "application.properties";
    }

    /**
     * 加载 application.properties
98 99 100 101 102 103
     *
     * @return
     */
    private static Properties getProps() {
        if (props == null) {
            try (InputStream input = SystemEnvHelper.class.getClassLoader().getResourceAsStream(
104
                    getPropsFileName())) {
105 106 107 108 109 110 111 112 113 114
                // load a properties file
                props = new Properties();
                props.load(input);
            } catch (IOException ex) {
                logger.error("[getProps] error", ex);
            }
        }
        return props;
    }

115
}