J2EE的web项目里经常用到的类(3)

来源:互联网 发布:语文行知天下八上答案 编辑:程序博客网 时间:2024/05/01 23:56

Property文件在J2EE项目里经常储存一些“无关痛痒”的数据,但也要有专门的类来管理,因为是多线程访问,所以要实现线程的同步,毕竟property的数据不是存放在数据库里。

  1. import java.util.*;
  2. import java.io.*;
  3. import org.apache.commons.logging.Log;
  4. import org.apache.commons.logging.LogFactory;
  5. public class PropertyManager {
  6.     static protected Log log = LogFactory.getLog(PropertyManager.class);
  7.     private static PropertyManager manager = null;
  8.     private static Object managerLock = new Object();
  9.     private static String propsName = "/mypro.properties";
  10.     /**
  11.      * Returns a property.
  12.      *
  13.      * @param name the name of the property to return.
  14.      * @return the property value specified by name.
  15.      */
  16.     public static String getProperty(String name) {
  17.         if (manager == null) {
  18.             synchronized (managerLock) {
  19.                 if (manager == null) {
  20.                     manager = new PropertyManager(propsName);
  21.                 }
  22.             }
  23.         }
  24.         return manager.getProp(name);
  25.     }
  26.     /**
  27.      * Sets a property. If the property doesn't already exists, a new
  28.      * one will be created.
  29.      *
  30.      * @param name the name of the property being set.
  31.      * @param value the value of the property being set.
  32.      */
  33.     public static void setProperty(String name, String value) {
  34.         if (manager == null) {
  35.             synchronized (managerLock) {
  36.                 if (manager == null) {
  37.                     manager = new PropertyManager(propsName);
  38.                 }
  39.             }
  40.         }
  41.         manager.setProp(name, value);
  42.     }
  43.     /**
  44.      * Deletes a property. If the property doesn't exist, the method
  45.      * does nothing.
  46.      *
  47.      * @param name the name of the property to delete.
  48.      */
  49.     public static void deleteProperty(String name) {
  50.         if (manager == null) {
  51.             synchronized (managerLock) {
  52.                 if (manager == null) {
  53.                     manager = new PropertyManager(propsName);
  54.                 }
  55.             }
  56.         }
  57.         manager.deleteProp(name);
  58.     }
  59.     /**
  60.      * Returns the names of properties.
  61.      *
  62.      * @return an Enumeration of the Yazd property names.
  63.      */
  64.     public static Enumeration propertyNames() {
  65.         if (manager == null) {
  66.             synchronized (managerLock) {
  67.                 if (manager == null) {
  68.                     manager = new PropertyManager(propsName);
  69.                 }
  70.             }
  71.         }
  72.         return manager.propNames();
  73.     }
  74.     /**
  75.      * Returns true if the properties are readable. This method is mainly
  76.      * valuable at setup time to ensure that the properties file is setup
  77.      * correctly.
  78.      */
  79.     public static boolean propertyFileIsReadable() {
  80.         if (manager == null) {
  81.             synchronized (managerLock) {
  82.                 if (manager == null) {
  83.                     manager = new PropertyManager(propsName);
  84.                 }
  85.             }
  86.         }
  87.         return manager.propFileIsReadable();
  88.     }
  89.     /**
  90.      * Returns true if the properties are writable. This method is mainly
  91.      * valuable at setup time to ensure that the properties file is setup
  92.      * correctly.
  93.      */
  94.     public static boolean propertyFileIsWritable() {
  95.         if (manager == null) {
  96.             synchronized (managerLock) {
  97.                 if (manager == null) {
  98.                     manager = new PropertyManager(propsName);
  99.                 }
  100.             }
  101.         }
  102.         return manager.propFileIsWritable();
  103.     }
  104.     /**
  105.      * Returns true if the properties file exists where the path property
  106.      * purports that it does.
  107.      */
  108.     public static boolean propertyFileExists() {
  109.         if (manager == null) {
  110.             synchronized (managerLock) {
  111.                 if (manager == null) {
  112.                     manager = new PropertyManager(propsName);
  113.                 }
  114.             }
  115.         }
  116.         return manager.propFileExists();
  117.     }
  118.     private Properties properties = null;
  119.     private Object propertiesLock = new Object();
  120.     private String resourceURI;
  121.     /**
  122.      * Creates a new PropertyManager. Singleton access only.
  123.      */
  124.     private PropertyManager(String resourceURI) {
  125.         this.resourceURI = resourceURI;
  126.     }
  127.     /**
  128.      * 
  129.      * @param name the name of the property to get.
  130.      * @return the property specified by name.
  131.      */
  132.     protected String getProp(String name) {
  133.         //If properties aren't loaded yet. We also need to make this thread
  134.         //safe, so synchronize...
  135.         if (properties == null) {
  136.             synchronized (propertiesLock) {
  137.                 //Need an additional check
  138.                 if (properties == null) {
  139.                     loadProps();
  140.                 }
  141.             }
  142.         }
  143.         String property = properties.getProperty(name);
  144.         if (property == null) {
  145.             return null;
  146.         } else {
  147.             return property.trim();
  148.         }
  149.     }
  150.     /**
  151.      * Sets a property. Because the properties must be saved to disk
  152.      * every time a property is set, property setting is relatively slow.
  153.      */
  154.     protected void setProp(String name, String value) {
  155.         //Only one thread should be writing to the file system at once.
  156.         synchronized (propertiesLock) {
  157.             //Create the properties object if necessary.
  158.             if (properties == null) {
  159.                 loadProps();
  160.             }
  161.             properties.setProperty(name, value);
  162.             saveProps();
  163.         }
  164.     }
  165.     protected void deleteProp(String name) {
  166.         //Only one thread should be writing to the file system at once.
  167.         synchronized (propertiesLock) {
  168.             //Create the properties object if necessary.
  169.             if (properties == null) {
  170.                 loadProps();
  171.             }
  172.             properties.remove(name);
  173.             saveProps();
  174.         }
  175.     }
  176.     protected Enumeration propNames() {
  177.         //If properties aren't loaded yet. We also need to make this thread
  178.         //safe, so synchronize...
  179.         if (properties == null) {
  180.             synchronized (propertiesLock) {
  181.                 //Need an additional check
  182.                 if (properties == null) {
  183.                     loadProps();
  184.                 }
  185.             }
  186.         }
  187.         return properties.propertyNames();
  188.     }
  189.     /**
  190.      * Loads Yazd properties from the disk.
  191.      */
  192.     private void loadProps() {
  193.         properties = new Properties();
  194.         InputStream in = null;
  195.         try {
  196.             in = getClass().getResourceAsStream(resourceURI);
  197.             properties.load(in);
  198.         } catch (Exception e) {
  199.             log.error("Error reading  properties in PropertyManager.loadProps() ", e);
  200.         } finally {
  201.             try {
  202.                 in.close();
  203.             } catch (Exception e) {
  204.             }
  205.         }
  206.     }
  207.     /**
  208.      * Saves Yazd properties to disk.
  209.      */
  210.     private void saveProps() {
  211.         //Now, save the properties to disk. In order for this to work, the user
  212.         //needs to have set the path field in the properties file. Trim
  213.         //the String to make sure there are no extra spaces.
  214.         String path = properties.getProperty("path").trim();
  215.         OutputStream out = null;
  216.         try {
  217.             out = new FileOutputStream(path);
  218.             properties.store(out, "yazd.properties -- " + (new java.util.Date()));
  219.         } catch (Exception ioe) {
  220.             log.error(
  221.                 "There was an error writing yazd.properties to "
  222.                     + path
  223.                     + ". "
  224.                     + "Ensure that the path exists and that the Yazd process has permission "
  225.                     + "to write to it -- ",
  226.                 ioe);
  227.         } finally {
  228.             try {
  229.                 out.close();
  230.             } catch (Exception e) {
  231.             }
  232.         }
  233.     }
  234.     /**
  235.      * Returns true if the properties are readable. This method is mainly
  236.      * valuable at setup time to ensure that the properties file is setup
  237.      * correctly.
  238.      */
  239.     public boolean propFileIsReadable() {
  240.         try {
  241.             InputStream in = getClass().getResourceAsStream(resourceURI);
  242.             return true;
  243.         } catch (Exception e) {
  244.             return false;
  245.         }
  246.     }
  247.     /**
  248.      * Returns true if the yazd.properties file exists where the path property
  249.      * purports that it does.
  250.      */
  251.     public boolean propFileExists() {
  252.         String path = getProp("path");
  253.         if (path == null) {
  254.             return false;
  255.         }
  256.         File file = new File(path);
  257.         if (file.isFile()) {
  258.             return true;
  259.         } else {
  260.             return false;
  261.         }
  262.     }
  263.     /**
  264.      * Returns true if the properties are writable. This method is mainly
  265.      * valuable at setup time to ensure that the properties file is setup
  266.      * correctly.
  267.      */
  268.     public boolean propFileIsWritable() {
  269.         String path = getProp("path");
  270.         File file = new File(path);
  271.         if (file.isFile()) {
  272.             //See if we can write to the file
  273.             if (file.canWrite()) {
  274.                 return true;
  275.             } else {
  276.                 return false;
  277.             }
  278.         } else {
  279.             return false;
  280.         }
  281.     }
  282. }


原创粉丝点击