Java tutorial
/* * Copyright (C) 2008 feilong * * 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.sunchenbin.store.feilong.core.util; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; import com.sunchenbin.store.feilong.core.io.UncheckedIOException; import com.sunchenbin.store.feilong.core.lang.ClassLoaderUtil; /** * ?properties?. * * @author feilong * @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" * @version 1.4.0 201583 ?3:18:50 * @since 1.4.0 */ public final class PropertiesUtil { /** Don't let anyone instantiate this class. */ private PropertiesUtil() { //AssertionError?. ?????. ???. //see Effective Java 2nd throw new AssertionError("No " + getClass().getName() + " instances for you!"); } /** * ??map. * * <p> * 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. * </p> * * @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) { return new HashMap<String, String>((Map) properties); } // [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> * @see java.util.Properties#load(InputStream) * @see "org.springframework.core.io.support.PropertiesLoaderUtils#loadProperties(Resource)" */ public static Properties getProperties(InputStream inputStream) { 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] }