KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > identitymaps > FullIdentityMap


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.identitymaps;
23
24 import java.util.*;
25
26 /**
27  * <p><b>Purpose</b>: A FullIdentityMap holds all objects stored within it for the life of the application
28  * <p><b>Responsibilities</b>:<ul>
29  * <li> Guarantees identity
30  * <li> Holds all cached objects indefinetly.
31  * </ul>
32  * @since TOPLink/Java 1.0
33  */

34 public class FullIdentityMap extends IdentityMap {
35
36     /** Hashtable of CacheKeys stored using their key */
37     protected Hashtable cacheKeys;
38
39     public FullIdentityMap(int size) {
40         super(size);
41         cacheKeys = new Hashtable(size);
42     }
43
44     /**
45      * INTERNAL:
46      * Clones itself.
47      */

48     public Object JavaDoc clone() {
49         FullIdentityMap clone = (FullIdentityMap)super.clone();
50         clone.setCacheKeys(new Hashtable(getCacheKeys().size()));
51
52         for (Enumeration cacheKeysEnum = getCacheKeys().elements();
53                  cacheKeysEnum.hasMoreElements();) {
54             CacheKey key = (CacheKey)((CacheKey)cacheKeysEnum.nextElement()).clone();
55             clone.getCacheKeys().put(key, key);
56         }
57
58         return clone;
59     }
60
61     /**
62      * INTERNAL:
63      * Used to print all the Locks in every identity map in this session.
64      * The output of this method will go to log passed in as a parameter.
65      */

66     public void collectLocks(HashMap threadList) {
67         Iterator cacheKeyIterator = this.cacheKeys.values().iterator();
68         while (cacheKeyIterator.hasNext()) {
69             CacheKey cacheKey = (CacheKey)cacheKeyIterator.next();
70             if (cacheKey.isAcquired()) {
71                 Thread JavaDoc activeThread = cacheKey.getMutex().getActiveThread();
72                 Set set = (Set)threadList.get(activeThread);
73                 if (set == null) {
74                     set = new HashSet();
75                     threadList.put(activeThread, set);
76                 }
77                 set.add(cacheKey);
78             }
79         }
80     }
81
82     /**
83      * Allow for the cache to be iterated on.
84      */

85     public Enumeration elements() {
86         return new IdentityMapEnumeration(this);
87     }
88
89     /**
90      * Return the object indexed in the recevier at the cache key.
91      * If now object for the key exists, return null.
92      * @return a CacheKey for the primary key or null
93      */

94     protected synchronized CacheKey getCacheKey(CacheKey searchKey) {
95         return (CacheKey)getCacheKeys().get(searchKey);
96     }
97
98     public Hashtable getCacheKeys() {
99         return cacheKeys;
100     }
101
102     /**
103      * Return the number of objects in the IdentityMap.
104      */

105     public int getSize() {
106         return cacheKeys.size();
107     }
108
109     /**
110      * Return the number of actual objects of type myClass in the IdentityMap.
111      * Recurse = true will include subclasses of myClass in the count.
112        */

113     public int getSize(Class JavaDoc myClass, boolean recurse) {
114         int i = 0;
115         Enumeration keys = getCacheKeys().keys();
116
117         while (keys.hasMoreElements()) {
118             CacheKey key = (CacheKey)keys.nextElement();
119             Object JavaDoc obj = key.getObject();
120
121             if (obj != null) {
122                 if (recurse && myClass.isInstance(obj)) {
123                     i++;
124                 } else if (obj.getClass().equals(myClass)) {
125                     i++;
126                 }
127             }
128         }
129
130         return i;
131     }
132
133     /**
134      * Allow for the cache keys to be iterated on.
135      */

136     public Enumeration keys() {
137         return new IdentityMapKeyEnumeration(this);
138     }
139
140     /**
141      * Store the object in the cache at its primary key.
142      * @param primaryKey is the primary key for the object.
143      * @param object is the domain object to cache.
144      * @param writeLockValue is the current write lock value of object, if null the version is ignored.
145      */

146     public CacheKey put(Vector primaryKey, Object JavaDoc object, Object JavaDoc writeLockValue, long readTime) {
147         CacheKey cacheKey = getCacheKey(primaryKey);
148
149         // Find the cache key in the hashtable, reset it.
150
if (cacheKey != null) {
151             // The cache key has to be locked during the re-setting, keep other threads from accessing the object.
152
resetCacheKey(cacheKey, object, writeLockValue);
153
154             // Still must put the object to ensure the LRU caching.
155
put(cacheKey);
156         } else {
157             // Cache key not found, create a new one and put it into the hashtable.
158
cacheKey = createCacheKey(primaryKey, object, writeLockValue, readTime);
159
160             put(cacheKey);
161         }
162
163         return cacheKey;
164     }
165
166     /**
167      * Store the object in the cache with the cache key.
168      */

169     protected void put(CacheKey cacheKey) {
170         //synchronized because subclasses may not sync around call
171
synchronized(this){
172             getCacheKeys().put(cacheKey, cacheKey);
173         }
174         cacheKey.setOwningMap(this);
175     }
176
177     /**
178      * Removes the CacheKey from the Hashtable.
179      * @return The object held within the CacheKey or null if no object cached for given primaryKey
180      */

181     public Object JavaDoc remove(CacheKey cacheKey) {
182         if (cacheKey != null) {
183             //Cache key needs to be locked when removing from the Hashtable
184
cacheKey.acquire();
185
186             //remove opration from the hashtable has to be synchronized
187
synchronized (this) {
188                 getCacheKeys().remove(cacheKey);
189             }
190
191             //Cache key needs to be released after removing from the Hashtable
192
cacheKey.release();
193         } else {
194             return null;
195         }
196
197         return cacheKey.getObject();
198     }
199
200     public void resetCacheKey(CacheKey key, Object JavaDoc object, Object JavaDoc writeLockValue) {
201         resetCacheKey(key, object, writeLockValue, 0);
202     }
203
204     public void resetCacheKey(CacheKey key, Object JavaDoc object, Object JavaDoc writeLockValue, long readTime) {
205         key.acquire();
206         key.setObject(object);
207         key.setWriteLockValue(writeLockValue);
208         key.setReadTime(readTime);
209         key.release();
210     }
211
212     protected void setCacheKeys(Hashtable cacheKeys) {
213         this.cacheKeys = cacheKeys;
214     }
215 }
216
Popular Tags