1 | /*
|
---|
2 | * Copyright 2003-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.types.selectors.modifiedselector;
|
---|
19 |
|
---|
20 |
|
---|
21 | import java.util.Iterator;
|
---|
22 | import java.util.Vector;
|
---|
23 | import java.util.Enumeration;
|
---|
24 | import java.util.Properties;
|
---|
25 | import java.io.File;
|
---|
26 | import java.io.BufferedInputStream;
|
---|
27 | import java.io.FileInputStream;
|
---|
28 | import java.io.BufferedOutputStream;
|
---|
29 | import java.io.FileOutputStream;
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Use java.util.Properties for storing the values.
|
---|
34 | * The use of this Cache-implementation requires the use of the parameter
|
---|
35 | * <param name="cache.cachefile" .../> for defining, where to store the
|
---|
36 | * properties file.
|
---|
37 | *
|
---|
38 | * The ModifiedSelector sets the <i>cachefile</i> to the default value
|
---|
39 | * <i>cache.properties</i>.
|
---|
40 | *
|
---|
41 | * Supported <param>s are:
|
---|
42 | * <table>
|
---|
43 | * <tr>
|
---|
44 | * <th>name</th><th>values</th><th>description</th><th>required</th>
|
---|
45 | * </tr>
|
---|
46 | * <tr>
|
---|
47 | * <td> cache.cachefile </td>
|
---|
48 | * <td> <i>path to file</i> </td>
|
---|
49 | * <td> the name of the properties file </td>
|
---|
50 | * <td> yes </td>
|
---|
51 | * </tr>
|
---|
52 | * </table>
|
---|
53 | *
|
---|
54 | * @version 2003-09-13
|
---|
55 | * @since Ant 1.6
|
---|
56 | */
|
---|
57 | public class PropertiesfileCache implements Cache {
|
---|
58 |
|
---|
59 |
|
---|
60 | // ----- member variables - configuration -----
|
---|
61 |
|
---|
62 |
|
---|
63 | /** Where to store the properties? */
|
---|
64 | private File cachefile = null;
|
---|
65 |
|
---|
66 | /** Object for storing the key-value-pairs. */
|
---|
67 | private Properties cache = new Properties();
|
---|
68 |
|
---|
69 |
|
---|
70 | // ----- member variables - internal use -----
|
---|
71 |
|
---|
72 |
|
---|
73 | /** Is the cache already loaded? Prevents from multiple load operations. */
|
---|
74 | private boolean cacheLoaded = false;
|
---|
75 |
|
---|
76 | /** Must the cache be saved? Prevents from multiple save operations. */
|
---|
77 | private boolean cacheDirty = true;
|
---|
78 |
|
---|
79 |
|
---|
80 | // ----- Constructors -----
|
---|
81 |
|
---|
82 |
|
---|
83 | /** Bean-Constructor. */
|
---|
84 | public PropertiesfileCache() {
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Constructor.
|
---|
89 | * @param cachefile set the cachefile
|
---|
90 | */
|
---|
91 | public PropertiesfileCache(File cachefile) {
|
---|
92 | this.cachefile = cachefile;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | // ----- Cache-Configuration -----
|
---|
97 |
|
---|
98 |
|
---|
99 | public void setCachefile(File file) {
|
---|
100 | cachefile = file;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public File getCachefile() { return cachefile; }
|
---|
104 |
|
---|
105 | public boolean isValid() {
|
---|
106 | return (cachefile != null);
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | // ----- Data Access
|
---|
111 |
|
---|
112 |
|
---|
113 | public void load() {
|
---|
114 | if ((cachefile != null) && cachefile.isFile() && cachefile.canRead()) {
|
---|
115 | try {
|
---|
116 | BufferedInputStream bis = new BufferedInputStream(
|
---|
117 | new FileInputStream(cachefile));
|
---|
118 | cache.load(bis);
|
---|
119 | bis.close();
|
---|
120 | } catch (Exception e) {
|
---|
121 | e.printStackTrace();
|
---|
122 | }
|
---|
123 | }
|
---|
124 | // after loading the cache is up to date with the file
|
---|
125 | cacheLoaded = true;
|
---|
126 | cacheDirty = false;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Saves modification of the cache.
|
---|
131 | * Cache is only saved if there is one ore more entries.
|
---|
132 | * Because entries can not be deleted by this API, this Cache
|
---|
133 | * implementation checks the existence of entries before creating the file
|
---|
134 | * for performance optimisation.
|
---|
135 | */
|
---|
136 | public void save() {
|
---|
137 | if (!cacheDirty) {
|
---|
138 | return;
|
---|
139 | }
|
---|
140 | if ((cachefile != null) && cache.propertyNames().hasMoreElements()) {
|
---|
141 | try {
|
---|
142 | BufferedOutputStream bos = new BufferedOutputStream(
|
---|
143 | new FileOutputStream(cachefile));
|
---|
144 | cache.store(bos, null);
|
---|
145 | bos.flush();
|
---|
146 | bos.close();
|
---|
147 | } catch (Exception e) {
|
---|
148 | e.printStackTrace();
|
---|
149 | }
|
---|
150 | }
|
---|
151 | cacheDirty = false;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Deletes the cache and its underlying file. */
|
---|
155 | public void delete() {
|
---|
156 | cache = new Properties();
|
---|
157 | cachefile.delete();
|
---|
158 | cacheLoaded = true;
|
---|
159 | cacheDirty = false;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Returns a value for a given key from the cache.
|
---|
164 | * @param key the key
|
---|
165 | * @return the stored value
|
---|
166 | */
|
---|
167 | public Object get(Object key) {
|
---|
168 | if (!cacheLoaded) {
|
---|
169 | load();
|
---|
170 | }
|
---|
171 | try {
|
---|
172 | return cache.getProperty(String.valueOf(key));
|
---|
173 | } catch (ClassCastException e) {
|
---|
174 | return null;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Saves a key-value-pair in the cache.
|
---|
180 | * @param key the key
|
---|
181 | * @param value the value
|
---|
182 | */
|
---|
183 | public void put(Object key, Object value) {
|
---|
184 | cache.put(String.valueOf(key), String.valueOf(value));
|
---|
185 | cacheDirty = true;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Returns an iterator over the keys in the cache.
|
---|
190 | * @return An iterator over the keys.
|
---|
191 | */
|
---|
192 | public Iterator iterator() {
|
---|
193 | Vector v = new java.util.Vector();
|
---|
194 | Enumeration en = cache.propertyNames();
|
---|
195 | while (en.hasMoreElements()) {
|
---|
196 | v.add(en.nextElement());
|
---|
197 | }
|
---|
198 | return v.iterator();
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | // ----- additional -----
|
---|
203 |
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Override Object.toString().
|
---|
207 | * @return information about this cache
|
---|
208 | */
|
---|
209 | public String toString() {
|
---|
210 | StringBuffer buf = new StringBuffer();
|
---|
211 | buf.append("<PropertiesfileCache:");
|
---|
212 | buf.append("cachefile=").append(cachefile);
|
---|
213 | buf.append(";noOfEntries=").append(cache.size());
|
---|
214 | buf.append(">");
|
---|
215 | return buf.toString();
|
---|
216 | }
|
---|
217 | }
|
---|