1 22 package org.jboss.mq; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.Externalizable ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.ObjectInput ; 30 import java.io.ObjectInputStream ; 31 import java.io.ObjectOutput ; 32 import java.io.ObjectOutputStream ; 33 import java.io.ObjectStreamClass ; 34 import java.io.Serializable ; 35 36 import javax.jms.JMSException ; 37 import javax.jms.MessageFormatException ; 38 import javax.jms.MessageNotWriteableException ; 39 import javax.jms.ObjectMessage ; 40 41 import org.jboss.util.Classes; 42 43 50 public class SpyObjectMessage extends SpyMessage implements ObjectMessage , Externalizable 51 { 52 54 55 private final static long serialVersionUID = 8809953915407712952L; 56 57 59 60 boolean isByteArray = false; 61 62 byte[] objectBytes = null; 63 64 66 68 70 72 public void setObject(Serializable object) throws JMSException 73 { 74 if (header.msgReadOnly) 75 { 76 throw new MessageNotWriteableException ("setObject"); 77 } 78 if (object == null) 79 { 80 objectBytes = null; 81 return; 82 } 83 try 84 { 85 if (object instanceof byte[]) 86 { 87 isByteArray = true; 89 objectBytes = new byte[((byte[]) object).length]; 90 System.arraycopy(object, 0, objectBytes, 0, objectBytes.length); 91 } 92 else 93 { 94 isByteArray = false; 95 ByteArrayOutputStream byteArray = new ByteArrayOutputStream (); 96 ObjectOutputStream objectOut = new ObjectOutputStream (byteArray); 97 objectOut.writeObject(object); 98 objectBytes = byteArray.toByteArray(); 99 objectOut.close(); 100 } 101 } 102 catch (IOException e) 103 { 104 MessageFormatException mfe = new MessageFormatException ("Object cannot be serialized: " + e.getMessage()); 105 mfe.setLinkedException(e); 106 throw mfe; 107 } 108 } 109 110 public Serializable getObject() throws JMSException 111 { 112 113 Serializable retVal = null; 114 try 115 { 116 if (null != objectBytes) 117 { 118 if (isByteArray) 119 { 120 retVal = new byte[objectBytes.length]; 121 System.arraycopy(objectBytes, 0, retVal, 0, objectBytes.length); 122 } 123 else 124 { 125 126 134 class ObjectInputStreamExt extends ObjectInputStream 135 { 136 ObjectInputStreamExt(InputStream is) throws IOException 137 { 138 super(is); 139 } 140 141 protected Class resolveClass(ObjectStreamClass v) throws IOException , ClassNotFoundException 142 { 143 return Classes.loadClass(v.getName()); 144 } 145 } 146 ObjectInputStream input = new ObjectInputStreamExt(new ByteArrayInputStream (objectBytes)); 147 retVal = (Serializable ) input.readObject(); 148 input.close(); 149 } 150 } 151 } 152 catch (ClassNotFoundException e) 153 { 154 throw new MessageFormatException ("ClassNotFoundException: " + e.getMessage()); 155 } 156 catch (IOException e) 157 { 158 throw new MessageFormatException ("IOException: " + e.getMessage()); 159 } 160 return retVal; 161 } 162 163 165 public void clearBody() throws JMSException 166 { 167 objectBytes = null; 168 super.clearBody(); 169 } 170 171 public SpyMessage myClone() throws JMSException 172 { 173 SpyObjectMessage result = MessagePool.getObjectMessage(); 174 result.copyProps(this); 175 result.isByteArray = this.isByteArray; 176 if (objectBytes != null) 177 { 178 result.objectBytes = new byte[this.objectBytes.length]; 179 System.arraycopy(this.objectBytes, 0, result.objectBytes, 0, this.objectBytes.length); 180 } 181 return result; 182 } 183 184 186 public void writeExternal(ObjectOutput out) throws IOException 187 { 188 super.writeExternal(out); 189 out.writeBoolean(isByteArray); 190 if (objectBytes == null) 191 { 192 out.writeInt(-1); 193 } 194 else 195 { 196 out.writeInt(objectBytes.length); 197 out.write(objectBytes); 198 } 199 } 200 201 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 202 { 203 super.readExternal(in); 204 isByteArray = in.readBoolean(); 205 int length = in.readInt(); 206 if (length < 0) 207 { 208 objectBytes = null; 209 } 210 else 211 { 212 objectBytes = new byte[length]; 213 in.readFully(objectBytes); 214 } 215 } 216 217 219 221 223 } 225 | Popular Tags |