Purple Code

com.purpletech.io
Class CachedFS

java.lang.Object
  |
  +--com.purpletech.io.CachedFS

public class CachedFS
extends java.lang.Object

Cached File System: sits on top of a file system, loads files from disk and stores them in memory. Refreshes them if they've changed. Uses com.purpletech.util.Cache (which means that accessing an item resets its timeout clock).

Allows a filter (Loader) that processes files immediately after they're loaded, turning them into interesting objects (like byte[]s or XML documents). By default, the files are returned as String objects (assuming they're text files), but that can easily change by calling setLoader(new CachedFS.BinaryLoader()) which returns them as byte[]s. You can also create your own Loader by implementing the CachedFS.Loader interface. You can also register different loaders for different file extensions (like .gif).


Nested Class Summary
static class CachedFS.BinaryLoader
           
static interface CachedFS.Loader
          Callback that processes a file, turning it into an object, as it's loaded.
static interface CachedFS.Tester
          Callback that provides an extra check as to whether a file needs to be reloaded.
static class CachedFS.TextLoader
           
 
Field Summary
protected  Cache cache
           
static boolean verbose
           
 
Constructor Summary
CachedFS()
          JavaBean initializer -- must call setRoot immediately
CachedFS(java.io.File root)
           
CachedFS(java.lang.String root)
           
 
Method Summary
 void delete(java.lang.String path)
           
 long getCreated(java.lang.String path)
          use JNI to find out the date the file was created -- not yet implemented
 java.lang.Object getFile(java.lang.String path)
          Load a file, or use the cached version if the file hasn't changed.
 java.util.List getFilenames(java.lang.String path)
          Get a list of all files in a given directory.
 long getLastModified(java.lang.String path)
           
 long getModified(java.lang.String path)
           
 java.util.List list(java.lang.String path)
          convenience wrapper
protected  java.lang.Object loadFile(java.lang.String path, java.io.File f)
           
static void main(java.lang.String[] args)
           
protected  java.io.File makeFileObject(java.lang.String path)
           
 void move(java.lang.String path, java.lang.String topath)
           
 void setCacheTimeout(long x)
           
 void setLoader(CachedFS.Loader loader)
          register a default loader.
 void setLoader(java.lang.String ext, CachedFS.Loader loader)
          register a loader for a given extension.
 void setRoot(java.io.File root)
           
 void setRoot(java.lang.String root)
           
 void setTester(CachedFS.Tester t)
           
 java.io.OutputStream writeFile(java.lang.String path)
          Make sure you close the OutputStream! Otherwise, the lock will remain.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

verbose

public static boolean verbose

cache

protected Cache cache
Constructor Detail

CachedFS

public CachedFS()
JavaBean initializer -- must call setRoot immediately


CachedFS

public CachedFS(java.io.File root)
Parameters:
root - the root directory

CachedFS

public CachedFS(java.lang.String root)
Parameters:
root - the root directory
Method Detail

setCacheTimeout

public void setCacheTimeout(long x)

setRoot

public void setRoot(java.lang.String root)
Parameters:
root - the root directory

setRoot

public void setRoot(java.io.File root)
Parameters:
root - the root directory

setLoader

public void setLoader(CachedFS.Loader loader)
register a default loader. E.g. fs.setLoader(new CachedFS.TextLoader())


setLoader

public void setLoader(java.lang.String ext,
                      CachedFS.Loader loader)
register a loader for a given extension. E.g. fs.addLoader("gif", new CachedFS.BinaryLoader())

Parameters:
ext - the file extension to process
loader - the Loader to process that type of file

setTester

public void setTester(CachedFS.Tester t)

makeFileObject

protected java.io.File makeFileObject(java.lang.String path)

getFile

public java.lang.Object getFile(java.lang.String path)
                         throws java.io.IOException
Load a file, or use the cached version if the file hasn't changed.

Parameters:
path - path to file, relative to fs root
java.io.IOException

loadFile

protected java.lang.Object loadFile(java.lang.String path,
                                    java.io.File f)
                             throws java.io.IOException
java.io.IOException

writeFile

public java.io.OutputStream writeFile(java.lang.String path)
                               throws java.io.IOException
Make sure you close the OutputStream! Otherwise, the lock will remain. (Use finally { out.close(); })

java.io.IOException

getFilenames

public java.util.List getFilenames(java.lang.String path)
                            throws java.io.IOException
Get a list of all files in a given directory. Note that it's not cached, since we have no easy way to tell if the contents have changed.

Parameters:
path - directory to list
Returns:
list of all file names in directory. Directories end in '/'
java.io.IOException

list

public java.util.List list(java.lang.String path)
                    throws java.io.IOException
convenience wrapper

java.io.IOException

delete

public void delete(java.lang.String path)
            throws java.io.IOException
java.io.IOException

move

public void move(java.lang.String path,
                 java.lang.String topath)
          throws java.io.IOException
java.io.IOException

getLastModified

public long getLastModified(java.lang.String path)

getModified

public long getModified(java.lang.String path)

getCreated

public long getCreated(java.lang.String path)
use JNI to find out the date the file was created -- not yet implemented


main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
java.io.IOException

Documentation generated March 5 2003