com.unisa.storm.Properties.java Source code

Java tutorial

Introduction

Here is the source code for com.unisa.storm.Properties.java

Source

package com.unisa.storm;

/**
 * This file is part of UniSAHadoop.
 * 
 *     UniSAHadoop 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.
 * 
 *     UniSAHadoop 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 UniSAHadoop.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.net.URL;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;

/**
 * Singleton Class used to read the storm.properties file
 * @author jarrodmartin
 *
 */
public class Properties {

    private static final Logger LOGGER = Logger.getLogger(Properties.class);
    private static Properties singleton;

    private static String properties_file;

    private Configuration config;

    /**
    *  Defines the properties file
    */
    private Properties() {
        //BasicConfigurator.configure();
        try {
            if (properties_file == null) {
                System.out.println("Loading local config...");
                this.config = new PropertiesConfiguration(
                        this.getClass().getResource("/resource/storm.properties"));
                //this.config = new PropertiesConfiguration(this.getClass().getResource(path));
            } else {
                System.out.println("Loading config: " + properties_file);
                this.config = new PropertiesConfiguration(new File(properties_file).toURI().toURL());
            }

        } catch (Exception ex) {
            LOGGER.fatal("Could not load configuration", ex);
            LOGGER.trace(null, ex);
        }
    }

    private static Properties get() {
        if (singleton == null)
            singleton = new Properties();
        return singleton;
    }

    /**
    *  Returns the string value of the parameter given the parameter name
    * @param key
    * @return value of the parameter as String
    */
    public static String getString(String key) {
        return get().config.getString(key);
    }

    /**
    * Returns the integer value of the parameter given the parameter name
    * @param key
    * @return value of the parameter as Integer
    */
    public static Integer getInt(String key) {
        return get().config.getInt(key);
    }

    public static List getList(String key) {
        List<Object> vals = get().config.getList(key);
        return vals;
    }

    public static void setPropertiesFile(String filepath) {
        properties_file = filepath;
    }

}