KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > sessions > IdentityMapAccessor


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, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.sessions;
23
24 import java.util.*;
25 import oracle.toplink.essentials.internal.identitymaps.*;
26 import oracle.toplink.essentials.internal.descriptors.*;
27 import oracle.toplink.essentials.queryframework.*;
28 import oracle.toplink.essentials.expressions.*;
29 import oracle.toplink.essentials.exceptions.*;
30 import oracle.toplink.essentials.sessions.Record;
31 import oracle.toplink.essentials.sessions.DatabaseRecord;
32 import oracle.toplink.essentials.internal.sessions.AbstractSession;
33 import oracle.toplink.essentials.internal.sessions.AbstractRecord;
34 import oracle.toplink.essentials.descriptors.ClassDescriptor;
35 import oracle.toplink.essentials.logging.SessionLog;
36 import oracle.toplink.essentials.internal.helper.WriteLockManager;
37 import oracle.toplink.essentials.internal.queryframework.JoinedAttributeManager;
38
39 /**
40  * INTERNAL:
41  * Internal subclass that provides access to identity maps through the session.
42  * Implements the IdentityMapAccessor interface which provides all publicly available
43  * identity map functionality to users.
44  * This is the main class that should be used to access identity maps. In general, any
45  * function that accesses the identity map manager should go through this class
46  * Any session specific functionality appears in subclasses
47  */

48 public class IdentityMapAccessor implements oracle.toplink.essentials.sessions.IdentityMapAccessor, java.io.Serializable JavaDoc {
49
50     /** This is the identity map manager for this accessor. It should only be accessed through the getter **/
51     protected IdentityMapManager identityMapManager = null;
52     protected AbstractSession session = null;
53
54     /**
55      * INTERNAL:
56      * An IdentityMapAccessor sits between the session and the identityMapManager
57      * It needs references in both directions
58      */

59     public IdentityMapAccessor(AbstractSession session, IdentityMapManager identityMapManager) {
60         this.session = session;
61         this.identityMapManager = identityMapManager;
62     }
63
64     /**
65      * INTERNAL:
66      * Deferred lock the identity map for the object, this is used for avoiding deadlock
67      * The return cacheKey should be used to release the deferred lock
68      */

69     public CacheKey acquireDeferredLock(Vector primarKey, Class JavaDoc javaClass, ClassDescriptor descriptor) {
70         return getIdentityMapManager().acquireDeferredLock(primarKey, javaClass, descriptor);
71     }
72
73     /**
74      * INTERNAL:
75      * Lock the identity map for the object, this must be done when building objects.
76      * The return cacheKey should be used to release the lock
77      */

78     public CacheKey acquireLock(Vector primarKey, Class JavaDoc javaClass, ClassDescriptor descriptor) {
79         return acquireLock(primarKey, javaClass, false, descriptor);
80     }
81
82     /**
83      * INTERNAL:
84      * Provides access for setting a concurrency lock on an object in the IdentityMap.
85      * called with true from the merge process, if true then the refresh will not refresh the object
86      * @see IdentityMap#aquire
87      */

88     public CacheKey acquireLock(Vector primaryKey, Class JavaDoc domainClass, boolean forMerge, ClassDescriptor descriptor) {
89         return getIdentityMapManager().acquireLock(primaryKey, domainClass, forMerge, descriptor);
90     }
91
92     /**
93     * INTERNAL:
94     * Provides access for setting a concurrency lock on an object in the IdentityMap.
95     * called with true from the merge process, if true then the refresh will not refresh the object
96     * @see IdentityMap#aquire
97      */

98     public CacheKey acquireLockNoWait(Vector primaryKey, Class JavaDoc domainClass, boolean forMerge, ClassDescriptor descriptor) {
99         return getIdentityMapManager().acquireLockNoWait(primaryKey, domainClass, forMerge, descriptor);
100     }
101
102     /**
103      * INTERNAL:
104      * Find the cachekey for the provided primary key and place a readlock on it.
105      * This will allow multiple users to read the same object but prevent writes to
106      * the object while the read lock is held.
107      */

108     public CacheKey acquireReadLockOnCacheKey(Vector primaryKey, Class JavaDoc domainClass, ClassDescriptor descriptor) {
109         return getIdentityMapManager().acquireReadLockOnCacheKey(primaryKey, domainClass, descriptor);
110     }
111
112     /**
113      * INTERNAL:
114      * Find the cachekey for the provided primary key and place a readlock on it.
115      * This will allow multiple users to read the same object but prevent writes to
116      * the object while the read lock is held.
117      * If no readlock can be acquired then do not wait but return null.
118      */

119     public CacheKey acquireReadLockOnCacheKeyNoWait(Vector primaryKey, Class JavaDoc domainClass, ClassDescriptor descriptor) {
120         return getIdentityMapManager().acquireReadLockOnCacheKeyNoWait(primaryKey, domainClass, descriptor);
121     }
122
123     /**
124         * INTERNAL:
125         * Lock the entire cache if the cache isolation requires.
126         * By default concurrent reads and writes are allowed.
127         * By write, unit of work merge is meant.
128         */

129     public boolean acquireWriteLock() {
130         return getIdentityMapManager().acquireWriteLock();
131     }
132
133     /**
134      * ADVANCED:
135      * Clear all the query caches
136      */

137     public void clearQueryCache() {
138         getIdentityMapManager().clearQueryCache();
139     }
140
141     /**
142  * ADVANCED:
143  * Clear the query class associated with the passed-in read query
144  */

145     public void clearQueryCache(ReadQuery query) {
146         getIdentityMapManager().clearQueryCache(query);
147     }
148
149     /**
150  * ADVANCED:
151  * Clear the query cache associated with the named query on the session
152  */

153     public void clearQueryCache(String JavaDoc sessionQueryName) {
154         getIdentityMapManager().clearQueryCache((ReadQuery)session.getQuery(sessionQueryName));
155     }
156
157     /**
158  * ADVANCED:
159  * Clear the query cache associated with the named query on the descriptor for the given class
160  */

161     public void clearQueryCache(String JavaDoc descriptorQueryName, Class JavaDoc queryClass) {
162         getIdentityMapManager().clearQueryCache((ReadQuery)session.getDescriptor(queryClass).getQueryManager().getQuery(descriptorQueryName));
163     }
164
165     /**
166  * INTERNAL:
167      * Return whether the identity maps contain an item of the given class and key
168      */

169     public boolean containsKey(Vector key, Class JavaDoc theClass, ClassDescriptor descriptor) {
170         return getIdentityMapManager().containsKey(key, theClass, descriptor);
171     }
172
173     /**
174      * ADVANCED:
175      * Return if their is an object for the primary key.
176      */

177     public boolean containsObjectInIdentityMap(Object JavaDoc object) {
178         return containsObjectInIdentityMap(getSession().keyFromObject(object), object.getClass());
179     }
180
181     /**
182      * ADVANCED:
183      * Return if their is an object for the primary key.
184      */

185     public boolean containsObjectInIdentityMap(Vector primaryKey, Class JavaDoc theClass) {
186         ClassDescriptor descriptor = getSession().getDescriptor(theClass);
187         return containsObjectInIdentityMap(primaryKey, theClass, descriptor);
188     }
189     
190     /**
191      * INTERNAL:
192      * Return if their is an object for the primary key.
193      */

194     public boolean containsObjectInIdentityMap(Vector primaryKey, Class JavaDoc theClass, ClassDescriptor descriptor) {
195         return getIdentityMapManager().containsKey(primaryKey, theClass, descriptor);
196     }
197
198     /**
199      * ADVANCED:
200      * Return if their is an object for the row containing primary key and the class.
201      */

202     public boolean containsObjectInIdentityMap(Record rowContainingPrimaryKey, Class JavaDoc theClass) {
203         return containsObjectInIdentityMap(extractPrimaryKeyFromRow(rowContainingPrimaryKey, theClass), theClass);
204     }
205
206     /**
207      * INTERNAL:
208      * Extract primary key from a row
209     * @param DatabaseRow to extract primary key from
210     * @param Class
211     * @return Vector primary key
212      */

213     protected Vector extractPrimaryKeyFromRow(Record rowContainingPrimaryKey, Class JavaDoc theClass) {
214         return this.session.getDescriptor(theClass).getObjectBuilder().extractPrimaryKeyFromRow((AbstractRecord)rowContainingPrimaryKey, this.session);
215     }
216
217     /**
218     * INTERNAL:
219     * Retrieve the cache key for the given object from the identity maps
220     * @param Object object the object to get the cache key for
221     * @return CacheKey
222     */

223     public CacheKey getCacheKeyForObject(Object JavaDoc object, ClassDescriptor descriptor) {
224         return getCacheKeyForObject(getSession().keyFromObject(object, descriptor), object.getClass(), descriptor);
225     }
226
227     /**
228      * INTERNAL:
229      * This method is used to get a list of those classes with IdentityMaps in the Session.
230      */

231     public Vector getClassesRegistered() {
232         return getIdentityMapManager().getClassesRegistered();
233     }
234
235     /**
236      * ADVANCED:
237      * Query the cache in-memory.
238      * If the expression is too complex an exception will be thrown.
239      */

240     public Vector getAllFromIdentityMap(Expression selectionCriteria, Class JavaDoc theClass, Record translationRow) throws QueryException {
241         return getAllFromIdentityMap(selectionCriteria, theClass, translationRow, new InMemoryQueryIndirectionPolicy());
242     }
243
244     /**
245      * ADVANCED:
246      * Query the cache in-memory.
247      * If the expression is too complex an exception will be thrown.
248      * Only return objects that are invalid in the cache if specified.
249      */

250     public Vector getAllFromIdentityMap(Expression selectionCriteria, Class JavaDoc theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean shouldReturnInvalidatedObjects) throws QueryException {
251         return getIdentityMapManager().getAllFromIdentityMap(selectionCriteria, theClass, translationRow, valueHolderPolicy, shouldReturnInvalidatedObjects);
252     }
253
254     /**
255      * ADVANCED:
256      * Query the cache in-memory.
257      * If the expression is too complex an exception will be thrown.
258      */

259     public Vector getAllFromIdentityMap(Expression selectionCriteria, Class JavaDoc theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy) throws QueryException {
260         return getAllFromIdentityMap(selectionCriteria, theClass, translationRow, valueHolderPolicy, true);
261     }
262
263     /**
264      * ADVANCED:
265      * Return the object from the identity with primary and class of the given object.
266      */

267     public Object JavaDoc getFromIdentityMap(Object JavaDoc object) {
268         return getFromIdentityMap(getSession().keyFromObject(object), object.getClass());
269     }
270
271     /**
272      * INTERNAL:
273      * Retrieve the cache key for the given identity information
274      * @param Vector the primary key of the cache key to be retrieved
275      * @param Class the class of the cache key to be retrieved
276      * @return CacheKey
277      */

278     public CacheKey getCacheKeyForObject(Vector primaryKey, Class JavaDoc myClass, ClassDescriptor descriptor) {
279         return getIdentityMapManager().getCacheKeyForObject(primaryKey, myClass, descriptor);
280     }
281
282     /**
283      * ADVANCED:
284      * Return the object from the identity with the primary and class.
285      */

286     public Object JavaDoc getFromIdentityMap(Vector primaryKey, Class JavaDoc theClass) {
287         return getFromIdentityMap(primaryKey, theClass, true);
288     }
289     
290     /**
291      * INTERNAL:
292      * Return the object from the identity with the primary and class.
293      */

294     public Object JavaDoc getFromIdentityMap(Vector primaryKey, Class JavaDoc theClass, ClassDescriptor descriptor, JoinedAttributeManager joinedAttributeManager) {
295         return getFromIdentityMap(primaryKey, theClass, true, descriptor, joinedAttributeManager);
296     }
297
298     /**
299      * ADVANCED:
300      * Return the object from the identity with the primary and class.
301      * Only return invalidated objects if requested.
302      */

303     public Object JavaDoc getFromIdentityMap(Vector primaryKey, Class JavaDoc theClass, boolean shouldReturnInvalidatedObjects) {
304         return getFromIdentityMap(primaryKey, theClass, shouldReturnInvalidatedObjects, getSession().getDescriptor(theClass), null);
305     }
306     
307     /**
308      * INTERNAL:
309      * Return the object from the identity with the primary and class.
310      * Only return invalidated objects if requested.
311      */

312     public Object JavaDoc getFromIdentityMap(Vector primaryKey, Class JavaDoc theClass, boolean shouldReturnInvalidatedObjects, ClassDescriptor descriptor, JoinedAttributeManager joinedAttributeManager) {
313         return getIdentityMapManager().getFromIdentityMap(primaryKey, theClass, shouldReturnInvalidatedObjects, descriptor);
314     }
315
316     /**
317      * ADVANCED:
318      * Return the object from the identity with the primary and class.
319      */

320     public Object JavaDoc getFromIdentityMap(Record rowContainingPrimaryKey, Class JavaDoc theClass) {
321         return getFromIdentityMap(extractPrimaryKeyFromRow((AbstractRecord)rowContainingPrimaryKey, theClass), theClass);
322     }
323
324     /**
325      * ADVANCED:
326      * Return the object from the identity with the primary and class.
327      * Only return invalidated objects if requested.
328      */

329     public Object JavaDoc getFromIdentityMap(Record rowContainingPrimaryKey, Class JavaDoc theClass, boolean shouldReturnInvalidatedObjects) {
330         return getFromIdentityMap(extractPrimaryKeyFromRow((AbstractRecord)rowContainingPrimaryKey, theClass), theClass, shouldReturnInvalidatedObjects);
331     }
332
333     /**
334      * ADVANCED:
335      * Query the cache in-memory.
336      * If the object is not found null is returned.
337      * If the expression is too complex an exception will be thrown.
338      */

339     public Object JavaDoc getFromIdentityMap(Expression selectionCriteria, Class JavaDoc theClass, Record translationRow) throws QueryException {
340         return getFromIdentityMap(selectionCriteria, theClass, translationRow, new InMemoryQueryIndirectionPolicy());
341     }
342
343     /**
344      * ADVANCED:
345      * Query the cache in-memory.
346      * If the object is not found null is returned.
347      * If the expression is too complex an exception will be thrown.
348      */

349     public Object JavaDoc getFromIdentityMap(Expression selectionCriteria, Class JavaDoc theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy) throws QueryException {
350         return getFromIdentityMap(selectionCriteria, theClass, translationRow, valueHolderPolicy, false);
351     }
352
353     /**
354      * INTERNAL:
355      * Query the cache in-memory.
356      * If the object is not found null is returned.
357      * If the expression is too complex an exception will be thrown.
358      */

359     public Object JavaDoc getFromIdentityMap(Expression selectionCriteria, Class JavaDoc theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean conforming) {
360         return getFromIdentityMap(selectionCriteria, theClass, translationRow, valueHolderPolicy, conforming, true, getSession().getDescriptor(theClass));
361     }
362
363     /**
364      * INTERNAL:
365      * Query the cache in-memory.
366      * If the object is not found null is returned.
367      * If the expression is too complex an exception will be thrown.
368      */

369     public Object JavaDoc getFromIdentityMap(Expression selectionCriteria, Class JavaDoc theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean conforming, boolean shouldReturnInvalidatedObjects) {
370         return getFromIdentityMap(selectionCriteria, theClass, translationRow, valueHolderPolicy, conforming, shouldReturnInvalidatedObjects, getSession().getDescriptor(theClass));
371     }
372     
373     /**
374      * INTERNAL:
375      * Query the cache in-memory.
376      * If the object is not found null is returned.
377      * If the expression is too complex an exception will be thrown.
378      */

379     public Object JavaDoc getFromIdentityMap(Expression selectionCriteria, Class JavaDoc theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean conforming, boolean shouldReturnInvalidatedObjects, ClassDescriptor descriptor) {
380         return getIdentityMapManager().getFromIdentityMap(selectionCriteria, theClass, translationRow, valueHolderPolicy, conforming, shouldReturnInvalidatedObjects, descriptor);
381     }
382
383     /**
384      * INTERNAL:
385      * Return the object from the identity with the primary and class.
386      */

387     public Object JavaDoc getFromIdentityMapWithDeferredLock(Vector primaryKey, Class JavaDoc theClass, ClassDescriptor descriptor) {
388         return getFromIdentityMapWithDeferredLock(primaryKey, theClass, true, descriptor);
389     }
390
391     /**
392      * INTERNAL:
393      * Return the object from the identity with the primary and class.
394      * Only return invalidated objects if requested
395      */

396     public Object JavaDoc getFromIdentityMapWithDeferredLock(Vector primaryKey, Class JavaDoc theClass, boolean shouldReturnInvalidatedObjects, ClassDescriptor descriptor) {
397         return getIdentityMapManager().getFromIdentityMapWithDeferredLock(primaryKey, theClass, shouldReturnInvalidatedObjects, descriptor);
398     }
399
400     /**
401      * INTERNAL:
402      * Get the IdentityMapManager for this IdentityMapAccessor
403      * This method should be used for all IdentityMapManager access since it may
404      * be overridden in sub classes.
405      */

406     public IdentityMapManager getIdentityMapManager() {
407         if (session.hasBroker()) {
408             return getSession().getBroker().getIdentityMapAccessorInstance().getIdentityMapManager();
409         }
410         return identityMapManager;
411     }
412
413     /**
414      * INTERNAL:
415      * Get the identity map for the given class from the IdentityMapManager
416      */

417     public IdentityMap getIdentityMap(Class JavaDoc theClass) {
418         ClassDescriptor descriptor = getSession().getDescriptor(theClass);
419         return this.getIdentityMap(descriptor);
420     }
421
422     /**
423      * INTERNAL:
424      * Get the identity map for the given class from the IdentityMapManager
425      */

426     public IdentityMap getIdentityMap(ClassDescriptor descriptor) {
427         if (descriptor == null) {
428             throw ValidationException.missingDescriptor(descriptor.getJavaClassName());
429         }
430         return getIdentityMapManager().getIdentityMap(descriptor);
431     }
432
433     /**
434      * ADVANCED:
435      * Return the remaining life of this object. This method is associated with use of
436      * TopLink's cache invalidation feature and returns the difference between the next expiry
437      * time of the object and its read time. The method will return 0 for invalidated objects.
438      */

439     public long getRemainingValidTime(Object JavaDoc object) {
440         Vector primaryKey = getSession().keyFromObject(object);
441         ClassDescriptor descriptor = getSession().getDescriptor(object);
442         CacheKey key = getCacheKeyForObject(primaryKey, object.getClass(), descriptor);
443         if (key == null) {
444             throw QueryException.objectDoesNotExistInCache(object);
445         }
446         return descriptor.getCacheInvalidationPolicy().getRemainingValidTime(key);
447     }
448
449     /**
450      * INTERNAL:
451      * get the session associated with this IdentityMapAccessor
452      */

453     public AbstractSession getSession() {
454         return session;
455     }
456
457     /**
458      * INTERNAL:
459      * Get the wrapper object from the cache key associated with the given primary key,
460      * this is used for EJB.
461      */

462     public Object JavaDoc getWrapper(Vector primaryKey, Class JavaDoc theClass) {
463         return getIdentityMapManager().getWrapper(primaryKey, theClass);
464     }
465
466     /**
467      * INTERNAL:
468      * Returns the single write Lock manager for this session
469      */

470     public WriteLockManager getWriteLockManager() {
471         return getIdentityMapManager().getWriteLockManager();
472     }
473
474     /**
475     * ADVANCED:
476     * Extract the write lock value from the identity map.
477     */

478     public Object JavaDoc getWriteLockValue(Object JavaDoc object) {
479         return getWriteLockValue(getSession().keyFromObject(object), object.getClass());
480     }
481
482     /**
483      * ADVANCED:
484      * Extract the write lock value from the identity map.
485      */

486     public Object JavaDoc getWriteLockValue(Vector primaryKey, Class JavaDoc theClass) {
487         return getWriteLockValue(primaryKey, theClass, getSession().getDescriptor(theClass));
488     }
489         
490     /**
491      * ADVANCED:
492      * Extract the write lock value from the identity map.
493      */

494     public Object JavaDoc getWriteLockValue(Vector primaryKey, Class JavaDoc theClass, ClassDescriptor descriptor) {
495         return getIdentityMapManager().getWriteLockValue(primaryKey, theClass, descriptor);
496     }
497
498     /**
499      * PUBLIC:
500      * Reset the entire object cache.
501      * <p> NOTE: be careful using this method. This method blows away both this session's and its parents caches,
502      * this includes the server cache or any other cache. This throws away any objects that have been read in.
503      * Extream caution should be used before doing this because object identity will no longer
504      * be maintained for any objects currently read in. This should only be called
505      * if the application knows that it no longer has references to object held in the cache.
506      */

507     public void initializeAllIdentityMaps() {
508         getSession().log(SessionLog.FINER, SessionLog.CACHE, "initialize_all_identitymaps");
509         getIdentityMapManager().initializeIdentityMaps();
510     }
511
512     /**
513      * PUBLIC:
514      * Reset the identity map for only the instances of the class.
515      * For inheritance the user must make sure that they only use the root class.
516      * Caution must be used in doing this to ensure that the objects within the identity map
517      * are not referenced from other objects of other classes or from the application.
518      */

519     public void initializeIdentityMap(Class JavaDoc theClass) {
520         getSession().log(SessionLog.FINER, SessionLog.CACHE, "initialize_identitymap", theClass);
521         getIdentityMapManager().initializeIdentityMap(theClass);
522     }
523
524     /**
525      * PUBLIC:
526      * Reset the entire local object cache.
527      * This throws away any objects that have been read in.
528      * Extream caution should be used before doing this because object identity will no longer
529      * be maintained for any objects currently read in. This should only be called
530      * if the application knows that it no longer has references to object held in the cache.
531      */

532     public void initializeIdentityMaps() {
533         getSession().log(SessionLog.FINER, SessionLog.CACHE, "initialize_identitymaps");
534         getIdentityMapManager().initializeIdentityMaps();
535         getSession().getCommitManager().reinitialize();
536     }
537
538     /**
539      * ADVANCED:
540      * Set an object to be invalid in the TopLink identity maps.
541      * If the object does not exist in the cache, thiss method will return
542      * without any action
543      */

544     public void invalidateObject(Object JavaDoc object) {
545         invalidateObject(getSession().keyFromObject(object), object.getClass());
546     }
547
548     /**
549      * ADVANCED:
550      * Set an object to be invalid in the TopLink identity maps.
551      * If the object does not exist in the cache, this method will return
552      * without any action
553      */

554     public void invalidateObject(Vector primaryKey, Class JavaDoc theClass) {
555         ClassDescriptor descriptor = getSession().getDescriptor(theClass);
556         //forward the call to getCacheKeyForObject locally in case subclasses overload
557
CacheKey key = this.getCacheKeyForObject(primaryKey, theClass, descriptor);
558         if (key != null) {
559             key.setInvalidationState(CacheKey.CACHE_KEY_INVALID);
560         }
561     }
562
563     /**
564      * ADVANCED:
565      * Set an object to be invalid in the TopLink identity maps.
566      * If the object does not exist in the cache, this method will return
567      * without any action
568      */

569     public void invalidateObject(Record rowContainingPrimaryKey, Class JavaDoc theClass) {
570         invalidateObject(extractPrimaryKeyFromRow(rowContainingPrimaryKey, theClass), theClass);
571     }
572
573     /**
574      * ADVANCED:
575      * Set all of the objects from the given Expression to be invalid in the TopLink Identity Maps
576      */

577     public void invalidateObjects(Expression selectionCriteria) {
578         invalidateObjects(getAllFromIdentityMap(selectionCriteria, selectionCriteria.getBuilder().getQueryClass(), new DatabaseRecord(1)));
579     }
580
581     /**
582      * ADVANCED:
583      * Set all of the objects in the given collection to be invalid in the TopLink Identity Maps
584      * This method will take no action for any objects in the collection that do not exist in the cache.
585      */

586     public void invalidateObjects(Vector collection) {
587         Enumeration enumtr = collection.elements();
588         while (enumtr.hasMoreElements()) {
589             invalidateObject(enumtr.nextElement());
590         }
591     }
592
593     /**
594        * ADVANCED:
595        * Set all of the objects of a specific class to be invalid in TopLink's identity maps
596      * Will set the recurse on inheritance to true.
597        */

598     public void invalidateClass(Class JavaDoc myClass) {
599         invalidateClass(myClass, true);
600     }
601
602     /**
603      * ADVANCED:
604      * Set all of the objects of a specific class to be invalid in TopLink's identity maps.
605     * User can set the recurse flag to false if they do not want to invalidate
606     * all the classes within an inheritance tree.
607      */

608     public void invalidateClass(Class JavaDoc myClass, boolean recurse) {
609         //forward the call to getIdentityMap locally in case subclasses overload
610
IdentityMap identityMap = this.getIdentityMap(myClass);
611         synchronized (identityMap) {
612             Enumeration keys = identityMap.keys();
613
614             while (keys.hasMoreElements()) {
615                 CacheKey key = (CacheKey)keys.nextElement();
616                 Object JavaDoc obj = key.getObject();
617
618                 if (recurse || ((obj != null) && obj.getClass().equals(myClass))) {
619                     key.setInvalidationState(CacheKey.CACHE_KEY_INVALID);
620                 }
621             }
622         }
623     }
624
625     /**
626      * ADVANCED:
627      * Set all of the objects from all identity maps to be invalid in TopLink's
628      * identity maps.
629      */

630     public void invalidateAll() {
631         Enumeration identiyMapClasses = getIdentityMapManager().getIdentityMapClasses();
632         
633         while (identiyMapClasses.hasMoreElements()) {
634             invalidateClass((Class JavaDoc) identiyMapClasses.nextElement());
635         }
636     }
637     
638     /**
639      * ADVANCED:
640      * Return true if this object is valid in TopLink's identity maps
641      * return false otherwise
642      */

643     public boolean isValid(Object JavaDoc object) {
644         return isValid(getSession().keyFromObject(object), object.getClass());
645     }
646
647     /**
648      * ADVANCED:
649      * Return true if this object is valid in TopLink's identity maps
650      * return false otherwise
651      */

652     public boolean isValid(Vector primaryKey, Class JavaDoc theClass) {
653         ClassDescriptor descriptor = getSession().getDescriptor(theClass);
654         //forward the call to getCacheKeyForObject locally in case subclasses overload
655
CacheKey key = this.getCacheKeyForObject(primaryKey, theClass, descriptor);
656         if (key == null) {
657             throw QueryException.classPkDoesNotExistInCache(theClass, primaryKey);
658         }
659         return !descriptor.getCacheInvalidationPolicy().isInvalidated(key, System.currentTimeMillis());
660     }
661
662     /**
663      * ADVANCED:
664      * Return true if this object is valid in TopLink's identity maps
665      * return false otherwise
666      */

667     public boolean isValid(AbstractRecord rowContainingPrimaryKey, Class JavaDoc theClass) {
668         return isValid(extractPrimaryKeyFromRow(rowContainingPrimaryKey, theClass), theClass);
669     }
670
671     /**
672      * PUBLIC:
673      * Used to print all the objects in the identity map of the passed in class.
674      * The output of this method will be logged to this session's SessionLog at SEVERE level.
675      */

676     public void printIdentityMap(Class JavaDoc businessClass) {
677         if (getSession().shouldLog(SessionLog.SEVERE, SessionLog.CACHE)) {
678             getIdentityMapManager().printIdentityMap(businessClass);
679         }
680     }
681
682     /**
683      * PUBLIC:
684      * Used to print all the objects in every identity map in this session.
685      * The output of this method will be logged to this session's SessionLog at SEVERE level.
686      */

687     public void printIdentityMaps() {
688         if (getSession().shouldLog(SessionLog.SEVERE, SessionLog.CACHE)) {
689             getIdentityMapManager().printIdentityMaps();
690         }
691     }
692
693     /**
694      * PUBLIC:
695      * Used to print all the locks in every identity map in this session.
696      * The output of this method will be logged to this session's SessionLog at FINEST level.
697      */

698     public void printIdentityMapLocks() {
699         if (getSession().shouldLog(SessionLog.FINEST, SessionLog.CACHE)) {
700             getIdentityMapManager().printLocks();
701         }
702     }
703
704     /**
705      * ADVANCED:
706      * Register the object with the identity map.
707      * The object must always be registered with its version number if optimistic locking is used.
708      */

709     public Object JavaDoc putInIdentityMap(Object JavaDoc object) {
710         return putInIdentityMap(object, getSession().keyFromObject(object));
711     }
712
713     /**
714      * ADVANCED:
715      * Register the object with the identity map.
716      * The object must always be registered with its version number if optimistic locking is used.
717      */

718     public Object JavaDoc putInIdentityMap(Object JavaDoc object, Vector key) {
719         return putInIdentityMap(object, key, null);
720     }
721
722     /**
723      * ADVANCED:
724      * Register the object with the identity map.
725      * The object must always be registered with its version number if optimistic locking is used.
726      */

727     public Object JavaDoc putInIdentityMap(Object JavaDoc object, Vector key, Object JavaDoc writeLockValue) {
728         return putInIdentityMap(object, key, writeLockValue, 0);
729     }
730
731     /**
732      * ADVANCED:
733      * Register the object with the identity map.
734      * The object must always be registered with its version number if optimistic locking is used.
735      * The readTime may also be included in the cache key as it is constructed
736      */

737     public Object JavaDoc putInIdentityMap(Object JavaDoc object, Vector key, Object JavaDoc writeLockValue, long readTime) {
738         ClassDescriptor descriptor = getSession().getDescriptor(object);
739         return putInIdentityMap(object, key, writeLockValue, readTime, descriptor);
740     }
741     
742     /**
743      * ADVANCED:
744      * Register the object with the identity map.
745      * The object must always be registered with its version number if optimistic locking is used.
746      * The readTime may also be included in the cache key as it is constructed
747      */

748     public Object JavaDoc putInIdentityMap(Object JavaDoc object, Vector key, Object JavaDoc writeLockValue, long readTime, ClassDescriptor descriptor) {
749         CacheKey cacheKey = internalPutInIdentityMap(object, key, writeLockValue, readTime, descriptor);
750         if (cacheKey == null) {
751             return null;
752         }
753         return cacheKey.getObject();
754     }
755
756     /**
757      * INTERNAL:
758      * Register the object with the identity map.
759      * The object must always be registered with its version number if optimistic locking is used.
760      * The readTime may also be included in the cache key as it is constructed.
761      * Return the cache-key.
762      */

763     public CacheKey internalPutInIdentityMap(Object JavaDoc object, Vector key, Object JavaDoc writeLockValue, long readTime, ClassDescriptor descriptor) {
764         return getIdentityMapManager().putInIdentityMap(object, key, writeLockValue, readTime, descriptor);
765     }
766
767     /**
768     * INTERNAL:
769     * Lock the entire cache if the cache isolation requires.
770     * By default concurrent reads and writes are allowed.
771     * By write, unit of work merge is meant.
772     */

773     public void releaseWriteLock() {
774         getIdentityMapManager().releaseWriteLock();
775     }
776
777     /**
778      * ADVANCED:
779      * Remove the object from the object cache.
780      * Caution should be used when calling to avoid violating object identity.
781      * The application should only call this is it knows that no references to the object exist.
782      */

783     public Object JavaDoc removeFromIdentityMap(Object JavaDoc object) {
784         return removeFromIdentityMap(getSession().keyFromObject(object), object.getClass());
785     }
786
787     /**
788      * ADVANCED:
789      * Remove the object from the object cache.
790      */

791     public Object JavaDoc removeFromIdentityMap(Vector key, Class JavaDoc theClass) {
792         ClassDescriptor descriptor = getSession().getDescriptor(theClass);
793         return removeFromIdentityMap(key, theClass, descriptor);
794     }
795     
796     /**
797      * INTERNAL:
798      * Remove the object from the object cache.
799      */

800     public Object JavaDoc removeFromIdentityMap(Vector key, Class JavaDoc theClass, ClassDescriptor descriptor) {
801         return getIdentityMapManager().removeFromIdentityMap(key, theClass, descriptor);
802     }
803
804     /**
805      * INTERNAL:
806      * Set the IdentityMapManager for this IdentityMapAccessor
807      */

808     public void setIdentityMapManager(IdentityMapManager identityMapManager) {
809         this.identityMapManager = identityMapManager;
810     }
811
812     /**
813      * INTERNAL:
814      * Update the wrapper object the cache key associated with the given primary key,
815      * this is used for EJB.
816      */

817     public void setWrapper(Vector primaryKey, Class JavaDoc theClass, Object JavaDoc wrapper) {
818         getIdentityMapManager().setWrapper(primaryKey, theClass, wrapper);
819     }
820
821     /**
822      * ADVANCED:
823      * Update the write lock value in the identity map.
824      */

825     public void updateWriteLockValue(Object JavaDoc object, Object JavaDoc writeLockValue) {
826         updateWriteLockValue(getSession().keyFromObject(object), object.getClass(), writeLockValue);
827     }
828
829     /**
830      * ADVANCED:
831      * Update the write lock value in the identity map.
832      */

833     public void updateWriteLockValue(Vector primaryKey, Class JavaDoc theClass, Object JavaDoc writeLockValue) {
834         getIdentityMapManager().setWriteLockValue(primaryKey, theClass, writeLockValue);
835     }
836
837     /**
838      * INTERNAL:
839      * This can be used to help debugging an object identity problem.
840      * An object identity problem is when an object in the cache references an object not in the cache.
841      * This method will validate that all cached objects are in a correct state.
842      */

843     public void validateCache() {
844         //pass certain calls to this in order to allow subclasses to implement own behaviour
845
getSession().log(SessionLog.FINER, SessionLog.CACHE, "validate_cache");
846         // This define an inner class for process the itteration operation, don't be scared, its just an inner class.
847
DescriptorIterator iterator = new DescriptorIterator() {
848             public void iterate(Object JavaDoc object) {
849                 if (!containsObjectInIdentityMap(this.session.getDescriptor(object.getClass()).getObjectBuilder().extractPrimaryKeyFromObject(object, this.getSession()), object.getClass())) {
850                     this.session.log(SessionLog.FINEST, SessionLog.CACHE, "stack_of_visited_objects_that_refer_to_the_corrupt_object", getVisitedStack());
851                     this.session.log(SessionLog.FINER, SessionLog.CACHE, "corrupt_object_referenced_through_mapping", getCurrentMapping());
852                     this.session.log(SessionLog.FINER, SessionLog.CACHE, "corrupt_object", object);
853                 }
854             }
855         };
856
857         iterator.setSession(getSession());
858         Iterator descriptors = getSession().getDescriptors().values().iterator();
859         while (descriptors.hasNext()) {
860             Class JavaDoc javaClass = ((ClassDescriptor)descriptors.next()).getJavaClass();
861             for (Enumeration mapEnum = getIdentityMap(javaClass).elements();
862                      mapEnum.hasMoreElements();) {
863                 iterator.startIterationOn(mapEnum.nextElement());
864             }
865         }
866     }
867 }
868
Popular Tags