org.w3c.tools.resources.store
Class SimpleResourceStore

java.lang.Object
  |
  +--org.w3c.tools.resources.store.SimpleResourceStore

public class SimpleResourceStore
extends java.lang.Object
implements ResourceStore

A very naive resource store. This resource store keeps all the resources in a file. It loads all resources (on a per-demand basis), and never unloads them.


Field Summary
protected  ResourceStoreManager manager
          Our resource store manager.
 
Constructor Summary
SimpleResourceStore()
           
 
Method Summary
 boolean acceptUnload()
          Can the resource store manager unload that store from memory.
 void addResource(org.w3c.tools.resources.Resource resource)
          Add a new resource to the resource store.
 java.util.Enumeration enumerateResourceIdentifiers()
          Enumerate all the resources identifier in this repository.
protected  java.io.RandomAccessFile getFileAt(int pos)
          Get our file, positionned at the given position.
 java.lang.String getIdentifier()
          Get the identifier for that store.
protected  byte[] getResourceBytes(org.w3c.tools.resources.store.ResourceIndex index, byte[] into)
          Get the bytes that are the pickled version of the given resource.
 int getVersion()
          Get the store format version number.
 boolean hasResource(java.lang.String identifier)
          Does this store defines this resource.
 void initialize(ResourceStoreManager manager, java.lang.Object token, java.io.File repository)
          Initialize this simple store with the given file.
protected  void internalSave(boolean unload)
          Internal save: save the repository back to disk.
protected  void loadIndex(java.util.Hashtable target)
          Restore our whole index from our repository.
 org.w3c.tools.resources.Resource loadResource(java.lang.String identifier, java.util.Hashtable defs)
          Load a resource, or get one from the cache.
 org.w3c.tools.resources.Resource lookupResource(java.lang.String identifier)
          Lookup this resource.
static void main(java.lang.String[] args)
           
 void markModified(org.w3c.tools.resources.Resource resource)
          Mark the given resource as being modified.
protected  void markUsed()
          Mark the store as having been used recently.
protected  int pickleResource(org.w3c.tools.resources.Resource resource, java.io.DataOutputStream out)
          Pickle the given resource into the given output stream.
protected  void printIndex()
          Debug - print this resource store index.
 void removeResource(java.lang.String identifier)
          Remove a resource from this resource store.
 void renameResource(java.lang.String oldid, java.lang.String newid)
          Rename a resource in the store.
 void save()
          Save all our modified resources back to our file.
 void saveResource(org.w3c.tools.resources.Resource resource)
          Save a given resource.
 void shutdown()
          Shutdown this store.
 java.lang.String toString()
          Print a simple resource store.
protected  void warning(java.lang.String msg)
          Emit the given string as a warning, to whoever it is appropriate.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

manager

protected ResourceStoreManager manager
Our resource store manager.
Constructor Detail

SimpleResourceStore

public SimpleResourceStore()
Method Detail

markUsed

protected final void markUsed()
Mark the store as having been used recently.

getIdentifier

public java.lang.String getIdentifier()
Get the identifier for that store. We use the repository as an identifier, which is safe here.
Specified by:
getIdentifier in interface ResourceStore
Returns:
The absolute path of the repository.

warning

protected void warning(java.lang.String msg)
Emit the given string as a warning, to whoever it is appropriate.
Parameters:
msg - The warning message.

getFileAt

protected java.io.RandomAccessFile getFileAt(int pos)
                                      throws java.io.IOException
Get our file, positionned at the given position. We won't keep our repository file always open (too much of these resource stroe would burn our available file descriptors).
Parameters:
at - The position at wich you want the stream.

loadIndex

protected void loadIndex(java.util.Hashtable target)
                  throws java.io.IOException
Restore our whole index from our repository.

printIndex

protected void printIndex()
Debug - print this resource store index.

getResourceBytes

protected byte[] getResourceBytes(org.w3c.tools.resources.store.ResourceIndex index,
                                  byte[] into)
                           throws java.io.IOException
Get the bytes that are the pickled version of the given resource. This opens the file for each resource, it could of course be optimized for the cases were all the resources are to be loaded at once.
Parameters:
identifier - The resource identifier.
Returns:
The length, in the instance buffer, of the bytes that makes this resource.

pickleResource

protected int pickleResource(org.w3c.tools.resources.Resource resource,
                             java.io.DataOutputStream out)
                      throws java.io.IOException
Pickle the given resource into the given output stream.
Parameters:
resource - The resource to pickle.
out - The data output stream to pickle to.
Returns:
The size of the pickled version of the resource.

getVersion

public int getVersion()
Get the store format version number.
Specified by:
getVersion in interface ResourceStore
Returns:
An ineteger version number, guaranteed to change whenever the format used by the store changes.

loadResource

public org.w3c.tools.resources.Resource loadResource(java.lang.String identifier,
                                                     java.util.Hashtable defs)
                                              throws org.w3c.tools.resources.InvalidResourceException
Load a resource, or get one from the cache.
Specified by:
loadResource in interface ResourceStore
Parameters:
identifier - The resource identifier.
Returns:
A Resource instance, or null if the resource doesn't exist in that store.
Throws:
org.w3c.tools.resources.InvalidResourceException - If the resource couldn't be restored from its pickled format.

internalSave

protected void internalSave(boolean unload)
                     throws java.io.IOException
Internal save: save the repository back to disk.
Parameters:
unload - Should we unload any existing resources ?

lookupResource

public org.w3c.tools.resources.Resource lookupResource(java.lang.String identifier)
Lookup this resource.
Specified by:
lookupResource in interface ResourceStore
Parameters:
identifier - The resource identifier.
Returns:
A Resource instance, or null if either the resource doesn't exist, or it isn't loaded yet.

saveResource

public void saveResource(org.w3c.tools.resources.Resource resource)
Save a given resource. If the resource has been modified, we save all our resources.
Specified by:
saveResource in interface ResourceStore
Parameters:
resource - The resource to be save right now.

addResource

public void addResource(org.w3c.tools.resources.Resource resource)
Add a new resource to the resource store.
Specified by:
addResource in interface ResourceStore
Parameters:
resource - The resource to add.

removeResource

public void removeResource(java.lang.String identifier)
Remove a resource from this resource store.
Specified by:
removeResource in interface ResourceStore
Parameters:
identifier - The identifier of the resource to be removed.

renameResource

public void renameResource(java.lang.String oldid,
                           java.lang.String newid)
Rename a resource in the store.
Specified by:
renameResource in interface ResourceStore
Parameters:
oldid - The old resource identifier.
newid - The new resource identifier.

markModified

public void markModified(org.w3c.tools.resources.Resource resource)
Mark the given resource as being modified.
Specified by:
markModified in interface ResourceStore
Parameters:
identifier - The identifier of the modified resource.

acceptUnload

public boolean acceptUnload()
Can the resource store manager unload that store from memory. We check all the loaded resource, to see if they are willing to be unloaded; we do unload straight all the resources that accept (to reduce memory usage), and return false only if one of our resource refuse to be unloaded.
Specified by:
acceptUnload in interface ResourceStore

shutdown

public void shutdown()
Shutdown this store.
Specified by:
shutdown in interface ResourceStore

save

public void save()
Save all our modified resources back to our file.
Specified by:
save in interface ResourceStore

enumerateResourceIdentifiers

public java.util.Enumeration enumerateResourceIdentifiers()
Enumerate all the resources identifier in this repository.
Specified by:
enumerateResourceIdentifiers in interface ResourceStore

hasResource

public boolean hasResource(java.lang.String identifier)
Does this store defines this resource.
Specified by:
hasResource in interface ResourceStore
Parameters:
identifier - The resource we are looking for.
Returns:
A boolean true if found.

toString

public java.lang.String toString()
Print a simple resource store.
Overrides:
toString in class java.lang.Object

initialize

public void initialize(ResourceStoreManager manager,
                       java.lang.Object token,
                       java.io.File repository)
Initialize this simple store with the given file.
Specified by:
initialize in interface ResourceStore
Parameters:
manager - The resource store manager that loaded use.
token - Our identification token from the above resource store manager (this is an opaque object).
file - The repository file.

main

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