io.uengine.web.configuration.ConfigurationHelper.java Source code

Java tutorial

Introduction

Here is the source code for io.uengine.web.configuration.ConfigurationHelper.java

Source

/**
 * Copyright (C) 2011  Flamingo Project (http://www.opencloudengine.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package io.uengine.web.configuration;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Configuration Singleton Instance   Helper.
 *
 * @author Byoung Gon, Kim
 * @since 0.1
 */
@Component
public class ConfigurationHelper implements InitializingBean {

    /**
     * SLF4J Logging
     */
    private Logger logger = LoggerFactory.getLogger(ConfigurationHelper.class);

    @Autowired
    @Qualifier("app")
    private Properties app;

    @Autowired
    @Qualifier("config")
    private Properties config;

    /**
     * Property Key Value   map
     */
    private Map<String, String> map = new HashMap();

    /**
     * Property Key Value   properties.
     */
    private Properties props = new Properties();

    /**
     * ResourceBundleHelper? Singleton Instance
     */
    private static ConfigurationHelper helper;

    /**
     * Default constructor.
     */
    public ConfigurationHelper() {
    }

    /**
     * ConfigurationHelper? Singleton Instance .
     *
     * @return ConfigurationHelper? Singleton Instance
     */
    public static ConfigurationHelper getHelper() {
        return helper;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.helper = this;
        inject(app);
        inject(config);
    }

    private void inject(Properties config) {
        Set<Object> configs = config.keySet();
        for (Object obj : configs) {
            String key = (String) obj;
            String value = config.getProperty(key);

            this.map.put(key, value);
            this.props.put(key, value);

            logger.debug("Loaded {}={}", key, value);
        }
    }

    public Properties getProperties() {
        return this.props;
    }

    /**
     *  ? ? .
     *
     * @return ?
     */
    public int size() {
        return map.size();
    }

    /**
     *  Key? Value ?.
     *
     * @param key Key
     * @return Key? Value?  ? ?
     */
    public String get(String key) {
        return map.get(key);
    }

    /**
     *  Key? Value ?.
     *
     * @param key          Key
     * @param defaultValue 
     * @return Key? Value?  ? ?
     */
    public String get(String key, String defaultValue) {
        String value = map.get(key);
        if (!StringUtils.isEmpty(value)) {
            return value;
        }
        return defaultValue;
    }

    /**
     *  Key  ?.
     *
     * @param key   ? Key
     * @return   <tt>false</tt>
     */
    public boolean containsKey(String key) {
        return map.containsKey(key);
    }

    /**
     *  Key?  Long Value .
     *
     * @param key Key
     * @return Key?  Long Value
     */
    public long getLong(String key) {
        return Long.parseLong(get(key));
    }

    /**
     *  Key?  Boolean Value .
     *
     * @param key Key
     * @return Key?  Boolean Value
     */
    public boolean getBoolean(String key) {
        try {
            return Boolean.parseBoolean(get(key));
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     *  Key?  Boolean Value .
     *
     * @param key          Key
     * @param defaultValue 
     * @return Key?  Boolean Value
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        if (containsKey(key)) {
            return Boolean.parseBoolean(get(key));
        } else {
            return defaultValue;
        }
    }

    /**
     *  Key?  Long Value . ?  ? .
     *
     * @param key          Key
     * @param defaultValue 
     * @return Key?  Long Value
     */
    public long getLong(String key, long defaultValue) {
        if (containsKey(key)) {
            return Long.parseLong(get(key));
        } else {
            return defaultValue;
        }
    }
}