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. package org.apache.tools.ant.types.selectors.modifiedselector;
  18. import java.util.Iterator;
  19. import java.util.Vector;
  20. import java.util.Enumeration;
  21. import java.util.Properties;
  22. import java.io.File;
  23. import java.io.BufferedInputStream;
  24. import java.io.FileInputStream;
  25. import java.io.BufferedOutputStream;
  26. import java.io.FileOutputStream;
  27. /**
  28. * Use java.util.Properties for storing the values.
  29. * The use of this Cache-implementation requires the use of the parameter
  30. * <param name="cache.cachefile" .../> for defining, where to store the
  31. * properties file.
  32. *
  33. * The ModifiedSelector sets the <i>cachefile</i> to the default value
  34. * <i>cache.properties</i>.
  35. *
  36. * Supported <param>s are:
  37. * <table>
  38. * <tr>
  39. * <th>name</th><th>values</th><th>description</th><th>required</th>
  40. * </tr>
  41. * <tr>
  42. * <td> cache.cachefile </td>
  43. * <td> <i>path to file</i> </td>
  44. * <td> the name of the properties file </td>
  45. * <td> yes </td>
  46. * </tr>
  47. * </table>
  48. *
  49. * @version 2003-09-13
  50. * @since Ant 1.6
  51. */
  52. public class PropertiesfileCache implements Cache {
  53. // ----- member variables - configuration -----
  54. /** Where to store the properties? */
  55. private File cachefile = null;
  56. /** Object for storing the key-value-pairs. */
  57. private Properties cache = new Properties();
  58. // ----- member variables - internal use -----
  59. /** Is the cache already loaded? Prevents from multiple load operations. */
  60. private boolean cacheLoaded = false;
  61. /** Must the cache be saved? Prevents from multiple save operations. */
  62. private boolean cacheDirty = true;
  63. // ----- Constructors -----
  64. /** Bean-Constructor. */
  65. public PropertiesfileCache() {
  66. }
  67. /**
  68. * Constructor.
  69. * @param cachefile set the cachefile
  70. */
  71. public PropertiesfileCache(File cachefile) {
  72. this.cachefile = cachefile;
  73. }
  74. // ----- Cache-Configuration -----
  75. public void setCachefile(File file) {
  76. cachefile = file;
  77. }
  78. public File getCachefile() { return cachefile; }
  79. public boolean isValid() {
  80. return (cachefile != null);
  81. }
  82. // ----- Data Access
  83. public void load() {
  84. if ((cachefile != null) && cachefile.isFile() && cachefile.canRead()) {
  85. try {
  86. BufferedInputStream bis = new BufferedInputStream(
  87. new FileInputStream(cachefile));
  88. cache.load(bis);
  89. bis.close();
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. // after loading the cache is up to date with the file
  95. cacheLoaded = true;
  96. cacheDirty = false;
  97. }
  98. /**
  99. * Saves modification of the cache.
  100. * Cache is only saved if there is one ore more entries.
  101. * Because entries can not be deleted by this API, this Cache
  102. * implementation checks the existence of entries before creating the file
  103. * for performance optimisation.
  104. */
  105. public void save() {
  106. if (!cacheDirty) {
  107. return;
  108. }
  109. if ((cachefile != null) && cache.propertyNames().hasMoreElements()) {
  110. try {
  111. BufferedOutputStream bos = new BufferedOutputStream(
  112. new FileOutputStream(cachefile));
  113. cache.store(bos, null);
  114. bos.flush();
  115. bos.close();
  116. } catch (Exception e) {
  117. e.printStackTrace();
  118. }
  119. }
  120. cacheDirty = false;
  121. }
  122. /** Deletes the cache and its underlying file. */
  123. public void delete() {
  124. cache = new Properties();
  125. cachefile.delete();
  126. cacheLoaded = true;
  127. cacheDirty = false;
  128. }
  129. /**
  130. * Returns a value for a given key from the cache.
  131. * @param key the key
  132. * @return the stored value
  133. */
  134. public Object get(Object key) {
  135. if (!cacheLoaded) {
  136. load();
  137. }
  138. try {
  139. return cache.getProperty(String.valueOf(key));
  140. } catch (ClassCastException e) {
  141. return null;
  142. }
  143. }
  144. /**
  145. * Saves a key-value-pair in the cache.
  146. * @param key the key
  147. * @param value the value
  148. */
  149. public void put(Object key, Object value) {
  150. cache.put(String.valueOf(key), String.valueOf(value));
  151. cacheDirty = true;
  152. }
  153. /**
  154. * Returns an iterator over the keys in the cache.
  155. * @return An iterator over the keys.
  156. */
  157. public Iterator iterator() {
  158. Vector v = new java.util.Vector();
  159. Enumeration en = cache.propertyNames();
  160. while (en.hasMoreElements()) {
  161. v.add(en.nextElement());
  162. }
  163. return v.iterator();
  164. }
  165. // ----- additional -----
  166. /**
  167. * Override Object.toString().
  168. * @return information about this cache
  169. */
  170. public String toString() {
  171. StringBuffer buf = new StringBuffer();
  172. buf.append("<PropertiesfileCache:");
  173. buf.append("cachefile=").append(cachefile);
  174. buf.append(";noOfEntries=").append(cache.size());
  175. buf.append(">");
  176. return buf.toString();
  177. }
  178. }