stefan      2004/07/07 09:04:56

  Modified:    proposals/jcrri/src/org/apache/slide/jcr/core/state
                        PersistentItemStateManager.java
                        TransientItemStateManager.java
  Log:
  jcrri
  
  Revision  Changes    Path
  1.4       +81 -12    
jakarta-slide/proposals/jcrri/src/org/apache/slide/jcr/core/state/PersistentItemStateManager.java
  
  Index: PersistentItemStateManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/proposals/jcrri/src/org/apache/slide/jcr/core/state/PersistentItemStateManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PersistentItemStateManager.java   22 Jun 2004 18:02:33 -0000      1.3
  +++ PersistentItemStateManager.java   7 Jul 2004 16:04:56 -0000       1.4
  @@ -25,11 +25,12 @@
   
   import org.apache.log4j.Logger;
   import org.apache.slide.jcr.core.*;
  +import org.apache.slide.jcr.core.nodetype.NodeDefId;
   import org.apache.slide.jcr.core.nodetype.NodeTypeRegistry;
  +import org.apache.slide.jcr.core.nodetype.PropDefId;
   
   import javax.jcr.PropertyType;
  -import javax.jcr.StringValue;
  -import javax.jcr.Value;
  +import javax.jcr.nodetype.NoSuchNodeTypeException;
   import java.io.PrintStream;
   import java.util.Iterator;
   
  @@ -39,7 +40,8 @@
    * @author Stefan Guggisberg
    * @version $Revision$, $Date$
    */
  -public class PersistentItemStateManager extends ItemStateCache implements 
ItemStateListener {
  +public class PersistentItemStateManager extends ItemStateCache
  +     implements ItemStateProvider, ItemStateListener {
   
       private static Logger log = Logger.getLogger(PersistentItemStateManager.class);
   
  @@ -53,8 +55,11 @@
        *
        * @param persistMgr
        * @param rootNodeUUID
  +     * @param ntReg
        */
  -    public PersistentItemStateManager(PersistenceManager persistMgr, String 
rootNodeUUID)
  +    public PersistentItemStateManager(PersistenceManager persistMgr,
  +                                   String rootNodeUUID,
  +                                   NodeTypeRegistry ntReg)
            throws ItemStateException {
        this.persistMgr = persistMgr;
   
  @@ -62,21 +67,39 @@
            root = getNodeState(new NodeId(rootNodeUUID));
        } catch (NoSuchItemStateException e) {
            // create root node
  -         root = createPersistentRootNodeState(rootNodeUUID);
  +         root = createPersistentRootNodeState(rootNodeUUID, ntReg);
        }
       }
   
  -    private PersistentNodeState createPersistentRootNodeState(String rootNodeUUID)
  +    private PersistentNodeState createPersistentRootNodeState(String rootNodeUUID,
  +                                                           NodeTypeRegistry ntReg)
            throws ItemStateException {
        PersistentNodeState rootState = createNodeState(rootNodeUUID, 
NodeTypeRegistry.NT_UNSTRUCTURED, null);
   
        // @todo FIXME need to manually setup root node by creating mandatory 
jcr:primaryType property
  +     NodeDefId nodeDefId = null;
  +     PropDefId propDefId = null;
  +
  +     try {
  +         // FIXME relies on definition of nt:unstructured and nt:base:
  +         // first (and only) child node definition in nt:unstructured is applied to 
root node
  +         nodeDefId = new 
NodeDefId(ntReg.getNodeTypeDef(NodeTypeRegistry.NT_UNSTRUCTURED).getChildNodeDefs()[0]);
  +         // first property definition in nt:base is jcr:primaryType
  +         propDefId = new 
PropDefId(ntReg.getNodeTypeDef(NodeTypeRegistry.NT_BASE).getPropertyDefs()[0]);
  +     } catch (NoSuchNodeTypeException nsnte) {
  +         String msg = "failed to create root node";
  +         log.error(msg, nsnte);
  +         throw new ItemStateException(msg, nsnte);
  +     }
  +     rootState.setDefinitionId(nodeDefId);
  +
        QName propName = new QName(NamespaceRegistryImpl.NS_JCR_URI, "primaryType");
        rootState.addPropertyEntry(propName);
   
        PersistentPropertyState prop = createPropertyState(rootNodeUUID, propName);
        prop.setValues(new 
InternalValue[]{InternalValue.create(NodeTypeRegistry.NT_UNSTRUCTURED)});
        prop.setType(PropertyType.STRING);
  +     prop.setDefinitionId(propDefId);
   
        rootState.store();
        prop.store();
  @@ -131,6 +154,48 @@
        ps.println(id + " (" + state + ")");
       }
   
  +    //----------------------------------------------------< ItemStateProvider >
  +    /**
  +     * @see ItemStateProvider#getItemState(ItemId)
  +     */
  +    public ItemState getItemState(ItemId id)
  +         throws NoSuchItemStateException, ItemStateException {
  +     if (id.denotesNode()) {
  +         return getNodeState((NodeId) id);
  +     } else {
  +         return getPropertyState((PropertyId) id);
  +     }
  +    }
  +
  +    /**
  +     * @see ItemStateProvider#hasItemState(ItemId)
  +     */
  +    public boolean hasItemState(ItemId id) {
  +     try {
  +         getItemState(id);
  +         return true;
  +     } catch (ItemStateException ise) {
  +         return false;
  +     }
  +    }
  +
  +    /**
  +     * @see ItemStateProvider#getItemStateInAttic(ItemId)
  +     */
  +    public ItemState getItemStateInAttic(ItemId id)
  +         throws NoSuchItemStateException, ItemStateException {
  +     // n/a
  +     throw new NoSuchItemStateException(id.toString());
  +    }
  +
  +    /**
  +     * @see ItemStateProvider#hasItemStateInAttic(ItemId)
  +     */
  +    public boolean hasItemStateInAttic(ItemId id) {
  +     // n/a
  +     return false;
  +    }
  +
       //---------------< methods for listing and retrieving ItemState instances >
       /**
        * @param id
  @@ -138,7 +203,7 @@
        * @throws NoSuchItemStateException
        * @throws ItemStateException
        */
  -    public synchronized PersistentNodeState getNodeState(NodeId id)
  +    synchronized PersistentNodeState getNodeState(NodeId id)
            throws NoSuchItemStateException, ItemStateException {
        // check cache
        if (isCached(id)) {
  @@ -160,7 +225,7 @@
        * @throws NoSuchItemStateException
        * @throws ItemStateException
        */
  -    public synchronized PersistentPropertyState getPropertyState(PropertyId id)
  +    synchronized PersistentPropertyState getPropertyState(PropertyId id)
            throws NoSuchItemStateException, ItemStateException {
        // check cache
        if (isCached(id)) {
  @@ -188,7 +253,7 @@
        * @return
        * @throws ItemStateException
        */
  -    public synchronized PersistentNodeState createNodeState(String uuid, QName 
nodeTypeName, String parentUUID)
  +    synchronized PersistentNodeState createNodeState(String uuid, QName 
nodeTypeName, String parentUUID)
            throws ItemStateException {
        NodeId id = new NodeId(uuid);
        // check cache
  @@ -208,12 +273,16 @@
       }
   
       /**
  +     * Creates a <code>PersistentPropertyState</code> instance representing new,
  +     * i.e. not yet existing state. Call <code>[EMAIL PROTECTED] 
PersistentPropertyState#store()}</code>
  +     * on the returned object to make it persistent.
  +     *
        * @param parentUUID
        * @param propName
        * @return
        * @throws ItemStateException
        */
  -    public PersistentPropertyState createPropertyState(String parentUUID, QName 
propName)
  +    PersistentPropertyState createPropertyState(String parentUUID, QName propName)
            throws ItemStateException {
        PropertyId id = new PropertyId(parentUUID, propName);
        // check cache
  
  
  
  1.7       +46 -143   
jakarta-slide/proposals/jcrri/src/org/apache/slide/jcr/core/state/TransientItemStateManager.java
  
  Index: TransientItemStateManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/proposals/jcrri/src/org/apache/slide/jcr/core/state/TransientItemStateManager.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TransientItemStateManager.java    24 Jun 2004 16:31:07 -0000      1.6
  +++ TransientItemStateManager.java    7 Jul 2004 16:04:56 -0000       1.7
  @@ -25,14 +25,13 @@
   
   import org.apache.commons.collections.ReferenceMap;
   import org.apache.log4j.Logger;
  -import org.apache.slide.jcr.core.*;
  +import org.apache.slide.jcr.core.ItemId;
  +import org.apache.slide.jcr.core.NodeId;
  +import org.apache.slide.jcr.core.PropertyId;
  +import org.apache.slide.jcr.core.QName;
   
  -import javax.jcr.RepositoryException;
   import java.io.PrintStream;
  -import java.util.ArrayList;
  -import java.util.Collections;
   import java.util.Iterator;
  -import java.util.List;
   
   /**
    * <code>TransientItemStateManager</code> ...
  @@ -40,103 +39,28 @@
    * @author Stefan Guggisberg
    * @version $Revision$, $Date$
    */
  -public class TransientItemStateManager extends ItemStateCache {
  +class TransientItemStateManager extends ItemStateCache implements ItemStateProvider 
{
   
       private static Logger log = Logger.getLogger(TransientItemStateManager.class);
   
       private final Attic attic;
   
  -    private final HierarchyManager hierMgr;
  -
       /**
        * Creates a new <code>TransientItemStateManager</code> instance.
        */
  -    public TransientItemStateManager(HierarchyManager hierMgr) {
  +    TransientItemStateManager() {
        // we're keeping hard references in the cache
        super(ReferenceMap.HARD, ReferenceMap.HARD);
  -
  -     this.hierMgr = hierMgr;
        attic = new Attic();
       }
   
  -    private void collectDescendantItemStates(ItemId id, List descendents) {
  -     try {
  -         if (id.denotesNode()) {
  -             NodeId parentId = (NodeId) id;
  -             ItemId[] childIds = hierMgr.listChildren(parentId);
  -             for (int i = 0; i < childIds.length; i++) {
  -                 ItemId childId = childIds[i];
  -                 if (isCached(childId)) {
  -                     descendents.add(retrieve(childId));
  -                 }
  -                 // recurse
  -                 collectDescendantItemStates(childId, descendents);
  -             }
  -             // also add transient child nodes that have been unlinked from
  -             // the specified parent node but are not orphaned yet (i.e.
  -             // they are still linked to at least one other parent node)
  -             if (isCached(parentId)) {
  -                 NodeState parentState = (NodeState) retrieve(parentId);
  -                 Iterator iter = 
parentState.getRemovedChildNodeEntries().iterator();
  -                 while (iter.hasNext()) {
  -                     // removed child nodes
  -                     NodeState.ChildNodeEntry cne = (NodeState.ChildNodeEntry) 
iter.next();
  -                     NodeId removedChildId = new NodeId(cne.getUUID());
  -                     if (isCached(removedChildId)) {
  -                         descendents.add(retrieve(removedChildId));
  -                     }
  -                 }
  -             }
  -         }
  -     } catch (RepositoryException re) {
  -         log.warn("inconsistent hierarchy state", re);
  -     }
  -    }
  -
  -    private void collectDescendantItemStatesInAttic(ItemId id, List descendents) {
  -     try {
  -         if (id.denotesNode()) {
  -             NodeId parentId = (NodeId) id;
  -
  -             // traverse zombie children (i.e. children marked as removed)
  -             ItemId[] childIds = hierMgr.listZombieChildren(parentId);
  -             for (int i = 0; i < childIds.length; i++) {
  -                 ItemId childId = childIds[i];
  -                 // check attic
  -                 if (attic.isCached(childId)) {
  -                     // found on attic, add to descendents list
  -                     descendents.add(attic.retrieve(childId));
  -                 }
  -                 // recurse
  -                 collectDescendantItemStatesInAttic(childId, descendents);
  -             }
  -
  -             // traverse existing children (because parentId might denote
  -             // a node that was marked removed)
  -             childIds = hierMgr.listChildren(parentId);
  -             for (int i = 0; i < childIds.length; i++) {
  -                 ItemId childId = childIds[i];
  -                 // check attic
  -                 if (attic.isCached(childId)) {
  -                     // found on attic, add to descendents list
  -                     descendents.add(attic.retrieve(childId));
  -                 }
  -                 // recurse
  -                 collectDescendantItemStatesInAttic(childId, descendents);
  -             }
  -         }
  -     } catch (RepositoryException re) {
  -         log.warn("inconsistent hierarchy state", re);
  -     }
  -    }
  -
       /**
        * Dumps the state of this <code>TransientItemStateManager</code> instance
        * (used for diagnostic purposes).
        *
        * @param ps
        */
  -    public void dump(PrintStream ps) {
  +    void dump(PrintStream ps) {
        ps.println("TransientItemStateManager (" + this + ")");
        ps.println();
        ps.println("entries in cache:");
  @@ -187,13 +111,12 @@
        ps.println(id + " (" + state + ")");
       }
   
  -    //---------------< methods for listing and retrieving ItemState instances >
  +    //----------------------------------------------------< ItemStateProvider >
       /**
  -     * @param id
  -     * @return
  -     * @throws NoSuchItemStateException
  +     * @see ItemStateProvider#getItemState(ItemId)
        */
  -    public ItemState getItemState(ItemId id) throws NoSuchItemStateException {
  +    public ItemState getItemState(ItemId id)
  +         throws NoSuchItemStateException, ItemStateException {
        ItemState state = retrieve(id);
        if (state != null) {
            return state;
  @@ -203,70 +126,50 @@
       }
   
       /**
  -     * @param id
  -     * @return
  +     * @see ItemStateProvider#hasItemState(ItemId)
        */
  -    public boolean itemStateExists(ItemId id) {
  +    public boolean hasItemState(ItemId id) {
        try {
            getItemState(id);
  -     } catch (NoSuchItemStateException e) {
  +         return true;
  +     } catch (ItemStateException ise) {
            return false;
        }
  -     return true;
  -    }
  -
  -    /**
  -     * @return
  -     */
  -    public boolean hasAnyItemStates() {
  -     return !isEmpty();
       }
   
       /**
  -     * Returns an iterator over those instances in the cache that are direct or
  -     * indirect descendents of the instance with the given <code>parentId</code>.
  -     * The instance with the given <code>parentId</code> itself (if there is
  -     * such in the cache) will not be included.
  -     * <p/>
  -     * The instances are returned in depth-first tree traversal order.
  -     *
  -     * @param parentId the id of the common parent of the instances to be returned.
  -     * @return an iterator over descendant instances
  +     * @see ItemStateProvider#getItemStateInAttic(ItemId)
        */
  -    public Iterator getDescendantItemStates(ItemId parentId) {
  -     // @todo need a more efficient way to find descendents in cache (e.g. using 
hierarchical index)
  -     ArrayList descendents = new ArrayList();
  -     collectDescendantItemStates(parentId, descendents);
  -     return Collections.unmodifiableList(descendents).iterator();
  +    public ItemState getItemStateInAttic(ItemId id)
  +         throws NoSuchItemStateException, ItemStateException {
  +     ItemState state = attic.retrieve(id);
  +     if (state != null) {
  +         return state;
  +     } else {
  +         throw new NoSuchItemStateException(id.toString());
  +     }
       }
   
       /**
  -     * Same as <code>[EMAIL PROTECTED] #getDescendantItemStates(ItemId)}</code>
  -     * except that items in the attic are returned.
  -     *
  -     * @param parentId the id of the common parent of the instances to be returned.
  -     * @return an iterator over descendant instances in the attic
  +     * @see ItemStateProvider#hasItemStateInAttic(ItemId)
        */
  -    public Iterator getDescendantItemStatesInAttic(ItemId parentId) {
  -     // @todo need a more efficient way to find descendents in attic (e.g. using 
hierarchical index)
  -     ArrayList descendents = new ArrayList();
  -     collectDescendantItemStatesInAttic(parentId, descendents);
  -     return Collections.unmodifiableList(descendents).iterator();
  +    public boolean hasItemStateInAttic(ItemId id) {
  +     try {
  +         getItemStateInAttic(id);
  +         return true;
  +     } catch (ItemStateException ise) {
  +         return false;
  +     }
       }
   
  +    //----------< more methods for listing and retrieving ItemState instances >
       /**
  -     * @param id
        * @return
  -     * @throws NoSuchItemStateException
        */
  -    public ItemState getItemStateInAttic(ItemId id) throws NoSuchItemStateException 
{
  -     ItemState state = attic.retrieve(id);
  -     if (state != null) {
  -         return state;
  -     } else {
  -         throw new NoSuchItemStateException(id.toString());
  -     }
  +    boolean hasAnyItemStates() {
  +     return !isEmpty();
       }
  +
       //----------------< methods for creating & discarding ItemState instances >
       /**
        * @param uuid
  @@ -276,7 +179,7 @@
        * @return
        * @throws ItemStateException
        */
  -    public NodeState createNodeState(String uuid, QName nodeTypeName, String 
parentUUID, int initialStatus)
  +    NodeState createNodeState(String uuid, QName nodeTypeName, String parentUUID, 
int initialStatus)
            throws ItemStateException {
        NodeId id = new NodeId(uuid);
        // check cache
  @@ -298,7 +201,7 @@
        * @return
        * @throws ItemStateException
        */
  -    public NodeState createNodeState(NodeState overlayedState, int initialStatus)
  +    NodeState createNodeState(NodeState overlayedState, int initialStatus)
            throws ItemStateException {
        ItemId id = overlayedState.getId();
        // check cache
  @@ -321,7 +224,7 @@
        * @return
        * @throws ItemStateException
        */
  -    public PropertyState createPropertyState(String parentUUID, QName propName, int 
initialStatus)
  +    PropertyState createPropertyState(String parentUUID, QName propName, int 
initialStatus)
            throws ItemStateException {
        PropertyId id = new PropertyId(parentUUID, propName);
        // check cache
  @@ -343,7 +246,7 @@
        * @return
        * @throws ItemStateException
        */
  -    public PropertyState createPropertyState(PropertyState overlayedState, int 
initialStatus)
  +    PropertyState createPropertyState(PropertyState overlayedState, int 
initialStatus)
            throws ItemStateException {
        PropertyId id = new PropertyId(overlayedState.getParentUUID(), 
overlayedState.getName());
        // check cache
  @@ -365,7 +268,7 @@
        * @param state the <code>ItemState</code> instance that should be disposed
        * @see ItemState#discard()
        */
  -    public void disposeItemState(ItemState state) {
  +    void disposeItemState(ItemState state) {
        // discard item state, this will invalidate the wrapping Item
        // instance of the transient state
        state.discard();
  @@ -381,7 +284,7 @@
        * @param state the <code>ItemState</code> instance that should be moved to
        *              the attic
        */
  -    public void moveItemStateToAttic(ItemState state) {
  +    void moveItemStateToAttic(ItemState state) {
        // remove from cache
        evict(state.getId());
        // add to attic
  @@ -395,7 +298,7 @@
        * @param state the <code>ItemState</code> instance that should be disposed
        * @see ItemState#discard()
        */
  -    public void disposeItemStateInAttic(ItemState state) {
  +    void disposeItemStateInAttic(ItemState state) {
        // discard item state, this will invalidate the wrapping Item
        // instance of the transient state
        state.discard();
  @@ -408,7 +311,7 @@
       /**
        * Disposes all transient item states in the cache and in the attic.
        */
  -    public void disposeAllItemStates() {
  +    void disposeAllItemStates() {
        // dispose item states in cache
        Iterator iter = entries();
        while (iter.hasNext()) {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to