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

Quick Search    Search Deep

Source code: hk/hku/cecid/phoenix/common/util/TextProperty.java


1   /*
2    * Academic Free License
3    * Version 1.0
4    *
5    * This Academic Free License applies to any software and associated 
6    * documentation (the "Software") whose owner (the "Licensor") has placed the 
7    * statement "Licensed under the Academic Free License Version 1.0" immediately 
8    * after the copyright notice that applies to the Software. 
9    *
10   * Permission is hereby granted, free of charge, to any person obtaining a copy 
11   * of the Software (1) to use, copy, modify, merge, publish, perform, 
12   * distribute, sublicense, and/or sell copies of the Software, and to permit 
13   * persons to whom the Software is furnished to do so, and (2) under patent 
14   * claims owned or controlled by the Licensor that are embodied in the Software 
15   * as furnished by the Licensor, to make, use, sell and offer for sale the 
16   * Software and derivative works thereof, subject to the following conditions: 
17   *
18   * - Redistributions of the Software in source code form must retain all 
19   *   copyright notices in the Software as furnished by the Licensor, this list 
20   *   of conditions, and the following disclaimers. 
21   * - Redistributions of the Software in executable form must reproduce all 
22   *   copyright notices in the Software as furnished by the Licensor, this list 
23   *   of conditions, and the following disclaimers in the documentation and/or 
24   *   other materials provided with the distribution. 
25   * - Neither the names of Licensor, nor the names of any contributors to the 
26   *   Software, nor any of their trademarks or service marks, may be used to 
27   *   endorse or promote products derived from this Software without express 
28   *   prior written permission of the Licensor. 
29   *
30   * DISCLAIMERS: LICENSOR WARRANTS THAT THE COPYRIGHT IN AND TO THE SOFTWARE IS 
31   * OWNED BY THE LICENSOR OR THAT THE SOFTWARE IS DISTRIBUTED BY LICENSOR UNDER 
32   * A VALID CURRENT LICENSE. EXCEPT AS EXPRESSLY STATED IN THE IMMEDIATELY 
33   * PRECEDING SENTENCE, THE SOFTWARE IS PROVIDED BY THE LICENSOR, CONTRIBUTORS 
34   * AND COPYRIGHT OWNERS "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
35   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
36   * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE 
37   * LICENSOR, CONTRIBUTORS OR COPYRIGHT OWNERS BE LIABLE FOR ANY CLAIM, DAMAGES 
38   * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
39   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE. 
40   *
41   * This license is Copyright (C) 2002 Lawrence E. Rosen. All rights reserved. 
42   * Permission is hereby granted to copy and distribute this license without 
43   * modification. This license may not be modified without the express written 
44   * permission of its copyright owner. 
45   */
46  
47  /* ===== 
48   *
49   * $Header: /ebxml/staff/cecid/cvs_repository/common/src/hk/hku/cecid/phoenix/common/util/TextProperty.java,v 1.6 2002/12/13 03:59:03 kcyee Exp $
50   *
51   * Code authored by:
52   *
53   * kcyee [2002-05-28]
54   *
55   * Code reviewed by:
56   *
57   * username [YYYY-MM-DD]
58   *
59   * Remarks:
60   *
61   * =====
62   */
63  
64  package hk.hku.cecid.phoenix.common.util;
65  
66  import java.io.FileInputStream;
67  import java.io.FileOutputStream;
68  import java.io.IOException;
69  import java.util.Properties;
70  
71  /**
72   * This is a concrete implementation of the property object for loading
73   * and saving the property content into a text-based file. The format used
74   * is exactly the same of Java properties file.
75   *
76   * @author kcyee
77   * @version $Revision: 1.6 $
78   */
79  public class TextProperty extends Property {
80  
81      /**
82       * Internal variable for holding the file name of the property file
83       */
84      protected String fileName;
85  
86      /**
87       * Internal variable for holding the Java build in Properties object
88       * for manipulation
89       */
90      protected Properties prop;
91  
92      /**
93       * Internal constructor. Not to be called by user. This loads the
94       * content of the specified property file into memory.
95       *
96       * @param fileName the file name of the property file to be loaded
97       * @throws IOException when error occuring while loading
98       */
99      TextProperty(String fileName) throws IOException {
100         this.fileName = fileName;
101         prop = new Properties();
102         prop.load(new FileInputStream(fileName));
103     }
104 
105     /**
106      * Default constructor. This is for creating a brand new text-based 
107      * property object.
108      */
109     public TextProperty() {
110         this.fileName = null;
111         prop = new Properties();
112     }
113 
114     /**
115      * Gets the property value given the path (key). Not supported
116      * for text based properties file.
117      *
118      * @param path the path (key) of the property
119      * @return the property values given the path (key)
120      */
121     public synchronized String[] getMultiple(String path) {
122         throw new Error("Not supported");
123     }
124 
125     /**
126      * Gets the property value given the path (key).
127      *
128      * @param path the path (key) of the property
129      * @return the property value given the path (key)
130      */
131     public synchronized String get(String path) {
132         return get(path, null);
133     }
134 
135     /**
136      * Gets the property value given the path (key). If the property value
137      * is not found, the default value passed in is returned.
138      *
139      * @param path the path (key) of the property
140      * @param defaultValue the value to be returned if the property value
141      *                     is not found
142      * @return the property value given the path (key)
143      */
144     public synchronized String get(String path, String defaultValue) {
145         if (path == null) {
146             return null;
147         }
148         String ret = getFromCache(path);
149         if (ret == null) {
150             ret = prop.getProperty(path, defaultValue);
151             saveToCache(path, ret);
152         }
153         return ret;
154     }
155 
156     /**
157      * Sets the property value of the given path (key).
158      *
159      * @param path the path (key) of the property to be set
160      * @param value the property value to be set
161      */
162     public synchronized void set(String path, String value) {
163         if (path == null || value == null) {
164             return;
165         }
166         saveToCache(path, value);
167         prop.setProperty(path, value);
168     }
169 
170     /**
171      * Saves the property object to the same location when loading.
172      *
173      * @throws IOException when error occurs when saving the property object
174      */
175     public synchronized void save() throws IOException {
176         save(fileName);
177     }
178 
179     /**
180      * Saves the property object to the specified location.
181      *
182      * @param location the location for saving the property object
183      * @throws IOException when error occurs when saving the property object
184      */
185     public synchronized void save(String fileName) throws IOException {
186         if (fileName == null) {
187             throw new IOException("Cannot save to <null>!");
188         }
189         prop.store(new FileOutputStream(fileName), null);
190     }
191 }