1 18 19 package org.apache.tools.ant.types.selectors.modifiedselector; 20 21 22 import java.util.Iterator ; 23 import java.util.Vector ; 24 import java.util.Enumeration ; 25 import java.util.Properties ; 26 import java.io.File ; 27 import java.io.BufferedInputStream ; 28 import java.io.FileInputStream ; 29 import java.io.BufferedOutputStream ; 30 import java.io.FileOutputStream ; 31 32 33 58 public class PropertiesfileCache implements Cache { 59 60 61 63 64 65 private File cachefile = null; 66 67 68 private Properties cache = new Properties (); 69 70 71 73 74 75 private boolean cacheLoaded = false; 76 77 78 private boolean cacheDirty = true; 79 80 81 83 84 85 public PropertiesfileCache() { 86 } 87 88 92 public PropertiesfileCache(File cachefile) { 93 this.cachefile = cachefile; 94 } 95 96 97 99 100 104 public void setCachefile(File file) { 105 cachefile = file; 106 } 107 108 109 113 public File getCachefile() { 114 return cachefile; 115 } 116 117 121 public boolean isValid() { 122 return (cachefile != null); 123 } 124 125 126 128 129 132 public void load() { 133 if ((cachefile != null) && cachefile.isFile() && cachefile.canRead()) { 134 try { 135 BufferedInputStream bis = new BufferedInputStream ( 136 new FileInputStream (cachefile)); 137 cache.load(bis); 138 bis.close(); 139 } catch (Exception e) { 140 e.printStackTrace(); 141 } 142 } 143 cacheLoaded = true; 145 cacheDirty = false; 146 } 147 148 155 public void save() { 156 if (!cacheDirty) { 157 return; 158 } 159 if ((cachefile != null) && cache.propertyNames().hasMoreElements()) { 160 try { 161 BufferedOutputStream bos = new BufferedOutputStream ( 162 new FileOutputStream (cachefile)); 163 cache.store(bos, null); 164 bos.flush(); 165 bos.close(); 166 } catch (Exception e) { 167 e.printStackTrace(); 168 } 169 } 170 cacheDirty = false; 171 } 172 173 174 public void delete() { 175 cache = new Properties (); 176 cachefile.delete(); 177 cacheLoaded = true; 178 cacheDirty = false; 179 } 180 181 186 public Object get(Object key) { 187 if (!cacheLoaded) { 188 load(); 189 } 190 try { 191 return cache.getProperty(String.valueOf(key)); 192 } catch (ClassCastException e) { 193 return null; 194 } 195 } 196 197 202 public void put(Object key, Object value) { 203 cache.put(String.valueOf(key), String.valueOf(value)); 204 cacheDirty = true; 205 } 206 207 211 public Iterator iterator() { 212 Vector v = new java.util.Vector (); 213 Enumeration en = cache.propertyNames(); 214 while (en.hasMoreElements()) { 215 v.add(en.nextElement()); 216 } 217 return v.iterator(); 218 } 219 220 221 223 224 228 public String toString() { 229 StringBuffer buf = new StringBuffer (); 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 |