1 4 package com.tc.object.msg; 5 6 import com.tc.bytes.TCByteBuffer; 7 import com.tc.exception.TCRuntimeException; 8 import com.tc.io.TCByteBufferInputStream; 9 import com.tc.io.TCByteBufferOutput; 10 import com.tc.io.TCSerializable; 11 import com.tc.net.protocol.tcm.MessageChannel; 12 import com.tc.net.protocol.tcm.MessageMonitor; 13 import com.tc.net.protocol.tcm.TCMessageHeader; 14 import com.tc.net.protocol.tcm.TCMessageType; 15 import com.tc.object.session.SessionID; 16 17 import java.io.ByteArrayInputStream ; 18 import java.io.ByteArrayOutputStream ; 19 import java.io.IOException ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.io.Serializable ; 23 24 public class JMXMessage extends DSOMessageBase implements TCSerializable { 25 26 private static final byte JMX_OBJECT = 0; 27 private Object jmxObject; 28 29 public JMXMessage(MessageMonitor monitor, TCByteBufferOutput out, MessageChannel channel, TCMessageType type) { 30 super(monitor, out, channel, type); 31 } 32 33 public JMXMessage(SessionID sessionID, MessageMonitor monitor, MessageChannel channel, TCMessageHeader header, 34 TCByteBuffer[] data) { 35 super(sessionID, monitor, channel, header, data); 36 } 37 38 protected void dehydrateValues() { 39 putNVPair(JMX_OBJECT, this); 40 } 41 42 protected boolean hydrateValue(byte name) throws IOException { 43 switch (name) { 44 case JMX_OBJECT: 45 jmxObject = getObject(this); 46 return true; 47 default: 48 return false; 49 } 50 } 51 52 public Object getJMXObject() { 53 return jmxObject; 54 } 55 56 public void setJMXObject(Serializable jmxObject) { 57 this.jmxObject = jmxObject; 58 } 59 60 public void serializeTo(TCByteBufferOutput serialOutput) { 61 try { 62 ByteArrayOutputStream bao = new ByteArrayOutputStream (1024); 63 ObjectOutputStream oos = new ObjectOutputStream (bao); 64 oos.writeObject(jmxObject); 65 oos.close(); 66 67 byte serializedObject[] = bao.toByteArray(); 68 serialOutput.writeInt(serializedObject.length); 69 serialOutput.write(serializedObject); 70 } catch (IOException e) { 71 e.printStackTrace(); 72 throw new TCRuntimeException(e); 73 } 74 } 75 76 public Object deserializeFrom(TCByteBufferInputStream serialInput) throws IOException { 77 try { 78 int length = serialInput.readInt(); 79 byte serializedObject[] = new byte[length]; 80 serialInput.read(serializedObject); 81 ByteArrayInputStream bis = new ByteArrayInputStream (serializedObject); 82 ObjectInputStream ois = new ObjectInputStream (bis); 83 return ois.readObject(); 84 } catch (IOException e) { 85 throw e; 86 } catch (ClassNotFoundException e) { 87 e.printStackTrace(); 88 throw new TCRuntimeException(e); 89 } 90 } 91 92 } 93 | Popular Tags |