com.feilong.commons.core.configure.PropertiesUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.commons.core.configure.PropertiesUtil.java

Source

/*
 * Copyright (C) 2008 feilong (venusdrogon@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.commons.core.configure;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.feilong.commons.core.io.UncheckedIOException;
import com.feilong.commons.core.lang.ClassLoaderUtil;

/**
 * ?properties?.
 *
 * @author <a href="mailto:venusdrogon@163.com"></a>
 * @version 1.0 2010-3-22 ?10:05:19
 * @see "org.apache.velocity.texen.util.PropertiesUtil"
 * @see "org.apache.cxf.common.util.PropertiesLoaderUtils"
 * @see "org.springframework.core.io.support.PropertiesLoaderUtils"
 * @see "org.springframework.core.io.support.PropertiesLoaderSupport"
 * @see "org.apache.commons.configuration.PropertiesConfiguration"
 * @since 1.0.0
 */
public final class PropertiesUtil implements BaseConfigure {

    /** Don't let anyone instantiate this class. */
    private PropertiesUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     * ??map<br>
     * Create a new HashMap and pass an instance of Properties.<br>
     * Properties is an implementation of a Map which keys and values stored as in a string.
     * 
     * @param properties
     *            the properties
     * @return the map
     * @see org.apache.commons.collections.MapUtils#toProperties(Map)
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Map<String, String> toMap(Properties properties) {
        Map<String, String> map = new HashMap<String, String>((Map) properties);
        return map;
    }

    // [start] getPropertiesValue

    /**
     * ?Properties?.
     * 
     * @param clz
     *            ?
     * @param propertiesPath
     *            Properties "/WEB-INF/classes/feilong.user.properties"
     * @param key
     *            
     * @return ?Properties?
     * @see #getProperties(Class, String)
     */
    public static String getPropertiesValue(Class<?> clz, String propertiesPath, String key) {
        Properties properties = getProperties(clz, propertiesPath);
        return properties.getProperty(key);
    }

    /**
     * ClassLoaderproperties.
     * 
     * @param clz
     *            ?Class
     * @param propertiesPath
     *            Properties "/WEB-INF/classes/feilong.user.properties"
     * @param key
     *            ?.??. null.
     * @return ClassLoaderproperties
     * @see #getPropertiesWithClassLoader(Class, String)
     * @see java.util.Properties#getProperty(String)
     */
    public static String getPropertiesValueWithClassLoader(Class<?> clz, String propertiesPath, String key) {
        Properties properties = getPropertiesWithClassLoader(clz, propertiesPath);
        return properties.getProperty(key);
    }

    // [end]

    // [start] getProperties

    /**
     * klassClassLoader,ClassLoader ?Properties.
     * 
     * @param klass
     *             klass  ClassLoader,? getResourceAsStream
     * @param propertiesPath
     *            ? class {@link PropertiesUtil},? src/main/resources?, messages/feilong-core-message_en_US.properties<br>
     *            <ul>
     *            <li> {@link #getProperties(Class, String)} ?
     *            {@code getProperties(PropertiesUtil.class,"/messages/feilong-core-message_en_US.properties")} <br>
     *            ?propertiesPath ?? <b>"/messages/feilong-core-message_en_US.properties"</b>, ???</li>
     *            <li> {@link #getPropertiesWithClassLoader(Class, String)} ?
     *            {@code getPropertiesWithClassLoader(PropertiesUtil.class,"messages/feilong-core-message_en_US.properties")}<br>
     *            ?propertiesPath ?? <b>"messages/feilong-core-message_en_US.properties"</b>,ClassLoader JVMBootstrapLoader?.<br>
     *            ??"messages/feilong-core-message_en_US.properties" ??/?)</li>
     *            </ul>
     * @return Properties
     * @see ClassLoaderUtil#getClassLoaderByClass(Class)
     * @see java.lang.ClassLoader#getResourceAsStream(String)
     * @see #getProperties(InputStream)
     * @see #getProperties(Class, String)
     */
    public static Properties getPropertiesWithClassLoader(Class<?> klass, String propertiesPath) {
        ClassLoader classLoader = ClassLoaderUtil.getClassLoaderByClass(klass);
        InputStream inputStream = classLoader.getResourceAsStream(propertiesPath);
        return getProperties(inputStream);
    }

    /**
     * {@link Class#getResourceAsStream(String)}  InputStream,? {@link #getProperties(InputStream)}.
     * 
     * @param klass
     *            , klass 
     * @param propertiesPath
     *            ? class {@link PropertiesUtil},? src/main/resources?, messages/feilong-core-message_en_US.properties<br>
     *            <ul>
     *            <li> {@link #getProperties(Class, String)} ?
     *            {@code getProperties(PropertiesUtil.class,"/messages/feilong-core-message_en_US.properties")} <br>
     *            ?propertiesPath ?? <b>"/messages/feilong-core-message_en_US.properties"</b>, ???</li>
     *            <li> {@link #getPropertiesWithClassLoader(Class, String)} ?
     *            {@code getPropertiesWithClassLoader(PropertiesUtil.class,"messages/feilong-core-message_en_US.properties")}<br>
     *            ?propertiesPath ?? <b>"messages/feilong-core-message_en_US.properties"</b>,ClassLoader JVMBootstrapLoader?.<br>
     *            ??"messages/feilong-core-message_en_US.properties" ??/?)</li>
     *            </ul>
     * @return ?Properties
     * @see java.lang.Class#getResourceAsStream(String)
     * @see #getProperties(InputStream)
     * @see #getPropertiesWithClassLoader(Class, String)
     */
    public static Properties getProperties(Class<?> klass, String propertiesPath) {
        // klass.getResourceAsStreamclassLoader.getResourceAsStream
        // ????ClassLoader??.
        InputStream inputStream = klass.getResourceAsStream(propertiesPath);
        return getProperties(inputStream);
    }

    /**
     * ?Properties.
     *
     * @param inputStream
     *            inputStream
     * @return <ul>
     *         <li>, {@link java.util.Properties#load(InputStream)}</li>
     *         </ul>
     * @throws NullPointerException
     *             if null==inputStream
     * @throws UncheckedIOException
     *             the unchecked io exception
     * @see java.util.Properties#load(InputStream)
     * @see "org.springframework.core.io.support.PropertiesLoaderUtils#loadProperties(Resource)"
     */
    public static Properties getProperties(InputStream inputStream)
            throws NullPointerException, UncheckedIOException {
        if (null == inputStream) {
            throw new NullPointerException("the inputStream is null or empty!");
        }
        Properties properties = new Properties();

        try {
            properties.load(inputStream);
            return properties;
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    // [end]
}