com.clustercontrol.agent.util.AgentProperties.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.agent.util.AgentProperties.java

Source

/*
    
Copyright (C) 2011 NTT DATA Corporation
    
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, version 2.
    
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.
    
 */

package com.clustercontrol.agent.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Agent.properties????
 * ?????
 */
public class AgentProperties {
    /** ? */
    private static Log m_log = LogFactory.getLog(AgentProperties.class);

    private static final Properties properties = new Properties();

    private AgentProperties() {
    }

    /**
     * Agent.properties????
     * @param propFileName
     */
    public static void init(String propFileName) {
        m_log.debug("init() : propFileName = " + propFileName);
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(propFileName);

            // ???????
            properties.load(inputStream);
        } catch (FileNotFoundException e) {
            m_log.error(e.getMessage(), e);
        } catch (IOException e) {
            m_log.error(e.getMessage(), e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    m_log.error(e.getMessage(), e);
                }
            }
        }
    }

    /**
     * ?????????
     * ???????????
     * ??????????
     * ???????????null ????
     * @param key 
     * @return ???????
     */
    public static String getProperty(String key) {
        m_log.debug(key + " = " + properties.getProperty(key));
        return properties.getProperty(key);
    }

    /**
     * ?????????
     * ???????????
     * ??????????
     * ????????????????
     * @param key 
     * @param defaultValue 
     * @return ???????
     */
    public static String getProperty(String key, String defaultValue) {
        m_log.debug(key + " = " + properties.getProperty(key) + ", defaultValue = " + defaultValue);
        return properties.getProperty(key, defaultValue);
    }

    /**
     * Agent.properties???????
     * @return
     */
    public static Properties getProperties() {
        m_log.debug("getProperties() : call");
        return properties;
    }

    /**
     * ?????
     * 
     * @param key
     * @param value
     */
    public static void setProperty(String key, String value) {
        if (m_log.isDebugEnabled()) {
            m_log.debug("key:" + key + ", value:" + value);
        }
        properties.setProperty(key, value);
    }
}