1 6 7 package org.jfox.jms.message; 8 9 import java.io.ByteArrayInputStream ; 10 import java.io.ByteArrayOutputStream ; 11 import java.io.IOException ; 12 import java.io.InvalidClassException ; 13 import java.io.NotSerializableException ; 14 import java.io.ObjectInputStream ; 15 import java.io.ObjectOutputStream ; 16 import java.io.Serializable ; 17 import javax.jms.JMSException ; 18 import javax.jms.MessageFormatException ; 19 import javax.jms.MessageNotWriteableException ; 20 import javax.jms.ObjectMessage ; 21 22 35 36 public class ObjectMessageImpl 37 extends JMSMessage 38 implements ObjectMessage , Serializable { 39 40 private byte[] body; 41 42 45 public ObjectMessageImpl() { 46 super(); 47 } 48 49 65 public void setObject(Serializable object) throws JMSException { 66 if (isBodyModifiable()) { 67 try { 68 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 69 ObjectOutputStream oos = new ObjectOutputStream (baos); 70 oos.writeObject(object); 71 oos.flush(); 72 oos.close(); 73 body = baos.toByteArray(); 74 } catch (InvalidClassException e) { 75 throw new MessageFormatException (e.getMessage()); 76 } catch (NotSerializableException e) { 77 throw new MessageFormatException (e.getMessage()); 78 } catch (IOException e) { 79 throw new MessageFormatException (e.getMessage()); 80 } 81 } else { 82 throw new MessageNotWriteableException ("ObjectMessage body read_only"); 83 } 84 } 85 86 97 public Serializable getObject() throws JMSException { 98 Serializable object = null; 99 100 if (body != null) { 101 try { 102 ByteArrayInputStream bais = new ByteArrayInputStream (body); 103 ObjectInputStream ins = new ObjectInputStream (bais); 104 object = (Serializable ) ins.readObject(); 105 ins.close(); 106 } catch (ClassNotFoundException e) { 107 throw new MessageFormatException (e.getMessage()); 108 } catch (InvalidClassException e) { 109 throw new MessageFormatException (e.getMessage()); 110 } catch (IOException e) { 111 throw new MessageFormatException (e.getMessage()); 112 } 113 } 114 return object; 115 } 116 117 123 public void clearBody() throws JMSException { 124 super.clearBody(); 125 body = null; 126 } 127 } 128 | Popular Tags |