KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package com.tc.object.applicator;
5
6 import com.tc.logging.TCLogger;
7 import com.tc.logging.TCLogging;
8 import com.tc.object.ClientObjectManager;
9 import com.tc.object.LiteralValues;
10 import com.tc.object.ObjectID;
11 import com.tc.object.TCObject;
12 import com.tc.object.dna.impl.DNAEncoding;
13 import com.tc.util.Assert;
14
15 import java.util.Map JavaDoc;
16
17 public abstract class BaseApplicator implements ChangeApplicator {
18
19   private static final TCLogger logger = TCLogging.getLogger(BaseApplicator.class);
20   private static final LiteralValues literals = new LiteralValues();
21
22   protected final DNAEncoding encoding;
23
24   protected BaseApplicator(DNAEncoding encoding) {
25     this.encoding = encoding;
26   }
27
28   protected final Object JavaDoc getDehydratableObject(Object JavaDoc pojo, ClientObjectManager objectManager) {
29     if (pojo == null) {
30       return ObjectID.NULL_ID;
31     } else if (literals.isLiteralInstance(pojo)) {
32       return pojo;
33     } else {
34       TCObject tcObject = objectManager.lookupExistingOrNull(pojo);
35       if (tcObject == null) {
36         // When we dehydrate complex objects, traverser bails out on the first non portable
37
// object. We dont want to dehydrate things that are not added in the ClientObjectManager.
38
logger
39             .warn("Not dehydrating object of type " + pojo.getClass().getName() + "@" + System.identityHashCode(pojo));
40         return null;
41       }
42       return tcObject.getObjectID();
43     }
44   }
45
46   protected final boolean isLiteralInstance(Object JavaDoc pojo) {
47     return literals.isLiteralInstance(pojo);
48   }
49
50   protected boolean isPortableReference(Class JavaDoc c) {
51     return !literals.isLiteral(c.getName());
52   }
53
54   protected Object JavaDoc createParentIfNecessary(Map JavaDoc visited, ClientObjectManager objectManager, Map JavaDoc cloned, Object JavaDoc v) {
55     return objectManager.createParentCopyInstanceIfNecessary(visited, cloned, v);
56   }
57
58   protected Object JavaDoc createCopyIfNecessary(ClientObjectManager objectManager, Map JavaDoc visited, Map JavaDoc cloned, Object JavaDoc originalValue) {
59     Object JavaDoc copyKey;
60     if (originalValue == null || isLiteralInstance(originalValue)) {
61       copyKey = originalValue;
62     } else if (visited.containsKey(originalValue)) {
63       Assert.eval(visited.get(originalValue) != null);
64       copyKey = visited.get(originalValue);
65     } else {
66       Assert.eval(!isLiteralInstance(originalValue));
67       copyKey = objectManager.createNewCopyInstance(originalValue, createParentIfNecessary(visited, objectManager, cloned, originalValue));
68
69       visited.put(originalValue, copyKey);
70       cloned.put(originalValue, copyKey);
71     }
72     return copyKey;
73   }
74
75 }
76
Popular Tags