001: package org.mockejb.jms;
002:
003: import java.io.*;
004: import javax.jms.*;
005:
006: /**
007: * <code>ObjectMessage</code> implementation.
008: * @author Dimitar Gospodinov
009: * @see javax.jms.ObjectMessage
010: */
011: public class ObjectMessageImpl extends MessageImpl implements
012: ObjectMessage {
013:
014: private byte[] serializedObject = null;
015:
016: /**
017: * Creates empty <code>ObjectMessageImpl</code>.
018: */
019: public ObjectMessageImpl() {
020: super ();
021: }
022:
023: /**
024: * Creates new <code>ObjectMessageImpl</code> and sets its body to
025: * <code>object</code>
026: * @param object
027: * @throws JMSException
028: */
029: public ObjectMessageImpl(Serializable object) throws JMSException {
030: setObject(object);
031: }
032:
033: /**
034: * Creates new <code>ObjectMessageImpl</code> and sets its header, properties
035: * and body to the corresponded values from <code>msg</code>
036: * @param msg
037: * @throws JMSException
038: */
039: public ObjectMessageImpl(ObjectMessage msg) throws JMSException {
040: super (msg);
041: setObject(msg.getObject());
042: }
043:
044: /**
045: * @see javax.jms.ObjectMessage#setObject(java.io.Serializable)
046: */
047: public void setObject(Serializable object) throws JMSException {
048: checkBodyWriteable();
049: try {
050: ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
051: ObjectOutputStream objectOut = new ObjectOutputStream(
052: bytesOut);
053: objectOut.writeObject(object);
054: objectOut.flush();
055: objectOut.close();
056: serializedObject = bytesOut.toByteArray();
057: bytesOut.close();
058: } catch (IOException ex) {
059: throw new JMSException(ex.getMessage());
060: }
061: }
062:
063: /**
064: * @see javax.jms.ObjectMessage#getObject()
065: */
066: public Serializable getObject() throws JMSException {
067: if (serializedObject == null) {
068: return null;
069: }
070: Serializable result;
071: try {
072: ByteArrayInputStream bytesIn = new ByteArrayInputStream(
073: serializedObject);
074: ObjectInputStream objectIn = new ObjectInputStream(bytesIn);
075: result = (Serializable) objectIn.readObject();
076: objectIn.close();
077: bytesIn.close();
078: return result;
079: } catch (IOException ex) {
080: throw new JMSException(ex.getMessage());
081: } catch (ClassNotFoundException ex) {
082: throw new JMSException(ex.getMessage());
083: }
084: }
085:
086: /**
087: * @see javax.jms.ObjectMessage#clearBody()
088: */
089: public void clearBody() throws JMSException {
090: super .clearBody();
091: serializedObject = null;
092: }
093:
094: // Non-standard methods
095:
096: /**
097: * Sets message body in read-only mode.
098: * @throws JMSException
099: */
100: void resetBody() throws JMSException {
101: setBodyReadOnly();
102: }
103:
104: }
|