1 46 package org.mr.api.jms; 47 48 62 63 import java.io.IOException ; 64 import java.io.Serializable ; 65 import javax.jms.JMSException ; 66 import javax.jms.MessageFormatException ; 67 import javax.jms.MessageNotWriteableException ; 68 69 import javax.jms.ObjectMessage ; 70 import java.io.ByteArrayInputStream ; 71 import java.io.ByteArrayOutputStream ; 72 import java.io.ObjectInputStream ; 73 import java.io.ObjectOutputStream ; 74 75 import org.mr.core.util.byteable.Byteable; 76 import org.mr.core.util.byteable.ByteableInputStream; 77 import org.mr.core.util.byteable.ByteableOutputStream; 78 import org.mr.core.util.byteable.ByteableRegistry; 79 80 81 82 public class MantaObjectMessage extends MantaMessage implements ObjectMessage { 83 84 87 private static final long serialVersionUID = 7209547801089501407L; 88 private byte[] objectAsBytes = null; 90 92 95 MantaObjectMessage(){ 96 97 } 98 99 104 MantaObjectMessage(MantaSession sess) throws JMSException { 105 creatingSession = sess; 106 flags = (flags & ONLY_MSG_TYPES) | OBJECT_MESSAGE; 107 } 109 117 MantaObjectMessage(MantaSession sess, Serializable obj) throws JMSException { 118 this(sess); 119 this.setObject(obj); 120 } 121 125 public void setObject(Serializable object) throws JMSException { 126 if (!isWriteable()) 127 throw new MessageNotWriteableException ("MNJMS00015 : FAILED ON METHOD setObject(). MESSAGE IS NOT IN A WRITEABLE STATE."); 128 129 makeObject(object); 130 131 } 132 133 private void makeObject(Serializable object) throws JMSException { 134 try { 135 ByteArrayOutputStream byteOutput = new ByteArrayOutputStream (); 136 ObjectOutputStream objectOutput = new ObjectOutputStream (byteOutput); 137 objectOutput.writeObject(object); 138 objectOutput.flush(); 139 objectAsBytes = byteOutput.toByteArray(); 140 objectOutput.close(); 141 } 142 catch (IOException ioe) { 143 throw new MessageFormatException ("MNJMS00016 : FAILED TO INSTANTIATE OBJECT ON METHOD makeObject(). ERROR TEXT : "+ioe.getMessage()); 144 145 } 146 } 147 148 151 public Serializable getObject() throws JMSException { 152 Serializable returnObj = null; 153 if (this.objectAsBytes!=null) { 155 try { 156 ByteArrayInputStream byteInput = 157 new ByteArrayInputStream (objectAsBytes); 158 ObjectInputStream objectInput = new ObjectInputStream (byteInput); 159 returnObj = (Serializable ) objectInput.readObject(); 160 objectInput.close(); 161 } 162 catch (Exception ie) { 163 throw new MessageFormatException ("MNJMS00017 : FAILED ON METHOD getObject(). ERROR TEXT : "+ie.getMessage()); 164 165 } 166 } 167 168 return returnObj; 169 } 171 172 175 public final void clearBody() throws JMSException { 176 if (objectAsBytes==null) 177 return; 178 179 super.clearBody(); 180 flags=flags|ORIG_BODY_SAVED; 185 objectAsBytes = null; 187 } 189 190 192 public void toBytes(ByteableOutputStream out) throws IOException { 193 super.toBytes(out); 194 if (objectAsBytes==null) 195 out.writeInt(0); 196 else { 197 out.writeInt(this.objectAsBytes.length); 198 out.write(this.objectAsBytes); 199 } 200 out.flush(); 201 } 202 203 public Byteable createInstance(ByteableInputStream in) throws IOException { 204 205 MantaObjectMessage com; 206 try { 207 208 com = new MantaObjectMessage(null); 209 createHeadersAndProperties(com,in); 210 int byteArrayLength = in.readInt(); 211 byte[] objectBytes=null; 212 if (byteArrayLength>0) { 213 objectBytes = new byte[byteArrayLength]; 214 in.read(objectBytes); 215 ByteArrayInputStream baos = new ByteArrayInputStream (objectBytes); 216 ObjectInputStream ois = new ObjectInputStream (baos); 217 Serializable theObj = (Serializable ) ois.readObject(); 218 com.makeObject(theObj); 219 } 220 else 221 com.objectAsBytes=null; 222 223 } 224 catch (Exception ie) { 225 throw new IOException ("MNJMS00FFC : METHOD createInstance() FAILED ON MantaObjectMessage. EXCEPTION TEXT : "+ie.getMessage()); 226 } 227 228 return com; 229 } 230 231 public void registerToByteableRegistry() { 232 ByteableRegistry.registerByteableFactory(getByteableName() , this); 233 } 234 235 public final static String BYTEABLENAME = "MantaObjectMsg"; 236 237 public String getByteableName() { 238 239 return BYTEABLENAME; 240 } 241 242 public static void register() throws JMSException { 243 MantaObjectMessage instance = new MantaObjectMessage(null); 244 instance.registerToByteableRegistry(); 245 } 246 247 260 261 262 264 public MantaMessage makeCopy() throws JMSException { 265 MantaObjectMessage copy = new MantaObjectMessage(); 266 initCopy(copy); 267 copy.objectAsBytes = objectAsBytes; 269 270 return copy; 271 } 272 274 } | Popular Tags |