KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > impl > InMemoryManagedObjectStore


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.objectserver.impl;
5
6 import com.tc.object.ObjectID;
7 import com.tc.objectserver.api.ShutdownError;
8 import com.tc.objectserver.core.api.ManagedObject;
9 import com.tc.objectserver.managedobject.ManagedObjectStateFactory;
10 import com.tc.objectserver.persistence.api.ManagedObjectStore;
11 import com.tc.objectserver.persistence.api.PersistenceTransaction;
12 import com.tc.text.PrettyPrinter;
13 import com.tc.util.Assert;
14 import com.tc.util.SyncObjectIdSet;
15 import com.tc.util.SyncObjectIdSetImpl;
16
17 import java.util.Collection JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 public class InMemoryManagedObjectStore implements ManagedObjectStore {
26
27   private long objectIDSequence = 1000;
28   private final Map JavaDoc roots = new HashMap JavaDoc();
29   private final Map JavaDoc managed;
30   private boolean inShutdown = false;
31
32   public InMemoryManagedObjectStore(Map JavaDoc managed) {
33     this.managed = managed;
34   }
35
36   public synchronized boolean containsObject(ObjectID id) {
37     assertNotInShutdown();
38     return this.managed.containsKey(id);
39   }
40
41   public synchronized void addNewObject(ManagedObject managedObject) {
42     assertNotInShutdown();
43     localPut(managedObject);
44   }
45
46   private void localPut(ManagedObject managedObject) {
47     this.managed.put(managedObject.getID(), managedObject);
48   }
49
50   public synchronized void commitObject(PersistenceTransaction tx, ManagedObject managedObject) {
51     assertNotInShutdown();
52     assertContains(managedObject);
53   }
54
55   public synchronized void commitAllObjects(PersistenceTransaction tx, Collection JavaDoc managedObjects) {
56     assertNotInShutdown();
57     for (Iterator JavaDoc i = managedObjects.iterator(); i.hasNext();) {
58       assertContains((ManagedObject) i.next());
59     }
60   }
61
62   private void removeObjectByID(PersistenceTransaction tx, ObjectID id) {
63     this.managed.remove(id);
64   }
65
66   public synchronized void removeAllObjectsByID(PersistenceTransaction tx, Collection JavaDoc ids) {
67     assertNotInShutdown();
68     for (Iterator JavaDoc i = ids.iterator(); i.hasNext();) {
69       removeObjectByID(tx, (ObjectID) i.next());
70     }
71   }
72
73   public void removeAllObjectsByIDNow(PersistenceTransaction tx, Collection JavaDoc objectIds) {
74     removeAllObjectsByID(tx, objectIds);
75   }
76
77   public synchronized SyncObjectIdSet getAllObjectIDs() {
78     assertNotInShutdown();
79     SyncObjectIdSet rv = new SyncObjectIdSetImpl();
80     rv.addAll(managed.keySet());
81     return rv;
82   }
83
84   public synchronized int getObjectCount() {
85     return managed.size();
86   }
87
88   public synchronized ManagedObject getObjectByID(ObjectID id) {
89     assertNotInShutdown();
90     return (ManagedObject) this.managed.get(id);
91   }
92
93   public synchronized PrettyPrinter prettyPrint(PrettyPrinter out) {
94     out.println(getClass().getName()).duplicateAndIndent().print("managed: ").visit(managed).println();
95     return out;
96   }
97
98   public synchronized void shutdown() {
99     assertNotInShutdown();
100     this.inShutdown = true;
101   }
102
103   public synchronized boolean inShutdown() {
104     return this.inShutdown;
105   }
106
107   private synchronized void assertNotInShutdown() {
108     if (inShutdown) throw new ShutdownError();
109   }
110
111   private synchronized void assertContains(ManagedObject managedObject) {
112     if (!containsObject(managedObject.getID())) throw new AssertionError JavaDoc("Object store does not contain "
113                                                                          + managedObject);
114   }
115
116   public ObjectID getRootID(String JavaDoc name) {
117     return (ObjectID) (roots.containsKey(name) ? roots.get(name) : ObjectID.NULL_ID);
118   }
119
120   public Set getRoots() {
121     return new HashSet JavaDoc(roots.values());
122   }
123
124   public Set getRootNames() {
125     return roots.keySet();
126   }
127
128   public void addNewRoot(PersistenceTransaction tx, String JavaDoc rootName, ObjectID id) {
129     roots.put(rootName, id);
130   }
131
132   public synchronized long nextObjectIDBatch(int batchSize) {
133     long rv = objectIDSequence;
134     objectIDSequence += batchSize;
135     return rv;
136   }
137
138   public void setNextAvailableObjectID(long startID) {
139     Assert.assertTrue(startID >= objectIDSequence);
140     objectIDSequence = startID;
141   }
142
143   public void setTransientData(ManagedObjectStateFactory stateFactory) {
144     assertNotInShutdown();
145   }
146
147   public Map JavaDoc getRootNamesToIDsMap() {
148     return Collections.unmodifiableMap(roots);
149   }
150
151 }
152
Popular Tags