1 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 ; 21 import java.util.HashMap ; 22 import java.util.IdentityHashMap ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 31 public class ClientTransactionImpl extends AbstractClientTransaction { 32 private final RuntimeLogger runtimeLogger; 33 private final Map objectChanges = new HashMap (); 34 private final Map newRoots = new HashMap (); 35 private final List notifies = new LinkedList (); 36 private final List dmis = new LinkedList (); 37 38 private final Map referenced = new IdentityHashMap (); 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 getNewRoots() { 59 return newRoots; 60 } 61 62 public Map getChangeBuffers() { 63 return this.objectChanges; 64 } 65 66 protected void basicLiteralValueChanged(TCObject source, Object newValue, Object oldValue) { 67 if (runtimeLogger.fieldChangeDebug()) { 68 runtimeLogger.literalValueChanged(source, newValue); 69 } 70 71 getOrCreateChangeBuffer(source).literalValueChanged(newValue); 72 addReferenced(oldValue); 74 addReferenced(newValue); 75 } 76 77 protected void basicFieldChanged(TCObject source, String classname, String fieldname, Object 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 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 [] 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 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 pojo) { 122 Assert.assertNotNull("pojo", pojo); 123 referenced.put(pojo, null); 124 } 125 126 public Collection 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 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 creationCountByClass = new HashMap (); 152 if (!objectChanges.isEmpty()) { 153 int currentIndex = 0; 154 for (Iterator 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 instanceClass = tco.getTCClass().getPeerClass(); 159 Integer counter = (Integer ) creationCountByClass.get(instanceClass); 160 if (counter == null) { 161 counter = new Integer (1); 162 } else { 163 counter = new Integer (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 |