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

Manik Surtani msurtani at jboss.com
Mon Jan 15 11:19:09 EST 2007


  User: msurtani
  Date: 07/01/15 11:19:09

  Modified:    src/org/jboss/cache/marshall      MethodDeclarations.java
                        CacheMarshaller200.java
  Added:       src/org/jboss/cache/marshall      NodeDataMarker.java
                        NodeDataExceptionMarker.java NodeData.java
  Log:
  * JBCACHE-752
  * Refactored GravitationResult
  * Updated CacheMarshaller200 to marshall GravitateResults and NodeData
  * Moved NodeData (and related objects) to o.j.c.marshall pkg
  
  Revision  Changes    Path
  1.33      +2 -2      JBossCache/src/org/jboss/cache/marshall/MethodDeclarations.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: MethodDeclarations.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/marshall/MethodDeclarations.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -b -r1.32 -r1.33
  --- MethodDeclarations.java	15 Jan 2007 11:22:38 -0000	1.32
  +++ MethodDeclarations.java	15 Jan 2007 16:19:09 -0000	1.33
  @@ -31,7 +31,7 @@
    * allowing lookup operations both ways.
    *
    * @author <a href="galder.zamarreno at jboss.com">Galder Zamarreno</a>
  - * @version $Revision: 1.32 $
  + * @version $Revision: 1.33 $
    */
   public class MethodDeclarations
   {
  @@ -265,7 +265,7 @@
            remoteAssignToBuddyGroupMethod = CacheImpl.class.getDeclaredMethod("_remoteAssignToBuddyGroup", BuddyGroup.class, Map.class);
   
            dataGravitationCleanupMethod = CacheImpl.class.getDeclaredMethod("_dataGravitationCleanup", GlobalTransaction.class, Fqn.class, Fqn.class);
  -         dataGravitationMethod = CacheImpl.class.getDeclaredMethod("_gravitateData", Fqn.class, boolean.class, boolean.class);
  +         dataGravitationMethod = CacheImpl.class.getDeclaredMethod("gravitateData", Fqn.class, boolean.class);
   
            // ------------ move() api
            moveMethodLocal = CacheImpl.class.getDeclaredMethod("_move", Fqn.class, Fqn.class);
  
  
  
  1.5       +140 -27   JBossCache/src/org/jboss/cache/marshall/CacheMarshaller200.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheMarshaller200.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/marshall/CacheMarshaller200.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -r1.4 -r1.5
  --- CacheMarshaller200.java	12 Jan 2007 17:02:02 -0000	1.4
  +++ CacheMarshaller200.java	15 Jan 2007 16:19:09 -0000	1.5
  @@ -12,25 +12,27 @@
   import org.jboss.cache.GlobalTransaction;
   import org.jboss.cache.Region;
   import org.jboss.cache.RegionManager;
  +import org.jboss.cache.buddyreplication.GravitateResult;
   import org.jboss.cache.optimistic.DefaultDataVersion;
   import org.jboss.cache.rpc.RpcTreeCache;
   import org.jgroups.Address;
   import org.jgroups.stack.IpAddress;
   
  +import java.io.Externalizable;
   import java.io.IOException;
   import java.io.ObjectInputStream;
   import java.io.ObjectOutputStream;
   import java.io.Serializable;
   import java.util.ArrayList;
  +import java.util.Collection;
   import java.util.HashMap;
  +import java.util.HashSet;
  +import java.util.LinkedList;
   import java.util.List;
   import java.util.Map;
   import java.util.Set;
  -import java.util.LinkedList;
   import java.util.TreeMap;
  -import java.util.HashSet;
   import java.util.TreeSet;
  -import java.util.Collection;
   
   /**
    * An enhanced marshaller for RPC calls between CacheImpl instances.
  @@ -58,12 +60,19 @@
      protected static final int MAGICNUMBER_TREE_MAP = 13;
      protected static final int MAGICNUMBER_HASH_SET = 14;
      protected static final int MAGICNUMBER_TREE_SET = 15;
  +   protected static final int MAGICNUMBER_NODEDATA_MARKER = 16;
  +   protected static final int MAGICNUMBER_NODEDATA_EXCEPTION_MARKER = 17;
  +   protected static final int MAGICNUMBER_NODEDATA = 18;
  +   protected static final int MAGICNUMBER_GRAVITATERESULT = 19;
      protected static final int MAGICNUMBER_NULL = 99;
      protected static final int MAGICNUMBER_SERIALIZABLE = 100;
      protected static final int MAGICNUMBER_REF = 101;
   
      protected static final InactiveRegionException IRE = new InactiveRegionException("Cannot unmarshall to an inactive region");
   
  +   // this is pretty nasty, and may need more thought.
  +   protected final ThreadLocal<String> regionForCall = new ThreadLocal<String>();
  +
      public CacheMarshaller200(RegionManager manager, boolean defaultInactive, boolean useRegionBasedMarshalling)
      {
         init(manager, defaultInactive, useRegionBasedMarshalling);
  @@ -73,6 +82,17 @@
         }
      }
   
  +   /**
  +    * Tests if the type of object being marshalled is a method call or a return value
  +    *
  +    * @param o object to marshall
  +    * @return true if the object is a return value to a method call; false otherwise
  +    */
  +   protected boolean isReturnValue(Object o)
  +   {
  +      return !(o instanceof MethodCall);
  +   }
  +
      // -------- Marshaller interface
   
      public void objectToStream(Object o, ObjectOutputStream out) throws Exception
  @@ -82,11 +102,35 @@
   
         if (useRegionBasedMarshalling)
         {
  +         String fqnAsString = null;
  +         if (isReturnValue(o))
  +         {
  +            // we are marshalling a return value.
  +            // let's see if an incoming unmarshalling call for this exists,and had registered a Fqn region.
  +            fqnAsString = regionForCall.get();
  +
  +            // if the return value is null we're easy ...
  +            // otherwise, we need to marshall the retval.
  +            if (o != null && fqnAsString != null)
  +            {
  +               // first publish the region
  +               marshallObject(fqnAsString, out, refMap);
  +            }
  +            else
  +            {
  +               // marshall another null as region
  +               marshallObject(null, out, refMap);
  +            }
  +         }
  +         else
  +         {
  +            // this is an outgoing call.
            // we first marshall the Fqn as a String (ugh!)
            MethodCall call = (MethodCall) o;
  -         String fqnAsString = extractFqnAsString(call);
  +            fqnAsString = extractFqnAsString(call);
            marshallObject(fqnAsString, out, refMap);
         }
  +      }
   
         marshallObject(o, out, refMap);
      }
  @@ -107,7 +151,16 @@
            {
               region = findRegion(fqn);
            }
  -         retValue = region == null ? unmarshallObject(in, refMap) : unmarshallObject(in, region.getClassLoader(), refMap);
  +         if (region == null)
  +         {
  +            retValue = unmarshallObject(in, refMap);
  +         }
  +         else
  +         {
  +            retValue = unmarshallObject(in, region.getClassLoader(), refMap);
  +            // only set this if this is an incoming method call and not a return value.
  +            if (!isReturnValue(retValue)) regionForCall.set(fqn);
  +         }
         }
         else
         {
  @@ -271,6 +324,26 @@
            out.writeShort(refId);
            marshallString((String) o, out);
         }
  +      else if (o instanceof NodeDataMarker)
  +      {
  +         out.writeByte(MAGICNUMBER_NODEDATA_MARKER);
  +         ((Externalizable) o).writeExternal(out);
  +      }
  +      else if (o instanceof NodeDataExceptionMarker)
  +      {
  +         out.writeByte(MAGICNUMBER_NODEDATA_EXCEPTION_MARKER);
  +         ((Externalizable) o).writeExternal(out);
  +      }
  +      else if (o instanceof NodeData)
  +      {
  +         out.writeByte(MAGICNUMBER_NODEDATA);
  +         ((Externalizable) o).writeExternal(out);
  +      }
  +      else if (o instanceof GravitateResult)
  +      {
  +         out.writeByte(MAGICNUMBER_GRAVITATERESULT);
  +         marshallGravitateResult((GravitateResult) o, out, refMap);
  +      }
         else if (o instanceof Serializable)
         {
            int refId = createReference(o, refMap);
  @@ -288,6 +361,17 @@
         }
      }
   
  +   private void marshallGravitateResult(GravitateResult gravitateResult, ObjectOutputStream out, Map<Object, Integer> refMap) throws Exception
  +   {
  +      marshallObject(gravitateResult.isDataFound(), out, refMap);
  +      if (gravitateResult.isDataFound())
  +      {
  +         marshallObject(gravitateResult.getNodeData(), out, refMap);
  +         marshallObject(gravitateResult.getBuddyBackupFqn(), out, refMap);
  +      }
  +
  +   }
  +
      private int createReference(Object o, Map<Object, Integer> refMap)
      {
         int reference = refMap.size();
  @@ -450,6 +534,20 @@
               retVal = unmarshallString(in);
               refMap.put(reference, retVal);
               return retVal;
  +         case MAGICNUMBER_NODEDATA_MARKER:
  +            retVal = new NodeDataMarker();
  +            ((NodeDataMarker) retVal).readExternal(in);
  +            return retVal;
  +         case MAGICNUMBER_NODEDATA_EXCEPTION_MARKER:
  +            retVal = new NodeDataExceptionMarker();
  +            ((NodeDataExceptionMarker) retVal).readExternal(in);
  +            return retVal;
  +         case MAGICNUMBER_NODEDATA:
  +            retVal = new NodeData();
  +            ((NodeData) retVal).readExternal(in);
  +            return retVal;
  +         case MAGICNUMBER_GRAVITATERESULT:
  +            return unmarshallGravitateResult(in, refMap);
            default:
               if (log.isErrorEnabled())
               {
  @@ -459,6 +557,21 @@
         }
      }
   
  +   private GravitateResult unmarshallGravitateResult(ObjectInputStream in, Map<Integer, Object> refMap) throws Exception
  +   {
  +      Boolean found = (Boolean) unmarshallObject(in, refMap);
  +      if (found)
  +      {
  +         List<NodeData> stuff = (List<NodeData>) unmarshallObject(in, refMap);
  +         Fqn fqn = (Fqn) unmarshallObject(in, refMap);
  +         return GravitateResult.subtreeResult(stuff, fqn);
  +      }
  +      else
  +      {
  +         return GravitateResult.noDataFound();
  +      }
  +   }
  +
      private String unmarshallString(ObjectInputStream in) throws Exception
      {
         //return StringUtil.readString(in, null);
  
  
  
  1.1      date: 2007/01/15 16:19:09;  author: msurtani;  state: Exp;JBossCache/src/org/jboss/cache/marshall/NodeDataMarker.java
  
  Index: NodeDataMarker.java
  ===================================================================
  package org.jboss.cache.marshall;
  
  public class NodeDataMarker extends NodeData
  {
  
     private static final long serialVersionUID = 4851793846346021014L;
  
     public NodeDataMarker()
     {
        super();
     }
  
     public boolean isMarker()
     {
        return true;
     }
  }
  
  
  
  1.1      date: 2007/01/15 16:19:09;  author: msurtani;  state: Exp;JBossCache/src/org/jboss/cache/marshall/NodeDataExceptionMarker.java
  
  Index: NodeDataExceptionMarker.java
  ===================================================================
  package org.jboss.cache.marshall;
  
  import java.io.IOException;
  import java.io.ObjectInput;
  import java.io.ObjectOutput;
  
  public class NodeDataExceptionMarker extends NodeData
  {
  
     private static final long serialVersionUID = 240199474174502551L;
     private Throwable cause;
     private Object cacheNodeIdentity;
  
     public NodeDataExceptionMarker()
     {
        super();
     }
  
     public NodeDataExceptionMarker(Throwable t, Object node)
     {
        cause = t;
        cacheNodeIdentity = node;
     }
  
     public Throwable getCause()
     {
        return cause;
     }
  
     public Object getCacheNodeIdentity()
     {
        return cacheNodeIdentity;
     }
  
     public boolean isExceptionMarker()
     {
        return true;
     }
  
     public void writeExternal(ObjectOutput out) throws IOException
     {
        super.writeExternal(out);
        out.writeObject(cause);
        out.writeObject(cacheNodeIdentity);
     }
  
     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
     {
        super.readExternal(in);
        cause = (Throwable) in.readObject();
        cacheNodeIdentity = in.readObject();
     }
  }
  
  
  
  1.1      date: 2007/01/15 16:19:09;  author: msurtani;  state: Exp;JBossCache/src/org/jboss/cache/marshall/NodeData.java
  
  Index: NodeData.java
  ===================================================================
  package org.jboss.cache.marshall;
  
  import org.jboss.cache.Fqn;
  
  import java.io.Externalizable;
  import java.io.IOException;
  import java.io.ObjectInput;
  import java.io.ObjectOutput;
  import java.util.Map;
  
  /**
   * Serializable representation of the data of a node (FQN and attributes)
   *
   * @author Bela Ban
   * @version $Id: NodeData.java,v 1.1 2007/01/15 16:19:09 msurtani Exp $
   */
  public class NodeData implements Externalizable
  {
     private Fqn fqn = null;
     private Map<Object, Object> attrs = null;
  
     static final long serialVersionUID = -7571995794010294485L;
  
     public NodeData()
     {
     }
  
     public NodeData(Fqn fqn)
     {
        this.fqn = fqn;
     }
  
     public NodeData(Fqn fqn, Map<Object, Object> attrs)
     {
        this.fqn = fqn;
        this.attrs = attrs;
     }
  
     public NodeData(String fqn, Map<Object, Object> attrs)
     {
        this.fqn = Fqn.fromString(fqn);
        this.attrs = attrs;
     }
  
     public Map<Object, Object> getAttributes()
     {
        return attrs;
     }
  
     public Fqn getFqn()
     {
        return fqn;
     }
  
     public boolean isMarker()
     {
        return false;
     }
  
     public boolean isExceptionMarker()
     {
        return false;
     }
  
     public void writeExternal(ObjectOutput out) throws IOException
     {
        out.writeObject(fqn);
        if (attrs != null)
        {
           out.writeBoolean(true);
           out.writeObject(attrs);
        }
        else
        {
           out.writeBoolean(false);
        }
     }
  
     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
     {
        fqn = (Fqn) in.readObject();
        if (in.readBoolean())
        {
           attrs = (Map) in.readObject();
        }
     }
  
     public String toString()
     {
        return "fqn: " + fqn + ", attrs=" + attrs;
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list