001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.management.remote.protocol.terracotta;
005:
006: import com.tc.bytes.TCByteBuffer;
007: import com.tc.exception.TCRuntimeException;
008: import com.tc.io.TCByteBufferInput;
009: import com.tc.io.TCByteBufferOutput;
010: import com.tc.io.TCSerializable;
011: import com.tc.net.protocol.tcm.MessageChannel;
012: import com.tc.net.protocol.tcm.MessageMonitor;
013: import com.tc.net.protocol.tcm.TCMessageHeader;
014: import com.tc.net.protocol.tcm.TCMessageType;
015: import com.tc.object.msg.DSOMessageBase;
016: import com.tc.object.session.SessionID;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023:
024: import javax.management.remote.message.Message;
025:
026: public final class JmxRemoteTunnelMessage extends DSOMessageBase
027: implements TCSerializable {
028:
029: private static final byte TUNNEL_MESSAGE = 0;
030: private static final byte FLAG = 1;
031:
032: private static final byte SYN_FLAG = 1 << 0;
033: private static final byte DATA_FLAG = 1 << 1;
034: private static final byte FIN_FLAG = 1 << 2;
035:
036: private Message tunneledMessage;
037: private byte flag;
038:
039: public JmxRemoteTunnelMessage(SessionID sessionID,
040: MessageMonitor monitor, TCByteBufferOutput out,
041: MessageChannel channel, TCMessageType type) {
042: super (sessionID, monitor, out, channel, type);
043: flag = DATA_FLAG;
044: }
045:
046: public JmxRemoteTunnelMessage(SessionID sessionID,
047: MessageMonitor monitor, MessageChannel channel,
048: TCMessageHeader header, TCByteBuffer[] data) {
049: super (sessionID, monitor, channel, header, data);
050: flag = DATA_FLAG;
051: }
052:
053: protected boolean hydrateValue(final byte name) throws IOException {
054: switch (name) {
055: case TUNNEL_MESSAGE:
056: setTunneledMessage((Message) getObject(this ));
057: return true;
058: case FLAG:
059: setFlag(getByteValue());
060: return true;
061: default:
062: return false;
063: }
064: }
065:
066: protected void dehydrateValues() {
067: putNVPair(FLAG, flag);
068: putNVPair(TUNNEL_MESSAGE, this );
069: }
070:
071: public synchronized void serializeTo(TCByteBufferOutput serialOutput) {
072: try {
073: final ByteArrayOutputStream bao = new ByteArrayOutputStream(
074: 1024);
075: final ObjectOutputStream oos = new ObjectOutputStream(bao);
076: oos.writeObject(tunneledMessage);
077: oos.close();
078:
079: final byte serializedObject[] = bao.toByteArray();
080: serialOutput.writeByte(flag);
081: serialOutput.writeInt(serializedObject.length);
082: serialOutput.write(serializedObject);
083: } catch (IOException ioe) {
084: throw new TCRuntimeException(ioe);
085: }
086: }
087:
088: public synchronized Object deserializeFrom(
089: TCByteBufferInput serialInput) throws IOException {
090: try {
091: flag = serialInput.readByte();
092: final int length = serialInput.readInt();
093: final byte serializedObject[] = new byte[length];
094: serialInput.read(serializedObject);
095: final ByteArrayInputStream bis = new ByteArrayInputStream(
096: serializedObject);
097: final ObjectInputStream ois = new ObjectInputStream(bis);
098: return ois.readObject();
099: } catch (ClassNotFoundException cnfe) {
100: throw new TCRuntimeException(cnfe);
101: }
102: }
103:
104: synchronized void setInitConnection() {
105: setFlag(SYN_FLAG);
106: }
107:
108: synchronized boolean getInitConnection() {
109: return flag == SYN_FLAG;
110: }
111:
112: synchronized void setCloseConnection() {
113: setFlag(FIN_FLAG);
114: }
115:
116: synchronized boolean getCloseConnection() {
117: return flag == FIN_FLAG;
118: }
119:
120: synchronized void setTunneledMessage(final Message tunneledMessage) {
121: this .tunneledMessage = tunneledMessage;
122: }
123:
124: synchronized Message getTunneledMessage() {
125: return tunneledMessage;
126: }
127:
128: private synchronized void setFlag(final byte flag) {
129: this.flag = flag;
130: }
131:
132: }
|