KickJava   Java API By Example, From Geeks To Geeks.

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


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.async.api.EventContext;
7 import com.tc.bytes.TCByteBuffer;
8 import com.tc.io.TCByteBufferOutput;
9 import com.tc.lang.Recyclable;
10 import com.tc.net.protocol.tcm.MessageChannel;
11 import com.tc.net.protocol.tcm.MessageMonitor;
12 import com.tc.net.protocol.tcm.TCMessageHeader;
13 import com.tc.net.protocol.tcm.TCMessageType;
14 import com.tc.object.dna.impl.ObjectStringSerializer;
15 import com.tc.object.session.SessionID;
16 import com.tc.object.tx.TransactionBatch;
17 import com.tc.object.tx.TransactionID;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24
25 /**
26  * @author steve
27  */

28 public class CommitTransactionMessageImpl extends DSOMessageBase implements EventContext, CommitTransactionMessage {
29   private static final byte BATCH_TRANSACTION_DATA_ID = 1;
30   private static final byte ACKNOWLEDGED_TRANSACTION_ID = 2;
31   private static final byte SERIALIZER_ID = 3;
32   private ObjectStringSerializer serializer;
33
34   private Recyclable batch; // This is used to recycle buffers on
35
// the write side
36
private TCByteBuffer[] batchData;
37   private final Set JavaDoc acknowledgedTransactionIDs = new HashSet JavaDoc();
38
39   public CommitTransactionMessageImpl(MessageMonitor monitor, TCByteBufferOutput out, MessageChannel channel,
40                                       TCMessageType type) {
41     super(monitor, out, channel, type);
42   }
43
44   public CommitTransactionMessageImpl(SessionID sessionID, MessageMonitor monitor, MessageChannel channel, TCMessageHeader header,
45                                       TCByteBuffer[] data) {
46     super(sessionID, monitor, channel, header, data);
47   }
48
49   public ObjectStringSerializer getSerializer() {
50     return serializer;
51   }
52
53   protected void dehydrateValues() {
54     for (Iterator JavaDoc i = acknowledgedTransactionIDs.iterator(); i.hasNext();) {
55       putNVPair(ACKNOWLEDGED_TRANSACTION_ID, ((TransactionID) i.next()).toLong());
56     }
57     putNVPair(SERIALIZER_ID, serializer);
58     putNVPair(BATCH_TRANSACTION_DATA_ID, batchData);
59     batchData = null;
60   }
61
62   protected boolean hydrateValue(byte name) throws IOException JavaDoc {
63     switch (name) {
64       case BATCH_TRANSACTION_DATA_ID: {
65         this.batchData = getInputStream().toArray();
66         return true;
67       }
68       case ACKNOWLEDGED_TRANSACTION_ID: {
69         this.acknowledgedTransactionIDs.add(new TransactionID(getInputStream().readLong()));
70         return true;
71       }
72       case SERIALIZER_ID:
73         this.serializer = (ObjectStringSerializer) getObject(new ObjectStringSerializer());
74         return true;
75
76       default: {
77         return false;
78       }
79     }
80   }
81
82   public void setBatch(TransactionBatch batch, ObjectStringSerializer serializer) {
83     this.batch = batch;
84     setBatchData(batch.getData(), serializer);
85     this.acknowledgedTransactionIDs.addAll(batch.getAcknowledgedTransactionIDs());
86   }
87
88   // This is here for a test
89
synchronized void setBatchData(TCByteBuffer[] batchData, ObjectStringSerializer serializer) {
90     if (this.batchData != null) throw new AssertionError JavaDoc("Attempt to set TransactionBatch more than once.");
91     this.batchData = batchData;
92     this.serializer = serializer;
93   }
94
95   public synchronized TCByteBuffer[] getBatchData() {
96     return batchData;
97   }
98
99   public Collection JavaDoc addAcknowledgedTransactionIDsTo(Collection JavaDoc c) {
100     c.addAll(this.acknowledgedTransactionIDs);
101     return c;
102   }
103
104   public void doRecycleOnRead() {
105     // recycle will be called later
106
}
107
108   protected boolean isOutputStreamRecycled() {
109     return true;
110   }
111
112   public void doRecycleOnWrite() {
113     // recycle only those buffers created for this message
114
recycleOutputStream();
115     if (batch != null) {
116       batch.recycle();
117     }
118   }
119
120 }
121
Popular Tags