org.w3c.tools.resources.store
Class ResourceStoreManager

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

public class ResourceStoreManager
extends java.lang.Object
implements ResourceSpace


Field Summary
protected  boolean closed
          Is this store shutdown ?
 boolean debugMemory
           
protected  ResourceEventQueue eventQueue
           
protected  java.io.File index
          Our index file.
protected  LRUList lru
          The store entries least recetenly used list.
static java.lang.String root_rep
           
protected  java.io.File root_repository
          Our root repository.
protected  java.lang.String server_name
          server name;
protected  ResourceStoreState state
           
protected  java.io.File storedir
          Our store directory.
static int STOREFILE_IDENTIFIER
          The identifier for resource store files.
protected  org.w3c.tools.resources.store.StoreManagerSweeper sweeper
          Our sweeper thread:
 
Constructor Summary
ResourceStoreManager(java.lang.String server_name, java.io.File storedir, java.lang.String default_root_class, java.lang.String default_root_name, int max_loaded_store, java.util.Hashtable defs)
          Create a new resource store manager for given store directory.
 
Method Summary
 void acquireChildren(SpaceEntry sentry)
          Acquire the StoreEntry of the space entry.
 void acquireChildren(SpaceEntry sentry, java.io.File repository, boolean transientFlag)
          acquire children from an external file.
 ResourceReference addResource(SpaceEntry sentry, Resource resource, java.util.Hashtable defs)
          Add this resource to the StoreEntry of the space entry.
protected  void checkClosed()
          Check that this resource store manager isn't closed.
protected  void checkMaxLoadedStore()
           
 void checkpoint()
          Checkpoint all modified resource stores, by saving them to disk.
 void collect()
          Collect enough entries to go back into fixed limits.
 java.lang.String createResourceStoreRepository()
          Create a resource store repository name.
protected  void decrLoadedStore()
           
 void deleteChildren(SpaceEntry sentry)
          Delete all the children of resource indentified by its space entry.
 void deleteResource(SpaceEntry sentry, Resource resource)
          delete this resource from the StoreEntry (and the repository).
 void displayIndex()
           
 java.util.Enumeration enumerateResourceIdentifiers(SpaceEntry sentry)
          Enumerate the name (ie identifiers) of the space entry children.
 int getCurrentStoreIdentifier()
           
 ResourceEventQueue getEventQueue()
           
protected  java.io.File getIndexFile()
          Get the index file.
protected  int getMaxLoadedStore()
           
protected  java.lang.Integer getRootKey()
          Get The root key.
protected  java.io.File getRootRepository()
          Get the root repository.
protected  void incrLoadedStore()
           
protected  void loadEntriesIndex()
          Load the index file.
 ResourceReference loadResource(SpaceEntry sentry, java.lang.String identifier, java.util.Hashtable defs)
          Restore the resource whose name is given.
 ResourceReference loadRootResource(java.lang.String identifier, java.util.Hashtable defs)
          Restore the resource whose name is given from the root StoreEntry.
 ResourceReference lookupResource(SpaceEntry sentry, java.lang.String identifier)
          Lookup this resource.
 void markModified(SpaceEntry sentry, Resource resource)
          Mark the given resource as being modified.
 void markUsed(java.lang.Object token)
          Mark the given store as having been used recently.
static void pickleProlog(java.io.DataOutputStream out, ResourceStore store)
          Dump a prolog in given stream for that resource store.
protected  org.w3c.tools.resources.store.StoreEntry pickLRUEntry()
          Pick the least recently used entry, and remove all links to it.
 void renameResource(SpaceEntry sentry, java.lang.String oldid, java.lang.String newid)
          Rename a resource in this resource space.
 void salvage()
           
 ResourceStoreState salvageState()
          Try to salvage the resource store manager state.
 void saveChildren(SpaceEntry sentry)
          Save all the children of the resource indentified by its spaec entry.
protected  void saveEntriesIndex()
          Save the index file.
 void saveResource(SpaceEntry sentry, Resource resource)
          Save this resource to the StoreEntry of the space entry.
 void shutdown()
          Shutdown this resource store manager.
static void unpickleProlog(java.io.DataInputStream in, ResourceStore store)
          Read in the prolog from the given input stream.
protected  boolean used(java.lang.String rep)
           
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, toString, wait, wait, wait
 

Field Detail

STOREFILE_IDENTIFIER

public static final int STOREFILE_IDENTIFIER
The identifier for resource store files. All files that are used as resource store, and whose resource store implementation make use of pickleProlog will start with a dump of that integer.
See Also:
pickleProlog(java.io.DataOutputStream, org.w3c.tools.resources.store.ResourceStore), unpickleProlog(java.io.DataInputStream, org.w3c.tools.resources.store.ResourceStore)

root_rep

public static final java.lang.String root_rep

debugMemory

public boolean debugMemory

closed

protected boolean closed
Is this store shutdown ?

storedir

protected java.io.File storedir
Our store directory.

index

protected java.io.File index
Our index file.

server_name

protected java.lang.String server_name
server name;

root_repository

protected java.io.File root_repository
Our root repository.

lru

protected LRUList lru
The store entries least recetenly used list.

state

protected ResourceStoreState state

sweeper

protected org.w3c.tools.resources.store.StoreManagerSweeper sweeper
Our sweeper thread:

eventQueue

protected ResourceEventQueue eventQueue
Constructor Detail

ResourceStoreManager

public ResourceStoreManager(java.lang.String server_name,
                            java.io.File storedir,
                            java.lang.String default_root_class,
                            java.lang.String default_root_name,
                            int max_loaded_store,
                            java.util.Hashtable defs)
Create a new resource store manager for given store directory. The resource store manager will manage the collection of stores contained in the directory, and keep track of the stores state.
Parameters:
storedir - The store directory to manage.
Method Detail

getEventQueue

public ResourceEventQueue getEventQueue()
Specified by:
getEventQueue in interface ResourceSpace

getMaxLoadedStore

protected final int getMaxLoadedStore()

incrLoadedStore

protected void incrLoadedStore()

decrLoadedStore

protected void decrLoadedStore()

checkClosed

protected final void checkClosed()
Check that this resource store manager isn't closed.
Throws:
java.lang.RuntimeException - If the store manager was closed.

pickLRUEntry

protected org.w3c.tools.resources.store.StoreEntry pickLRUEntry()
Pick the least recently used entry, and remove all links to it. After this method as run, the least recently used entry for some store will be returned. The store manager will have discarded all its link to it, and the entry shutdown will have to be performed by the caller.
Returns:
An StoreEntry instance, to be cleaned up.

collect

public void collect()
Collect enough entries to go back into fixed limits.

createResourceStoreRepository

public java.lang.String createResourceStoreRepository()
Create a resource store repository name. This method will return a new resource store repository key. When used in conjunction with the loadResourceStore method that takes a key as a parameter, this allows to caller to abstract itself from the physical location of the repository.
Returns:
A fresh resource store key, guaranteed to be uniq.

getCurrentStoreIdentifier

public int getCurrentStoreIdentifier()

getIndexFile

protected java.io.File getIndexFile()
Get the index file.
Returns:
A File instance.

getRootRepository

protected java.io.File getRootRepository()
Get the root repository.
Returns:
A File instance.

getRootKey

protected java.lang.Integer getRootKey()
Get The root key.
Returns:
A String instance

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.

saveEntriesIndex

protected void saveEntriesIndex()
Save the index file.

loadEntriesIndex

protected void loadEntriesIndex()
Load the index file.

shutdown

public void shutdown()
Shutdown this resource store manager. Go through all entries, and shut them down.
Specified by:
shutdown in interface ResourceSpace

checkpoint

public void checkpoint()
Checkpoint all modified resource stores, by saving them to disk.
Specified by:
checkpoint in interface ResourceSpace

markUsed

public void markUsed(java.lang.Object token)
Mark the given store as having been used recently.
Parameters:
token - The token the resource store manager provided you when it initialized the store.

pickleProlog

public static void pickleProlog(java.io.DataOutputStream out,
                                ResourceStore store)
                         throws java.io.IOException
Dump a prolog in given stream for that resource store. Using the resource store manager prolog in a resource store implementation is optional. However, it is recommended that whenever possible, you use it to get the benefits of the main method of that class.
Parameters:
out - The output stream to dump the prolog to.
store - The resource store which is being dumped.
Throws:
java.io.IOException - If the prolog couldn't be pickled to the given output stream.
See Also:
unpickleProlog(java.io.DataInputStream, org.w3c.tools.resources.store.ResourceStore)

unpickleProlog

public static void unpickleProlog(java.io.DataInputStream in,
                                  ResourceStore store)
                           throws java.io.IOException,
                                  NotAStoreException,
                                  InvalidStoreClassException,
                                  StoreClassVersionException
Read in the prolog from the given input stream. This method checks the consistency between the implementation of the store that wants to read from that stream, and the implementation of the store that has been used to create it.

Using that method, as well as using the pickleProlog method is optional; however, using one makes using the other mandatory.

Parameters:
in - The input stream to read the prolog from.
store - The store that is to be created from the given input stream.
Throws:
java.io.IOException - If the stream couldn't be read.
NotAStoreException - If the given repository wasn't flagged as containing a resource store.
InvalidStoreClassException - If the loading class is not the saving class.
StoreClassVersionException - If the saved store version number doesn't match the loader's class version number.
See Also:
pickleProlog(java.io.DataOutputStream, org.w3c.tools.resources.store.ResourceStore)

used

protected boolean used(java.lang.String rep)

salvage

public void salvage()

displayIndex

public void displayIndex()

salvageState

public ResourceStoreState salvageState()
Try to salvage the resource store manager state. That's pretty easy and robust, it should really work well if no one hacked the store directory.
Returns:
A ResourceStoreState instance.

loadRootResource

public ResourceReference loadRootResource(java.lang.String identifier,
                                          java.util.Hashtable defs)
Restore the resource whose name is given from the root StoreEntry.
Specified by:
loadRootResource in interface ResourceSpace
Parameters:
identifier - The identifier of the resource to restore.
defs - Default attribute values.

lookupResource

public ResourceReference lookupResource(SpaceEntry sentry,
                                        java.lang.String identifier)
Lookup this resource.
Specified by:
lookupResource in interface ResourceSpace
Parameters:
sentry - The resource space entry.
identifier - The resource identifier.
Returns:
A Resource instance, or null if either the resource doesn't exist, or it isn't loaded yet.

loadResource

public ResourceReference loadResource(SpaceEntry sentry,
                                      java.lang.String identifier,
                                      java.util.Hashtable defs)
Restore the resource whose name is given.
Specified by:
loadResource in interface ResourceSpace
Parameters:
sentry - The resource space entry.
identifier - The identifier of the resource to restore.
defs - Default attribute values.

addResource

public ResourceReference addResource(SpaceEntry sentry,
                                     Resource resource,
                                     java.util.Hashtable defs)
Add this resource to the StoreEntry of the space entry.
Specified by:
addResource in interface ResourceSpace
Parameters:
sentry - The resource space entry.
resource - The resource to add.
defs - Default attribute values.

saveResource

public void saveResource(SpaceEntry sentry,
                         Resource resource)
Save this resource to the StoreEntry of the space entry.
Specified by:
saveResource in interface ResourceSpace
Parameters:
sentry - The resource space entry
resource - The resource to save.

markModified

public void markModified(SpaceEntry sentry,
                         Resource resource)
Mark the given resource as being modified.
Specified by:
markModified in interface ResourceSpace
Parameters:
sentry - The resource space entry.
resource - The resource to mark as modified.

renameResource

public void renameResource(SpaceEntry sentry,
                           java.lang.String oldid,
                           java.lang.String newid)
Rename a resource in this resource space.
Specified by:
renameResource in interface ResourceSpace
Parameters:
sentry - The resource space entry.
oldid - The old resorce identifier.
newid - The new resorce identifier.

deleteResource

public void deleteResource(SpaceEntry sentry,
                           Resource resource)
delete this resource from the StoreEntry (and the repository).
Specified by:
deleteResource in interface ResourceSpace
Parameters:
sentry - The resource space entry
resource - The resource to delete.

deleteChildren

public void deleteChildren(SpaceEntry sentry)
Delete all the children of resource indentified by its space entry.
Specified by:
deleteChildren in interface ResourceSpace
Parameters:
sentry - The resource space entry

saveChildren

public void saveChildren(SpaceEntry sentry)
Save all the children of the resource indentified by its spaec entry.
Specified by:
saveChildren in interface ResourceSpace
Parameters:
sentry - The resource space entry

checkMaxLoadedStore

protected void checkMaxLoadedStore()

acquireChildren

public void acquireChildren(SpaceEntry sentry,
                            java.io.File repository,
                            boolean transientFlag)
acquire children from an external file.
Specified by:
acquireChildren in interface ResourceSpace
Parameters:
sentry - The resource space entry.
repository - The file used to store children.

acquireChildren

public void acquireChildren(SpaceEntry sentry)
Acquire the StoreEntry of the space entry.
Specified by:
acquireChildren in interface ResourceSpace
Parameters:
sentry - The resource space entry.

enumerateResourceIdentifiers

public java.util.Enumeration enumerateResourceIdentifiers(SpaceEntry sentry)
Enumerate the name (ie identifiers) of the space entry children.
Specified by:
enumerateResourceIdentifiers in interface ResourceSpace
Parameters:
sentry - The space entry.
all - Should all resources be listed.
Returns:
An enumeration, providing one element per child, which is the name of the child, as a String.