Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/simscomputing/util/ApplicationProperties.java


1   package com.simscomputing.util;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileOutputStream;
6   import java.io.FileNotFoundException;
7   import java.io.IOException;
8   import java.util.Enumeration;
9   import java.util.HashMap;
10  import java.util.Map;
11  import java.util.Properties;
12  
13  /**
14  Encapsulates the java.util.Properties class to be more programmer-friendly.
15  Given a file name that resolves to a file that is java.util.Properties-readable,
16  all FileStream work is done for the client programmer.  This class also
17  offers the capability to find properties based on a substring to match.
18  
19  @see java.util.Properties
20  
21  @author Dave Lamy (daveandjeri@stubbydog.com)
22  @version $Revision: 1.1.1.1 $ $Date: 2000/02/21 21:22:36 $
23  */
24  public class ApplicationProperties {
25    private final File propertiesFile;
26    private Properties appProperties;
27  
28  
29    /**
30    Will first verify that the given String file name is a valid file, and
31    if so will go ahead and load the properties in that file.  This
32    constructor signature will immediately load the properties from
33    the given file (same as calling <code>ApplicationProperties(fileName, true)
34    </code>.
35  
36    @param fileName - the absolute file path and name.
37    @throws IOException - if there are problems during the loading of the file.
38    */
39    public ApplicationProperties(String fileName) throws IOException {
40      this(fileName, true);
41    } // constructor
42  
43    /**
44    Will first verify that the given String file name is a valid file, and
45    if it is valid and the loadNow parameter is set to <code>true</code>,
46    will go ahead and load the properties in that file
47  
48    @param fileName - the absolute file name (path and all).
49    @param loadNow - whether or not to go ahead and load the properties from
50                     the file.
51    @throws IOException - if there are problems during the loading of the file.
52    */
53    public ApplicationProperties(String fileName, boolean loadNow) throws IOException {
54      // set the class variables
55      propertiesFile = new File(fileName);
56      if (!propertiesFile.exists()) {
57        propertiesFile.createNewFile();
58      } // if
59      if (!propertiesFile.isFile()) {
60        throw new FileNotFoundException(fileName + "\nSpecified file name must resolve to an actual file.");
61      } // if
62      appProperties = new Properties();
63  
64      if (loadNow) {
65        // go ahead and populate the properties object
66        loadProperties();
67      } // if
68  
69    } // constructor
70  
71    /**
72    Gets a property value given a property name.  Will return null if no name
73    matches.
74  
75    @param propertyName - the property name to retrieve
76    @return String - the property's value
77    */
78    public String getProperty(String propertyName) {
79      return appProperties.getProperty(propertyName);
80    }
81  
82    /**
83    Returns a Map of all property name/value pairs that match the passed-in String
84    in any way.  The test for matching is done via the String method indexOf, meaning
85    that the match expression will resolve to true if the passed-in String can be
86    found anywhere in the property name.
87  
88    @param propertyNameToMatch - the String to attempt to match to the property name.
89    @return Map - a Map of all property name/value pairs whose names matched the given
90            String.
91    */
92    /*FIXME:  Perhaps offer beginsWith and endsWith functionality?*/  
93    public Map /*String propertyName, String value*/ getPropertiesLike(String propertyNameToMatch) {
94      Map retMap = new HashMap();
95      Enumeration allPropertyNames = appProperties.propertyNames();
96      while (allPropertyNames.hasMoreElements()) {
97        String propertyName = (String)allPropertyNames.nextElement();
98        if (propertyName.indexOf(propertyNameToMatch) > -1) {
99          retMap.put(propertyName, appProperties.getProperty(propertyName));
100       } // if
101     } // while
102     return retMap;
103   } // getPropertiesLike
104 
105   /**
106   Sets a property value given a property name.  Will create an entry if none
107   exists for the given property name; will overwrite if one does already exist.
108   The set will not be persisted until storeProperties() is called.
109 
110   @param propertyName - the property name to set
111   @param propertyValue - the property's value
112   */
113   public void setProperty(String propertyName, String propertyValue) {
114     appProperties.setProperty(propertyName, propertyValue);
115   } // setProperty
116 
117   /**
118   Tests for the existence of the given property name.
119 
120   @param propertyName - the name to look for
121   @return boolean - the success or failure of the search
122   */
123   public boolean containsPropertyName(String propertyName) {
124     return appProperties.containsKey(propertyName);
125   } // containsPropertyName
126 
127   /**
128   Tests for the existence of the given property value.
129 
130   @param propertyValue - the value to look for
131   @return boolean - the success or failure of the search
132   */
133   public boolean containsPropertyValue(String propertyValue) {
134     return appProperties.containsValue(propertyValue);
135   } // containsPropertyValue
136 
137   /**
138   Removes the given property.  Removal will not be persisted until
139   storeProperties() is called.
140   */
141   public void removeProperty(String propertyName) {
142     appProperties.remove(propertyName);
143   }
144 
145   /**
146   Loads all properties from the properties file given at construction time.
147   This method works like a reload (or a first-time load if the object was
148   constructed with the loadNow parameter set to <code>false</code>).  All
149   in-memory changes will be lost, so if previous changes are to be persisted,
150   call <code>storeProperties()</code> prior to performing the reload.
151   */
152   public void loadProperties() throws IOException {
153     FileInputStream propertiesStream = new FileInputStream(propertiesFile);
154     appProperties.load(propertiesStream);
155   } // loadProperties
156 
157   /**
158   Stores the properties back into the file.  The storage process will overwrite
159   all data that was in the properties file before the storeProperties was called.
160   */
161   public void storeProperties() throws IOException {
162     FileOutputStream outStream = new FileOutputStream(propertiesFile);
163     appProperties.store(outStream, propertiesFile.toString());
164   } // storeProperties
165 
166 
167 
168 }