[jboss-cvs] JBossCache/src/org/jboss/cache ...

Manik Surtani msurtani at jboss.com
Tue Oct 31 12:06:44 EST 2006


  User: msurtani
  Date: 06/10/31 12:06:44

  Modified:    src/org/jboss/cache  Tag: Branch_JBossCache_1_4_0 Node.java
  Log:
  Fixed javadoc
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.38.2.1  +267 -178  JBossCache/src/org/jboss/cache/Node.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Node.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/Node.java,v
  retrieving revision 1.38
  retrieving revision 1.38.2.1
  diff -u -b -r1.38 -r1.38.2.1
  --- Node.java	14 Jun 2006 18:58:18 -0000	1.38
  +++ Node.java	31 Oct 2006 17:06:44 -0000	1.38.2.1
  @@ -7,18 +7,7 @@
   package org.jboss.cache;
   
   
  -import java.io.Externalizable;
  -import java.io.IOException;
  -import java.io.ObjectInput;
  -import java.io.ObjectOutput;
  -import java.util.Collection;
  -import java.util.HashMap;
  -import java.util.HashSet;
  -import java.util.Iterator;
  -import java.util.Map;
  -import java.util.Set;
  -import java.util.Collections;
  -
  +import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   import org.jboss.cache.factories.NodeFactory;
  @@ -26,12 +15,20 @@
   import org.jboss.cache.lock.LockingException;
   import org.jboss.cache.lock.TimeoutException;
   import org.jboss.cache.lock.UpgradeException;
  -import org.jboss.cache.lock.IsolationLevel;
   import org.jboss.cache.marshall.MethodCallFactory;
   import org.jboss.cache.marshall.MethodDeclarations;
   import org.jgroups.blocks.MethodCall;
   
  -import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
  +import java.io.Externalizable;
  +import java.io.IOException;
  +import java.io.ObjectInput;
  +import java.io.ObjectOutput;
  +import java.util.Collection;
  +import java.util.HashMap;
  +import java.util.HashSet;
  +import java.util.Iterator;
  +import java.util.Map;
  +import java.util.Set;
   
   /**
    * Basic data node class.
  @@ -39,7 +36,9 @@
   public class Node extends AbstractNode implements Externalizable
   {
   
  -   /** The serialVersionUID */
  +   /**
  +    * The serialVersionUID
  +    */
      private static final long serialVersionUID = -5040432493172658796L;
      
      private static Log log = LogFactory.getLog(Node.class);
  @@ -53,7 +52,7 @@
       * True if all children have been loaded.
       * This is set when TreeCache.getChildrenNames() is called.
       */
  -   private boolean children_loaded=false;
  +   private boolean children_loaded = false;
   
      /** 
       * Lock manager that manages locks to be acquired when accessing the node
  @@ -69,30 +68,36 @@
      /**
       * Construct an empty node; used by serialization.
       */
  -   public Node() {
  +   public Node()
  +   {
      }
   
      /**
       * Constructs a new node with a name, etc.
       */
  -   public Node(Object child_name, Fqn fqn, Node parent, Map data, TreeCache cache) {
  +   public Node(Object child_name, Fqn fqn, Node parent, Map data, TreeCache cache)
  +   {
         init(child_name, fqn, cache);
  -      if (data != null) {
  +      if (data != null)
  +      {
            this.data().putAll(data);
         }
      }
   
      /**
       * Constructs a new node with a name, etc.
  +    *
       * @param mapSafe <code>true</code> if param <code>data</code> can safely be 
       *                directly assigned to this object's {@link #data} field;
       *                <code>false</code> if param <code>data</code>'s contents
       *                should be copied into this object's {@link #data} field.
       */
      public Node(Object child_name, Fqn fqn, Node parent, Map data, boolean mapSafe,
  -                   TreeCache cache) {
  +               TreeCache cache)
  +   {
         init(child_name, fqn, cache);
  -      if (data != null) {
  +      if (data != null)
  +      {
            if (mapSafe)
               this.data = data;
            else
  @@ -103,7 +108,8 @@
      /**
       * Constructs a new node with a single key and value.
       */
  -   public Node(Object child_name, Fqn fqn, Node parent, Object key, Object value, TreeCache cache) {
  +   public Node(Object child_name, Fqn fqn, Node parent, Object key, Object value, TreeCache cache)
  +   {
         init(child_name, fqn, cache);
         data().put(key, value);
      }
  @@ -111,11 +117,12 @@
      /**
       * Initializes with a name and FQN and cache.
       */
  -   protected final void init(Object child_name, Fqn fqn, TreeCache cache) {
  +   protected final void init(Object child_name, Fqn fqn, TreeCache cache)
  +   {
         if (cache == null)
            throw new IllegalArgumentException("no cache init for " + fqn);
         this.cache = cache;
  -      this.fqn=fqn;
  +      this.fqn = fqn;
         if (!fqn.isRoot() && !child_name.equals(fqn.getLast()))
            throw new IllegalArgumentException("Child " + child_name + " must be last part of " + fqn);
      }
  @@ -130,23 +137,30 @@
         return cache.peek(fqn.getParent());
      }
   
  -   private synchronized void initLock() {
  +   private synchronized void initLock()
  +   {
         if (lock_ == null)
            lock_ = new IdentityLock(cache, fqn);
      }
   
  -   protected synchronized Map children() {
  -      if (children == null) {
  -          if (getFqn().isRoot()) {
  +   protected synchronized Map children()
  +   {
  +      if (children == null)
  +      {
  +         if (getFqn().isRoot())
  +         {
                 children = new ConcurrentReaderHashMap(64);                      
  -          } else {
  +         }
  +         else
  +         {
                 children = new ConcurrentReaderHashMap(4);
             }
         }
         return children;
      }
   
  -   private void setTreeCacheInstance(TreeCache cache) {
  +   private void setTreeCacheInstance(TreeCache cache)
  +   {
         this.cache = cache;
         this.lock_ = null;
      }
  @@ -155,34 +169,43 @@
       * Set the tree cache instance recursively down to the children as well.
       * Note that this method is not currently thread safe.
       */
  -   public void setRecursiveTreeCacheInstance(TreeCache cache) {
  +   public void setRecursiveTreeCacheInstance(TreeCache cache)
  +   {
         
         setTreeCacheInstance(cache);
   
  -      if(children != null) {
  -         for(Iterator it = children.keySet().iterator(); it.hasNext(); ) {
  -            DataNode nd = (DataNode)children.get(it.next());
  +      if (children != null)
  +      {
  +         for (Iterator it = children.keySet().iterator(); it.hasNext();)
  +         {
  +            DataNode nd = (DataNode) children.get(it.next());
               nd.setRecursiveTreeCacheInstance(cache);
            }
         }
      }
   
  -   public boolean getChildrenLoaded() {
  +   public boolean getChildrenLoaded()
  +   {
         return children_loaded;
      }
   
  -   public void setChildrenLoaded(boolean flag) {
  -      children_loaded=flag;
  +   public void setChildrenLoaded(boolean flag)
  +   {
  +      children_loaded = flag;
      }
   
  -   public Object get(Object key) {
  -      synchronized(this) {
  -         return data == null? null : data.get(key);
  +   public Object get(Object key)
  +   {
  +      synchronized (this)
  +      {
  +         return data == null ? null : data.get(key);
         }
      }
   
  -   public boolean containsKey(Object key) {
  -      synchronized(this) {
  +   public boolean containsKey(Object key)
  +   {
  +      synchronized (this)
  +      {
            return data != null && data.containsKey(key);
         }
      }
  @@ -190,8 +213,10 @@
       /**
        * Returns the data keys, or an empty set if there are no keys.
        */
  -   public Set getDataKeys() {
  -      synchronized(this) {
  +   public Set getDataKeys()
  +   {
  +      synchronized (this)
  +      {
            if (data == null)
               //return Collections.emptySet(); // this is JDK5 only!!  Sucks!
               return new HashSet(0);
  @@ -199,84 +224,103 @@
         }
      }
   
  -   boolean isReadLocked() {
  +   boolean isReadLocked()
  +   {
         return lock_ != null && lock_.isReadLocked();
      }
   
  -   boolean isWriteLocked() {
  +   boolean isWriteLocked()
  +   {
         return lock_ != null && lock_.isWriteLocked();
      }
   
  -   public boolean isLocked() {
  +   public boolean isLocked()
  +   {
         return isWriteLocked() || isReadLocked();
      }
   
      /**
       * @deprecated Use getLock() instead
       */
  -   public IdentityLock getImmutableLock() {
  +   public IdentityLock getImmutableLock()
  +   {
         initLock();
         return lock_;
      }
   
  -   public IdentityLock getLock() {
  +   public IdentityLock getLock()
  +   {
         initLock();
         return lock_;
      }
   
  -   public Map getData() {
  -      synchronized(this) {
  -         if(data == null)
  +   public Map getData()
  +   {
  +      synchronized (this)
  +      {
  +         if (data == null)
               return null;
            return new HashMap(data);
         }
      }
   
  -   public int numAttributes() {
  -      synchronized(this) {
  -         return data != null? data.size() : 0;
  +   public int numAttributes()
  +   {
  +      synchronized (this)
  +      {
  +         return data != null ? data.size() : 0;
         }
      }
   
  -   public void put(Map data, boolean erase) {
  -      synchronized(this) {
  -         if(erase) {
  -            if(this.data != null)
  +   public void put(Map data, boolean erase)
  +   {
  +      synchronized (this)
  +      {
  +         if (erase)
  +         {
  +            if (this.data != null)
                  this.data.clear();
            }
  -         if(data == null) return;
  +         if (data == null) return;
            this.data().putAll(data);
         }
      }
   
  -   public Object put(Object key, Object value) {
  -      synchronized(this) {
  +   public Object put(Object key, Object value)
  +   {
  +      synchronized (this)
  +      {
            return this.data().put(key, value);
         }
      }
   
  -   public TreeNode getOrCreateChild(Object child_name, GlobalTransaction gtx, boolean createIfNotExists) {
  +   public TreeNode getOrCreateChild(Object child_name, GlobalTransaction gtx, boolean createIfNotExists)
  +   {
         DataNode child;
         if (child_name == null)
            throw new IllegalArgumentException("null child name");
   
  -      child = (DataNode)children().get(child_name);
  -      if (createIfNotExists && child == null) {
  +      child = (DataNode) children().get(child_name);
  +      if (createIfNotExists && child == null)
  +      {
            // construct the new child outside the synchronized block to avoid
            // spending any more time than necessary in the synchronized section
            Fqn child_fqn = new Fqn(this.fqn, child_name);
  -         DataNode newChild = (DataNode)NodeFactory.getInstance().createNodeOfType(this, child_name, child_fqn, this, null, cache); 
  +         DataNode newChild = (DataNode) NodeFactory.getInstance().createNodeOfType(this, child_name, child_fqn, this, null, cache);
            if (newChild == null)
               throw new IllegalStateException();
  -         synchronized(this) {
  +         synchronized (this)
  +         {
                // check again to see if the child exists
                // after acquiring exclusive lock
  -             child = (Node)children().get(child_name);
  -             if (child == null) {
  +            child = (Node) children().get(child_name);
  +            if (child == null)
  +            {
                    child = newChild;
                    children.put(child_name, child);
  -                 if(gtx != null) {
  -                     MethodCall undo_op=MethodCallFactory.create(MethodDeclarations.removeNodeMethodLocal,
  +               if (gtx != null)
  +               {
  +                  MethodCall undo_op = MethodCallFactory.create(MethodDeclarations.removeNodeMethodLocal,
                                                          new Object[]{gtx, child_fqn, Boolean.FALSE});
                        cache.addUndoOperation(gtx, undo_op);
                        // add the node name to the list maintained for the current tx
  @@ -287,8 +331,10 @@
            }
            
            // notify if we actually created a new child
  -         if (newChild == child) {
  -             if (trace) {
  +         if (newChild == child)
  +         {
  +            if (trace)
  +            {
                    log.trace("created child: fqn=" + child_fqn);
                }
                cache.notifyNodeCreated(child.getFqn());
  @@ -298,55 +344,68 @@
         
      }
   
  -   public TreeNode createChild(Object child_name, Fqn fqn, TreeNode parent) {
  +   public TreeNode createChild(Object child_name, Fqn fqn, TreeNode parent)
  +   {
         return getOrCreateChild(child_name, null, true);
      }
   
  -   public TreeNode createChild(Object child_name, Fqn fqn, TreeNode parent, Object key, Object value) {
  +   public TreeNode createChild(Object child_name, Fqn fqn, TreeNode parent, Object key, Object value)
  +   {
         TreeNode n = getOrCreateChild(child_name, null, true);
         n.put(key, value);
         return n;
      }
   
  -   public Object remove(Object key) {
  -      synchronized(this) {
  -         return data != null?  data.remove(key) : null;
  +   public Object remove(Object key)
  +   {
  +      synchronized (this)
  +      {
  +         return data != null ? data.remove(key) : null;
         }
      }
   
  -   public void clear() {
  -      synchronized(this) {
  -         if(data != null) {
  +   public void clear()
  +   {
  +      synchronized (this)
  +      {
  +         if (data != null)
  +         {
               data.clear();
  -            data=null;
  +            data = null;
            }
         }
      }
   
  -   public void printDetails(StringBuffer sb, int indent) {
  +   public void printDetails(StringBuffer sb, int indent)
  +   {
         Map tmp;
  -      synchronized(this) {
  -         tmp=data != null? new HashMap(data) : null;
  +      synchronized (this)
  +      {
  +         tmp = data != null ? new HashMap(data) : null;
         }
         printDetailsInMap(sb, indent, tmp);
      }
   
  -   public void printLockInfo(StringBuffer sb, int indent) {
  -      boolean locked=lock_ != null && lock_.isLocked();
  +   public void printLockInfo(StringBuffer sb, int indent)
  +   {
  +      boolean locked = lock_ != null && lock_.isLocked();
   
         printIndent(sb, indent);
         sb.append(Fqn.SEPARATOR).append(getName());
  -      if(locked) {
  +      if (locked)
  +      {
            sb.append("\t(");
            lock_.toString(sb);
            sb.append(")");
         }
   
  -      if(children != null && children.size() > 0) {
  -         Collection values=children.values();
  -         for(Iterator it=values.iterator(); it.hasNext();) {
  +      if (children != null && children.size() > 0)
  +      {
  +         Collection values = children.values();
  +         for (Iterator it = values.iterator(); it.hasNext();)
  +         {
               sb.append("\n");
  -            ((DataNode)it.next()).printLockInfo(sb, indent + INDENT);
  +            ((DataNode) it.next()).printLockInfo(sb, indent + INDENT);
            }
         }
      }
  @@ -354,134 +413,160 @@
      /**
       * Returns a debug string.
       */
  -   public String toString() {
  -      StringBuffer sb=new StringBuffer();
  +   public String toString()
  +   {
  +      StringBuffer sb = new StringBuffer();
         sb.append("\nfqn=" + fqn);
  -      synchronized (this) {
  -         if(data != null)
  +      synchronized (this)
  +      {
  +         if (data != null)
               sb.append("\ndata=" + data);
         }
  -      if (lock_ != null) {
  +      if (lock_ != null)
  +      {
            sb.append("\n read locked=").append(isReadLocked());
            sb.append("\n write locked=").append(isWriteLocked());
         }
         return sb.toString();
      }
   
  -   public Object clone() throws CloneNotSupportedException {
  -      DataNode parent = (DataNode)getParent();
  -      DataNode n = (DataNode)NodeFactory.getInstance().createNodeOfType(parent, getName(), fqn, parent != null ? (DataNode) parent.clone() : null, data, cache);
  -      n.setChildren(children == null? null : (HashMap)((HashMap)children).clone());
  +   public Object clone() throws CloneNotSupportedException
  +   {
  +      DataNode parent = (DataNode) getParent();
  +      DataNode n = (DataNode) NodeFactory.getInstance().createNodeOfType(parent, getName(), fqn, parent != null ? (DataNode) parent.clone() : null, data, cache);
  +      n.setChildren(children == null ? null : (HashMap) ((HashMap) children).clone());
         return n;
      }
   
  -   public boolean acquire(Object caller, long timeout, int lock_type) throws LockingException, TimeoutException, InterruptedException {
  +   public boolean acquire(Object caller, long timeout, int lock_type) throws LockingException, TimeoutException, InterruptedException
  +   {
         // Note that we rely on IdentityLock for synchronization
  -      try {
  -         if(lock_type == LOCK_TYPE_NONE)
  +      try
  +      {
  +         if (lock_type == LOCK_TYPE_NONE)
               return true;
  -         else if(lock_type == LOCK_TYPE_READ)
  +         else if (lock_type == LOCK_TYPE_READ)
               return acquireReadLock(caller, timeout);
            else
               return acquireWriteLock(caller, timeout);
         }
  -      catch(UpgradeException e) {
  +      catch (UpgradeException e)
  +      {
            StringBuffer buf = new StringBuffer("failure upgrading lock: fqn=").append(fqn).append(", caller=").append(caller).
                     append(", lock=").append(lock_.toString(true));
  -         if(trace)
  +         if (trace)
               log.trace(buf.toString());
            throw new UpgradeException(buf.toString(), e);
         }
  -      catch(LockingException e) {
  +      catch (LockingException e)
  +      {
            StringBuffer buf = new StringBuffer("failure acquiring lock: fqn=").append(fqn).append(", caller=").append(caller).
                     append(", lock=").append(lock_.toString(true));
  -         if(trace)
  +         if (trace)
               log.trace(buf.toString());
            throw new LockingException(buf.toString(), e);
         }
  -      catch(TimeoutException e) {
  +      catch (TimeoutException e)
  +      {
            StringBuffer buf = new StringBuffer("failure acquiring lock: fqn=").append(fqn).append(", caller=").append(caller).
                     append(", lock=").append(lock_.toString(true));
  -         if(trace)
  +         if (trace)
               log.trace(buf.toString());
            throw new TimeoutException(buf.toString(), e);
         }
      }
   
  -   protected boolean acquireReadLock(Object caller, long timeout) throws LockingException, TimeoutException, InterruptedException {
  +   protected boolean acquireReadLock(Object caller, long timeout) throws LockingException, TimeoutException, InterruptedException
  +   {
         initLock();
  -      if(trace) {
  +      if (trace)
  +      {
            log.trace(new StringBuffer("acquiring RL: fqn=").append(fqn).append(", caller=").append(caller).
                  append(", lock=").append(lock_.toString(DataNode.PRINT_LOCK_DETAILS)));
         }
  -      boolean flag=lock_.acquireReadLock(caller, timeout);
  -      if(trace) {
  +      boolean flag = lock_.acquireReadLock(caller, timeout);
  +      if (trace)
  +      {
            log.trace(new StringBuffer("acquired RL: fqn=").append(fqn).append(", caller=").append(caller).
                  append(", lock=").append(lock_.toString(DataNode.PRINT_LOCK_DETAILS)));
         }
         return flag;
      }
   
  -   protected boolean acquireWriteLock(Object caller, long timeout) throws LockingException, TimeoutException, InterruptedException {
  +   protected boolean acquireWriteLock(Object caller, long timeout) throws LockingException, TimeoutException, InterruptedException
  +   {
         initLock();
  -      if(trace) {
  +      if (trace)
  +      {
            log.trace(new StringBuffer("acquiring WL: fqn=").append(fqn).append(", caller=").append(caller).
                  append(", lock=").append(lock_.toString(DataNode.PRINT_LOCK_DETAILS)));
         }
  -      boolean flag=lock_.acquireWriteLock(caller, timeout);
  -      if(trace) {
  +      boolean flag = lock_.acquireWriteLock(caller, timeout);
  +      if (trace)
  +      {
            log.trace(new StringBuffer("acquired WL: fqn=").append(fqn).append(", caller=").append(caller).
                  append(", lock=").append(lock_.toString(DataNode.PRINT_LOCK_DETAILS)));
         }
         return flag;
      }
   
  -   public Set acquireAll(Object caller, long timeout, int lock_type) throws LockingException, TimeoutException, InterruptedException {
  +   public Set acquireAll(Object caller, long timeout, int lock_type) throws LockingException, TimeoutException, InterruptedException
  +   {
         DataNode tmp;
         boolean acquired;
  -      Set retval=new HashSet();
  +      Set retval = new HashSet();
   
  -      if(lock_type == LOCK_TYPE_NONE)
  +      if (lock_type == LOCK_TYPE_NONE)
            return retval;
  -      acquired=acquire(caller, timeout, lock_type);
  -      if(acquired)
  +      acquired = acquire(caller, timeout, lock_type);
  +      if (acquired)
            retval.add(getLock());
   
  -      if(children != null) {
  -         for(Iterator it=children.values().iterator(); it.hasNext();) {
  -            tmp=(DataNode)it.next();
  +      if (children != null)
  +      {
  +         for (Iterator it = children.values().iterator(); it.hasNext();)
  +         {
  +            tmp = (DataNode) it.next();
               retval.addAll(tmp.acquireAll(caller, timeout, lock_type));
            }
         }
         return retval;
      }
   
  -   public void release(Object caller) {
  +   public void release(Object caller)
  +   {
         if (lock_ != null)
            lock_.release(caller);
      }
   
  -   public void releaseForce() {
  +   public void releaseForce()
  +   {
         if (lock_ != null)
            lock_.releaseForce();
      }
   
  -   public void releaseAll(Object owner) {
  +   public void releaseAll(Object owner)
  +   {
         DataNode tmp;
  -      if(children != null) {
  -         for(Iterator it=children.values().iterator(); it.hasNext();) {
  -            tmp=(DataNode)it.next();
  +      if (children != null)
  +      {
  +         for (Iterator it = children.values().iterator(); it.hasNext();)
  +         {
  +            tmp = (DataNode) it.next();
               tmp.releaseAll(owner);
            }
         }
         release(owner);
      }
   
  -   public void releaseAllForce() {
  +   public void releaseAllForce()
  +   {
         DataNode tmp;
  -      if(children != null) {
  -         for(Iterator it=children.values().iterator(); it.hasNext();) {
  -            tmp=(DataNode)it.next();
  +      if (children != null)
  +      {
  +         for (Iterator it = children.values().iterator(); it.hasNext();)
  +         {
  +            tmp = (DataNode) it.next();
               tmp.releaseAllForce();
            }
         }
  @@ -492,9 +577,11 @@
       * Serializes this object to ObjectOutput.
       * Writes the {@link Fqn} elements, children as a Map, and data as a Map.
       * (This is no longer used within JBoss Cache.)
  -    * @see TreeCacheMarshaller
  +    *
  +    * @see org.jboss.cache.marshall.TreeCacheMarshaller
       */
  -   public void writeExternal(ObjectOutput out) throws IOException {
  +   public void writeExternal(ObjectOutput out) throws IOException
  +   {
         // DO NOT CHANGE THE WIRE FORMAT OF THIS CLASS!!
         // The whole purpose of implementing writeExternal() is to
         // exchange data with 1.2.3 versions of JBC.  Either the wire
  @@ -503,7 +590,8 @@
         out.writeObject(fqn);
         out.writeObject(getParent());
         out.writeObject(children);
  -      synchronized(this) {
  +      synchronized (this)
  +      {
            out.writeObject(data);
         }
      }
  @@ -512,12 +600,13 @@
       * Deserializes this object from ObjectInput.
       * (This is no longer used within JBoss Cache.)
       */
  -   public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  +   public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
  +   {
         in.readObject(); // "name", which we discard
         fqn = (Fqn) in.readObject();
         in.readObject(); // "parent", which we discard
  -      children = (Map)in.readObject();
  -      data=(Map)in.readObject();
  +      children = (Map) in.readObject();
  +      data = (Map) in.readObject();
         // Note that we don't have a cache at this point, so our LockStrategy
         // is going to be configured based on whatever LockStrategyFactory's
         // static isolationLevel_ field is set to. This gets overridden
  
  
  



More information about the jboss-cvs-commits mailing list