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

top.wboost.common.system.spring.properties.check.PropertiesChecker Maven / Gradle / Ivy

package top.wboost.common.system.spring.properties.check;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.InitializingBean;

import top.wboost.common.log.entity.Logger;
import top.wboost.common.log.util.LoggerUtil;
import top.wboost.common.system.exception.SystemException;
import top.wboost.common.util.StringUtil;
import top.wboost.common.utils.web.utils.PropertiesUtil;

/**
 * 配置值检测,检测失败抛出异常,需要在spring中注册
 * @author jwSun
 * @date 2017年6月12日 下午4:32:03
 */
//@AutoRootApplicationConfig("propertiesChecker")
public class PropertiesChecker implements InitializingBean {

    private Logger log = LoggerUtil.getLogger(getClass());

    /**不能为空字段 key:properties-name,val:properties-file**/
    private Map notNullMap;
    /**可以为空字段,但必须有key key:properties-name,val:properties-file**/
    private Map canNullMap;

    public Map getNotNullMap() {
        return notNullMap;
    }

    public void setNotNullMap(Map notNullMap) {
        this.notNullMap = notNullMap;
    }

    public Map getCanNullMap() {
        return canNullMap;
    }

    public void setCanNullMap(Map canNullMap) {
        this.canNullMap = canNullMap;
    }

    @PostConstruct
    public void checkProperties() {

    }

    public void afterPropertiesSet() throws Exception {
        log.info("properties config check init...");
        if (notNullMap != null) {
            for (Map.Entry nutNullEntry : notNullMap.entrySet()) {
                String propertiesVal = null;
                if ("".equals(nutNullEntry.getValue())) {
                    propertiesVal = PropertiesUtil.getProperty(nutNullEntry.getKey());
                } else {
                    propertiesVal = PropertiesUtil.getProperty(nutNullEntry.getKey(), nutNullEntry.getValue());
                }
                log.debug("find notNullMap properties : {} in {} , value is {}.", nutNullEntry.getKey(),
                        nutNullEntry.getValue(), propertiesVal);
                if (StringUtil.notEmpty(propertiesVal)) {
                    continue;
                } else {
                    throw new SystemException("notNullMap properties : " + nutNullEntry.getKey() + " in "
                            + nutNullEntry.getValue() + " ,value is null or empty.");
                }
            }
        }
        if (canNullMap != null) {
            for (Map.Entry canNullEntry : canNullMap.entrySet()) {
                String propertiesVal = null;
                if ("".equals(canNullEntry.getValue())) {
                    propertiesVal = PropertiesUtil.getProperty(canNullEntry.getKey());
                } else {
                    propertiesVal = PropertiesUtil.getProperty(canNullEntry.getKey(), canNullEntry.getValue());
                }
                log.debug("find canNullMap properties : {} in {} , value is {}.", canNullEntry.getKey(),
                        canNullEntry.getValue(), propertiesVal);
                if (propertiesVal == null) {
                    throw new SystemException("canNullMap properties : " + canNullEntry.getKey() + " in "
                            + canNullEntry.getValue() + " ,value is null.");
                }
            }
        }
        log.info("all properties check success");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy