KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > applicator > PhysicalApplicator


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.IdentityHashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
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   /**
40    * return the key value pairs of field names to shared objects for this source. We already updated the literals and
41    * set the new TCObjectClone
42    */

43   public Map JavaDoc connectedCopy(Object JavaDoc source, Object JavaDoc dest, Map JavaDoc visited, ClientObjectManager objectManager,
44                            OptimisticTransactionManager txManager) {
45     Map JavaDoc map = new HashMap JavaDoc();
46     Map JavaDoc cloned = new IdentityHashMap JavaDoc();
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 JavaDoc i = map.keySet().iterator(); i.hasNext();) {
56       String JavaDoc k = (String JavaDoc) i.next();
57       Object JavaDoc v = map.get(k);
58
59       Object JavaDoc 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 JavaDoc pojo, TraversedReferences addTo) {
68     if (!(pojo instanceof TransparentAccess)) return addTo;
69
70     Map JavaDoc map = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc po) throws IOException JavaDoc,
88       ClassNotFoundException JavaDoc {
89     DNACursor cursor = dna.getCursor();
90     String JavaDoc fieldName;
91     Object JavaDoc 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 JavaDoc pojo) {
104     if (!objectManager.isPortableInstance(pojo)) { return; }
105
106     TCClass tcc = clazz;
107     TCField[] fields;
108
109     Map JavaDoc fieldValues = null;
110
111     while (tcc != null) {
112       fields = tcc.getPortableFields();
113
114       if (fields.length > 0 && fieldValues == null) {
115         fieldValues = new HashMap JavaDoc();
116         if (pojo instanceof TransparentAccess) {
117           // only need to do this once. The generated method takes care of walking up the class hierarchy
118
getAllFields(pojo, fieldValues);
119         } else {
120           throw new AssertionError JavaDoc("wrong type: " + pojo.getClass());
121         }
122       }
123
124       for (int i = 0; i < fields.length; i++) {
125         String JavaDoc fieldName = fields[i].getName();
126         Object JavaDoc fieldValue = fieldValues.get(fieldName);
127
128         if (fieldValue == null) {
129           if (!fieldValues.containsKey(fieldName)) { throw new AssertionError JavaDoc(
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 JavaDoc value = getDehydratableObject(fieldValue, objectManager);
141         if (value == null) {
142           // instead of ignoring non-portable objects we send ObjectID.NULL_ID so that the state can
143
// be created at the server correctly if needed. Another reason is to null the reference across the cluster if
144
// such an attempt was made.
145
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 JavaDoc parentObject = fieldValues.get(clazz.getParentFieldName());
154       Assert.assertNotNull("parentObject", parentObject);
155       Object JavaDoc parentObjectID = getDehydratableObject(parentObject, objectManager);
156       if (parentObjectID != null) {
157         writer.setParentObjectID((ObjectID) parentObjectID);
158       }
159     }
160
161   }
162
163   public Object JavaDoc getNewInstance(ClientObjectManager objectManager, DNA dna) {
164     throw new UnsupportedOperationException JavaDoc();
165   }
166
167   private static void getAllFields(Object JavaDoc o, Map JavaDoc dest) {
168     // This is ugly, but doing the getStackTrace stuff inside of __tc_getallfields() seems
169
// to trip a hotspot bug (DEV-67)
170
if (o instanceof Throwable JavaDoc) {
171       // This has the side effect of getting the stack trace initilized in the target object
172
((Throwable JavaDoc) o).getStackTrace();
173     }
174     ((TransparentAccess) o).__tc_getallfields(dest);
175   }
176
177 }
178
Popular Tags