KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > modifiedselector > PropertiesfileCache


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.types.selectors.modifiedselector;
20
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Vector JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.BufferedInputStream JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.BufferedOutputStream JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31
32
33 /**
34  * Use java.util.Properties for storing the values.
35  * The use of this Cache-implementation requires the use of the parameter
36  * <param name="cache.cachefile" .../> for defining, where to store the
37  * properties file.
38  *
39  * The ModifiedSelector sets the <i>cachefile</i> to the default value
40  * <i>cache.properties</i>.
41  *
42  * Supported <param>s are:
43  * <table>
44  * <tr>
45  * <th>name</th><th>values</th><th>description</th><th>required</th>
46  * </tr>
47  * <tr>
48  * <td> cache.cachefile </td>
49  * <td> <i>path to file</i> </td>
50  * <td> the name of the properties file </td>
51  * <td> yes </td>
52  * </tr>
53  * </table>
54  *
55  * @version 2003-09-13
56  * @since Ant 1.6
57  */

58 public class PropertiesfileCache implements Cache {
59
60
61     // ----- member variables - configuration -----
62

63
64     /** Where to store the properties? */
65     private File JavaDoc cachefile = null;
66
67     /** Object for storing the key-value-pairs. */
68     private Properties JavaDoc cache = new Properties JavaDoc();
69
70
71     // ----- member variables - internal use -----
72

73
74     /** Is the cache already loaded? Prevents from multiple load operations. */
75     private boolean cacheLoaded = false;
76
77     /** Must the cache be saved? Prevents from multiple save operations. */
78     private boolean cacheDirty = true;
79
80
81     // ----- Constructors -----
82

83
84     /** Bean-Constructor. */
85     public PropertiesfileCache() {
86     }
87
88     /**
89      * Constructor.
90      * @param cachefile set the cachefile
91      */

92     public PropertiesfileCache(File JavaDoc cachefile) {
93         this.cachefile = cachefile;
94     }
95
96
97     // ----- Cache-Configuration -----
98

99
100     /**
101      * Setter.
102      * @param file new value
103      */

104     public void setCachefile(File JavaDoc file) {
105         cachefile = file;
106     }
107
108
109     /**
110      * Getter.
111      * @return the cachefile
112      */

113     public File JavaDoc getCachefile() {
114         return cachefile;
115     }
116
117     /**
118      * This cache is valid if the cachefile is set.
119      * @return true if all is ok false otherwise
120      */

121     public boolean isValid() {
122         return (cachefile != null);
123     }
124
125
126     // ----- Data Access
127

128
129     /**
130      * Load the cache from underlying properties file.
131      */

132     public void load() {
133         if ((cachefile != null) && cachefile.isFile() && cachefile.canRead()) {
134             try {
135                 BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(
136                     new FileInputStream JavaDoc(cachefile));
137                 cache.load(bis);
138                 bis.close();
139             } catch (Exception JavaDoc e) {
140                 e.printStackTrace();
141             }
142         }
143         // after loading the cache is up to date with the file
144
cacheLoaded = true;
145         cacheDirty = false;
146     }
147
148     /**
149      * Saves modification of the cache.
150      * Cache is only saved if there is one ore more entries.
151      * Because entries can not be deleted by this API, this Cache
152      * implementation checks the existence of entries before creating the file
153      * for performance optimisation.
154      */

155     public void save() {
156         if (!cacheDirty) {
157             return;
158         }
159         if ((cachefile != null) && cache.propertyNames().hasMoreElements()) {
160             try {
161                 BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(
162                       new FileOutputStream JavaDoc(cachefile));
163                 cache.store(bos, null);
164                 bos.flush();
165                 bos.close();
166             } catch (Exception JavaDoc e) {
167                 e.printStackTrace();
168             }
169         }
170         cacheDirty = false;
171     }
172
173     /** Deletes the cache and its underlying file. */
174     public void delete() {
175         cache = new Properties JavaDoc();
176         cachefile.delete();
177         cacheLoaded = true;
178         cacheDirty = false;
179     }
180
181     /**
182      * Returns a value for a given key from the cache.
183      * @param key the key
184      * @return the stored value
185      */

186     public Object JavaDoc get(Object JavaDoc key) {
187         if (!cacheLoaded) {
188             load();
189         }
190         try {
191             return cache.getProperty(String.valueOf(key));
192         } catch (ClassCastException JavaDoc e) {
193             return null;
194         }
195     }
196
197     /**
198      * Saves a key-value-pair in the cache.
199      * @param key the key
200      * @param value the value
201      */

202     public void put(Object JavaDoc key, Object JavaDoc value) {
203         cache.put(String.valueOf(key), String.valueOf(value));
204         cacheDirty = true;
205     }
206
207     /**
208      * Returns an iterator over the keys in the cache.
209      * @return An iterator over the keys.
210      */

211     public Iterator JavaDoc iterator() {
212         Vector JavaDoc v = new java.util.Vector JavaDoc();
213         Enumeration JavaDoc en = cache.propertyNames();
214         while (en.hasMoreElements()) {
215             v.add(en.nextElement());
216         }
217         return v.iterator();
218     }
219
220
221     // ----- additional -----
222

223
224     /**
225      * Override Object.toString().
226      * @return information about this cache
227      */

228     public String JavaDoc toString() {
229         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
230         buf.append("<PropertiesfileCache:");
231         buf.append("cachefile=").append(cachefile);
232         buf.append(";noOfEntries=").append(cache.size());
233         buf.append(">");
234         return buf.toString();
235     }
236 }
237
Popular Tags