org.sakaiproject.util.storage
Class BaseXmlFileStorage

java.lang.Object
  extended byorg.sakaiproject.util.storage.BaseXmlFileStorage

public class BaseXmlFileStorage
extends Object

BaseXmlFileStorage is a class that stores Resources (of some type) in an XML file backed memory store, provides locked access, and generally implements a service's "storage" class. The service's storage class can extend this to provide covers to turn Resource and Edit into something more type specific to the service.

Version:
$Revision: 2371 $
Author:
University of Michigan, CHEF Software Development Team
See Also:
org.chefproject.service.component.BaseUserService

Nested Class Summary
protected  class BaseXmlFileStorage.Container
          Holds the container object, a table of the resources contained.
 
Field Summary
protected  boolean m_caseInsensitive
          If set, we treat reasource ids as case insensitive.
protected  String m_containerTagName
          The xml tag name for the element holding each container entry.
protected  String m_entryTagName
          The xml tag name for the element holding each actual entry.
protected  String m_fileStoragePath
          A full path and file name to the storage file.
protected  Hashtable m_locks
          Store all locks (across all containers), keyed by entry Resource reference.
protected  String m_rootTagName
          The xml tag name for the root element holding the multiple entries.
protected  Hashtable m_store
          Two level store: Hashtables keyed by container ref of Container.
protected  StorageUser m_user
          The StorageUser to callback for new Resource and Edit objects.
 
Constructor Summary
BaseXmlFileStorage(String path, String root, String container, String entry, StorageUser user)
          Construct.
 
Method Summary
 void cancelContainer(Edit edit)
          Cancel the changes and release the locked container.
 void cancelResource(String container, Edit edit)
          Cancel the changes and release the lock.
protected  String caseId(String id)
          Fix the case of resource ids to support case insensitive ids if enabled
 boolean checkContainer(String ref)
          Check if a container by this id exists.
 boolean checkResource(String container, String id)
          Check if a resource by this id exists.
 void close()
          Close.
 void commitContainer(Edit edit)
          Commit the changes and release the locked container.
 void commitResource(String container, Edit edit)
          Commit the changes and release the lock.
 int countAllResources(String container)
          Count all entries.
protected  Document createDocument()
          Create and return the XML Document for our storaghe
 Edit editContainer(String ref)
          Return a lock on the container with this id, or null if a lock cannot be made.
 Edit editResource(String container, String id)
          Return a lock on the entry with this id, or null if a lock cannot be made.
protected  void finalize()
          Clean up.
protected  void flush()
          flush
 List getAllContainers()
          Get a list of all containers.
 List getAllResources(String container)
          Get all entries.
 List getAllResources(String container, int first, int last)
          Get all entries within a range sorted by id.
 Entity getContainer(String ref)
          Get the container with this id, or null if not found.
 List getContainerIdsMatching(String context)
          Access a list of container ids that match (start with) the root.
 Entity getResource(String container, String id)
          Get the entry with this id, or null if not found.
 List getResources(String container, Time afterDate, int limitedToLatest, String draftsForId, boolean pubViewOnly)
          Get resources filtered by date and count and drafts, in descending (latest first) order
 boolean isEmpty(String container)
          Determine if empty
protected  Document load()
          Load the Xml Document
 void open()
          Open and be ready to read / write.
 Edit putContainer(String ref)
          Add a new container with this id.
 Edit putResource(String container, String id, Object[] others)
          Add a new entry with this id.
 void removeContainer(Edit edit)
          Remove this container and all it contains.
 void removeResource(String container, Edit edit)
          Remove this entry.
protected  void setCaseInsensitivity(boolean setting)
          Enable / disable case insensitive ids.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_fileStoragePath

protected String m_fileStoragePath
A full path and file name to the storage file.


m_rootTagName

protected String m_rootTagName
The xml tag name for the root element holding the multiple entries.


m_containerTagName

protected String m_containerTagName
The xml tag name for the element holding each container entry.


m_entryTagName

protected String m_entryTagName
The xml tag name for the element holding each actual entry.


m_store

protected Hashtable m_store
Two level store: Hashtables keyed by container ref of Container.


m_locks

protected Hashtable m_locks
Store all locks (across all containers), keyed by entry Resource reference.


m_user

protected StorageUser m_user
The StorageUser to callback for new Resource and Edit objects.


m_caseInsensitive

protected boolean m_caseInsensitive
If set, we treat reasource ids as case insensitive.

Constructor Detail

BaseXmlFileStorage

public BaseXmlFileStorage(String path,
                          String root,
                          String container,
                          String entry,
                          StorageUser user)
Construct.

Parameters:
path - The storage path.
root - The xml tag name for the root element holding the multiple entries.
container - The xml tag name for the element holding each container entry (may be null if there's no container structure and all entries are in the root).
entry - The xml tag name for the element holding each actual entry.
user - The StorageUser class to call back for creation of Resource and Edit objects.
Method Detail

finalize

protected void finalize()
Clean up.


load

protected Document load()
Load the Xml Document


open

public void open()
Open and be ready to read / write.


createDocument

protected Document createDocument()
Create and return the XML Document for our storaghe


flush

protected void flush()
flush


close

public void close()
Close.


checkContainer

public boolean checkContainer(String ref)
Check if a container by this id exists.

Parameters:
ref - The container reference.
Returns:
true if a resource by this id exists, false if not.

getContainer

public Entity getContainer(String ref)
Get the container with this id, or null if not found.

Parameters:
ref - The container reference.
Returns:
The container with this id, or null if not found.

getAllContainers

public List getAllContainers()
Get a list of all containers.

Returns:
A list (Resource) of all containers, or empty if none defined.

putContainer

public Edit putContainer(String ref)
Add a new container with this id.

Parameters:
ref - The channel reference.
Returns:
The locked object with this id, or null if the id is in use.

editContainer

public Edit editContainer(String ref)
Return a lock on the container with this id, or null if a lock cannot be made.

Parameters:
ref - The container reference.
Returns:
The locked object with this id, or null if a lock cannot be made.

commitContainer

public void commitContainer(Edit edit)
Commit the changes and release the locked container.

Parameters:
edit - The entry to commit.

cancelContainer

public void cancelContainer(Edit edit)
Cancel the changes and release the locked container.

Parameters:
edit - The entry to cancel.

removeContainer

public void removeContainer(Edit edit)
Remove this container and all it contains.

Parameters:
edit - The entry to remove.

checkResource

public boolean checkResource(String container,
                             String id)
Check if a resource by this id exists.

Parameters:
container - The container id.
id - The id.
Returns:
true if a resource by this id exists, false if not.

getResource

public Entity getResource(String container,
                          String id)
Get the entry with this id, or null if not found.

Parameters:
container - The container id.
id - The id.
Returns:
The entry with this id, or null if not found.

getAllResources

public List getAllResources(String container)
Get all entries.

Parameters:
container - The container id.
Returns:
The list (Resource) of all entries.

isEmpty

public boolean isEmpty(String container)
Determine if empty

Returns:
true if empty, false if not.

getAllResources

public List getAllResources(String container,
                            int first,
                            int last)
Get all entries within a range sorted by id.

Parameters:
container - The container id.
first - The first position.
last - The last position.
Returns:
The list (Resource) of all entries within a range sorted by id.

countAllResources

public int countAllResources(String container)
Count all entries.

Parameters:
container - The container id.
Returns:
The count of all entries.

putResource

public Edit putResource(String container,
                        String id,
                        Object[] others)
Add a new entry with this id.

Parameters:
container - The container id.
id - The id.
others - Other fields for the newResource call
Returns:
The locked object with this id, or null if the id is in use.

editResource

public Edit editResource(String container,
                         String id)
Return a lock on the entry with this id, or null if a lock cannot be made.

Parameters:
container - The container id.
id - The id.
Returns:
The locked object with this id, or null if a lock cannot be made.

commitResource

public void commitResource(String container,
                           Edit edit)
Commit the changes and release the lock.

Parameters:
container - The container id.
edit - The entry to commit.

cancelResource

public void cancelResource(String container,
                           Edit edit)
Cancel the changes and release the lock.

Parameters:
container - The container id.
edit - The entry to cancel.

removeResource

public void removeResource(String container,
                           Edit edit)
Remove this entry.

Parameters:
container - The container id.
edit - The entry to remove.

caseId

protected String caseId(String id)
Fix the case of resource ids to support case insensitive ids if enabled

Returns:
The id, case modified as needed.

setCaseInsensitivity

protected void setCaseInsensitivity(boolean setting)
Enable / disable case insensitive ids.

Parameters:
setting - true to set case insensitivity, false to set case sensitivity.

getResources

public List getResources(String container,
                         Time afterDate,
                         int limitedToLatest,
                         String draftsForId,
                         boolean pubViewOnly)
Get resources filtered by date and count and drafts, in descending (latest first) order

Parameters:
container - The container id.
afterDate - if null, no date limit, else limited to only messages after this date.
limitedToLatest - if 0, no count limit, else limited to only the latest this number of messages.
draftsForId - how to handle drafts: null means no drafts, "*" means all, otherwise drafts only if created by this userId.
pubViewOnly - if true, include only messages marked pubview, else include any.
Returns:
A list of Message objects that meet the criteria; may be empty

getContainerIdsMatching

public List getContainerIdsMatching(String context)
Access a list of container ids that match (start with) the root.

Parameters:
context - The reference root to match.
Returns:
A List (String) of container id which match the root.