[jboss-cvs] JBossCache/src/org/jboss/cache/marshall ...
Manik Surtani
msurtani at jboss.com
Tue Aug 29 09:36:53 EDT 2006
User: msurtani
Date: 06/08/29 09:36:53
Modified: src/org/jboss/cache/marshall VersionAwareMarshaller.java
Added: src/org/jboss/cache/marshall TreeCacheMarshaller200.java
Log:
Creatd new TreeCacheMarshaller200
Revision Changes Path
1.8 +225 -215 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.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- VersionAwareMarshaller.java 11 Jul 2006 08:39:22 -0000 1.7
+++ VersionAwareMarshaller.java 29 Aug 2006 13:36:53 -0000 1.8
@@ -8,14 +8,14 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.jboss.invocation.MarshalledValueOutputStream;
import org.jboss.invocation.MarshalledValueInputStream;
+import org.jboss.invocation.MarshalledValueOutputStream;
import org.jgroups.blocks.RpcDispatcher;
-import java.io.ByteArrayOutputStream;
-import java.io.ObjectOutputStream;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
@@ -33,11 +33,11 @@
private Log log = LogFactory.getLog(VersionAwareMarshaller.class);
Marshaller defaultMarshaller;
- Map marshallers = new HashMap();
+ Map<Integer, Marshaller> marshallers = new HashMap<Integer, Marshaller>();
private static final int VERSION_LEGACY = 1;
private static final int VERSION_140 = 14;
- private static final int VERSION_200 =20;
+ private static final int VERSION_200 = 20;
private int versionInt;
@@ -56,18 +56,21 @@
switch (versionInt)
{
case VERSION_200:
+ defaultMarshaller = new TreeCacheMarshaller200(manager, defaultInactive, useRegionBasedMarshalling);
+ marshallers.put(VERSION_200, defaultMarshaller);
+ break;
case VERSION_140:
defaultMarshaller = new TreeCacheMarshaller140(manager, defaultInactive, useRegionBasedMarshalling);
- marshallers.put(new Integer(VERSION_140), defaultMarshaller);
+ marshallers.put(VERSION_140, defaultMarshaller);
break;
default:
defaultMarshaller = new LegacyTreeCacheMarshaller(manager, defaultInactive, useRegionBasedMarshalling);
- marshallers.put(new Integer(VERSION_LEGACY), defaultMarshaller);
+ marshallers.put(VERSION_LEGACY, defaultMarshaller);
}
if (log.isDebugEnabled())
{
- log.debug("Initialised with version "+version+" and versionInt " + versionInt);
+ log.debug("Initialised with version " + version + " and versionInt " + versionInt);
log.debug("Using default marshaller " + defaultMarshaller.getClass());
}
}
@@ -199,20 +202,27 @@
switch (versionId)
{
case VERSION_200:
+ marshaller = marshallers.get(VERSION_200);
+ if (marshaller == null)
+ {
+ marshaller = new TreeCacheMarshaller200(manager, defaultInactive, useRegionBasedMarshalling);
+ marshallers.put(VERSION_200, marshaller);
+ }
+ break;
case VERSION_140:
- marshaller = (Marshaller) marshallers.get(new Integer(VERSION_140));
+ marshaller = marshallers.get(VERSION_140);
if (marshaller == null)
{
marshaller = new TreeCacheMarshaller140(manager, defaultInactive, useRegionBasedMarshalling);
- marshallers.put(new Integer(VERSION_140), marshaller);
+ marshallers.put(VERSION_140, marshaller);
}
break;
default:
- marshaller = (Marshaller) marshallers.get(new Integer(VERSION_LEGACY));
+ marshaller = marshallers.get(VERSION_LEGACY);
if (marshaller == null)
{
marshaller = new LegacyTreeCacheMarshaller(manager, defaultInactive, useRegionBasedMarshalling);
- marshallers.put(new Integer(VERSION_LEGACY), marshaller);
+ marshallers.put(VERSION_LEGACY, marshaller);
}
}
return marshaller;
1.1 date: 2006/08/29 13:36:53; author: msurtani; state: Exp;JBossCache/src/org/jboss/cache/marshall/TreeCacheMarshaller200.java
Index: TreeCacheMarshaller200.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.CacheException;
import org.jboss.cache.Fqn;
import org.jboss.cache.GlobalTransaction;
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.Iterator;
import java.util.List;
import java.util.Map;
/**
* An enhanced marshaller for RPC calls between TreeCache instances.
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
*/
public class TreeCacheMarshaller200 extends Marshaller
{
// logger
private static Log log = LogFactory.getLog(TreeCacheMarshaller200.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_NULL = 99;
protected static final int MAGICNUMBER_SERIALIZABLE = 100;
protected static final int MAGICNUMBER_REF = 101;
public TreeCacheMarshaller200(RegionManager manager, boolean defaultInactive, boolean useRegionBasedMarshalling)
{
init(manager, defaultInactive, useRegionBasedMarshalling);
if (useRegionBasedMarshalling)
{
TreeCacheMarshaller200.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 (TreeCacheMarshaller200.log.isTraceEnabled()) TreeCacheMarshaller200.log.trace("Marshalling object " + o);
Map refMap = new HashMap();
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 refMap = new HashMap();
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);
try
{
Region region = null;
if (fqn != null)
{
region = findRegion(fqn);
}
retValue = region == null ? unmarshallObject(in, refMap) : unmarshallObject(in, region.getClassLoader(), refMap);
if (region != null && region.isQueueing())
{
Object originalRetValue = retValue;
if (TreeCacheMarshaller200.log.isDebugEnabled())
{
TreeCacheMarshaller200.log.debug("Received call on an ququing Fqn region (" + fqn + "). Calling enqueueMethodCallMethod");
}
retValue = MethodCallFactory.create(MethodDeclarations.enqueueMethodCallMethod, new Object[]{fqn, originalRetValue});
}
}
catch (TreeCacheMarshaller200.InactiveRegionException e)
{
if (TreeCacheMarshaller200.log.isDebugEnabled())
{
TreeCacheMarshaller200.log.debug("Received call on an inactive Fqn region (" + fqn + "). Calling notifyCallOnInactiveMetod");
}
retValue = MethodCallFactory.create(MethodDeclarations.notifyCallOnInactiveMethod, new Object[]{fqn});
}
}
else
{
retValue = unmarshallObject(in, refMap);
}
return retValue;
}
private Region findRegion(String fqn) throws TreeCacheMarshaller200.InactiveRegionException
{
Region region;
// obtain a region from RegionManager, if not, will use default.
region = fqn == null ? null : getRegion(fqn);
if (region != null)
{
// If the region has been marked inactive, we still have
// to return a MethodCall or RpcDispatcher will log an Error.
// So, return a call to the TreeCache "_notifyCallOnInactive" method
if (region.getStatus() == Region.STATUS_INACTIVE)
{
throw new TreeCacheMarshaller200.InactiveRegionException();
}
}
else if (defaultInactive)
{
// No region but default inactive means region is inactive
throw new TreeCacheMarshaller200.InactiveRegionException();
}
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 refMap) throws Exception
{
if (o == null)
{
out.writeByte(TreeCacheMarshaller200.MAGICNUMBER_NULL);
}
else if (refMap.containsKey(o)) // see if this object has been marshalled before.
{
out.writeByte(TreeCacheMarshaller200.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(TreeCacheMarshaller200.MAGICNUMBER_METHODCALL);
marshallMethodCall(call, out, refMap);
}
else
{
// treat this as a serializable object
// if (log.isWarnEnabled()) log.warn("Treating method call " + call + " as a normal Serializable object, not attempting to marshall with method ids.");
//
// int refId = createReference(o, refMap);
// out.writeByte(MAGICNUMBER_SERIALIZABLE);
// out.writeShort(refId);
// out.writeObject(call);
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(TreeCacheMarshaller200.MAGICNUMBER_FQN);
out.writeShort(refId);
marshallFqn((Fqn) o, out, refMap);
}
else if (o instanceof GlobalTransaction)
{
int refId = createReference(o, refMap);
out.writeByte(TreeCacheMarshaller200.MAGICNUMBER_GTX);
out.writeShort(refId);
marshallGlobalTransaction((GlobalTransaction) o, out, refMap);
}
else if (o instanceof IpAddress)
{
out.writeByte(TreeCacheMarshaller200.MAGICNUMBER_IPADDRESS);
marshallIpAddress((IpAddress) o, out);
}
else if (o instanceof List)
{
out.writeByte(TreeCacheMarshaller200.MAGICNUMBER_LIST);
marshallList((List) o, out, refMap);
}
else if (o instanceof Boolean)
{
out.writeByte(TreeCacheMarshaller200.MAGICNUMBER_BOOLEAN);
out.writeBoolean(((Boolean) o).booleanValue());
}
else if (o instanceof Integer)
{
out.writeByte(TreeCacheMarshaller200.MAGICNUMBER_INTEGER);
out.writeInt(((Integer) o).intValue());
}
else if (o instanceof Long)
{
out.writeByte(TreeCacheMarshaller200.MAGICNUMBER_LONG);
out.writeLong(((Long) o).longValue());
}
else if (o instanceof String)
{
int refId = createReference(o, refMap);
out.writeByte(TreeCacheMarshaller200.MAGICNUMBER_STRING);
out.writeShort(refId);
out.writeUTF((String) o);
}
else if (o instanceof Map)
{
out.writeByte(TreeCacheMarshaller200.MAGICNUMBER_MAP);
marshallMap((Map) o, out, refMap);
}
else if (o instanceof Serializable)
{
int refId = createReference(o, refMap);
if (TreeCacheMarshaller200.log.isTraceEnabled())
{
TreeCacheMarshaller200.log.trace("Warning: using object serialization for " + o.getClass());
}
out.writeByte(TreeCacheMarshaller200.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 refMap)
{
int reference = refMap.size();
refMap.put(o, new Integer(reference));
return reference;
}
private void marshallMethodCall(MethodCall methodCall, ObjectOutputStream out, Map 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 refMap) throws Exception
{
out.writeLong(globalTransaction.getId());
marshallObject(globalTransaction.getAddress(), out, refMap);
}
private void marshallFqn(Fqn fqn, ObjectOutputStream out, Map 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 refMap) throws Exception
{
out.writeInt(l.size());
Iterator i = l.iterator();
while (i.hasNext())
{
marshallObject(i.next(), out, refMap);
}
}
private void marshallMap(Map map, ObjectOutputStream out, Map refMap) throws Exception
{
int mapSize = map.size();
out.writeInt(mapSize);
if (mapSize == 0) return;
for (Object k : map.keySet())
{
marshallObject(k, out, refMap);
marshallObject(map.get(k), out, refMap);
}
}
// --------- Unmarshalling methods
private Object unmarshallObject(ObjectInputStream in, ClassLoader loader, Map 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 refMap) throws Exception
{
byte magicNumber = in.readByte();
Integer reference;
Object retVal;
switch (magicNumber)
{
case TreeCacheMarshaller200.MAGICNUMBER_NULL:
return null;
case TreeCacheMarshaller200.MAGICNUMBER_REF:
reference = new Integer(in.readShort());
if (!refMap.containsKey(reference))
{
throw new IOException("Unable to locate object reference " + reference + " in byte stream!");
}
return refMap.get(reference);
case TreeCacheMarshaller200.MAGICNUMBER_SERIALIZABLE:
reference = new Integer(in.readShort());
retVal = in.readObject();
refMap.put(reference, retVal);
return retVal;
case TreeCacheMarshaller200.MAGICNUMBER_METHODCALL:
retVal = unmarshallMethodCall(in, refMap);
return retVal;
case TreeCacheMarshaller200.MAGICNUMBER_FQN:
reference = new Integer(in.readShort());
retVal = unmarshallFqn(in, refMap);
refMap.put(reference, retVal);
return retVal;
case TreeCacheMarshaller200.MAGICNUMBER_GTX:
reference = new Integer(in.readShort());
retVal = unmarshallGlobalTransaction(in, refMap);
refMap.put(reference, retVal);
return retVal;
case TreeCacheMarshaller200.MAGICNUMBER_IPADDRESS:
retVal = unmarshallIpAddress(in);
return retVal;
case TreeCacheMarshaller200.MAGICNUMBER_LIST:
return unmarshallList(in, refMap);
case TreeCacheMarshaller200.MAGICNUMBER_BOOLEAN:
return in.readBoolean() ? Boolean.TRUE : Boolean.FALSE;
case TreeCacheMarshaller200.MAGICNUMBER_INTEGER:
return new Integer(in.readInt());
case TreeCacheMarshaller200.MAGICNUMBER_LONG:
retVal = new Long(in.readLong());
return retVal;
case TreeCacheMarshaller200.MAGICNUMBER_STRING:
reference = new Integer(in.readShort());
retVal = in.readUTF();
refMap.put(reference, retVal);
return retVal;
case TreeCacheMarshaller200.MAGICNUMBER_MAP:
return unmarshallMap(in, refMap);
default:
if (TreeCacheMarshaller200.log.isErrorEnabled())
{
TreeCacheMarshaller200.log.error("Unknown Magic Number " + magicNumber);
}
throw new Exception("Unknown magic number " + magicNumber);
}
}
private MethodCall unmarshallMethodCall(ObjectInputStream in, Map 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 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 refMap) throws Exception
{
boolean isRoot = in.readBoolean();
Fqn fqn;
if (!isRoot)
{
int numElements = in.readShort();
List elements = new ArrayList(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 refMap) throws Exception
{
int listSize = in.readInt();
List list = new ArrayList(listSize);
for (int i = 0; i < listSize; i++)
{
list.add(unmarshallObject(in, refMap));
}
return list;
}
private Map unmarshallMap(ObjectInputStream in, Map refMap) throws Exception
{
int mapSize = in.readInt();
if (mapSize == 0) return Collections.emptyMap();
Map map = new HashMap(mapSize);
for (int i = 0; i < mapSize; i++)
{
map.put(unmarshallObject(in, refMap), unmarshallObject(in, refMap));
}
return map;
}
class InactiveRegionException extends CacheException
{
public InactiveRegionException()
{
super();
}
public InactiveRegionException(String msg)
{
super(msg);
}
public InactiveRegionException(String msg, Throwable cause)
{
super(msg, cause);
}
}
}
More information about the jboss-cvs-commits
mailing list