01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.object.msg;
06:
07: import com.tc.bytes.TCByteBuffer;
08: import com.tc.exception.TCRuntimeException;
09: import com.tc.io.TCByteBufferInput;
10: import com.tc.io.TCByteBufferOutput;
11: import com.tc.io.TCSerializable;
12: import com.tc.net.protocol.tcm.MessageChannel;
13: import com.tc.net.protocol.tcm.MessageMonitor;
14: import com.tc.net.protocol.tcm.TCMessageHeader;
15: import com.tc.net.protocol.tcm.TCMessageType;
16: import com.tc.object.session.SessionID;
17:
18: import java.io.ByteArrayInputStream;
19: import java.io.ByteArrayOutputStream;
20: import java.io.IOException;
21: import java.io.ObjectInputStream;
22: import java.io.ObjectOutputStream;
23: import java.io.Serializable;
24:
25: public class JMXMessage extends DSOMessageBase implements
26: TCSerializable {
27:
28: private static final byte JMX_OBJECT = 0;
29: private Object jmxObject;
30:
31: public JMXMessage(SessionID sessionID, MessageMonitor monitor,
32: TCByteBufferOutput out, MessageChannel channel,
33: TCMessageType type) {
34: super (sessionID, monitor, out, channel, type);
35: }
36:
37: public JMXMessage(SessionID sessionID, MessageMonitor monitor,
38: MessageChannel channel, TCMessageHeader header,
39: TCByteBuffer[] data) {
40: super (sessionID, monitor, channel, header, data);
41: }
42:
43: protected void dehydrateValues() {
44: putNVPair(JMX_OBJECT, this );
45: }
46:
47: protected boolean hydrateValue(byte name) throws IOException {
48: switch (name) {
49: case JMX_OBJECT:
50: jmxObject = getObject(this );
51: return true;
52: default:
53: return false;
54: }
55: }
56:
57: public Object getJMXObject() {
58: return jmxObject;
59: }
60:
61: public void setJMXObject(Serializable jmxObject) {
62: this .jmxObject = jmxObject;
63: }
64:
65: public void serializeTo(TCByteBufferOutput serialOutput) {
66: try {
67: ByteArrayOutputStream bao = new ByteArrayOutputStream(1024);
68: ObjectOutputStream oos = new ObjectOutputStream(bao);
69: oos.writeObject(jmxObject);
70: oos.close();
71:
72: byte serializedObject[] = bao.toByteArray();
73: serialOutput.writeInt(serializedObject.length);
74: serialOutput.write(serializedObject);
75: } catch (IOException e) {
76: e.printStackTrace();
77: throw new TCRuntimeException(e);
78: }
79: }
80:
81: public Object deserializeFrom(TCByteBufferInput serialInput)
82: throws IOException {
83: try {
84: int length = serialInput.readInt();
85: byte serializedObject[] = new byte[length];
86: serialInput.read(serializedObject);
87: ByteArrayInputStream bis = new ByteArrayInputStream(
88: serializedObject);
89: ObjectInputStream ois = new ObjectInputStream(bis);
90: return ois.readObject();
91: } catch (IOException e) {
92: throw e;
93: } catch (ClassNotFoundException e) {
94: e.printStackTrace();
95: throw new TCRuntimeException(e);
96: }
97: }
98:
99: }
|