1 5 package com.tc.object.applicator; 6 7 import com.tc.object.ClientObjectManager; 8 import com.tc.object.ObjectID; 9 import com.tc.object.TCClass; 10 import com.tc.object.TCObject; 11 import com.tc.object.TraversedReferences; 12 import com.tc.object.bytecode.Manageable; 13 import com.tc.object.bytecode.TransparentAccess; 14 import com.tc.object.dna.api.DNA; 15 import com.tc.object.dna.api.DNACursor; 16 import com.tc.object.dna.api.DNAWriter; 17 import com.tc.object.dna.api.PhysicalAction; 18 import com.tc.object.dna.impl.DNAEncoding; 19 import com.tc.object.field.TCField; 20 import com.tc.object.tx.optimistic.OptimisticTransactionManager; 21 import com.tc.object.tx.optimistic.TCObjectClone; 22 import com.tc.util.Assert; 23 24 import java.io.IOException ; 25 import java.util.HashMap ; 26 import java.util.IdentityHashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 public class PhysicalApplicator extends BaseApplicator { 31 32 private final TCClass clazz; 33 34 public PhysicalApplicator(TCClass clazz, DNAEncoding encoding) { 35 super(encoding); 36 this.clazz = clazz; 37 } 38 39 43 public Map connectedCopy(Object source, Object dest, Map visited, ClientObjectManager objectManager, 44 OptimisticTransactionManager txManager) { 45 Map map = new HashMap (); 46 Map cloned = new IdentityHashMap (); 47 48 TransparentAccess da = (TransparentAccess) dest; 49 50 Manageable sourceManageable = (Manageable) source; 51 Manageable destManaged = (Manageable) da; 52 53 getAllFields(source, map); 54 55 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 56 String k = (String ) i.next(); 57 Object v = map.get(k); 58 59 Object copyValue = createCopyIfNecessary(objectManager, visited, cloned, v); 60 da.__tc_setfield(k, copyValue); 61 } 62 63 destManaged.__tc_managed(new TCObjectClone(sourceManageable.__tc_managed(), txManager)); 64 return cloned; 65 } 66 67 public TraversedReferences getPortableObjects(Object pojo, TraversedReferences addTo) { 68 if (!(pojo instanceof TransparentAccess)) return addTo; 69 70 Map map = new HashMap (); 71 getAllFields(pojo, map); 72 73 TCField[] fields = clazz.getPortableFields(); 74 if (clazz.isNonStaticInner()) { 75 addTo.addNamedReference(clazz.getName(), clazz.getParentFieldName(), map.get(clazz.getParentFieldName())); 76 } 77 for (int i = 0; i < fields.length; i++) { 78 Object o = map.get(fields[i].getName()); 79 80 if (o != null && isPortableReference(o.getClass())) { 81 addTo.addNamedReference(fields[i].getName(), o); 82 } 83 } 84 return addTo; 85 } 86 87 public void hydrate(ClientObjectManager objectManager, TCObject tcObject, DNA dna, Object po) throws IOException , 88 ClassNotFoundException { 89 DNACursor cursor = dna.getCursor(); 90 String fieldName; 91 Object fieldValue; 92 93 while (cursor.next(encoding)) { 94 PhysicalAction a = cursor.getPhysicalAction(); 95 Assert.eval(a.isTruePhysical()); 96 fieldName = a.getFieldName(); 97 fieldValue = a.getObject(); 98 99 tcObject.setValue(fieldName, fieldValue); 100 } 101 } 102 103 public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object pojo) { 104 if (!objectManager.isPortableInstance(pojo)) { return; } 105 106 TCClass tcc = clazz; 107 TCField[] fields; 108 109 Map fieldValues = null; 110 111 while (tcc != null) { 112 fields = tcc.getPortableFields(); 113 114 if (fields.length > 0 && fieldValues == null) { 115 fieldValues = new HashMap (); 116 if (pojo instanceof TransparentAccess) { 117 getAllFields(pojo, fieldValues); 119 } else { 120 throw new AssertionError ("wrong type: " + pojo.getClass()); 121 } 122 } 123 124 for (int i = 0; i < fields.length; i++) { 125 String fieldName = fields[i].getName(); 126 Object fieldValue = fieldValues.get(fieldName); 127 128 if (fieldValue == null) { 129 if (!fieldValues.containsKey(fieldName)) { throw new AssertionError ( 130 fieldName 131 + " does not exist in map returned from __tc_getallfields. Class is " 132 + tcc + ". field Values = " 133 + fieldValues); } 134 } 135 136 if (!objectManager.isPortableInstance(fieldValue)) { 137 continue; 138 } 139 140 Object value = getDehydratableObject(fieldValue, objectManager); 141 if (value == null) { 142 value = ObjectID.NULL_ID; 146 } 147 writer.addPhysicalAction(fieldName, value, fields[i].canBeReference()); 148 } 149 tcc = tcc.getSuperclass(); 150 } 151 152 if (clazz.isNonStaticInner() && fieldValues != null) { 153 Object parentObject = fieldValues.get(clazz.getParentFieldName()); 154 Assert.assertNotNull("parentObject", parentObject); 155 Object parentObjectID = getDehydratableObject(parentObject, objectManager); 156 if (parentObjectID != null) { 157 writer.setParentObjectID((ObjectID) parentObjectID); 158 } 159 } 160 161 } 162 163 public Object getNewInstance(ClientObjectManager objectManager, DNA dna) { 164 throw new UnsupportedOperationException (); 165 } 166 167 private static void getAllFields(Object o, Map dest) { 168 if (o instanceof Throwable ) { 171 ((Throwable ) o).getStackTrace(); 173 } 174 ((TransparentAccess) o).__tc_getallfields(dest); 175 } 176 177 } 178 | Popular Tags |