KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tx > ClientTransactionImpl


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.tx;
6
7 import com.tc.management.beans.tx.ClientTxMonitorMBean;
8 import com.tc.net.protocol.tcm.ChannelIDProvider;
9 import com.tc.object.ObjectID;
10 import com.tc.object.TCObject;
11 import com.tc.object.change.TCChangeBuffer;
12 import com.tc.object.change.TCChangeBufferImpl;
13 import com.tc.object.dmi.DmiDescriptor;
14 import com.tc.object.lockmanager.api.Notify;
15 import com.tc.object.logging.RuntimeLogger;
16 import com.tc.util.Assert;
17
18 import gnu.trove.TIntArrayList;
19
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.IdentityHashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * @author steve
30  */

31 public class ClientTransactionImpl extends AbstractClientTransaction {
32   private final RuntimeLogger runtimeLogger;
33   private final Map JavaDoc objectChanges = new HashMap JavaDoc();
34   private final Map JavaDoc newRoots = new HashMap JavaDoc();
35   private final List notifies = new LinkedList JavaDoc();
36   private final List dmis = new LinkedList JavaDoc();
37
38   // used to keep things referenced until the transaction is completely ACKED
39
private final Map JavaDoc referenced = new IdentityHashMap JavaDoc();
40
41   public ClientTransactionImpl(TransactionID txID, RuntimeLogger logger, ChannelIDProvider cidProvider) {
42     super(txID, cidProvider);
43     this.runtimeLogger = logger;
44   }
45
46   public boolean isConcurrent() {
47     return this.getTransactionType().isConcurrent();
48   }
49
50   public boolean hasChangesOrNotifies() {
51     return !(objectChanges.isEmpty() && newRoots.isEmpty() && notifies.isEmpty());
52   }
53
54   public boolean hasChanges() {
55     return !(objectChanges.isEmpty() && newRoots.isEmpty());
56   }
57
58   public Map JavaDoc getNewRoots() {
59     return newRoots;
60   }
61
62   public Map JavaDoc getChangeBuffers() {
63     return this.objectChanges;
64   }
65
66   protected void basicLiteralValueChanged(TCObject source, Object JavaDoc newValue, Object JavaDoc oldValue) {
67     if (runtimeLogger.fieldChangeDebug()) {
68       runtimeLogger.literalValueChanged(source, newValue);
69     }
70
71     getOrCreateChangeBuffer(source).literalValueChanged(newValue);
72     // To prevent it gcing on us.
73
addReferenced(oldValue);
74     addReferenced(newValue);
75   }
76
77   protected void basicFieldChanged(TCObject source, String JavaDoc classname, String JavaDoc fieldname, Object JavaDoc newValue, int index) {
78     if (runtimeLogger.fieldChangeDebug()) {
79       runtimeLogger.fieldChanged(source, classname, fieldname, newValue, index);
80     }
81
82     getOrCreateChangeBuffer(source).fieldChanged(classname, fieldname, newValue, index);
83   }
84
85   protected void basicArrayChanged(TCObject source, int startPos, Object JavaDoc array, int length) {
86     if (runtimeLogger.arrayChangeDebug()) {
87       runtimeLogger.arrayChanged(source, startPos, array);
88     }
89
90     getOrCreateChangeBuffer(source).arrayChanged(startPos, array, length);
91   }
92
93   protected void basicLogicalInvoke(TCObject source, int method, Object JavaDoc[] parameters) {
94     getOrCreateChangeBuffer(source).logicalInvoke(method, parameters);
95   }
96
97   protected void basicCreate(TCObject object) {
98     if (runtimeLogger.newManagedObjectDebug()) {
99       runtimeLogger.newManagedObject(object);
100     }
101
102     getOrCreateChangeBuffer(object);
103   }
104
105   protected void basicCreateRoot(String JavaDoc name, ObjectID root) {
106     newRoots.put(name, root);
107   }
108
109   private TCChangeBuffer getOrCreateChangeBuffer(TCObject object) {
110     addReferenced(object.getPeerObject());
111
112     TCChangeBuffer cb = (TCChangeBuffer) objectChanges.get(object);
113     if (cb == null) {
114       cb = new TCChangeBufferImpl(object);
115       objectChanges.put(object, cb);
116     }
117
118     return cb;
119   }
120
121   private void addReferenced(Object JavaDoc pojo) {
122     Assert.assertNotNull("pojo", pojo);
123     referenced.put(pojo, null);
124   }
125
126   public Collection JavaDoc getReferencesOfObjectsInTxn() {
127     return referenced.keySet();
128   }
129
130   public void addNotify(Notify notify) {
131     if (!notify.isNull()) notifies.add(notify);
132   }
133
134   public List addNotifiesTo(List l) {
135     l.addAll(notifies);
136     return l;
137   }
138
139   public String JavaDoc toString() {
140     return "ClientTransactionImpl [ " + getTransactionID() + " ]";
141   }
142
143   public int getNotifiesCount() {
144     return notifies.size();
145   }
146
147   public void updateMBean(ClientTxMonitorMBean txMBean) {
148     int modifiedObjectCount = 0;
149     final TIntArrayList writesPerObject = new TIntArrayList(objectChanges.size());
150
151     final Map JavaDoc creationCountByClass = new HashMap JavaDoc();
152     if (!objectChanges.isEmpty()) {
153       int currentIndex = 0;
154       for (Iterator JavaDoc iter = objectChanges.keySet().iterator(); iter.hasNext(); currentIndex++) {
155         final TCChangeBuffer buffer = (TCChangeBuffer) objectChanges.get(iter.next());
156         final TCObject tco = buffer.getTCObject();
157         if (tco.isNew()) {
158           final Class JavaDoc instanceClass = tco.getTCClass().getPeerClass();
159           Integer JavaDoc counter = (Integer JavaDoc) creationCountByClass.get(instanceClass);
160           if (counter == null) {
161             counter = new Integer JavaDoc(1);
162           } else {
163             counter = new Integer JavaDoc(counter.intValue() + 1);
164           }
165           creationCountByClass.put(instanceClass, counter);
166         } else {
167           ++modifiedObjectCount;
168         }
169         writesPerObject.add(buffer.getTotalEventCount());
170       }
171     }
172     txMBean.committedWriteTransaction(getNotifiesCount(), modifiedObjectCount, writesPerObject.toNativeArray(),
173                                       creationCountByClass);
174   }
175
176   public void addDmiDescritor(DmiDescriptor dd) {
177     dmis.add(dd);
178   }
179   
180   public List getDmiDescriptors() {
181     return dmis;
182   }
183
184 }
Popular Tags