Version: 1.1.1

org.biomoby.client
Class FileCache

java.lang.Object
  extended by org.biomoby.client.FileCache
All Implemented Interfaces:
SimpleCache, SimpleFileCache
Direct Known Subclasses:
ServletFileCache

public class FileCache
extends Object
implements SimpleFileCache

A simple cache implementation to save and later return (any) data, or to find that the given data are not available in the cache. It uses files to store data.

Version:
$Id: FileCache.java,v 1.5 2005/04/07 16:37:02 kawas Exp $
Author:
Martin Senger

Constructor Summary
FileCache(String rootDirName, String rootURLName)
          Constructor specifying where to create files with the cached objects.
 
Method Summary
 String createId(String rootName, String semanticType, String syntaxType, long lastModified, Properties props)
          It creates an 'id' in the form: rootName / semanticType / { prop1_prop2_... } (time).syntaxType The part between { and } (inclusive) will be replaced by a unique shorter string in order to create a reasonably long real file name.
 boolean existsInCache(String id)
          It expects the 'id' in the form as created by 'createId'.
 Object getContents(String id)
          This class does not implement this method.
 String getFilename(String id)
          Return a fully qualified path to a file that represents the cached object identified by the 'id'.
 String getURL(String id)
          Return a pointer to cached object identified by its 'id'.
 void removeFromCache(String id)
          Remove cached object identified by its 'id' from the cache.
 void removeOlderThen(long millis)
          Remove all cached objects that are in cache longer that specifies by 'millis'.
 void setContents(String id, byte[] data)
          Store 'data' as an object identified by 'id'.
 void setContents(String id, Object data)
          This class does not implement this method.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileCache

public FileCache(String rootDirName,
                 String rootURLName)
Constructor specifying where to create files with the cached objects. The cache files will be stored in
 <rootDirName>/cache/
 
The all not yet existing directories (for example the last 'cache' directory) will be created for you.

'rootURLName' is used by method getURL(java.lang.String) to return back cached data. It serves as a prefix (perhaps including the protocol, host and port) to returned URLs. In other words: 'rootDirName' tells where to create physical files, and 'rootULName' tells how to find these file using HTTP or other protocols.

Method Detail

createId

public String createId(String rootName,
                       String semanticType,
                       String syntaxType,
                       long lastModified,
                       Properties props)
It creates an 'id' in the form: rootName / semanticType / { prop1_prop2_... } (time).syntaxType The part between { and } (inclusive) will be replaced by a unique shorter string in order to create a reasonably long real file name.

Specified by:
createId in interface SimpleCache
Parameters:
rootName - is some high level name of the cached object
semanticType - is yet another feature of the cached object
syntaxType - is another property of the cached object (e.i. it may be used as the file name suffix if the cache is implemented as a filesystem)
lastModified - is time in millis indicating when the cached object was created or modified
props - are all remaining properties identifying the cached object
Returns:
a unique ID that can be used in other methods defined in this interface in order to identify the same cached object

existsInCache

public boolean existsInCache(String id)
It expects the 'id' in the form as created by 'createId'.

Specified by:
existsInCache in interface SimpleCache
Parameters:
id - a unique ID of the cached (or possibly cached) object
Returns:
true if the object identified by 'id' already exists in this cache

getContents

public Object getContents(String id)
                   throws IOException
This class does not implement this method. Use getFilename(java.lang.String) and read the returned file yourself.

Specified by:
getContents in interface SimpleCache
Parameters:
id - a unique ID of the object being returned
Returns:
data previously stored under 'id'; or null if such data do not exist
Throws:
IOException

setContents

public void setContents(String id,
                        Object data)
                 throws IOException
This class does not implement this method. Use setContents(String,byte[]) to store data in a file.

Specified by:
setContents in interface SimpleCache
Parameters:
id - a unique ID of the object being stored
data - are being stored
Throws:
IOException

getFilename

public String getFilename(String id)
                   throws IOException
Description copied from interface: SimpleFileCache
Return a fully qualified path to a file that represents the cached object identified by the 'id'. Note that this file does not need to exist yet (if no object with given 'id' was stored) - but the path to the file must exist, and if not it must be created by this method.

Specified by:
getFilename in interface SimpleFileCache
Parameters:
id - a unique ID of the cached (or possibly cached) object
Returns:
a filename with the full path representing an object 'id'
Throws:
IOException - if creating the parent directories caused problem

setContents

public void setContents(String id,
                        byte[] data)
                 throws IOException
Description copied from interface: SimpleFileCache
Store 'data' as an object identified by 'id'.

Specified by:
setContents in interface SimpleFileCache
Parameters:
id - a unique ID of the object being stored
data - are being stored
Throws:
IOException

getURL

public String getURL(String id)
Return a pointer to cached object identified by its 'id'. If a 'rootURLName' (given in the constructor) is not null it returns:
    <rootURLName>/<name>
where <name> is a name relative to the beginning of the cache (meaning that 'rootDirName' is not included here). If, however, the 'rootURLName ' is null it returns:
    file:<name>
where the <name> is the same as above.

Specified by:
getURL in interface SimpleFileCache
Parameters:
id - a unique ID

removeFromCache

public void removeFromCache(String id)
                     throws IOException
Description copied from interface: SimpleCache
Remove cached object identified by its 'id' from the cache.

Specified by:
removeFromCache in interface SimpleCache
Parameters:
id - a unique ID
Throws:
IOException - if the object exists but cannot be removed (meaning that it does not raise any exception if the object is not anymore in the cache)

removeOlderThen

public void removeOlderThen(long millis)
                     throws IOException
Description copied from interface: SimpleFileCache
Remove all cached objects that are in cache longer that specifies by 'millis'.

Specified by:
removeOlderThen in interface SimpleFileCache
Parameters:
millis - how many milliseconds must be an object stored to be remopved by calling this method
Throws:
IOException - if any object that exists in the cache and is targeted to be removed but cannot be removed

Version: 1.1.1

Submit a bug or feature
Generated: Sat May 29 04:26:35 EDT 2010