J2EE的web项目里经常用到的类(3)
来源:互联网 发布:语文行知天下八上答案 编辑:程序博客网 时间:2024/05/01 23:56
Property文件在J2EE项目里经常储存一些“无关痛痒”的数据,但也要有专门的类来管理,因为是多线程访问,所以要实现线程的同步,毕竟property的数据不是存放在数据库里。
- import java.util.*;
- import java.io.*;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class PropertyManager {
- static protected Log log = LogFactory.getLog(PropertyManager.class);
- private static PropertyManager manager = null;
- private static Object managerLock = new Object();
- private static String propsName = "/mypro.properties";
- /**
- * Returns a property.
- *
- * @param name the name of the property to return.
- * @return the property value specified by name.
- */
- public static String getProperty(String name) {
- if (manager == null) {
- synchronized (managerLock) {
- if (manager == null) {
- manager = new PropertyManager(propsName);
- }
- }
- }
- return manager.getProp(name);
- }
- /**
- * Sets a property. If the property doesn't already exists, a new
- * one will be created.
- *
- * @param name the name of the property being set.
- * @param value the value of the property being set.
- */
- public static void setProperty(String name, String value) {
- if (manager == null) {
- synchronized (managerLock) {
- if (manager == null) {
- manager = new PropertyManager(propsName);
- }
- }
- }
- manager.setProp(name, value);
- }
- /**
- * Deletes a property. If the property doesn't exist, the method
- * does nothing.
- *
- * @param name the name of the property to delete.
- */
- public static void deleteProperty(String name) {
- if (manager == null) {
- synchronized (managerLock) {
- if (manager == null) {
- manager = new PropertyManager(propsName);
- }
- }
- }
- manager.deleteProp(name);
- }
- /**
- * Returns the names of properties.
- *
- * @return an Enumeration of the Yazd property names.
- */
- public static Enumeration propertyNames() {
- if (manager == null) {
- synchronized (managerLock) {
- if (manager == null) {
- manager = new PropertyManager(propsName);
- }
- }
- }
- return manager.propNames();
- }
- /**
- * Returns true if the properties are readable. This method is mainly
- * valuable at setup time to ensure that the properties file is setup
- * correctly.
- */
- public static boolean propertyFileIsReadable() {
- if (manager == null) {
- synchronized (managerLock) {
- if (manager == null) {
- manager = new PropertyManager(propsName);
- }
- }
- }
- return manager.propFileIsReadable();
- }
- /**
- * Returns true if the properties are writable. This method is mainly
- * valuable at setup time to ensure that the properties file is setup
- * correctly.
- */
- public static boolean propertyFileIsWritable() {
- if (manager == null) {
- synchronized (managerLock) {
- if (manager == null) {
- manager = new PropertyManager(propsName);
- }
- }
- }
- return manager.propFileIsWritable();
- }
- /**
- * Returns true if the properties file exists where the path property
- * purports that it does.
- */
- public static boolean propertyFileExists() {
- if (manager == null) {
- synchronized (managerLock) {
- if (manager == null) {
- manager = new PropertyManager(propsName);
- }
- }
- }
- return manager.propFileExists();
- }
- private Properties properties = null;
- private Object propertiesLock = new Object();
- private String resourceURI;
- /**
- * Creates a new PropertyManager. Singleton access only.
- */
- private PropertyManager(String resourceURI) {
- this.resourceURI = resourceURI;
- }
- /**
- *
- * @param name the name of the property to get.
- * @return the property specified by name.
- */
- protected String getProp(String name) {
- //If properties aren't loaded yet. We also need to make this thread
- //safe, so synchronize...
- if (properties == null) {
- synchronized (propertiesLock) {
- //Need an additional check
- if (properties == null) {
- loadProps();
- }
- }
- }
- String property = properties.getProperty(name);
- if (property == null) {
- return null;
- } else {
- return property.trim();
- }
- }
- /**
- * Sets a property. Because the properties must be saved to disk
- * every time a property is set, property setting is relatively slow.
- */
- protected void setProp(String name, String value) {
- //Only one thread should be writing to the file system at once.
- synchronized (propertiesLock) {
- //Create the properties object if necessary.
- if (properties == null) {
- loadProps();
- }
- properties.setProperty(name, value);
- saveProps();
- }
- }
- protected void deleteProp(String name) {
- //Only one thread should be writing to the file system at once.
- synchronized (propertiesLock) {
- //Create the properties object if necessary.
- if (properties == null) {
- loadProps();
- }
- properties.remove(name);
- saveProps();
- }
- }
- protected Enumeration propNames() {
- //If properties aren't loaded yet. We also need to make this thread
- //safe, so synchronize...
- if (properties == null) {
- synchronized (propertiesLock) {
- //Need an additional check
- if (properties == null) {
- loadProps();
- }
- }
- }
- return properties.propertyNames();
- }
- /**
- * Loads Yazd properties from the disk.
- */
- private void loadProps() {
- properties = new Properties();
- InputStream in = null;
- try {
- in = getClass().getResourceAsStream(resourceURI);
- properties.load(in);
- } catch (Exception e) {
- log.error("Error reading properties in PropertyManager.loadProps() ", e);
- } finally {
- try {
- in.close();
- } catch (Exception e) {
- }
- }
- }
- /**
- * Saves Yazd properties to disk.
- */
- private void saveProps() {
- //Now, save the properties to disk. In order for this to work, the user
- //needs to have set the path field in the properties file. Trim
- //the String to make sure there are no extra spaces.
- String path = properties.getProperty("path").trim();
- OutputStream out = null;
- try {
- out = new FileOutputStream(path);
- properties.store(out, "yazd.properties -- " + (new java.util.Date()));
- } catch (Exception ioe) {
- log.error(
- "There was an error writing yazd.properties to "
- + path
- + ". "
- + "Ensure that the path exists and that the Yazd process has permission "
- + "to write to it -- ",
- ioe);
- } finally {
- try {
- out.close();
- } catch (Exception e) {
- }
- }
- }
- /**
- * Returns true if the properties are readable. This method is mainly
- * valuable at setup time to ensure that the properties file is setup
- * correctly.
- */
- public boolean propFileIsReadable() {
- try {
- InputStream in = getClass().getResourceAsStream(resourceURI);
- return true;
- } catch (Exception e) {
- return false;
- }
- }
- /**
- * Returns true if the yazd.properties file exists where the path property
- * purports that it does.
- */
- public boolean propFileExists() {
- String path = getProp("path");
- if (path == null) {
- return false;
- }
- File file = new File(path);
- if (file.isFile()) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * Returns true if the properties are writable. This method is mainly
- * valuable at setup time to ensure that the properties file is setup
- * correctly.
- */
- public boolean propFileIsWritable() {
- String path = getProp("path");
- File file = new File(path);
- if (file.isFile()) {
- //See if we can write to the file
- if (file.canWrite()) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- }
- J2EE的web项目里经常用到的类(3)
- J2EE的web项目里经常用到的类(1)
- J2EE的web项目里经常用到的类(2)
- java 项目中经常用到的工具类
- 开发Android项目经常用到的四个工具类
- 开发j2ee时经常用到的工具和一些框架
- servlet里经常会用到的一些GET方法
- 经常用到的SQL
- 经常用到的DML
- CSS 经常用到的
- 经常用到的网址
- 经常用到的sql
- 经常用到的命令
- 经常用到的方法
- 经常用到的快捷键
- 开发中经常用到的工具类
- 经常用到的css(笔记)
- 关于在web上经常用到的省市关联
- asp.net学习步骤(转)asp.net学习步骤(转)
- PHP 实现多服务器共享 SESSION 数据
- 关闭子窗口,刷新父窗口
- 如何使flex中的css文件转化为swf文件
- js得到当前web应用的根目录
- J2EE的web项目里经常用到的类(3)
- 学习flex不要再犯这样的错误
- 一个silverlight播放器的DEMO(Expression Encoder 2)
- Deeper inside DLL
- CString 操作指南
- 判断QQ在线的人
- PACKAGE 可变数组+行组触发器+表级触发器 解决数据操作问题
- VC 深入详解 学习笔记
- xml操作