properties的配置文件缓存模型
2015-06-24 15:46
423 查看
package com.travelzen.framework.core.config; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Map; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import com.travelzen.framework.core.exception.PropertyException; import com.travelzen.framework.core.util.TZUtil; /** * properties的配置文件缓存模型 * * @author jiangningcui * */ public class PropertiesUtil { private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class); private static Map<String, Properties> mapProperties = new ConcurrentHashMap<String, Properties>(); /** * 更新缓存 * * @param resourcePath * @param properties */ private static void saveOrUpdateProperties(String resourcePath, Properties properties) { if (!TZUtil.isEmpty(resourcePath) && !TZUtil.isEmpty(properties)) { mapProperties.put(resourcePath, properties); } } /** * 从inpustrema中获得数据 */ public static String getProperty(String resourcePath, byte[] data, String key, boolean needToCached, String defaultValue) { if (TZUtil.isEmpty(data)) return null; ByteArrayInputStream inputStream = new ByteArrayInputStream(data); Properties properties = getProperties(resourcePath, inputStream, needToCached); return getValue(properties, key, defaultValue); } /** * 从inpustrema中获得数据 */ public static Properties getProperty(String resourcePath, byte[] data, boolean needToCached) { if (TZUtil.isEmpty(data)) return null; ByteArrayInputStream inputStream = new ByteArrayInputStream(data); Properties properties = getProperties(resourcePath, inputStream, needToCached); return properties; } /** * 从inpustrema中获得数据 */ public static Properties getProperty(byte[] data) { if (TZUtil.isEmpty(data)) return null; ByteArrayInputStream inputStream = new ByteArrayInputStream(data); Properties properties = getProperties(inputStream); return properties; } /** * 保存某个数据到配置文件中 */ public static void updatePropretiesFile(String filePath, String key, String value) { if (filePath == null || key == null) return; try { Properties dateProps = getLocalfileProperties(filePath, false); if (dateProps != null) { FileOutputStream fos = new FileOutputStream(filePath); dateProps.setProperty(key, value); dateProps.store(fos, ""); fos.flush(); fos.close(); } } catch (Exception e) { logger.error(e.getMessage()); } } public static Properties getPropertiesFromCache(String resourcePath, boolean needToCached) { if (needToCached && !TZUtil.isEmpty(resourcePath) && mapProperties.containsKey(resourcePath)) { return mapProperties.get(resourcePath); } return null; } /** * loadProperties 注意,获得的不是Java的资源文件,而是外部的文件系统的文件 */ public static Properties getLocalfileProperties(String resourcePath, boolean needToCached) { if (needToCached && !TZUtil.isEmpty(resourcePath) && mapProperties.containsKey(resourcePath)) { return mapProperties.get(resourcePath); } BufferedReader input = null; try { input = new BufferedReader(new InputStreamReader(new FileInputStream(resourcePath), "UTF-8")); Properties properties = new Properties(); properties.load(input); if (needToCached) { saveOrUpdateProperties(resourcePath, properties); } return properties; } catch (IOException e1) { logger.error(e1.getLocalizedMessage() + " ->" + resourcePath); throw PropertyException.instanceConfNotFoundException("getLocalProperties上没有数据, resourcePath = " + resourcePath, e1); } finally { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * loadProperties 注意,获得的不是Java的资源文件,而是外部的文件系统的文件 */ public static Properties getProperties(InputStream input) { if (input != null) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(input, "UTF-8")); Properties properties = new Properties(); properties.load(reader); return properties; } catch (IOException e) { logger.error(e.getLocalizedMessage(), e); throw PropertyException.instanceConfNotFoundException("getLocalProperties上没有数据", e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } return null; } /** * loadProperties 注意,获得的不是Java的资源文件,而是外部的文件系统的文件 */ public static Properties getProperties(String resourcePath, InputStream input, boolean needToCached) { if (input != null) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(input, "UTF-8")); Properties properties = new Properties(); properties.load(reader); if (needToCached) { saveOrUpdateProperties(resourcePath, properties); } return properties; } catch (IOException e) { logger.error(e.getLocalizedMessage(), e); throw PropertyException.instanceConfNotFoundException("getLocalProperties上没有数据", e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } return null; } /** * 从properties获得制定的值 * * @param properties * @param key * @param defaultValue * @return */ public static String getValue(Properties properties, String key, String defaultValue) { if (!TZUtil.isEmpty(properties)) { String value = properties.getProperty(key); if (TZUtil.isEmpty(value)) value = defaultValue; // value = StringUtils.trimToNull(value); return value; } return defaultValue; } public static InputStream getInputStreamFromResource(String resourcePath) { DefaultResourceLoader loader = new DefaultResourceLoader(); Resource rc = loader.getResource(resourcePath); InputStream input = null; try { input = rc.getInputStream(); return input; } catch (IOException e1) { logger.error(e1.getLocalizedMessage()); e1.printStackTrace(); return null; } } public static String getPropertyFromResource(String resourcePath, String key) { InputStream input = getInputStreamFromResource(resourcePath); if (input != null) try { Properties properties = new Properties(); properties.load(input); return properties.getProperty(key).trim(); } catch (IOException e) { logger.error(e.getLocalizedMessage(), e); } return null; } }
相关文章推荐
- 【转】Tomcat版本是32位、64位问题
- IT架构之IT架构模型——思维导图
- Linux中的文件描述符与打开文件之间的关系
- nginx与Elasticsearch结合使用
- windows openssl
- 服务器硬件监控之Check_openmanage
- linux 实现无密码登陆 ---- sftp
- linux 添加用户、权限 - !!!!!!!!!!! - 博客园
- tomcat绑定域名
- WEB_容器_tomcat_工作原理_待
- Synopsys VCS MX vJ-2014.12 SP2 Linux64 1DVD编译代码模拟器
- hello welcome to linux`s world
- 傻瓜式tomcat发布:Windows远程控制Linux服务器发布Tomcat WEB程序
- 单词计数程序在hadoop上的实现
- 稳定模式在RESTful架构中的应用
- Apache简介——极客学院学习笔记
- 在CentOS 6.6操作系统下安装配置phpMyAdmin 4.3.13.1
- Linux类似迅雷的下载利器uGet
- eclipse中tomcat不能使用debug模式修改java文件
- 在Tomcat下指定Jsp生成的Java文件路径