FileDocCategorySizeDatePackage
PropertiesfileCache.javaAPI DocApache Ant 1.706274Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.types.selectors.modifiedselector

PropertiesfileCache

public class PropertiesfileCache extends Object implements Cache
Use java.util.Properties for storing the values. The use of this Cache-implementation requires the use of the parameter for defining, where to store the properties file. The ModifiedSelector sets the cachefile to the default value cache.properties. Supported s are:
namevaluesdescriptionrequired
cache.cachefile path to file the name of the properties file yes
version
2003-09-13
since
Ant 1.6

Fields Summary
private File
cachefile
Where to store the properties?
private Properties
cache
Object for storing the key-value-pairs.
private boolean
cacheLoaded
Is the cache already loaded? Prevents from multiple load operations.
private boolean
cacheDirty
Must the cache be saved? Prevents from multiple save operations.
Constructors Summary
public PropertiesfileCache()
Bean-Constructor.



    // -----  Constructors  -----


      
      
    
public PropertiesfileCache(File cachefile)
Constructor.

param
cachefile set the cachefile

        this.cachefile = cachefile;
    
Methods Summary
public voiddelete()
Deletes the cache and its underlying file.

        cache = new Properties();
        cachefile.delete();
        cacheLoaded = true;
        cacheDirty = false;
    
public java.lang.Objectget(java.lang.Object key)
Returns a value for a given key from the cache.

param
key the key
return
the stored value

        if (!cacheLoaded) {
            load();
        }
        try {
            return cache.getProperty(String.valueOf(key));
        } catch (ClassCastException e) {
            return null;
        }
    
public java.io.FilegetCachefile()
Getter.

return
the cachefile

        return cachefile;
    
public booleanisValid()
This cache is valid if the cachefile is set.

return
true if all is ok false otherwise

        return (cachefile != null);
    
public java.util.Iteratoriterator()
Returns an iterator over the keys in the cache.

return
An iterator over the keys.

        Vector v = new java.util.Vector();
        Enumeration en = cache.propertyNames();
        while (en.hasMoreElements()) {
            v.add(en.nextElement());
        }
        return v.iterator();
    
public voidload()
Load the cache from underlying properties file.

        if ((cachefile != null) && cachefile.isFile() && cachefile.canRead()) {
            try {
                BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(cachefile));
                cache.load(bis);
                bis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // after loading the cache is up to date with the file
        cacheLoaded = true;
        cacheDirty  = false;
    
public voidput(java.lang.Object key, java.lang.Object value)
Saves a key-value-pair in the cache.

param
key the key
param
value the value

        cache.put(String.valueOf(key), String.valueOf(value));
        cacheDirty = true;
    
public voidsave()
Saves modification of the cache. Cache is only saved if there is one ore more entries. Because entries can not be deleted by this API, this Cache implementation checks the existence of entries before creating the file for performance optimisation.

        if (!cacheDirty) {
            return;
        }
        if ((cachefile != null) && cache.propertyNames().hasMoreElements()) {
            try {
                BufferedOutputStream bos = new BufferedOutputStream(
                      new FileOutputStream(cachefile));
                cache.store(bos, null);
                bos.flush();
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        cacheDirty = false;
    
public voidsetCachefile(java.io.File file)
Setter.

param
file new value

        cachefile = file;
    
public java.lang.StringtoString()
Override Object.toString().

return
information about this cache

        StringBuffer buf = new StringBuffer();
        buf.append("<PropertiesfileCache:");
        buf.append("cachefile=").append(cachefile);
        buf.append(";noOfEntries=").append(cache.size());
        buf.append(">");
        return buf.toString();