All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.unique.commons.io.PropUtil Maven / Gradle / Ivy

/**
 * Copyright (c) 2014-2015, biezhi 王爵 ([email protected])
 *
 * 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 org.unique.commons.io;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unique.commons.utils.CollectionUtil;

/**
 * properties file util
 * @author biezhi
 * @since 1.0
 */
public class PropUtil {

	private static final Logger logger = LoggerFactory.getLogger(PropUtil.class);

	/**
	 * 根据文件名读取properties文件
	 */
	public static Properties getProperty(String resourceName) {
		InputStream in = null;
		Properties props = new Properties();
		try {
			in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
			if (in != null) {
				props.load(in);
			}
		} catch (IOException e) {
			logger.error("加载属性文件出错!", e);
		} finally {
			IOUtil.closeQuietly(in);
		}
		return props;
	}

	/**
	 * //TODO 根据文件名读取map数据
	 */
	public static Map getPropertyMap(String resourceName) {
		InputStream in = null;
		Map map = CollectionUtil.newHashMap();
		try {
			in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
			if (null == in) {
				throw new RuntimeException("没有找到资源文件 [" + resourceName + "]");
			}
			Properties prop = new Properties();
			prop.load(in);
			Set> set = prop.entrySet();
			Iterator> it = set.iterator();
			while (it.hasNext()) {
				Entry entry = it.next();
				String key = entry.getKey().toString();
				String value = entry.getValue().toString();
				String fuKey = getWildcard(value);
				if(null != fuKey && null != prop.get(fuKey)){
					String fuValue = prop.get(fuKey).toString();
					value = value.replaceAll("\\$\\{" + fuKey + "\\}", fuValue);
				}
				map.put(key, value);
			}
			logger.info("加载properties文件[" + resourceName + "]");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtil.closeQuietly(in);
		}
		return map;
	}
	
	private static String getWildcard(String str){
		if(null != str && str.indexOf("${") != -1){
			int start = str.indexOf("${");
			int end = str.indexOf("}");
			if(start != -1 && end != -1){
				return str.substring(start + 2, end);
			}
		}
		return null;
	}

	/**
	 * //TODO 根据property对象获取map格式数据
	 */
	public static Map getPropertyMap(Properties prop) {
		Map map = CollectionUtil.newHashMap();
		Set> set = prop.entrySet();
		Iterator> it = set.iterator();
		while (it.hasNext()) {
			Entry entry = it.next();
			map.put(entry.getKey().toString(), entry.getValue().toString());
		}
		return map;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy