servicepropertyreader.java

来自「struts+hibernate3的源程序」· Java 代码 · 共 76 行

JAVA
76
字号
package org.helpsoft.servicelocator;

import com.helpsoft.util.log.LogService;
import com.helpsoft.util.log.Logger;

import java.net.URL;
import java.util.Properties;
import java.io.Serializable;
import com.helpsoft.util.log.LogService;
import com.helpsoft.util.log.Logger;


/**
 * Read the service to implementation mappings from the services.properties file.
 * @author cao guangxin - www.relationinfo.com
 */
public class ServicePropertyReader implements Serializable {

    /**
     * De logger.
     */
    private static Logger log = LogService.getLogger(ServicePropertyReader.class);

    /**
     * property file waarin applicatie configuraties staan.
     */
    private static final String SERVICES_PROPERTIES_FILE_PATH = "/conf/service.properties";

    /**
     * property.
     */
    private static Properties properties = null;

    /**
     * Lees de woz online property file in.
     *
     * @return properties, null als de properties niet worden gevonden.
     *
     */
    public static synchronized Properties getProperties() {
        if (properties == null) {
            properties = new Properties();
            try {
                URL url = ServicePropertyReader.class.getResource(SERVICES_PROPERTIES_FILE_PATH);
                properties.load(url.openStream());
            } catch (Exception ex) {
                // If the properties are not loaded, it's fatal!
                log.fatal("Property file: " + SERVICES_PROPERTIES_FILE_PATH + " could not be read!", ex);
                return null;
            }
        }
        return properties;
    }

    /**
     * Read a property .
     *
     * @param propertyName the property name
     * @return property value of the property, otherwise: <tt>null</tt>
     */
    public static String getProperty(String propertyName) {
        return getProperties().getProperty(propertyName);
    }

    /**
     * Read a property with a fall back value.
     *
     * @param propertyName the property name.
     * @param defaultValue fall back value
     * @return property value if not null, otherwise the fallback value.
     */
    public static String getProperty(String propertyName, String defaultValue) {
        return getProperties().getProperty(propertyName, defaultValue);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?