KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > DbRemote > ProxyObjectGate


1 /* ProxyObjectGate.java
2 */

3
4 package org.ozoneDB.core.DbRemote;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 import org.ozoneDB.OzoneProxy;
10 import org.ozoneDB.core.GarbageCollector;
11 import org.ozoneDB.core.ObjectID;
12
13 /**
14     A gate for Proxy objects. Every Proxy which leaves the Database should wander
15     through a ProxyObjectGate to be registered (so that the objects referenced by such a Proxy
16     are known to be referenced and thus reachable).
17
18     Every Proxy which left the Database, was registered and is now known to be unreachable should
19     sign itself off exactly one time.
20
21     @author <A HREF="http://www.medium.net/">Medium.net</A>
22 */

23 public class ProxyObjectGate {
24     /**
25         This is a Mapping from {@link ObjectID} to Integer. It represents the
26         count of references this client holds to the database object represented by the objectID.
27
28         Entries have to be added when returning {@link OzoneProxy}s directly or indirectly.
29         Entries have to be removed if a {#link OzoneProxy} is finalize()d on the client side.
30         This way, the ozoneDB always know which objects are referenced and thus have to be
31         considered reachable, even if they were not reachable internally.
32     */

33     protected HashMap JavaDoc objectsReferencesByClient;
34
35     protected GarbageCollector garbageCollectorToBeNotifiedOfExportedReferences;
36
37     /**
38         This is an Integer which represents the number "1".
39     */

40     protected final static Integer JavaDoc one = new Integer JavaDoc(1);
41
42     /**
43         Creates a new ProxyObjectGate.
44     */

45     protected ProxyObjectGate() {
46         objectsReferencesByClient = new HashMap JavaDoc();
47     }
48
49     public void addObjectReferencedByClient(OzoneProxy proxy) {
50         if (proxy!=null) {
51             synchronized (objectsReferencesByClient) {
52                 ObjectID id = proxy.remoteID();
53
54                 if (garbageCollectorToBeNotifiedOfExportedReferences!=null) {
55                     garbageCollectorToBeNotifiedOfExportedReferences.notifyDatabaseObjectIsAboutToBeExported(id);
56                 }
57
58                 Object JavaDoc oldEntry = objectsReferencesByClient.put(id,one);
59
60                 if (oldEntry!=null) {
61                     objectsReferencesByClient.put(id,new Integer JavaDoc(((Integer JavaDoc) oldEntry).intValue()+1));
62                 }
63             }
64         }
65     }
66
67     protected void removeObjectReferencedByClient(OzoneProxy proxy) {
68         removeObjectReferencedByClient(proxy.remoteID());
69     }
70
71     protected void removeObjectReferencedByClient(ObjectID id) {
72         synchronized (objectsReferencesByClient) {
73             Object JavaDoc oldEntry = objectsReferencesByClient.remove(id);
74
75             if (oldEntry!=null) {
76                 if (oldEntry!=one) {
77                     int count = ((Integer JavaDoc) oldEntry).intValue();
78
79                     count--;
80
81                     if (count>0) {
82                         if (count>1) {
83                             objectsReferencesByClient.put(id,new Integer JavaDoc(count));
84                         } else {
85                             objectsReferencesByClient.put(id,one);
86                         }
87                     }
88                 } else {
89                     // The reference count was exactly one, now the reference is removed from the map.
90
}
91             } else {
92                 // oldEntry never may be null. What should we do here?
93
}
94         }
95     }
96
97     /**
98         Starts filtering references to database objects ({@link OzoneProxy}s) which
99         are exported to the client.
100         Every reference which is exported will be notified to the given GarbageCollector.
101         Additionally, references which are known to be used by the client are notified to the
102         given GarbageCollector within this call.
103     */

104     public void startFilterDatabaseObjectReferencesExports(GarbageCollector garbageCollector) {
105
106         synchronized (objectsReferencesByClient) {
107             this.garbageCollectorToBeNotifiedOfExportedReferences = garbageCollector;
108
109             Iterator JavaDoc i = objectsReferencesByClient.keySet().iterator();
110
111             while (i.hasNext()) {
112                 garbageCollector.notifyDatabaseObjectIsExported((ObjectID) i.next());
113             }
114         }
115     }
116 }
Popular Tags