001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object;
005:
006: import com.tc.exception.ImplementMe;
007: import com.tc.exception.TCNonPortableObjectError;
008: import com.tc.object.appevent.ApplicationEvent;
009: import com.tc.object.appevent.ApplicationEventContext;
010: import com.tc.object.bytecode.Manageable;
011: import com.tc.object.dna.api.DNA;
012: import com.tc.object.tx.ClientTransactionManager;
013: import com.tc.object.tx.optimistic.OptimisticTransactionManager;
014: import com.tc.util.Assert;
015:
016: import java.lang.ref.ReferenceQueue;
017: import java.lang.ref.WeakReference;
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.IdentityHashMap;
021: import java.util.Map;
022:
023: /**
024: * @author steve
025: */
026: public class TestClientObjectManager implements ClientObjectManager {
027: public final Map objects = new HashMap();
028: public final Map object2TCObject = new IdentityHashMap();
029: private int idSequence = 1;
030: private Object root = new IdentityHashMap();
031: private boolean isManaged;
032: private ReferenceQueue referenceQueue;
033: private ClientTransactionManager txManager;
034:
035: public void add(Object id, TCObject tc) {
036: objects.put(id, tc);
037: this .object2TCObject.put(tc.getPeerObject(), tc);
038: }
039:
040: public void setIsManaged(boolean b) {
041: this .isManaged = b;
042: }
043:
044: public boolean getIsManaged() {
045: return this .isManaged;
046: }
047:
048: public boolean isManaged(Object pojo) {
049: return this .object2TCObject.containsKey(pojo) || isManaged;
050: }
051:
052: public boolean isPortableInstance(Object pojo) {
053: return true;
054: }
055:
056: public boolean isPortableClass(Class clazz) {
057: return true;
058: }
059:
060: public void sharedIfManaged(Object pojo) {
061: if (isManaged(pojo)) {
062: lookupOrCreate(pojo);
063: }
064: }
065:
066: public synchronized TCObject lookupOrCreate(Object obj) {
067: // System.out.println(this + ".lookupOrCreate(" + obj + ")");
068: TCObject rv = lookup(obj);
069: if (rv == null) {
070: rv = new MockTCObject(new ObjectID(idSequence++), obj);
071: object2TCObject.put(obj, rv);
072: if (obj instanceof Manageable) {
073: ((Manageable) obj).__tc_managed(rv);
074: }
075: }
076: return rv;
077: }
078:
079: private synchronized TCObject lookup(Object obj) {
080: TCObject rv = (TCObject) object2TCObject.get(obj);
081: return rv;
082: }
083:
084: public Object lookupOrCreateRoot(String name, Object candidate) {
085: Object rv = null;
086: if (candidate == null) {
087: rv = this .root;
088: } else {
089: rv = candidate;
090: }
091: Assert.assertNotNull(rv);
092: return rv;
093: }
094:
095: public TCObject lookupIfLocal(ObjectID id) {
096: throw new ImplementMe();
097: }
098:
099: public Collection getAllObjectIDsAndClear(Collection c) {
100: c.addAll(objects.keySet());
101: return c;
102: }
103:
104: public TCObject lookup(ObjectID id) {
105: System.out.println(this + ".lookup(" + id + ")");
106: return (TCObject) objects.get(id);
107: }
108:
109: public WeakReference createNewPeer(TCClass clazz, int size,
110: ObjectID id, ObjectID parentID) {
111: throw new ImplementMe();
112: }
113:
114: public Object lookupObject(ObjectID id) {
115: return ((TCObject) objects.get(id)).getPeerObject();
116: }
117:
118: public Object lookupObject(ObjectID id, ObjectID parentContext) {
119: return ((TCObject) objects.get(id)).getPeerObject();
120: }
121:
122: public TCClass getOrCreateClass(Class clazz) {
123: throw new ImplementMe();
124: }
125:
126: public void setTransactionManager(ClientTransactionManager txManager) {
127: this .txManager = txManager;
128: }
129:
130: public void setReferenceQueue(ReferenceQueue rq) {
131: this .referenceQueue = rq;
132: }
133:
134: public ReferenceQueue getReferenceQueue() {
135: return this .referenceQueue;
136: }
137:
138: public ObjectID lookupExistingObjectID(Object obj) {
139: return ((TCObject) this .object2TCObject.get(obj)).getObjectID();
140: }
141:
142: public void markReferenced(TCObject tcobj) {
143: // mark referenced
144: }
145:
146: public void shutdown() {
147: //
148: }
149:
150: public ClientTransactionManager getTransactionManager() {
151: return txManager;
152: }
153:
154: public Class getClassFor(String className, String loaderDesc) {
155: throw new ImplementMe();
156: }
157:
158: public TCObject lookupExistingOrNull(Object pojo) {
159: // if (isManaged) {
160: // lookupOrCreate(pojo);
161: // }
162:
163: return (TCObject) this .object2TCObject.get(pojo);
164: }
165:
166: public Object lookupRoot(String name) {
167: throw new ImplementMe();
168: }
169:
170: public void unpause() {
171: return;
172: }
173:
174: public void pause() {
175: return;
176: }
177:
178: public void starting() {
179: return;
180: }
181:
182: public void checkPortabilityOfField(Object value, String fieldName,
183: Object pojo) throws TCNonPortableObjectError {
184: return;
185: }
186:
187: public void checkPortabilityOfLogicalAction(Object[] params,
188: int index, String methodName, Object pojo)
189: throws TCNonPortableObjectError {
190: return;
191: }
192:
193: public WeakReference createNewPeer(TCClass clazz, DNA dna) {
194: throw new ImplementMe();
195: }
196:
197: public Object deepCopy(Object source,
198: OptimisticTransactionManager optimisticTxManager) {
199: throw new ImplementMe();
200: }
201:
202: public Object createNewCopyInstance(Object source, Object parent) {
203: throw new ImplementMe();
204: }
205:
206: public Object createParentCopyInstanceIfNecessary(Map visited,
207: Map cloned, Object v) {
208: throw new ImplementMe();
209: }
210:
211: public void replaceRootIDIfNecessary(String rootName,
212: ObjectID newRootID) {
213: throw new ImplementMe();
214: }
215:
216: public Object lookupOrCreateRoot(String name, Object obj,
217: boolean dsoFinal) {
218: throw new ImplementMe();
219: }
220:
221: public TCObject lookupOrShare(Object pojo) {
222: throw new ImplementMe();
223: }
224:
225: public boolean isCreationInProgress() {
226: return false;
227: }
228:
229: public void addPendingCreateObjectsToTransaction() {
230: // do nothing
231: }
232:
233: public boolean hasPendingCreateObjects() {
234: return false;
235: }
236:
237: public Object lookupObjectNoDepth(ObjectID id) {
238: throw new ImplementMe();
239: }
240:
241: public Object lookupOrCreateRootNoDepth(String rootName,
242: Object object) {
243: throw new ImplementMe();
244: }
245:
246: public Object createOrReplaceRoot(String rootName, Object r) {
247: throw new ImplementMe();
248: }
249:
250: public void storeObjectHierarchy(Object pojo,
251: ApplicationEventContext context) {
252: throw new ImplementMe();
253: }
254:
255: public void sendApplicationEvent(Object pojo, ApplicationEvent event) {
256: throw new ImplementMe();
257: }
258:
259: public Object cloneAndInvokeLogicalOperation(Object pojo,
260: String methodName, Object[] parameters) {
261: throw new ImplementMe();
262: }
263:
264: }
|