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

Manik Surtani msurtani at jboss.com
Tue Jan 2 08:13:20 EST 2007


  User: msurtani
  Date: 07/01/02 08:13:20

  Modified:    src/org/jboss/cache/marshall     VersionAwareMarshaller.java
                        MethodDeclarations.java
  Added:       src/org/jboss/cache/marshall     CacheMarshaller200.java
  Removed:     src/org/jboss/cache/marshall     TreeCacheMarshaller200.java
  Log:
  - Fixed issues with AbstractNode.equals()
  - Fixed NPE with Cache.getChildrenNames()
  - Renamed TreeCacheMarshaller to CacheMarshaller to be in line with the Cache/CacheSPI naming
  
  Revision  Changes    Path
  1.12      +3 -3      JBossCache/src/org/jboss/cache/marshall/VersionAwareMarshaller.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: VersionAwareMarshaller.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/marshall/VersionAwareMarshaller.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -b -r1.11 -r1.12
  --- VersionAwareMarshaller.java	27 Nov 2006 17:07:06 -0000	1.11
  +++ VersionAwareMarshaller.java	2 Jan 2007 13:13:20 -0000	1.12
  @@ -19,7 +19,7 @@
   import java.util.StringTokenizer;
   
   /**
  - * A facade to various other marshallers like {@link org.jboss.cache.marshall.TreeCacheMarshaller200}
  + * A facade to various other marshallers like {@link org.jboss.cache.marshall.CacheMarshaller200}
    * which is version-aware.
    *
    * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
  @@ -53,7 +53,7 @@
         {
            case VERSION_200:
            default:
  -            defaultMarshaller = new TreeCacheMarshaller200(manager, defaultInactive, useRegionBasedMarshalling);
  +            defaultMarshaller = new CacheMarshaller200(manager, defaultInactive, useRegionBasedMarshalling);
               marshallers.put(VERSION_200, defaultMarshaller);
               break;
         }
  @@ -178,7 +178,7 @@
               marshaller = marshallers.get(VERSION_200);
               if (marshaller == null)
               {
  -               marshaller = new TreeCacheMarshaller200(manager, defaultInactive, useRegionBasedMarshalling);
  +               marshaller = new CacheMarshaller200(manager, defaultInactive, useRegionBasedMarshalling);
                  marshallers.put(VERSION_200, marshaller);
               }
               break;
  
  
  
  1.29      +3 -3      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.28
  retrieving revision 1.29
  diff -u -b -r1.28 -r1.29
  --- MethodDeclarations.java	30 Dec 2006 17:50:05 -0000	1.28
  +++ MethodDeclarations.java	2 Jan 2007 13:13:20 -0000	1.29
  @@ -31,7 +31,7 @@
    * allowing lookup operations both ways.
    *
    * @author <a href="galder.zamarreno at jboss.com">Galder Zamarreno</a>
  - * @version $Revision: 1.28 $
  + * @version $Revision: 1.29 $
    */
   public class MethodDeclarations
   {
  @@ -378,7 +378,7 @@
         {
            if (log.isWarnEnabled())
            {
  -            log.warn("Method " + method + " is not registered with " + TreeCacheMarshaller200.class);
  +            log.warn("Method " + method + " is not registered with " + CacheMarshaller200.class);
            }
         }
   
  @@ -394,7 +394,7 @@
            {
               log.error("Method id " + id + " is not registered");
            }
  -         throw new RuntimeException("Method id " + id + " is not registered with " + TreeCacheMarshaller200.class);
  +         throw new RuntimeException("Method id " + id + " is not registered with " + CacheMarshaller200.class);
         }
         return method;
      }
  
  
  
  1.1      date: 2007/01/02 13:13:20;  author: msurtani;  state: Exp;JBossCache/src/org/jboss/cache/marshall/CacheMarshaller200.java
  
  Index: CacheMarshaller200.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.marshall;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.GlobalTransaction;
  import org.jboss.cache.Region;
  import org.jboss.cache.RegionManager;
  import org.jboss.cache.optimistic.DefaultDataVersion;
  import org.jboss.cache.rpc.RpcTreeCache;
  import org.jgroups.Address;
  import org.jgroups.stack.IpAddress;
  
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  /**
   * An enhanced marshaller for RPC calls between CacheImpl instances.
   *
   * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
   */
  public class CacheMarshaller200 extends Marshaller
  {
     // logger
     private Log log = LogFactory.getLog(CacheMarshaller200.class);
  
     // magic numbers
     protected static final int MAGICNUMBER_METHODCALL = 1;
     protected static final int MAGICNUMBER_FQN = 2;
     protected static final int MAGICNUMBER_GTX = 3;
     protected static final int MAGICNUMBER_IPADDRESS = 4;
     protected static final int MAGICNUMBER_LIST = 5;
     protected static final int MAGICNUMBER_INTEGER = 6;
     protected static final int MAGICNUMBER_LONG = 7;
     protected static final int MAGICNUMBER_BOOLEAN = 8;
     protected static final int MAGICNUMBER_STRING = 9;
     protected static final int MAGICNUMBER_MAP = 10;
     protected static final int MAGICNUMBER_DEFAULT_DATA_VERSION = 11;
     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");
  
     public CacheMarshaller200(RegionManager manager, boolean defaultInactive, boolean useRegionBasedMarshalling)
     {
        init(manager, defaultInactive, useRegionBasedMarshalling);
        if (useRegionBasedMarshalling)
        {
           log.debug("Using region based marshalling logic : marshalling Fqn as a String first for every call.");
        }
     }
  
     // -------- Marshaller interface
  
     public void objectToStream(Object o, ObjectOutputStream out) throws Exception
     {
        if (log.isTraceEnabled()) log.trace("Marshalling object " + o);
        Map<Object, Integer> refMap = new HashMap<Object, Integer>();
  
        if (useRegionBasedMarshalling)
        {
           // we first marshall the Fqn as a String (ugh!)
           MethodCall call = (MethodCall) o;
           String fqnAsString = extractFqnAsString(call);
           marshallObject(fqnAsString, out, refMap);
        }
  
        marshallObject(o, out, refMap);
     }
  
     public Object objectFromStream(ObjectInputStream in) throws Exception
     {
        Object retValue;
        Map<Integer, Object> refMap = new HashMap<Integer, Object>();
  
        if (useRegionBasedMarshalling)
        {
           // first unmarshall the fqn as a String
           // This may be null if the call being unmarshalled is
           // not region-based
           String fqn = (String) unmarshallObject(in, refMap);
           Region region = null;
           if (fqn != null)
           {
              region = findRegion(fqn);
           }
           retValue = region == null ? unmarshallObject(in, refMap) : unmarshallObject(in, region.getClassLoader(), refMap);
        }
        else
        {
           retValue = unmarshallObject(in, refMap);
        }
  
        return retValue;
     }
  
     private Region findRegion(String fqn) throws InactiveRegionException
     {
        Region region;
        // obtain a region from MarshRegionManager, if not, will use default.
        region = fqn == null ? null : getRegion(fqn);
  
        if (region != null)
        {
           if (!region.isActive())
           {
  //            throw new InactiveRegionException("Cannot unmarshall message for region " + fqn
  //                  + ". This region is inactive.");
              throw IRE;
           }
        }
        else if (defaultInactive)
        {
           // No region but default inactive means region is inactive
  //         throw new InactiveRegionException("Cannot unmarshall message for region " + fqn + ". By default region " + fqn
  //               + " is inactive.");
           throw IRE;
        }
  
        return region;
     }
  
     private String extractFqnAsString(MethodCall call) throws Exception
     {
        String fqnAsString;
        if (call.getMethod().equals(MethodDeclarations.replicateMethod))
        {
           fqnAsString = extractFqnFromMethodCall(call);
        }
        else if (call.getMethod().equals(MethodDeclarations.replicateAllMethod))
        {
           fqnAsString = extractFqnFromListOfMethodCall(call);
        }
        else if (call.getMethod().equals(RpcTreeCache.dispatchRpcCallMethod))
        {
           MethodCall call2 = (MethodCall) call.getArgs()[1];
           fqnAsString = extractFqn(call2);
        }
        else
        {
           fqnAsString = extractFqn(call);
        }
  
        return fqnAsString;
     }
  
     // --------- Marshalling methods
  
     private void marshallObject(Object o, ObjectOutputStream out, Map<Object, Integer> refMap) throws Exception
     {
        if (o == null)
        {
           out.writeByte(MAGICNUMBER_NULL);
        }
        else if (refMap.containsKey(o)) // see if this object has been marshalled before.
        {
           out.writeByte(MAGICNUMBER_REF);
           out.writeShort(((Integer) refMap.get(o)).intValue());
        }
        else if (o instanceof MethodCall)
        {
           // first see if this is a 'known' method call.
           MethodCall call = (MethodCall) o;
  
           if (call.getMethodId() > -1)
           {
              out.writeByte(MAGICNUMBER_METHODCALL);
              marshallMethodCall(call, out, refMap);
           }
           else
           {
              throw new IllegalArgumentException("MethodCall does not have a valid method id.  Was this method call created with MethodCallFactory?");
           }
        }
        else if (o instanceof Fqn)
        {
           int refId = createReference(o, refMap);
           out.writeByte(MAGICNUMBER_FQN);
           out.writeShort(refId);
           marshallFqn((Fqn) o, out, refMap);
        }
        else if (o instanceof GlobalTransaction)
        {
           int refId = createReference(o, refMap);
           out.writeByte(MAGICNUMBER_GTX);
           out.writeShort(refId);
           marshallGlobalTransaction((GlobalTransaction) o, out, refMap);
        }
        else if (o instanceof IpAddress)
        {
           out.writeByte(MAGICNUMBER_IPADDRESS);
           marshallIpAddress((IpAddress) o, out);
        }
        else if (o instanceof DefaultDataVersion)
        {
           out.writeByte(MAGICNUMBER_DEFAULT_DATA_VERSION);
           out.writeLong(((DefaultDataVersion) o).getRawVersion());
        }
        else if (o instanceof List)
        {
           out.writeByte(MAGICNUMBER_LIST);
           marshallList((List) o, out, refMap);
        }
        else if (o instanceof Boolean)
        {
           out.writeByte(MAGICNUMBER_BOOLEAN);
           out.writeBoolean(((Boolean) o).booleanValue());
        }
        else if (o instanceof Integer)
        {
           out.writeByte(MAGICNUMBER_INTEGER);
           out.writeInt(((Integer) o).intValue());
        }
        else if (o instanceof Long)
        {
           out.writeByte(MAGICNUMBER_LONG);
           out.writeLong(((Long) o).longValue());
        }
        else if (o instanceof String)
        {
           int refId = createReference(o, refMap);
           out.writeByte(MAGICNUMBER_STRING);
           out.writeShort(refId);
           marshallString((String) o, out);
        }
        else if (o instanceof Map)
        {
           out.writeByte(MAGICNUMBER_MAP);
           marshallMap((Map) o, out, refMap);
        }
        else if (o instanceof Serializable)
        {
           int refId = createReference(o, refMap);
           if (log.isTraceEnabled())
           {
              log.trace("Warning: using object serialization for " + o.getClass());
           }
           out.writeByte(MAGICNUMBER_SERIALIZABLE);
           out.writeShort(refId);
           out.writeObject(o);
        }
        else
        {
           throw new Exception("Don't know how to marshall object of type " + o.getClass());
        }
     }
  
     private int createReference(Object o, Map<Object, Integer> refMap)
     {
        int reference = refMap.size();
        refMap.put(o, reference);
        return reference;
     }
  
     private void marshallString(String s, ObjectOutputStream out) throws Exception
     {
        //StringUtil.saveString(out, s);
        out.writeObject(s);
     }
  
     private void marshallMethodCall(MethodCall methodCall, ObjectOutputStream out, Map<Object, Integer> refMap) throws Exception
     {
        out.writeShort(methodCall.getMethodId());
        Object[] args = methodCall.getArgs();
        byte numArgs = (byte) (args == null ? 0 : args.length);
        out.writeByte(numArgs);
  
        for (int i = 0; i < numArgs; i++)
        {
           marshallObject(args[i], out, refMap);
        }
     }
  
     private void marshallGlobalTransaction(GlobalTransaction globalTransaction, ObjectOutputStream out, Map<Object, Integer> refMap) throws Exception
     {
        out.writeLong(globalTransaction.getId());
        marshallObject(globalTransaction.getAddress(), out, refMap);
     }
  
  
     private void marshallFqn(Fqn fqn, ObjectOutputStream out, Map<Object, Integer> refMap) throws Exception
     {
        boolean isRoot = fqn.isRoot();
        out.writeBoolean(isRoot);
        if (!isRoot)
        {
           out.writeShort(fqn.size());
           for (int i = 0; i < fqn.size(); i++)
           {
              marshallObject(fqn.get(i), out, refMap);
           }
        }
     }
  
     private void marshallIpAddress(IpAddress ipAddress, ObjectOutputStream out) throws Exception
     {
        ipAddress.writeExternal(out);
     }
  
     private void marshallList(List l, ObjectOutputStream out, Map<Object, Integer> refMap) throws Exception
     {
        out.writeInt(l.size());
        for (Object o : l)
        {
           marshallObject(o, out, refMap);
        }
     }
  
     private void marshallMap(Map map, ObjectOutputStream out, Map<Object, Integer> refMap) throws Exception
     {
        int mapSize = map.size();
        out.writeInt(mapSize);
        if (mapSize == 0) return;
  
        for (Map.Entry me : (Set<Map.Entry>) map.entrySet())
        {
           marshallObject(me.getKey(), out, refMap);
           marshallObject(me.getValue(), out, refMap);
        }
     }
  
     // --------- Unmarshalling methods
  
     private Object unmarshallObject(ObjectInputStream in, ClassLoader loader, Map<Integer, Object> refMap) throws Exception
     {
        if (loader == null)
        {
           return unmarshallObject(in, refMap);
        }
        else
        {
           Thread currentThread = Thread.currentThread();
           ClassLoader old = currentThread.getContextClassLoader();
           try
           {
              currentThread.setContextClassLoader(loader);
              return unmarshallObject(in, refMap);
           }
           finally
           {
              currentThread.setContextClassLoader(old);
           }
        }
     }
  
     private Object unmarshallObject(ObjectInputStream in, Map<Integer, Object> refMap) throws Exception
     {
        byte magicNumber = in.readByte();
        Integer reference;
        Object retVal;
        switch (magicNumber)
        {
           case MAGICNUMBER_NULL:
              return null;
           case MAGICNUMBER_REF:
              reference = (int) in.readShort();
              if (!refMap.containsKey(reference))
              {
                 throw new IOException("Unable to locate object reference " + reference + " in byte stream!");
              }
              return refMap.get(reference);
           case MAGICNUMBER_SERIALIZABLE:
              reference = (int) in.readShort();
              retVal = in.readObject();
              refMap.put(reference, retVal);
              return retVal;
           case MAGICNUMBER_METHODCALL:
              retVal = unmarshallMethodCall(in, refMap);
              return retVal;
           case MAGICNUMBER_FQN:
              reference = (int) in.readShort();
              retVal = unmarshallFqn(in, refMap);
              refMap.put(reference, retVal);
              return retVal;
           case MAGICNUMBER_GTX:
              reference = (int) in.readShort();
              retVal = unmarshallGlobalTransaction(in, refMap);
              refMap.put(reference, retVal);
              return retVal;
           case MAGICNUMBER_IPADDRESS:
              retVal = unmarshallIpAddress(in);
              return retVal;
           case MAGICNUMBER_DEFAULT_DATA_VERSION:
              retVal = new DefaultDataVersion(in.readLong());
              return retVal;
           case MAGICNUMBER_LIST:
              return unmarshallList(in, refMap);
           case MAGICNUMBER_BOOLEAN:
              return in.readBoolean() ? Boolean.TRUE : Boolean.FALSE;
           case MAGICNUMBER_INTEGER:
              return in.readInt();
           case MAGICNUMBER_LONG:
              retVal = in.readLong();
              return retVal;
           case MAGICNUMBER_STRING:
              reference = (int) in.readShort();
              retVal = unmarshallString(in);
              refMap.put(reference, retVal);
              return retVal;
           case MAGICNUMBER_MAP:
              return unmarshallMap(in, refMap);
           default:
              if (log.isErrorEnabled())
              {
                 log.error("Unknown Magic Number " + magicNumber);
              }
              throw new Exception("Unknown magic number " + magicNumber);
        }
     }
  
     private String unmarshallString(ObjectInputStream in) throws Exception
     {
        //return StringUtil.readString(in, null);
        return (String) in.readObject();
     }
  
     private MethodCall unmarshallMethodCall(ObjectInputStream in, Map<Integer, Object> refMap) throws Exception
     {
        short methodId = in.readShort();
        byte numArgs = in.readByte();
        Object[] args = null;
  
        if (numArgs > 0)
        {
           args = new Object[numArgs];
  
           for (int i = 0; i < numArgs; i++)
           {
              args[i] = unmarshallObject(in, refMap);
           }
        }
        return MethodCallFactory.create(MethodDeclarations.lookupMethod(methodId), args);
     }
  
     private GlobalTransaction unmarshallGlobalTransaction(ObjectInputStream in, Map<Integer, Object> refMap) throws Exception
     {
        GlobalTransaction gtx = new GlobalTransaction();
        long id = in.readLong();
        Object address = unmarshallObject(in, refMap);
        gtx.setId(id);
        gtx.setAddress((Address) address);
        return gtx;
     }
  
     private Fqn unmarshallFqn(ObjectInputStream in, Map<Integer, Object> refMap) throws Exception
     {
  
        boolean isRoot = in.readBoolean();
        Fqn fqn;
        if (!isRoot)
        {
           int numElements = in.readShort();
           List<Object> elements = new ArrayList<Object>(numElements);
           for (int i = 0; i < numElements; i++)
           {
              elements.add(unmarshallObject(in, refMap));
           }
           fqn = new Fqn(elements);
        }
        else
        {
           fqn = Fqn.ROOT;
        }
        return fqn;
     }
  
     private IpAddress unmarshallIpAddress(ObjectInputStream in) throws Exception
     {
        IpAddress ipAddress = new IpAddress();
        ipAddress.readExternal(in);
        return ipAddress;
     }
  
     private List unmarshallList(ObjectInputStream in, Map<Integer, Object> refMap) throws Exception
     {
        int listSize = in.readInt();
        List<Object> list = new ArrayList<Object>(listSize);
        for (int i = 0; i < listSize; i++)
        {
           list.add(unmarshallObject(in, refMap));
        }
        return list;
     }
  
     private Map unmarshallMap(ObjectInputStream in, Map<Integer, Object> refMap) throws Exception
     {
        int mapSize = in.readInt();
        if (mapSize == 0) return Collections.emptyMap();
        Map<Object, Object> map = new HashMap<Object, Object>(mapSize);
        for (int i = 0; i < mapSize; i++)
        {
           map.put(unmarshallObject(in, refMap), unmarshallObject(in, refMap));
        }
        return map;
     }
  
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list