001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.msg;
006:
007: import com.tc.async.api.EventContext;
008: import com.tc.bytes.TCByteBuffer;
009: import com.tc.io.TCByteBufferOutput;
010: import com.tc.lang.Recyclable;
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.dna.impl.ObjectStringSerializer;
016: import com.tc.object.session.SessionID;
017: import com.tc.object.tx.TransactionBatch;
018:
019: import java.io.IOException;
020:
021: /**
022: * @author steve
023: */
024: public class CommitTransactionMessageImpl extends DSOMessageBase
025: implements EventContext, CommitTransactionMessage {
026: private static final byte BATCH_TRANSACTION_DATA_ID = 1;
027: private static final byte SERIALIZER_ID = 2;
028: private ObjectStringSerializer serializer;
029:
030: private Recyclable batch; // This is used to recycle buffers on
031: // the write side
032: private TCByteBuffer[] batchData;
033:
034: public CommitTransactionMessageImpl(SessionID sessionID,
035: MessageMonitor monitor, TCByteBufferOutput out,
036: MessageChannel channel, TCMessageType type) {
037: super (sessionID, monitor, out, channel, type);
038: }
039:
040: public CommitTransactionMessageImpl(SessionID sessionID,
041: MessageMonitor monitor, MessageChannel channel,
042: TCMessageHeader header, TCByteBuffer[] data) {
043: super (sessionID, monitor, channel, header, data);
044: }
045:
046: public ObjectStringSerializer getSerializer() {
047: return serializer;
048: }
049:
050: protected void dehydrateValues() {
051: putNVPair(SERIALIZER_ID, serializer);
052: putNVPair(BATCH_TRANSACTION_DATA_ID, batchData);
053: batchData = null;
054: }
055:
056: protected boolean hydrateValue(byte name) throws IOException {
057: switch (name) {
058: case BATCH_TRANSACTION_DATA_ID: {
059: this .batchData = getInputStream().toArray();
060: return true;
061: }
062: case SERIALIZER_ID:
063: this .serializer = (ObjectStringSerializer) getObject(new ObjectStringSerializer());
064: return true;
065:
066: default: {
067: return false;
068: }
069: }
070: }
071:
072: public void setBatch(TransactionBatch batch,
073: ObjectStringSerializer serializer) {
074: this .batch = batch;
075: setBatchData(batch.getData(), serializer);
076: }
077:
078: // This is here for a test
079: synchronized void setBatchData(TCByteBuffer[] batchData,
080: ObjectStringSerializer serializer) {
081: if (this .batchData != null)
082: throw new AssertionError(
083: "Attempt to set TransactionBatch more than once.");
084: this .batchData = batchData;
085: this .serializer = serializer;
086: }
087:
088: public synchronized TCByteBuffer[] getBatchData() {
089: return batchData;
090: }
091:
092: public void doRecycleOnRead() {
093: // recycle will be called later
094: }
095:
096: protected boolean isOutputStreamRecycled() {
097: return true;
098: }
099:
100: public void doRecycleOnWrite() {
101: // recycle only those buffers created for this message
102: recycleOutputStream();
103: if (batch != null) {
104: batch.recycle();
105: }
106: }
107: }
|