1 4 package com.tc.objectserver.managedobject; 5 6 import com.tc.object.ObjectID; 7 import com.tc.object.SerializationUtil; 8 import com.tc.object.dna.api.DNACursor; 9 import com.tc.object.dna.api.DNAWriter; 10 import com.tc.object.dna.api.LogicalAction; 11 import com.tc.object.dna.api.PhysicalAction; 12 import com.tc.objectserver.mgmt.LogicalManagedObjectFacade; 13 import com.tc.objectserver.mgmt.ManagedObjectFacade; 14 import com.tc.util.Assert; 15 16 import java.io.IOException ; 17 import java.io.ObjectInput ; 18 import java.io.ObjectOutput ; 19 import java.util.Iterator ; 20 import java.util.LinkedList ; 21 import java.util.List ; 22 import java.util.Set ; 23 24 27 public class QueueManagedObjectState extends LogicalManagedObjectState { 28 private static final String TAKE_LOCK_FIELD_NAME = "java.util.concurrent.LinkedBlockingQueue.takeLock"; 29 private static final String PUT_LOCK_FIELD_NAME = "java.util.concurrent.LinkedBlockingQueue.putLock"; 30 private static final String CAPACITY_FIELD_NAME = "java.util.concurrent.LinkedBlockingQueue.capacity"; 31 32 private ObjectID takeLockField; 33 private ObjectID putLockField; 34 private Object capacityField; 35 36 private List references; 37 38 QueueManagedObjectState(ObjectInput in) throws IOException { 39 super(in); 40 } 41 42 protected QueueManagedObjectState(long classID) { 43 super(classID); 44 references = new LinkedList (); 45 } 46 47 public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException { 48 while (cursor.next()) { 49 Object action = cursor.getAction(); 50 if (action instanceof LogicalAction) { 51 LogicalAction logicalAction = (LogicalAction) action; 52 int method = logicalAction.getMethod(); 53 Object [] params = logicalAction.getParameters(); 54 applyMethod(objectID, includeIDs, method, params); 55 } else if (action instanceof PhysicalAction) { 56 PhysicalAction physicalAction = (PhysicalAction) action; 57 updateReference(physicalAction.getFieldName(), physicalAction.getObject()); 58 } 59 } 60 } 61 62 private void updateReference(String fieldName, Object value) { 63 if (TAKE_LOCK_FIELD_NAME.equals(fieldName)) { 64 takeLockField = (ObjectID) value; 65 } else if (PUT_LOCK_FIELD_NAME.equals(fieldName)) { 66 putLockField = (ObjectID) value; 67 } else if (CAPACITY_FIELD_NAME.equals(fieldName)) { 68 capacityField = value; 69 } 70 } 71 72 public void applyMethod(ObjectID objectID, BackReferences includeIDs, int method, Object [] params) { 73 switch (method) { 74 case SerializationUtil.PUT: 75 addChangeToCollector(objectID, params[0], includeIDs); 76 references.add(params[0]); 77 break; 78 case SerializationUtil.TAKE: 79 references.remove(0); 80 break; 81 case SerializationUtil.CLEAR: 82 references.clear(); 83 break; 84 case SerializationUtil.REMOVE_FIRST_N: 85 int n = ((Integer ) params[0]).intValue(); 86 for (int i = 0; i < n; i++) { 87 references.remove(0); 88 } 89 break; 90 case SerializationUtil.REMOVE_AT: 91 int i = ((Integer ) params[0]).intValue(); 92 Assert.assertTrue(references.size() > i); 93 references.remove(i); 94 break; 95 default: 96 throw new AssertionError ("Invalid method:" + method + " state:" + this); 97 } 98 } 99 100 private void addChangeToCollector(ObjectID objectID, Object newValue, BackReferences includeIDs) { 102 if (newValue instanceof ObjectID) { 103 getListener().changed(objectID, null, (ObjectID) newValue); 104 } 105 } 106 107 public void addObjectReferencesTo(ManagedObjectTraverser traverser) { 108 traverser.addReachableObjectIDs(getObjectReferences()); 109 } 110 111 112 protected void addAllObjectReferencesTo(Set refs) { 113 addAllObjectReferencesFromIteratorTo(references.iterator(), refs); 114 if (takeLockField != null) { 115 refs.add(takeLockField); 116 } 117 if (putLockField != null) { 118 refs.add(putLockField); 119 } 120 } 121 122 public void dehydrate(ObjectID objectID, DNAWriter writer) { 123 dehydrateFields(objectID, writer); 124 dehydrateMembers(objectID, writer); 125 } 126 127 private void dehydrateFields(ObjectID objectId, DNAWriter writer) { 128 writer.addPhysicalAction(TAKE_LOCK_FIELD_NAME, takeLockField); 129 writer.addPhysicalAction(PUT_LOCK_FIELD_NAME, putLockField); 130 writer.addPhysicalAction(CAPACITY_FIELD_NAME, capacityField); 131 } 132 133 private void dehydrateMembers(ObjectID objectID, DNAWriter writer) { 134 for (Iterator i = references.iterator(); i.hasNext();) { 135 Object o = i.next(); 136 writer.addLogicalAction(SerializationUtil.PUT, new Object [] { o }); 137 } 138 } 139 140 public String toString() { 141 return "QueueManagedStateObject(" + references + ")"; 142 } 143 144 public ManagedObjectFacade createFacade(ObjectID objectID, String className, int limit) { 145 final int size = references.size(); 146 147 if (limit < 0) { 148 limit = size; 149 } else { 150 limit = Math.min(limit, size); 151 } 152 153 Object [] data = new Object [limit]; 154 155 int index = 0; 156 for (Iterator iter = references.iterator(); iter.hasNext() && index < limit; index++) { 157 data[index] = iter.next(); 158 } 159 160 return LogicalManagedObjectFacade.createListInstance(objectID, className, data, size); 161 } 162 163 public byte getType() { 164 return QUEUE_TYPE; 165 } 166 167 private void writeField(ObjectOutput out, String fieldName, Object fieldValue) throws IOException { 168 out.writeUTF(fieldName); 169 if (fieldValue == null) { 170 out.writeBoolean(false); 171 } else { 172 out.writeBoolean(true); 173 if (fieldValue instanceof ObjectID) { 174 out.writeLong(((ObjectID) fieldValue).toLong()); 175 } else { 176 out.writeObject(fieldValue); 177 } 178 } 179 } 180 181 protected void basicWriteTo(ObjectOutput out) throws IOException { 182 writeField(out, TAKE_LOCK_FIELD_NAME, takeLockField); 183 writeField(out, PUT_LOCK_FIELD_NAME, putLockField); 184 writeField(out, CAPACITY_FIELD_NAME, capacityField); 185 186 out.writeInt(references.size()); 187 for (Iterator i = references.iterator(); i.hasNext();) { 188 out.writeObject(i.next()); 189 } 190 } 191 192 protected boolean basicEquals(LogicalManagedObjectState o) { 193 QueueManagedObjectState mo = (QueueManagedObjectState) o; 194 return ((takeLockField == mo.takeLockField) || (takeLockField != null && takeLockField.equals(mo.takeLockField))) 195 && ((putLockField == mo.putLockField) || (putLockField != null && putLockField.equals(mo.putLockField))) 196 && ((capacityField == mo.capacityField) || (capacityField != null && capacityField.equals(mo.capacityField))) 197 && references.equals(mo.references); 198 } 199 200 private static void readField(ObjectInput in, QueueManagedObjectState mo) throws ClassNotFoundException , IOException { 201 String fieldName = in.readUTF(); 202 boolean fieldExist = in.readBoolean(); 203 if (fieldExist) { 204 if (fieldName.equals(TAKE_LOCK_FIELD_NAME)) { 205 mo.takeLockField = new ObjectID(in.readLong()); 206 } else if (fieldName.equals(PUT_LOCK_FIELD_NAME)) { 207 mo.putLockField = new ObjectID(in.readLong()); 208 } else if (fieldName.equals(CAPACITY_FIELD_NAME)) { 209 mo.capacityField = in.readObject(); 210 } else { 211 throw new AssertionError ("Field not recognized in QueueManagedObjectState.readFrom()."); 212 } 213 } 214 } 215 216 static QueueManagedObjectState readFrom(ObjectInput in) throws IOException , ClassNotFoundException { 217 QueueManagedObjectState mo = new QueueManagedObjectState(in); 218 readField(in, mo); 219 readField(in, mo); 220 readField(in, mo); 221 222 223 int size = in.readInt(); 224 LinkedList list = new LinkedList (); 225 for (int i = 0; i < size; i++) { 226 list.add(in.readObject()); 227 } 228 mo.references = list; 229 return mo; 230 } 231 } | Popular Tags |