1 5 package org.terracotta.modules.iBatis_2_2_0.object.applicator; 6 7 import com.tc.exception.TCNotSupportedMethodException; 8 import com.tc.exception.TCRuntimeException; 9 import com.tc.object.ClientObjectManager; 10 import com.tc.object.ObjectID; 11 import com.tc.object.TCObject; 12 import com.tc.object.TraversedReferences; 13 import com.tc.object.applicator.BaseApplicator; 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.tx.optimistic.OptimisticTransactionManager; 20 import com.tc.util.Assert; 21 22 import java.io.IOException ; 23 import java.lang.reflect.Constructor ; 24 import java.lang.reflect.Field ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.util.Map ; 27 28 31 public class IBatisAccessPlanApplicator extends BaseApplicator { 32 private static final String CLASSNAME_FIELD_NAME = "com.ibatis.sqlmap.engine.accessplan.BaseAccessPlan.className"; 33 private static final String CLAZZ_FIELD_NAME = "com.ibatis.sqlmap.engine.accessplan.BaseAccessPlan.clazz"; 34 private static final String PROPERTY_NAMES_FIELD_NAME = "com.ibatis.sqlmap.engine.accessplan.BaseAccessPlan.clazz.propertyNames"; 35 36 public IBatisAccessPlanApplicator(DNAEncoding encoding) { 37 super(encoding); 38 } 39 40 public TraversedReferences getPortableObjects(Object pojo, TraversedReferences addTo) { 41 addTo.addAnonymousReference(getClazz(pojo)); 42 addTo.addAnonymousReference(getPropertyNames(pojo)); 43 return addTo; 44 } 45 46 private Object getClazz(Object pojo) { 47 return getField(pojo, "clazz"); 49 } 50 51 private Object getPropertyNames(Object pojo) { 52 return getField(pojo, "propertyNames"); 54 } 55 56 private Object getField(Object pojo, String fieldName) { 57 try { 58 Field field = pojo.getClass().getSuperclass().getDeclaredField(fieldName); 59 field.setAccessible(true); 60 return field.get(pojo); 61 } catch (IllegalArgumentException e) { 62 throw new TCRuntimeException(e); 63 } catch (IllegalAccessException e) { 64 throw new TCRuntimeException(e); 65 } catch (NoSuchFieldException e) { 66 throw new TCRuntimeException(e); 67 } 68 } 69 70 public void hydrate(ClientObjectManager objectManager, TCObject tcObject, DNA dna, Object po) throws IOException , 71 IllegalArgumentException , ClassNotFoundException { 72 } 74 75 public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object pojo) { 76 String className = pojo.getClass().getName(); 77 writer.addPhysicalAction(CLASSNAME_FIELD_NAME, className); 78 79 Class clazz = (Class )getClazz(pojo); 80 writer.addPhysicalAction(CLAZZ_FIELD_NAME, clazz); 81 82 Object propertyNames = getPropertyNames(pojo); 83 Object dehydratablePropertyNames = getDehydratableObject(propertyNames, objectManager); 84 writer.addPhysicalAction(PROPERTY_NAMES_FIELD_NAME, dehydratablePropertyNames); 85 } 86 87 public Object getNewInstance(ClientObjectManager objectManager, DNA dna) throws IOException , ClassNotFoundException { 88 DNACursor cursor = dna.getCursor(); 89 Assert.assertEquals(3, cursor.getActionCount()); 90 91 cursor.next(encoding); 92 PhysicalAction a = cursor.getPhysicalAction(); 93 String className = (String )a.getObject(); 94 95 cursor.next(encoding); 96 a = cursor.getPhysicalAction(); 97 Class target = (Class ) a.getObject(); 98 99 cursor.next(encoding); 100 a = cursor.getPhysicalAction(); 101 Object propertyNames = a.getObject(); 102 propertyNames = objectManager.lookupObject((ObjectID) propertyNames); 103 104 return create(target, className, propertyNames); 105 } 106 107 private Object create(Class target, String className, Object propertyNames) { 108 try { 109 Class clazz = target.getClassLoader().loadClass(className); 110 111 Constructor c = clazz.getDeclaredConstructor(new Class []{Class .class, String [].class}); 112 c.setAccessible(true); 113 Object o = c.newInstance(new Object []{target, propertyNames}); 114 115 return o; 116 } catch (NoSuchMethodException e) { 117 throw new TCRuntimeException(e); 118 } catch (IllegalArgumentException e) { 119 throw new TCRuntimeException(e); 120 } catch (IllegalAccessException e) { 121 throw new TCRuntimeException(e); 122 } catch (InvocationTargetException e) { 123 throw new TCRuntimeException(e); 124 } catch (ClassNotFoundException e) { 125 throw new TCRuntimeException(e); 126 } catch (InstantiationException e) { 127 throw new TCRuntimeException(e); 128 } 129 } 130 131 public Map connectedCopy(Object source, Object dest, Map visited, ClientObjectManager objectManager, 132 OptimisticTransactionManager txManager) { 133 throw new TCNotSupportedMethodException(); 134 } 135 } 136 | Popular Tags |