package org.mockejb.jms;
import java.io.*;
import javax.jms.*;
public class ObjectMessageImpl extends MessageImpl implements ObjectMessage {
private byte[] serializedObject = null;
public ObjectMessageImpl() {
super();
}
public ObjectMessageImpl(Serializable object) throws JMSException {
setObject(object);
}
public ObjectMessageImpl(ObjectMessage msg) throws JMSException {
super(msg);
setObject(msg.getObject());
}
public void setObject(Serializable object) throws JMSException {
checkBodyWriteable();
try {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(bytesOut);
objectOut.writeObject(object);
objectOut.flush();
objectOut.close();
serializedObject = bytesOut.toByteArray();
bytesOut.close();
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
}
}
public Serializable getObject() throws JMSException {
if (serializedObject == null) {
return null;
}
Serializable result;
try {
ByteArrayInputStream bytesIn =
new ByteArrayInputStream(serializedObject);
ObjectInputStream objectIn = new ObjectInputStream(bytesIn);
result = (Serializable) objectIn.readObject();
objectIn.close();
bytesIn.close();
return result;
} catch (IOException ex) {
throw new JMSException(ex.getMessage());
} catch (ClassNotFoundException ex) {
throw new JMSException(ex.getMessage());
}
}
public void clearBody() throws JMSException {
super.clearBody();
serializedObject = null;
}
void resetBody() throws JMSException {
setBodyReadOnly();
}
}