KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.object.msg;
6
7 import com.tc.bytes.TCByteBuffer;
8 import com.tc.io.TCByteBufferInputStream;
9 import com.tc.io.TCByteBufferOutput;
10 import com.tc.io.TCSerializable;
11 import com.tc.net.protocol.tcm.ChannelID;
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.ObjectID;
17 import com.tc.object.dmi.DmiDescriptor;
18 import com.tc.object.dna.api.DNA;
19 import com.tc.object.dna.impl.DNAImpl;
20 import com.tc.object.dna.impl.ObjectStringSerializer;
21 import com.tc.object.dna.impl.VersionizedDNAWrapper;
22 import com.tc.object.gtx.GlobalTransactionID;
23 import com.tc.object.lockmanager.api.LockContext;
24 import com.tc.object.lockmanager.api.LockID;
25 import com.tc.object.session.SessionID;
26 import com.tc.object.tx.TransactionID;
27 import com.tc.object.tx.TxnType;
28 import com.tc.util.Assert;
29
30 import java.io.IOException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.LinkedList JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Set JavaDoc;
41
42 /**
43  * @author steve
44  */

45 public class BroadcastTransactionMessageImpl extends DSOMessageBase implements BroadcastTransactionMessage {
46   private final static byte DNA_ID = 1;
47   private final static byte LOCK_ID = 2;
48   private final static byte CHANGE_ID = 3;
49   private final static byte TRANSACTION_ID = 4;
50   private final static byte COMMITTER_ID = 5;
51   private final static byte TRANSACTION_TYPE_ID = 6;
52   private final static byte GLOBAL_TRANSACTION_ID = 7;
53   private final static byte LOW_WATERMARK = 8;
54   private final static byte SERIALIZER_ID = 9;
55   private final static byte NOTIFIED = 10;
56   private final static byte LOOKUP_OBJECT_IDS = 11;
57   private final static byte ROOT_NAME_ID_PAIR = 12;
58   private final static byte DMI_ID = 13;
59
60   private List JavaDoc changes = new LinkedList JavaDoc();
61   private List JavaDoc lockIDs = new LinkedList JavaDoc();
62   private List JavaDoc dmis = new LinkedList JavaDoc();
63   private Set JavaDoc lookupObjectIDs = new HashSet JavaDoc();
64   private Collection JavaDoc notifies = new LinkedList JavaDoc();
65   private Map JavaDoc newRoots = new HashMap JavaDoc();
66
67   private long changeID;
68   private TransactionID transactionID;
69   private ChannelID committerID;
70   private TxnType transactionType;
71   private GlobalTransactionID globalTransactionID;
72   private GlobalTransactionID lowWatermark;
73   private ObjectStringSerializer serializer;
74
75   public BroadcastTransactionMessageImpl(MessageMonitor monitor, TCByteBufferOutput out, MessageChannel channel,
76                                          TCMessageType type) {
77     super(monitor, out, channel, type);
78   }
79
80   public BroadcastTransactionMessageImpl(SessionID sessionID, MessageMonitor monitor, MessageChannel channel,
81                                          TCMessageHeader header, TCByteBuffer[] data) {
82     super(sessionID, monitor, channel, header, data);
83   }
84
85   protected void dehydrateValues() {
86     putNVPair(TRANSACTION_TYPE_ID, transactionType.getType());
87     for (Iterator JavaDoc i = lockIDs.iterator(); i.hasNext();) {
88       LockID lockID = (LockID) i.next();
89       putNVPair(LOCK_ID, lockID.asString());
90     }
91
92     for (Iterator JavaDoc i = notifies.iterator(); i.hasNext();) {
93       LockContext notified = (LockContext) i.next();
94       putNVPair(NOTIFIED, notified);
95     }
96
97     putNVPair(SERIALIZER_ID, serializer);
98
99     putNVPair(CHANGE_ID, changeID);
100     putNVPair(TRANSACTION_ID, transactionID.toLong());
101     putNVPair(COMMITTER_ID, committerID.toLong());
102     putNVPair(GLOBAL_TRANSACTION_ID, globalTransactionID.toLong());
103     putNVPair(LOW_WATERMARK, lowWatermark.toLong());
104
105     for (Iterator JavaDoc i = changes.iterator(); i.hasNext();) {
106       DNAImpl dna = (DNAImpl) i.next();
107       putNVPair(DNA_ID, dna);
108     }
109     for (Iterator JavaDoc i = lookupObjectIDs.iterator(); i.hasNext();) {
110       ObjectID oid = (ObjectID) i.next();
111       putNVPair(LOOKUP_OBJECT_IDS, oid.toLong());
112     }
113     for (Iterator JavaDoc i = newRoots.keySet().iterator(); i.hasNext();) {
114       String JavaDoc key = (String JavaDoc) i.next();
115       ObjectID value = (ObjectID) newRoots.get(key);
116       putNVPair(ROOT_NAME_ID_PAIR, new RootIDPair(key, value));
117     }
118     for (Iterator JavaDoc i = dmis.iterator(); i.hasNext();) {
119       DmiDescriptor dd = (DmiDescriptor) i.next();
120       putNVPair(DMI_ID, dd);
121     }
122   }
123
124   protected boolean hydrateValue(byte name) throws IOException JavaDoc {
125     switch (name) {
126       case TRANSACTION_TYPE_ID:
127         this.transactionType = TxnType.typeFor(getByteValue());
128         return true;
129       case DNA_ID:
130         this.changes.add(getObject(new DNAImpl(serializer, false)));
131         return true;
132       case SERIALIZER_ID:
133         this.serializer = (ObjectStringSerializer) getObject(new ObjectStringSerializer());
134         return true;
135       case LOCK_ID:
136         this.lockIDs.add(new LockID(getStringValue()));
137         return true;
138       case NOTIFIED:
139         this.notifies.add(this.getObject(new LockContext()));
140         return true;
141       case CHANGE_ID:
142         this.changeID = getLongValue();
143         return true;
144       case TRANSACTION_ID:
145         this.transactionID = new TransactionID(getLongValue());
146         return true;
147       case COMMITTER_ID:
148         this.committerID = new ChannelID(getLongValue());
149         return true;
150       case GLOBAL_TRANSACTION_ID:
151         this.globalTransactionID = new GlobalTransactionID(getLongValue());
152         return true;
153       case LOW_WATERMARK:
154         this.lowWatermark = new GlobalTransactionID(getLongValue());
155         return true;
156       case LOOKUP_OBJECT_IDS:
157         this.lookupObjectIDs.add(new ObjectID(getLongValue()));
158         return true;
159       case ROOT_NAME_ID_PAIR:
160         RootIDPair rootIDPair = (RootIDPair) getObject(new RootIDPair());
161         this.newRoots.put(rootIDPair.getRootName(), rootIDPair.getRootID());
162         return true;
163       case DMI_ID:
164         DmiDescriptor dd = (DmiDescriptor) getObject(new DmiDescriptor());
165         dmis.add(dd);
166         return true;
167       default:
168         return false;
169     }
170   }
171
172   public void initialize(List JavaDoc chges, Set JavaDoc objectIDs, ObjectStringSerializer aSerializer, LockID[] lids, long cid,
173                          TransactionID txID, ChannelID commitID, GlobalTransactionID gtx, TxnType txnType,
174                          GlobalTransactionID lowGlobalTransactionIDWatermark, Collection JavaDoc theNotifies, Map JavaDoc roots,
175                          DmiDescriptor[] dmiDescs) {
176     Assert.eval(lids.length > 0);
177     Assert.assertNotNull(txnType);
178
179     this.changes = chges;
180     this.lockIDs = new LinkedList JavaDoc(Arrays.asList(lids));
181     this.changeID = cid;
182     this.transactionID = txID;
183     this.committerID = commitID;
184     this.transactionType = txnType;
185     this.globalTransactionID = gtx;
186     this.lowWatermark = lowGlobalTransactionIDWatermark;
187     this.serializer = aSerializer;
188     this.notifies.addAll(theNotifies);
189     this.lookupObjectIDs.addAll(objectIDs);
190     this.newRoots.putAll(roots);
191     for (int i = 0; i < dmiDescs.length; i++) {
192       this.dmis.add(dmiDescs[i]);
193     }
194   }
195
196   public LockID[] getLockIDs() {
197     return (LockID[]) lockIDs.toArray(new LockID[lockIDs.size()]);
198   }
199
200   public TxnType getTransactionType() {
201     return transactionType;
202   }
203
204   public Collection JavaDoc getObjectChanges() {
205     Collection JavaDoc versionizedChanges = new ArrayList JavaDoc(changes.size());
206     for (Iterator JavaDoc iter = changes.iterator(); iter.hasNext();) {
207       versionizedChanges.add(new VersionizedDNAWrapper((DNA) iter.next(), globalTransactionID.toLong()));
208
209     }
210     return versionizedChanges;
211   }
212
213   public Set JavaDoc getLookupObjectIDs() {
214     return lookupObjectIDs;
215   }
216
217   public long getChangeID() {
218     return changeID;
219   }
220
221   public TransactionID getTransactionID() {
222     return transactionID;
223   }
224
225   public ChannelID getCommitterID() {
226     return committerID;
227   }
228
229   public GlobalTransactionID getGlobalTransactionID() {
230     return this.globalTransactionID;
231   }
232
233   public GlobalTransactionID getLowGlobalTransactionIDWatermark() {
234     Assert.assertNotNull(this.lowWatermark);
235     return this.lowWatermark;
236   }
237
238   public Collection JavaDoc addNotifiesTo(List JavaDoc c) {
239     c.addAll(notifies);
240     return c;
241   }
242
243   public void doRecycleOnRead() {
244     // dont recycle yet
245
}
246
247   protected boolean isOutputStreamRecycled() {
248     return true;
249   }
250
251   public void doRecycleOnWrite() {
252     // recycle only those buffers created for this message
253
recycleOutputStream();
254   }
255
256   public Map JavaDoc getNewRoots() {
257     return this.newRoots;
258   }
259
260   private static class RootIDPair implements TCSerializable {
261     private String JavaDoc rootName;
262     private ObjectID rootID;
263
264     public RootIDPair() {
265       super();
266     }
267
268     public RootIDPair(String JavaDoc rootName, ObjectID rootID) {
269       this.rootName = rootName;
270       this.rootID = rootID;
271     }
272
273     public void serializeTo(TCByteBufferOutput serialOutput) {
274       serialOutput.writeString(rootName);
275       serialOutput.writeLong(rootID.toLong());
276
277     }
278
279     public Object JavaDoc deserializeFrom(TCByteBufferInputStream serialInput) throws IOException JavaDoc {
280       this.rootName = serialInput.readString();
281       this.rootID = new ObjectID(serialInput.readLong());
282       return this;
283     }
284
285     public ObjectID getRootID() {
286       return rootID;
287     }
288
289     public String JavaDoc getRootName() {
290       return rootName;
291     }
292   }
293
294   public List JavaDoc getDmiDescriptors() {
295     return dmis;
296   }
297
298 }
299
Popular Tags