KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > msg > JMXMessage


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.io.Serializable JavaDoc;
23
24 public class JMXMessage extends DSOMessageBase implements TCSerializable {
25
26   private static final byte JMX_OBJECT = 0;
27   private Object JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc bao = new ByteArrayOutputStream JavaDoc(1024);
63       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(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 JavaDoc e) {
71       e.printStackTrace();
72       throw new TCRuntimeException(e);
73     }
74   }
75
76   public Object JavaDoc deserializeFrom(TCByteBufferInputStream serialInput) throws IOException JavaDoc {
77     try {
78       int length = serialInput.readInt();
79       byte serializedObject[] = new byte[length];
80       serialInput.read(serializedObject);
81       ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(serializedObject);
82       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bis);
83       return ois.readObject();
84     } catch (IOException JavaDoc e) {
85       throw e;
86     } catch (ClassNotFoundException JavaDoc e) {
87       e.printStackTrace();
88       throw new TCRuntimeException(e);
89     }
90   }
91
92 }
93
Popular Tags