1 21 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 48 public class IdentityMapAccessor implements oracle.toplink.essentials.sessions.IdentityMapAccessor, java.io.Serializable { 49 50 51 protected IdentityMapManager identityMapManager = null; 52 protected AbstractSession session = null; 53 54 59 public IdentityMapAccessor(AbstractSession session, IdentityMapManager identityMapManager) { 60 this.session = session; 61 this.identityMapManager = identityMapManager; 62 } 63 64 69 public CacheKey acquireDeferredLock(Vector primarKey, Class javaClass, ClassDescriptor descriptor) { 70 return getIdentityMapManager().acquireDeferredLock(primarKey, javaClass, descriptor); 71 } 72 73 78 public CacheKey acquireLock(Vector primarKey, Class javaClass, ClassDescriptor descriptor) { 79 return acquireLock(primarKey, javaClass, false, descriptor); 80 } 81 82 88 public CacheKey acquireLock(Vector primaryKey, Class domainClass, boolean forMerge, ClassDescriptor descriptor) { 89 return getIdentityMapManager().acquireLock(primaryKey, domainClass, forMerge, descriptor); 90 } 91 92 98 public CacheKey acquireLockNoWait(Vector primaryKey, Class domainClass, boolean forMerge, ClassDescriptor descriptor) { 99 return getIdentityMapManager().acquireLockNoWait(primaryKey, domainClass, forMerge, descriptor); 100 } 101 102 108 public CacheKey acquireReadLockOnCacheKey(Vector primaryKey, Class domainClass, ClassDescriptor descriptor) { 109 return getIdentityMapManager().acquireReadLockOnCacheKey(primaryKey, domainClass, descriptor); 110 } 111 112 119 public CacheKey acquireReadLockOnCacheKeyNoWait(Vector primaryKey, Class domainClass, ClassDescriptor descriptor) { 120 return getIdentityMapManager().acquireReadLockOnCacheKeyNoWait(primaryKey, domainClass, descriptor); 121 } 122 123 129 public boolean acquireWriteLock() { 130 return getIdentityMapManager().acquireWriteLock(); 131 } 132 133 137 public void clearQueryCache() { 138 getIdentityMapManager().clearQueryCache(); 139 } 140 141 145 public void clearQueryCache(ReadQuery query) { 146 getIdentityMapManager().clearQueryCache(query); 147 } 148 149 153 public void clearQueryCache(String sessionQueryName) { 154 getIdentityMapManager().clearQueryCache((ReadQuery)session.getQuery(sessionQueryName)); 155 } 156 157 161 public void clearQueryCache(String descriptorQueryName, Class queryClass) { 162 getIdentityMapManager().clearQueryCache((ReadQuery)session.getDescriptor(queryClass).getQueryManager().getQuery(descriptorQueryName)); 163 } 164 165 169 public boolean containsKey(Vector key, Class theClass, ClassDescriptor descriptor) { 170 return getIdentityMapManager().containsKey(key, theClass, descriptor); 171 } 172 173 177 public boolean containsObjectInIdentityMap(Object object) { 178 return containsObjectInIdentityMap(getSession().keyFromObject(object), object.getClass()); 179 } 180 181 185 public boolean containsObjectInIdentityMap(Vector primaryKey, Class theClass) { 186 ClassDescriptor descriptor = getSession().getDescriptor(theClass); 187 return containsObjectInIdentityMap(primaryKey, theClass, descriptor); 188 } 189 190 194 public boolean containsObjectInIdentityMap(Vector primaryKey, Class theClass, ClassDescriptor descriptor) { 195 return getIdentityMapManager().containsKey(primaryKey, theClass, descriptor); 196 } 197 198 202 public boolean containsObjectInIdentityMap(Record rowContainingPrimaryKey, Class theClass) { 203 return containsObjectInIdentityMap(extractPrimaryKeyFromRow(rowContainingPrimaryKey, theClass), theClass); 204 } 205 206 213 protected Vector extractPrimaryKeyFromRow(Record rowContainingPrimaryKey, Class theClass) { 214 return this.session.getDescriptor(theClass).getObjectBuilder().extractPrimaryKeyFromRow((AbstractRecord)rowContainingPrimaryKey, this.session); 215 } 216 217 223 public CacheKey getCacheKeyForObject(Object object, ClassDescriptor descriptor) { 224 return getCacheKeyForObject(getSession().keyFromObject(object, descriptor), object.getClass(), descriptor); 225 } 226 227 231 public Vector getClassesRegistered() { 232 return getIdentityMapManager().getClassesRegistered(); 233 } 234 235 240 public Vector getAllFromIdentityMap(Expression selectionCriteria, Class theClass, Record translationRow) throws QueryException { 241 return getAllFromIdentityMap(selectionCriteria, theClass, translationRow, new InMemoryQueryIndirectionPolicy()); 242 } 243 244 250 public Vector getAllFromIdentityMap(Expression selectionCriteria, Class theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean shouldReturnInvalidatedObjects) throws QueryException { 251 return getIdentityMapManager().getAllFromIdentityMap(selectionCriteria, theClass, translationRow, valueHolderPolicy, shouldReturnInvalidatedObjects); 252 } 253 254 259 public Vector getAllFromIdentityMap(Expression selectionCriteria, Class theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy) throws QueryException { 260 return getAllFromIdentityMap(selectionCriteria, theClass, translationRow, valueHolderPolicy, true); 261 } 262 263 267 public Object getFromIdentityMap(Object object) { 268 return getFromIdentityMap(getSession().keyFromObject(object), object.getClass()); 269 } 270 271 278 public CacheKey getCacheKeyForObject(Vector primaryKey, Class myClass, ClassDescriptor descriptor) { 279 return getIdentityMapManager().getCacheKeyForObject(primaryKey, myClass, descriptor); 280 } 281 282 286 public Object getFromIdentityMap(Vector primaryKey, Class theClass) { 287 return getFromIdentityMap(primaryKey, theClass, true); 288 } 289 290 294 public Object getFromIdentityMap(Vector primaryKey, Class theClass, ClassDescriptor descriptor, JoinedAttributeManager joinedAttributeManager) { 295 return getFromIdentityMap(primaryKey, theClass, true, descriptor, joinedAttributeManager); 296 } 297 298 303 public Object getFromIdentityMap(Vector primaryKey, Class theClass, boolean shouldReturnInvalidatedObjects) { 304 return getFromIdentityMap(primaryKey, theClass, shouldReturnInvalidatedObjects, getSession().getDescriptor(theClass), null); 305 } 306 307 312 public Object getFromIdentityMap(Vector primaryKey, Class theClass, boolean shouldReturnInvalidatedObjects, ClassDescriptor descriptor, JoinedAttributeManager joinedAttributeManager) { 313 return getIdentityMapManager().getFromIdentityMap(primaryKey, theClass, shouldReturnInvalidatedObjects, descriptor); 314 } 315 316 320 public Object getFromIdentityMap(Record rowContainingPrimaryKey, Class theClass) { 321 return getFromIdentityMap(extractPrimaryKeyFromRow((AbstractRecord)rowContainingPrimaryKey, theClass), theClass); 322 } 323 324 329 public Object getFromIdentityMap(Record rowContainingPrimaryKey, Class theClass, boolean shouldReturnInvalidatedObjects) { 330 return getFromIdentityMap(extractPrimaryKeyFromRow((AbstractRecord)rowContainingPrimaryKey, theClass), theClass, shouldReturnInvalidatedObjects); 331 } 332 333 339 public Object getFromIdentityMap(Expression selectionCriteria, Class theClass, Record translationRow) throws QueryException { 340 return getFromIdentityMap(selectionCriteria, theClass, translationRow, new InMemoryQueryIndirectionPolicy()); 341 } 342 343 349 public Object getFromIdentityMap(Expression selectionCriteria, Class theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy) throws QueryException { 350 return getFromIdentityMap(selectionCriteria, theClass, translationRow, valueHolderPolicy, false); 351 } 352 353 359 public Object getFromIdentityMap(Expression selectionCriteria, Class theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean conforming) { 360 return getFromIdentityMap(selectionCriteria, theClass, translationRow, valueHolderPolicy, conforming, true, getSession().getDescriptor(theClass)); 361 } 362 363 369 public Object getFromIdentityMap(Expression selectionCriteria, Class theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean conforming, boolean shouldReturnInvalidatedObjects) { 370 return getFromIdentityMap(selectionCriteria, theClass, translationRow, valueHolderPolicy, conforming, shouldReturnInvalidatedObjects, getSession().getDescriptor(theClass)); 371 } 372 373 379 public Object getFromIdentityMap(Expression selectionCriteria, Class 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 387 public Object getFromIdentityMapWithDeferredLock(Vector primaryKey, Class theClass, ClassDescriptor descriptor) { 388 return getFromIdentityMapWithDeferredLock(primaryKey, theClass, true, descriptor); 389 } 390 391 396 public Object getFromIdentityMapWithDeferredLock(Vector primaryKey, Class theClass, boolean shouldReturnInvalidatedObjects, ClassDescriptor descriptor) { 397 return getIdentityMapManager().getFromIdentityMapWithDeferredLock(primaryKey, theClass, shouldReturnInvalidatedObjects, descriptor); 398 } 399 400 406 public IdentityMapManager getIdentityMapManager() { 407 if (session.hasBroker()) { 408 return getSession().getBroker().getIdentityMapAccessorInstance().getIdentityMapManager(); 409 } 410 return identityMapManager; 411 } 412 413 417 public IdentityMap getIdentityMap(Class theClass) { 418 ClassDescriptor descriptor = getSession().getDescriptor(theClass); 419 return this.getIdentityMap(descriptor); 420 } 421 422 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 439 public long getRemainingValidTime(Object 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 453 public AbstractSession getSession() { 454 return session; 455 } 456 457 462 public Object getWrapper(Vector primaryKey, Class theClass) { 463 return getIdentityMapManager().getWrapper(primaryKey, theClass); 464 } 465 466 470 public WriteLockManager getWriteLockManager() { 471 return getIdentityMapManager().getWriteLockManager(); 472 } 473 474 478 public Object getWriteLockValue(Object object) { 479 return getWriteLockValue(getSession().keyFromObject(object), object.getClass()); 480 } 481 482 486 public Object getWriteLockValue(Vector primaryKey, Class theClass) { 487 return getWriteLockValue(primaryKey, theClass, getSession().getDescriptor(theClass)); 488 } 489 490 494 public Object getWriteLockValue(Vector primaryKey, Class theClass, ClassDescriptor descriptor) { 495 return getIdentityMapManager().getWriteLockValue(primaryKey, theClass, descriptor); 496 } 497 498 507 public void initializeAllIdentityMaps() { 508 getSession().log(SessionLog.FINER, SessionLog.CACHE, "initialize_all_identitymaps"); 509 getIdentityMapManager().initializeIdentityMaps(); 510 } 511 512 519 public void initializeIdentityMap(Class theClass) { 520 getSession().log(SessionLog.FINER, SessionLog.CACHE, "initialize_identitymap", theClass); 521 getIdentityMapManager().initializeIdentityMap(theClass); 522 } 523 524 532 public void initializeIdentityMaps() { 533 getSession().log(SessionLog.FINER, SessionLog.CACHE, "initialize_identitymaps"); 534 getIdentityMapManager().initializeIdentityMaps(); 535 getSession().getCommitManager().reinitialize(); 536 } 537 538 544 public void invalidateObject(Object object) { 545 invalidateObject(getSession().keyFromObject(object), object.getClass()); 546 } 547 548 554 public void invalidateObject(Vector primaryKey, Class theClass) { 555 ClassDescriptor descriptor = getSession().getDescriptor(theClass); 556 CacheKey key = this.getCacheKeyForObject(primaryKey, theClass, descriptor); 558 if (key != null) { 559 key.setInvalidationState(CacheKey.CACHE_KEY_INVALID); 560 } 561 } 562 563 569 public void invalidateObject(Record rowContainingPrimaryKey, Class theClass) { 570 invalidateObject(extractPrimaryKeyFromRow(rowContainingPrimaryKey, theClass), theClass); 571 } 572 573 577 public void invalidateObjects(Expression selectionCriteria) { 578 invalidateObjects(getAllFromIdentityMap(selectionCriteria, selectionCriteria.getBuilder().getQueryClass(), new DatabaseRecord(1))); 579 } 580 581 586 public void invalidateObjects(Vector collection) { 587 Enumeration enumtr = collection.elements(); 588 while (enumtr.hasMoreElements()) { 589 invalidateObject(enumtr.nextElement()); 590 } 591 } 592 593 598 public void invalidateClass(Class myClass) { 599 invalidateClass(myClass, true); 600 } 601 602 608 public void invalidateClass(Class myClass, boolean recurse) { 609 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 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 630 public void invalidateAll() { 631 Enumeration identiyMapClasses = getIdentityMapManager().getIdentityMapClasses(); 632 633 while (identiyMapClasses.hasMoreElements()) { 634 invalidateClass((Class ) identiyMapClasses.nextElement()); 635 } 636 } 637 638 643 public boolean isValid(Object object) { 644 return isValid(getSession().keyFromObject(object), object.getClass()); 645 } 646 647 652 public boolean isValid(Vector primaryKey, Class theClass) { 653 ClassDescriptor descriptor = getSession().getDescriptor(theClass); 654 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 667 public boolean isValid(AbstractRecord rowContainingPrimaryKey, Class theClass) { 668 return isValid(extractPrimaryKeyFromRow(rowContainingPrimaryKey, theClass), theClass); 669 } 670 671 676 public void printIdentityMap(Class businessClass) { 677 if (getSession().shouldLog(SessionLog.SEVERE, SessionLog.CACHE)) { 678 getIdentityMapManager().printIdentityMap(businessClass); 679 } 680 } 681 682 687 public void printIdentityMaps() { 688 if (getSession().shouldLog(SessionLog.SEVERE, SessionLog.CACHE)) { 689 getIdentityMapManager().printIdentityMaps(); 690 } 691 } 692 693 698 public void printIdentityMapLocks() { 699 if (getSession().shouldLog(SessionLog.FINEST, SessionLog.CACHE)) { 700 getIdentityMapManager().printLocks(); 701 } 702 } 703 704 709 public Object putInIdentityMap(Object object) { 710 return putInIdentityMap(object, getSession().keyFromObject(object)); 711 } 712 713 718 public Object putInIdentityMap(Object object, Vector key) { 719 return putInIdentityMap(object, key, null); 720 } 721 722 727 public Object putInIdentityMap(Object object, Vector key, Object writeLockValue) { 728 return putInIdentityMap(object, key, writeLockValue, 0); 729 } 730 731 737 public Object putInIdentityMap(Object object, Vector key, Object writeLockValue, long readTime) { 738 ClassDescriptor descriptor = getSession().getDescriptor(object); 739 return putInIdentityMap(object, key, writeLockValue, readTime, descriptor); 740 } 741 742 748 public Object putInIdentityMap(Object object, Vector key, Object 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 763 public CacheKey internalPutInIdentityMap(Object object, Vector key, Object writeLockValue, long readTime, ClassDescriptor descriptor) { 764 return getIdentityMapManager().putInIdentityMap(object, key, writeLockValue, readTime, descriptor); 765 } 766 767 773 public void releaseWriteLock() { 774 getIdentityMapManager().releaseWriteLock(); 775 } 776 777 783 public Object removeFromIdentityMap(Object object) { 784 return removeFromIdentityMap(getSession().keyFromObject(object), object.getClass()); 785 } 786 787 791 public Object removeFromIdentityMap(Vector key, Class theClass) { 792 ClassDescriptor descriptor = getSession().getDescriptor(theClass); 793 return removeFromIdentityMap(key, theClass, descriptor); 794 } 795 796 800 public Object removeFromIdentityMap(Vector key, Class theClass, ClassDescriptor descriptor) { 801 return getIdentityMapManager().removeFromIdentityMap(key, theClass, descriptor); 802 } 803 804 808 public void setIdentityMapManager(IdentityMapManager identityMapManager) { 809 this.identityMapManager = identityMapManager; 810 } 811 812 817 public void setWrapper(Vector primaryKey, Class theClass, Object wrapper) { 818 getIdentityMapManager().setWrapper(primaryKey, theClass, wrapper); 819 } 820 821 825 public void updateWriteLockValue(Object object, Object writeLockValue) { 826 updateWriteLockValue(getSession().keyFromObject(object), object.getClass(), writeLockValue); 827 } 828 829 833 public void updateWriteLockValue(Vector primaryKey, Class theClass, Object writeLockValue) { 834 getIdentityMapManager().setWriteLockValue(primaryKey, theClass, writeLockValue); 835 } 836 837 843 public void validateCache() { 844 getSession().log(SessionLog.FINER, SessionLog.CACHE, "validate_cache"); 846 DescriptorIterator iterator = new DescriptorIterator() { 848 public void iterate(Object 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 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 |