[jboss-cvs] JBossCache/src/org/jboss/cache/marshall ...
Manik Surtani
msurtani at jboss.com
Wed Jan 17 09:13:06 EST 2007
User: msurtani
Date: 07/01/17 09:13:06
Modified: src/org/jboss/cache/marshall
VersionAwareMarshaller.java Marshaller.java
AbstractMarshaller.java
NodeDataExceptionMarker.java NodeDataMarker.java
NodeData.java CacheMarshaller200.java
Log:
JBCACHE-908
Revision Changes Path
1.15 +42 -1 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.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- VersionAwareMarshaller.java 15 Jan 2007 18:10:56 -0000 1.14
+++ VersionAwareMarshaller.java 17 Jan 2007 14:13:06 -0000 1.15
@@ -8,6 +8,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.jboss.cache.Fqn;
import org.jboss.cache.RegionManager;
import java.io.ByteArrayOutputStream;
@@ -123,6 +124,7 @@
// based on the default marshaller, construct an object output stream based on what's compatible.
out = ObjectSerializationFactory.createObjectOutputStream(bos);
out.writeShort(versionInt);
+ if (log.isTraceEnabled()) log.trace("Wrote version " + versionInt);
//now marshall the contents of the object
defaultMarshaller.objectToObjectStream(obj, out);
@@ -151,6 +153,7 @@
// create an input stream and read the first short
in = ObjectSerializationFactory.createObjectInputStream(buf);
versionId = in.readShort();
+ if (log.isTraceEnabled()) log.trace("Read version " + versionId);
}
catch (Exception e)
{
@@ -174,6 +177,7 @@
// create an input stream and read the first short
in = ObjectSerializationFactory.createObjectInputStream(is);
versionId = in.readShort();
+ if (log.isTraceEnabled()) log.trace("Read version " + versionId);
}
catch (Exception e)
{
@@ -186,6 +190,14 @@
return marshaller.objectFromObjectStream(in);
}
+ public void objectToObjectStream(Object obj, ObjectOutputStream out, Fqn region) throws Exception
+ {
+ //defaultMarshaller.objectToObjectStream(obj, out);
+ out.writeShort(versionInt);
+ if (log.isTraceEnabled()) log.trace("Wrote version " + versionInt);
+ defaultMarshaller.objectToObjectStream(obj, out, region);
+ }
+
/**
* Lazily instantiates and loads the relevant marshaller for a given version.
*
@@ -221,14 +233,43 @@
return defaultMarshaller.isInactive(fqn);
}
+ /**
+ * Acts as a versioning decorator to the default marshaller. Does not create or close the stream.
+ *
+ * @param obj object to marshall
+ * @param out output stream (should be created using {@link org.jboss.cache.marshall.ObjectSerializationFactory}
+ * @throws Exception
+ */
public void objectToObjectStream(Object obj, ObjectOutputStream out) throws Exception
{
+ //defaultMarshaller.objectToObjectStream(obj, out);
+ out.writeShort(versionInt);
+ if (log.isTraceEnabled()) log.trace("Wrote version " + versionInt);
+ //now marshall the contents of the object
defaultMarshaller.objectToObjectStream(obj, out);
+// out.close();
}
public Object objectFromObjectStream(ObjectInputStream in) throws Exception
{
- return objectFromStream(in);
+ Marshaller marshaller;
+ int versionId;
+ try
+ {
+ // just a peek - does not actually "remove" these bytes from the stream.
+ // create an input stream and read the first short
+ versionId = in.readShort();
+ if (log.isTraceEnabled()) log.trace("Read version " + versionId);
+ }
+ catch (Exception e)
+ {
+ log.error("Unable to read version id from first two bytes of stream, barfing.");
+ throw e;
+ }
+
+ marshaller = getMarshaller(versionId);
+
+ return marshaller.objectFromObjectStream(in);
}
/**
1.10 +13 -2 JBossCache/src/org/jboss/cache/marshall/Marshaller.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Marshaller.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/marshall/Marshaller.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- Marshaller.java 15 Jan 2007 18:10:56 -0000 1.9
+++ Marshaller.java 17 Jan 2007 14:13:06 -0000 1.10
@@ -1,5 +1,6 @@
package org.jboss.cache.marshall;
+import org.jboss.cache.Fqn;
import org.jgroups.blocks.RpcDispatcher;
import java.io.InputStream;
@@ -12,7 +13,7 @@
* The reason why this is implemented specially in JBoss Cache rather than resorting to
* Java serialization or even the more efficient JBoss serialization is that a lot of efficiency
* can be gained when a majority of the serialization that occurs has to do with a small set
- * of known types such as {@link Fqn} or {@link MethodCall}.
+ * of known types such as {@link org.jboss.cache.Fqn} or {@link MethodCall}.
* <p/>
* Unknown types (typically user data} falls back to JBoss serialization.
* <p/>
@@ -23,7 +24,7 @@
* This interface implements the JGroups building-block interface {@link org.jgroups.blocks.RpcDispatcher.Marshaller} which
* is used to marshall {@link MethodCall}s, their parameters and their response values.
* <p/>
- * The interface is also used by the {@link CacheLoader} framework to efficiently serialize data to be persisted, as well as
+ * The interface is also used by the {@link org.jboss.cache.loader.CacheLoader} framework to efficiently serialize data to be persisted, as well as
* the {@link org.jboss.cache.statetransfer.StateTransferManager} when serializing the cache for transferring state en-masse.
*
* @author <a href="mailto://manik@jboss.org">Manik Surtani</a>
@@ -58,6 +59,16 @@
Object objectFromStream(InputStream is) throws Exception;
/**
+ * Overloaded form of {@link #objectToObjectStream(Object,java.io.ObjectOutputStream)} which adds a hint to the Fqn region
+ *
+ * @param obj object to marshall
+ * @param region fqn that this object pertains to
+ * @param out stream to marshall to
+ * @throws Exception
+ */
+ void objectToObjectStream(Object obj, ObjectOutputStream out, Fqn region) throws Exception;
+
+ /**
* Tests whether a particular Fqn passed in is inactive.
*
* @param fqn
1.2 +0 -0 JBossCache/src/org/jboss/cache/marshall/AbstractMarshaller.java
(In the diff below, changes in quantity of whitespace are not shown.)
1.2 +5 -0 JBossCache/src/org/jboss/cache/marshall/NodeDataExceptionMarker.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: NodeDataExceptionMarker.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/marshall/NodeDataExceptionMarker.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- NodeDataExceptionMarker.java 15 Jan 2007 16:19:09 -0000 1.1
+++ NodeDataExceptionMarker.java 17 Jan 2007 14:13:06 -0000 1.2
@@ -50,4 +50,9 @@
cause = (Throwable) in.readObject();
cacheNodeIdentity = in.readObject();
}
+
+ public String toString()
+ {
+ return "NodeDataExceptionMarker";
+ }
}
1.2 +5 -0 JBossCache/src/org/jboss/cache/marshall/NodeDataMarker.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: NodeDataMarker.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/marshall/NodeDataMarker.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- NodeDataMarker.java 15 Jan 2007 16:19:09 -0000 1.1
+++ NodeDataMarker.java 17 Jan 2007 14:13:06 -0000 1.2
@@ -14,4 +14,9 @@
{
return true;
}
+
+ public String toString()
+ {
+ return "NodeDataMarker";
+ }
}
1.2 +2 -2 JBossCache/src/org/jboss/cache/marshall/NodeData.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: NodeData.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/marshall/NodeData.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- NodeData.java 15 Jan 2007 16:19:09 -0000 1.1
+++ NodeData.java 17 Jan 2007 14:13:06 -0000 1.2
@@ -12,7 +12,7 @@
* 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 $
+ * @version $Id: NodeData.java,v 1.2 2007/01/17 14:13:06 msurtani Exp $
*/
public class NodeData implements Externalizable
{
@@ -87,7 +87,7 @@
public String toString()
{
- return "fqn: " + fqn + ", attrs=" + attrs;
+ return "NodeData {fqn: " + fqn + ", attrs=" + attrs + "}";
}
}
1.7 +76 -46 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.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- CacheMarshaller200.java 15 Jan 2007 18:10:56 -0000 1.6
+++ CacheMarshaller200.java 17 Jan 2007 14:13:06 -0000 1.7
@@ -64,6 +64,7 @@
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_SHORT = 20;
protected static final int MAGICNUMBER_NULL = 99;
protected static final int MAGICNUMBER_SERIALIZABLE = 100;
protected static final int MAGICNUMBER_REF = 101;
@@ -97,59 +98,86 @@
public void objectToObjectStream(Object o, ObjectOutputStream out) throws Exception
{
- if (log.isTraceEnabled()) log.trace("Marshalling object " + o);
- Map<Object, Integer> refMap = new HashMap<Object, Integer>();
-
if (useRegionBasedMarshalling)
{
- String fqnAsString = null;
- if (isReturnValue(o))
+ String fqnAsString;
+ if (o == null)
+ {
+ // if the return value we're trying to marshall is null we're easy ...
+ fqnAsString = null;
+ }
+ else 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.
+ // we are marshalling a return value from a method call.
+ // let's see if an incoming unmarshalling call for this exists, in the same thread stack 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)
+ }
+ else
{
- // first publish the region
- marshallObject(fqnAsString, out, refMap);
+ // this is an outgoing method call.
+ // we first marshall the Fqn as a String
+ MethodCall call = (MethodCall) o;
+ fqnAsString = extractFqnAsString(call);
+ }
+ if (log.isTraceEnabled()) log.trace("Region based call. Using region " + fqnAsString);
+ objectToObjectStream(o, out, fqnAsString);
}
else
{
- // marshall another null as region
- marshallObject(null, out, refMap);
+ // not region based!
+ if (log.isTraceEnabled()) log.trace("Marshalling object " + o);
+ Map<Object, Integer> refMap = new HashMap<Object, Integer>();
+ marshallObject(o, out, refMap);
+ }
}
+
+ public Object objectFromObjectStream(ObjectInputStream in) throws Exception
+ {
+ if (useRegionBasedMarshalling)
+ {
+ return objectFromObjectStreamRegionBased(in);
}
else
{
- // this is an outgoing call.
- // we first marshall the Fqn as a String (ugh!)
- MethodCall call = (MethodCall) o;
- fqnAsString = extractFqnAsString(call);
- marshallObject(fqnAsString, out, refMap);
+ Map<Integer, Object> refMap = new HashMap<Integer, Object>();
+ Object retValue = unmarshallObject(in, refMap);
+ if (log.isTraceEnabled()) log.trace("Unmarshalled object " + retValue);
+ return retValue;
}
}
+ public void objectToObjectStream(Object obj, ObjectOutputStream out, Fqn region) throws Exception
+ {
+ objectToObjectStream(obj, out, region.toString());
+ }
+
+ protected void objectToObjectStream(Object o, ObjectOutputStream out, String region) throws Exception
+ {
+ if (log.isTraceEnabled()) log.trace("Marshalling object " + o);
+ Map<Object, Integer> refMap = new HashMap<Object, Integer>();
+ if (useRegionBasedMarshalling) // got to check again in case this meth is called directly
+ {
+ log.trace("Writing region to stream");
+ marshallObject(region, out, refMap);
+ }
marshallObject(o, out, refMap);
}
- public Object objectFromObjectStream(ObjectInputStream in) throws Exception
+ protected Object objectFromObjectStreamRegionBased(ObjectInputStream in) throws Exception
{
- Object retValue;
Map<Integer, Object> refMap = new HashMap<Integer, Object>();
+ String regionString = (String) unmarshallObject(in, refMap);
+ if (log.isTraceEnabled()) log.trace("Unmarshalled regionString " + regionString + " from stream");
- 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)
+ Object retValue;
+
+ if (regionString != null)
{
- region = findRegion(fqn);
+ region = findRegion(regionString);
}
if (region == null)
{
@@ -158,15 +186,11 @@
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
- {
- retValue = unmarshallObject(in, refMap);
+ if (!isReturnValue(retValue)) regionForCall.set(regionString);
}
-
+ if (log.isTraceEnabled()) log.trace("Unmarshalled object " + retValue);
return retValue;
}
@@ -317,6 +341,11 @@
out.writeByte(MAGICNUMBER_LONG);
out.writeLong(((Long) o).longValue());
}
+ else if (o instanceof Short)
+ {
+ out.writeByte(MAGICNUMBER_SHORT);
+ out.writeShort(((Short) o).shortValue());
+ }
else if (o instanceof String)
{
int refId = createReference(o, refMap);
@@ -527,8 +556,9 @@
case MAGICNUMBER_INTEGER:
return in.readInt();
case MAGICNUMBER_LONG:
- retVal = in.readLong();
- return retVal;
+ return in.readLong();
+ case MAGICNUMBER_SHORT:
+ return in.readShort();
case MAGICNUMBER_STRING:
reference = (int) in.readShort();
retVal = unmarshallString(in);
More information about the jboss-cvs-commits
mailing list