001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.modules.iBatis_2_2_0.object.applicator;
006:
007: import com.tc.exception.TCNotSupportedMethodException;
008: import com.tc.exception.TCRuntimeException;
009: import com.tc.object.ClientObjectManager;
010: import com.tc.object.ObjectID;
011: import com.tc.object.TCObject;
012: import com.tc.object.TraversedReferences;
013: import com.tc.object.applicator.BaseApplicator;
014: import com.tc.object.dna.api.DNA;
015: import com.tc.object.dna.api.DNACursor;
016: import com.tc.object.dna.api.DNAWriter;
017: import com.tc.object.dna.api.DNAEncoding;
018: import com.tc.object.dna.api.PhysicalAction;
019: import com.tc.object.tx.optimistic.OptimisticTransactionManager;
020: import com.tc.util.Assert;
021:
022: import java.io.IOException;
023: import java.lang.reflect.Constructor;
024: import java.lang.reflect.Field;
025: import java.lang.reflect.InvocationTargetException;
026: import java.util.Map;
027:
028: /**
029: * @author Antonio Si
030: */
031: public class IBatisAccessPlanApplicator extends BaseApplicator {
032: private static final String CLASSNAME_FIELD_NAME = "com.ibatis.sqlmap.engine.accessplan.BaseAccessPlan.className";
033: private static final String CLAZZ_FIELD_NAME = "com.ibatis.sqlmap.engine.accessplan.BaseAccessPlan.clazz";
034: private static final String PROPERTY_NAMES_FIELD_NAME = "com.ibatis.sqlmap.engine.accessplan.BaseAccessPlan.clazz.propertyNames";
035:
036: public IBatisAccessPlanApplicator(DNAEncoding encoding) {
037: super (encoding);
038: }
039:
040: public TraversedReferences getPortableObjects(Object pojo,
041: TraversedReferences addTo) {
042: addTo.addAnonymousReference(getClazz(pojo));
043: addTo.addAnonymousReference(getPropertyNames(pojo));
044: return addTo;
045: }
046:
047: private Object getClazz(Object pojo) {
048: //return get(pojo, "getGetters", new Class[] {}, new Object[] {});
049: return getField(pojo, "clazz");
050: }
051:
052: private Object getPropertyNames(Object pojo) {
053: //return get(pojo, "getGetters", new Class[] {}, new Object[] {});
054: return getField(pojo, "propertyNames");
055: }
056:
057: private Object getField(Object pojo, String fieldName) {
058: try {
059: Field field = pojo.getClass().getSuperclass()
060: .getDeclaredField(fieldName);
061: field.setAccessible(true);
062: return field.get(pojo);
063: } catch (IllegalArgumentException e) {
064: throw new TCRuntimeException(e);
065: } catch (IllegalAccessException e) {
066: throw new TCRuntimeException(e);
067: } catch (NoSuchFieldException e) {
068: throw new TCRuntimeException(e);
069: }
070: }
071:
072: public void hydrate(ClientObjectManager objectManager,
073: TCObject tcObject, DNA dna, Object po) throws IOException,
074: IllegalArgumentException, ClassNotFoundException {
075: //
076: }
077:
078: public void dehydrate(ClientObjectManager objectManager,
079: TCObject tcObject, DNAWriter writer, Object pojo) {
080: String className = pojo.getClass().getName();
081: writer.addPhysicalAction(CLASSNAME_FIELD_NAME, className);
082:
083: Class clazz = (Class) getClazz(pojo);
084: writer.addPhysicalAction(CLAZZ_FIELD_NAME, clazz);
085:
086: Object propertyNames = getPropertyNames(pojo);
087: Object dehydratablePropertyNames = getDehydratableObject(
088: propertyNames, objectManager);
089: writer.addPhysicalAction(PROPERTY_NAMES_FIELD_NAME,
090: dehydratablePropertyNames);
091: }
092:
093: public Object getNewInstance(ClientObjectManager objectManager,
094: DNA dna) throws IOException, ClassNotFoundException {
095: DNACursor cursor = dna.getCursor();
096: Assert.assertEquals(3, cursor.getActionCount());
097:
098: cursor.next(encoding);
099: PhysicalAction a = cursor.getPhysicalAction();
100: String className = (String) a.getObject();
101:
102: cursor.next(encoding);
103: a = cursor.getPhysicalAction();
104: Class target = (Class) a.getObject();
105:
106: cursor.next(encoding);
107: a = cursor.getPhysicalAction();
108: Object propertyNames = a.getObject();
109: propertyNames = objectManager
110: .lookupObject((ObjectID) propertyNames);
111:
112: return create(target, className, propertyNames);
113: }
114:
115: private Object create(Class target, String className,
116: Object propertyNames) {
117: try {
118: Class clazz = target.getClassLoader().loadClass(className);
119:
120: Constructor c = clazz.getDeclaredConstructor(new Class[] {
121: Class.class, String[].class });
122: c.setAccessible(true);
123: Object o = c.newInstance(new Object[] { target,
124: propertyNames });
125:
126: return o;
127: } catch (NoSuchMethodException e) {
128: throw new TCRuntimeException(e);
129: } catch (IllegalArgumentException e) {
130: throw new TCRuntimeException(e);
131: } catch (IllegalAccessException e) {
132: throw new TCRuntimeException(e);
133: } catch (InvocationTargetException e) {
134: throw new TCRuntimeException(e);
135: } catch (ClassNotFoundException e) {
136: throw new TCRuntimeException(e);
137: } catch (InstantiationException e) {
138: throw new TCRuntimeException(e);
139: }
140: }
141:
142: public Map connectedCopy(Object source, Object dest, Map visited,
143: ClientObjectManager objectManager,
144: OptimisticTransactionManager txManager) {
145: throw new TCNotSupportedMethodException();
146: }
147: }
|