1 21 package com.presumo.jms.message; 22 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.DataInput ; 26 import java.io.DataOutput ; 27 import java.io.IOException ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.io.Serializable ; 31 32 import javax.jms.ObjectMessage ; 33 import javax.jms.JMSException ; 34 import javax.jms.MessageFormatException ; 35 import javax.jms.MessageNotWriteableException ; 36 37 42 public final class JmsObjectMessage extends JmsMessage 43 implements ObjectMessage , java.io.Externalizable 44 { 45 private byte [] data; 49 50 public JmsObjectMessage() 54 { 55 super(); 56 } 57 public JmsObjectMessage(String name) 58 { 59 super(name); 60 } 61 62 public JmsObjectMessage(byte [] msg, int offset, int length) 63 throws IOException 64 { 65 super(msg, offset, length); 66 } 67 68 72 75 public void setObject(Serializable object) throws JMSException 76 { 77 checkWrite(); 78 79 try { 80 81 ByteArrayOutputStream byteStream = new ByteArrayOutputStream (); 82 ObjectOutputStream objectStream = new ObjectOutputStream (byteStream); 83 objectStream.writeObject(object); 84 objectStream.flush(); 85 objectStream.close(); 86 this.data = byteStream.toByteArray(); 87 88 } catch (IOException e) { 89 JMSException jmse = new MessageFormatException ("Unable to serialize object."); 90 jmse.setLinkedException(e); 91 throw jmse; 92 } 93 } 94 95 96 99 public Serializable getObject() throws JMSException 100 { 101 if (data == null || data.length == 0) { 102 return null; 103 } 104 105 Serializable retval = null; 106 try { 107 108 ByteArrayInputStream byteStream = new ByteArrayInputStream (data); 109 ObjectInputStream objectStream = new ObjectInputStream (byteStream); 110 retval = (Serializable ) objectStream.readObject(); 111 objectStream.close(); 112 113 } catch (IOException e) { 114 JMSException jmse = new MessageFormatException ("Unable to deserialize object."); 115 jmse.setLinkedException(e); 116 throw jmse; 117 } catch (ClassNotFoundException e) { 118 JMSException jmse = new MessageFormatException ("Unable to deserialize object."); 119 jmse.setLinkedException(e); 120 throw jmse; 121 } 122 return retval; 123 } 124 125 128 public void clearBody() throws JMSException 129 { 130 checkWrite(); 131 super.clearBody(); 132 this.data = null; 133 } 134 135 136 138 public void unmarshal(DataInput in) throws IOException 139 { 140 super.unmarshal(in); 141 142 int arraySize = in.readInt(); 143 if (arraySize > 0) { 144 this.data = new byte[arraySize]; 145 in.readFully(data, 0, arraySize); 146 } 147 } 148 149 151 public void marshal(DataOutput out) throws IOException 152 { 153 super.marshal(out); 154 155 if (data == null || data.length == 0) 156 out.writeInt(0); 157 else { 158 out.writeInt(data.length); 159 out.write(data); 160 } 161 } 162 163 public String toString() 164 { 165 StringBuffer result = new StringBuffer (); 166 result.append(super.toString()); 167 if (data == null) { 168 result.append(" No serialized object "); 169 } else { 170 result.append(" Serialized object size: " + data.length); 171 } 172 return result.toString(); 173 } 174 175 179 180 final byte getMarshalingID() 181 { 182 return MessageEncoder.OBJECT_MESSAGE; 183 } 184 185 } 186 187 | Popular Tags |