KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > management > remote > protocol > terracotta > JmxRemoteTunnelMessage


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.management.remote.protocol.terracotta;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.ObjectInputStream JavaDoc;
10 import java.io.ObjectOutputStream JavaDoc;
11
12 import javax.management.remote.message.Message;
13
14 import com.tc.bytes.TCByteBuffer;
15 import com.tc.exception.TCRuntimeException;
16 import com.tc.io.TCByteBufferInputStream;
17 import com.tc.io.TCByteBufferOutput;
18 import com.tc.io.TCSerializable;
19 import com.tc.net.protocol.tcm.MessageChannel;
20 import com.tc.net.protocol.tcm.MessageMonitor;
21 import com.tc.net.protocol.tcm.TCMessageHeader;
22 import com.tc.net.protocol.tcm.TCMessageType;
23 import com.tc.object.msg.DSOMessageBase;
24 import com.tc.object.session.SessionID;
25
26 public final class JmxRemoteTunnelMessage extends DSOMessageBase implements TCSerializable {
27
28   private static final byte TUNNEL_MESSAGE = 0;
29   private static final byte FLAG = 1;
30
31   private static final byte SYN_FLAG = 1 << 0;
32   private static final byte DATA_FLAG = 1 << 1;
33   private static final byte FIN_FLAG = 1 << 2;
34
35   private Message tunneledMessage;
36   private byte flag;
37
38   public JmxRemoteTunnelMessage(MessageMonitor monitor, TCByteBufferOutput out, MessageChannel channel,
39                                 TCMessageType type) {
40     super(monitor, out, channel, type);
41     flag = DATA_FLAG;
42   }
43
44   public JmxRemoteTunnelMessage(SessionID sessionID, MessageMonitor monitor, MessageChannel channel,
45                                 TCMessageHeader header, TCByteBuffer[] data) {
46     super(sessionID, monitor, channel, header, data);
47     flag = DATA_FLAG;
48   }
49
50   protected boolean hydrateValue(final byte name) throws IOException JavaDoc {
51     switch (name) {
52       case TUNNEL_MESSAGE:
53         setTunneledMessage((Message) getObject(this));
54         return true;
55       case FLAG:
56         setFlag(getByteValue());
57         return true;
58       default:
59         return false;
60     }
61   }
62
63   protected void dehydrateValues() {
64     putNVPair(FLAG, flag);
65     putNVPair(TUNNEL_MESSAGE, this);
66   }
67
68   public synchronized void serializeTo(TCByteBufferOutput serialOutput) {
69     try {
70       final ByteArrayOutputStream JavaDoc bao = new ByteArrayOutputStream JavaDoc(1024);
71       final ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(bao);
72       oos.writeObject(tunneledMessage);
73       oos.close();
74
75       final byte serializedObject[] = bao.toByteArray();
76       serialOutput.writeByte(flag);
77       serialOutput.writeInt(serializedObject.length);
78       serialOutput.write(serializedObject);
79     } catch (IOException JavaDoc ioe) {
80       throw new TCRuntimeException(ioe);
81     }
82   }
83
84   public synchronized Object JavaDoc deserializeFrom(TCByteBufferInputStream serialInput) throws IOException JavaDoc {
85     try {
86       flag = serialInput.readByte();
87       final int length = serialInput.readInt();
88       final byte serializedObject[] = new byte[length];
89       serialInput.read(serializedObject);
90       final ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(serializedObject);
91       final ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bis);
92       return ois.readObject();
93     } catch (ClassNotFoundException JavaDoc cnfe) {
94       throw new TCRuntimeException(cnfe);
95     }
96   }
97
98   synchronized void setInitConnection() {
99     setFlag(SYN_FLAG);
100   }
101
102   synchronized boolean getInitConnection() {
103     return flag == SYN_FLAG;
104   }
105
106   synchronized void setCloseConnection() {
107     setFlag(FIN_FLAG);
108   }
109
110   synchronized boolean getCloseConnection() {
111     return flag == FIN_FLAG;
112   }
113
114   synchronized void setTunneledMessage(final Message tunneledMessage) {
115     this.tunneledMessage = tunneledMessage;
116   }
117
118   synchronized Message getTunneledMessage() {
119     return tunneledMessage;
120   }
121
122   private synchronized void setFlag(final byte flag) {
123     this.flag = flag;
124   }
125
126 }
127
Popular Tags