package org.jboss.mq;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import javax.jms.JMSException;
import javax.jms.MessageFormatException;
import javax.jms.MessageNotWriteableException;
import javax.jms.ObjectMessage;
import org.jboss.util.Classes;
public class SpyObjectMessage extends SpyMessage implements ObjectMessage, Externalizable
{
private final static long serialVersionUID = 8809953915407712952L;
boolean isByteArray = false;
byte[] objectBytes = null;
public void setObject(Serializable object) throws JMSException
{
if (header.msgReadOnly)
{
throw new MessageNotWriteableException("setObject");
}
if (object == null)
{
objectBytes = null;
return;
}
try
{
if (object instanceof byte[])
{
isByteArray = true;
objectBytes = new byte[((byte[]) object).length];
System.arraycopy(object, 0, objectBytes, 0, objectBytes.length);
}
else
{
isByteArray = false;
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(byteArray);
objectOut.writeObject(object);
objectBytes = byteArray.toByteArray();
objectOut.close();
}
}
catch (IOException e)
{
throw new MessageFormatException("Object cannot be serialized");
}
}
public Serializable getObject() throws JMSException
{
Serializable retVal = null;
try
{
if (null != objectBytes)
{
if (isByteArray)
{
retVal = new byte[objectBytes.length];
System.arraycopy(objectBytes, 0, retVal, 0, objectBytes.length);
}
else
{
class ObjectInputStreamExt extends ObjectInputStream
{
ObjectInputStreamExt(InputStream is) throws IOException
{
super(is);
}
protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException
{
return Classes.loadClass(v.getName());
}
}
ObjectInputStream input = new ObjectInputStreamExt(new ByteArrayInputStream(objectBytes));
retVal = (Serializable) input.readObject();
input.close();
}
}
}
catch (ClassNotFoundException e)
{
throw new MessageFormatException("ClassNotFoundException: " + e.getMessage());
}
catch (IOException e)
{
throw new MessageFormatException("IOException: " + e.getMessage());
}
return retVal;
}
public void clearBody() throws JMSException
{
objectBytes = null;
super.clearBody();
}
public SpyMessage myClone() throws JMSException
{
SpyObjectMessage result = MessagePool.getObjectMessage();
result.copyProps(this);
result.isByteArray = this.isByteArray;
if (objectBytes != null)
{
result.objectBytes = new byte[this.objectBytes.length];
System.arraycopy(this.objectBytes, 0, result.objectBytes, 0, this.objectBytes.length);
}
return result;
}
public void writeExternal(ObjectOutput out) throws IOException
{
super.writeExternal(out);
out.writeBoolean(isByteArray);
if (objectBytes == null)
{
out.writeInt(-1);
}
else
{
out.writeInt(objectBytes.length);
out.write(objectBytes);
}
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
super.readExternal(in);
isByteArray = in.readBoolean();
int length = in.readInt();
if (length < 0)
{
objectBytes = null;
}
else
{
objectBytes = new byte[length];
in.readFully(objectBytes);
}
}
}