1 21 package oracle.toplink.essentials.internal.identitymaps; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 import java.util.*; 27 import java.io.*; 28 import java.lang.reflect.*; 29 30 import oracle.toplink.essentials.internal.helper.*; 31 import oracle.toplink.essentials.internal.descriptors.*; 32 import oracle.toplink.essentials.exceptions.*; 33 import oracle.toplink.essentials.expressions.*; 34 import oracle.toplink.essentials.queryframework.*; 35 import oracle.toplink.essentials.internal.localization.*; 36 import oracle.toplink.essentials.logging.SessionLog; 37 import oracle.toplink.essentials.sessions.SessionProfiler; 38 import oracle.toplink.essentials.sessions.Record; 39 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 40 import oracle.toplink.essentials.internal.security.PrivilegedGetConstructorFor; 41 import oracle.toplink.essentials.internal.security.PrivilegedMethodInvoker; 42 import oracle.toplink.essentials.internal.security.PrivilegedInvokeConstructor; 43 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 44 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 45 import oracle.toplink.essentials.internal.sessions.AbstractSession; 46 import oracle.toplink.essentials.descriptors.ClassDescriptor; 47 48 58 public class IdentityMapManager implements Serializable, Cloneable { 59 60 61 protected Hashtable identityMaps; 62 63 64 protected Map queryResults; 65 66 67 protected AbstractSession session; 68 69 70 protected transient ConcurrencyManager cacheMutex; 71 72 73 protected IdentityMap lastAccessedIdentityMap = null; 74 protected Class lastAccessedIdentityMapClass = null; 75 76 77 protected transient WriteLockManager writeLockManager; 78 79 80 protected Boolean isCacheAccessPreCheckRequired; 81 82 public IdentityMapManager(AbstractSession session) { 83 this.session = session; 84 this.cacheMutex = new ConcurrencyManager(); 85 this.identityMaps = new Hashtable(); 86 this.queryResults = JavaPlatform.getQueryCacheMap(); 87 } 88 89 92 public CacheKey acquireDeferredLock(Vector primaryKey, Class domainClass, ClassDescriptor descriptor) { 93 CacheKey cacheKey = null; 94 if (isCacheAccessPreCheckRequired()) { 95 getSession().startOperationProfile(SessionProfiler.CACHE); 96 acquireReadLock(); 97 try { 98 cacheKey = getIdentityMap(descriptor).acquireDeferredLock(primaryKey); 99 } finally { 100 releaseReadLock(); 101 } 102 getSession().endOperationProfile(SessionProfiler.CACHE); 103 } else { 104 cacheKey = getIdentityMap(descriptor).acquireDeferredLock(primaryKey); 105 } 106 107 return cacheKey; 108 } 109 110 115 public CacheKey acquireLock(Vector primaryKey, Class domainClass, boolean forMerge, ClassDescriptor descriptor) { 116 CacheKey cacheKey = null; 117 if (isCacheAccessPreCheckRequired()) { 118 getSession().startOperationProfile(SessionProfiler.CACHE); 119 acquireReadLock(); 120 try { 121 cacheKey = getIdentityMap(descriptor).acquireLock(primaryKey, forMerge); 122 } finally { 123 releaseReadLock(); 124 } 125 getSession().endOperationProfile(SessionProfiler.CACHE); 126 } else { 127 cacheKey = getIdentityMap(descriptor).acquireLock(primaryKey, forMerge); 128 } 129 130 return cacheKey; 131 } 132 133 138 public CacheKey acquireLockNoWait(Vector primaryKey, Class domainClass, boolean forMerge, ClassDescriptor descriptor) { 139 CacheKey cacheKey = null; 140 if (isCacheAccessPreCheckRequired()) { 141 getSession().startOperationProfile(SessionProfiler.CACHE); 142 acquireReadLock(); 143 try { 144 cacheKey = getIdentityMap(descriptor).acquireLockNoWait(primaryKey, forMerge); 145 } finally { 146 releaseReadLock(); 147 } 148 getSession().endOperationProfile(SessionProfiler.CACHE); 149 } else { 150 cacheKey = getIdentityMap(descriptor).acquireLockNoWait(primaryKey, forMerge); 151 } 152 153 return cacheKey; 154 } 155 156 160 protected boolean isCacheAccessPreCheckRequired() { 161 if (this.isCacheAccessPreCheckRequired == null) { 162 if ((getSession().getProfiler() != null) || getSession().getDatasourceLogin().shouldSynchronizedReadOnWrite()) { 163 this.isCacheAccessPreCheckRequired = Boolean.TRUE; 164 } else { 165 this.isCacheAccessPreCheckRequired = Boolean.FALSE; 166 } 167 } 168 return this.isCacheAccessPreCheckRequired.booleanValue(); 169 } 170 171 174 public void clearCacheAccessPreCheck() { 175 this.isCacheAccessPreCheckRequired = null; 176 } 177 178 182 public void acquireReadLock() { 183 getSession().startOperationProfile(SessionProfiler.CACHE); 184 185 if (getSession().getDatasourceLogin().shouldSynchronizedReadOnWrite()) { 186 getCacheMutex().acquireReadLock(); 187 } 188 189 getSession().endOperationProfile(SessionProfiler.CACHE); 190 } 191 192 198 public CacheKey acquireReadLockOnCacheKey(Vector primaryKey, Class domainClass, ClassDescriptor descriptor) { 199 CacheKey cacheKey = null; 200 if (isCacheAccessPreCheckRequired()) { 201 getSession().startOperationProfile(SessionProfiler.CACHE); 202 acquireReadLock(); 203 try { 204 cacheKey = getIdentityMap(descriptor).acquireReadLockOnCacheKey(primaryKey); 205 } finally { 206 releaseReadLock(); 207 } 208 getSession().endOperationProfile(SessionProfiler.CACHE); 209 } else { 210 cacheKey = getIdentityMap(descriptor).acquireReadLockOnCacheKey(primaryKey); 211 } 212 213 return cacheKey; 214 } 215 216 223 public CacheKey acquireReadLockOnCacheKeyNoWait(Vector primaryKey, Class domainClass, ClassDescriptor descriptor) { 224 CacheKey cacheKey = null; 225 if (isCacheAccessPreCheckRequired()) { 226 getSession().startOperationProfile(SessionProfiler.CACHE); 227 acquireReadLock(); 228 try { 229 cacheKey = getIdentityMap(descriptor).acquireReadLockOnCacheKeyNoWait(primaryKey); 230 } finally { 231 releaseReadLock(); 232 } 233 getSession().endOperationProfile(SessionProfiler.CACHE); 234 } else { 235 cacheKey = getIdentityMap(descriptor).acquireReadLockOnCacheKeyNoWait(primaryKey); 236 } 237 238 return cacheKey; 239 } 240 241 246 public boolean acquireWriteLock() { 247 if (getSession().getDatasourceLogin().shouldSynchronizedReadOnWrite() || getSession().getDatasourceLogin().shouldSynchronizeWrites()) { 248 getCacheMutex().acquire(); 249 return true; 250 } 251 return false; 252 } 253 254 258 public IdentityMap buildNewIdentityMap(ClassDescriptor descriptor) throws ValidationException, DescriptorException { 259 if (getSession().isUnitOfWork()) { 260 return new FullIdentityMap(100); 261 } 262 263 try { 264 Constructor constructor = null; 265 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 266 try { 267 constructor = (Constructor)AccessController.doPrivileged(new PrivilegedGetConstructorFor(descriptor.getIdentityMapClass(), new Class [] { ClassConstants.PINT }, false)); 268 return (IdentityMap)AccessController.doPrivileged(new PrivilegedInvokeConstructor(constructor, new Object [] { new Integer (descriptor.getIdentityMapSize())})); 269 } catch (PrivilegedActionException exception) { 270 throw DescriptorException.invalidIdentityMap(descriptor, exception.getException()); 271 } 272 } else { 273 constructor = PrivilegedAccessHelper.getConstructorFor(descriptor.getIdentityMapClass(), new Class [] { ClassConstants.PINT }, false); 274 return (IdentityMap)PrivilegedAccessHelper.invokeConstructor(constructor, new Object [] { new Integer (descriptor.getIdentityMapSize())}); 275 276 } 277 } catch (Exception exception) { 278 throw DescriptorException.invalidIdentityMap(descriptor, exception); 279 } 280 } 281 282 286 public void clearLastAccessedIdentityMap() { 287 lastAccessedIdentityMap = null; 288 lastAccessedIdentityMapClass = null; 289 } 290 291 295 public Object clone() { 296 IdentityMapManager manager = null; 297 298 try { 299 manager = (IdentityMapManager)super.clone(); 300 manager.setIdentityMaps(new Hashtable()); 301 for (Enumeration identityMapEnum = getIdentityMaps().keys(); 302 identityMapEnum.hasMoreElements();) { 303 Class theClass = (Class )identityMapEnum.nextElement(); 304 manager.getIdentityMaps().put(theClass, ((IdentityMap)getIdentityMaps().get(theClass)).clone()); 305 } 306 } catch (Exception e) { 307 ; 308 } 309 310 return manager; 311 } 312 313 316 public void clearQueryCache() { 317 this.queryResults = JavaPlatform.getQueryCacheMap(); 318 } 319 320 326 public void clearQueryCache(ReadQuery query) { 327 if (query != null) { 328 queryResults.remove(query); 329 } 330 } 331 332 public boolean containsKey(Vector key, Class theClass, ClassDescriptor descriptor) { 333 for (int index = 0; index < key.size(); index++) { 335 if (key.elementAt(index) == null) { 336 return false; 337 } 338 } 339 340 IdentityMap map = getIdentityMap(descriptor); 341 boolean contains; 342 343 if (isCacheAccessPreCheckRequired()) { 344 getSession().startOperationProfile(SessionProfiler.CACHE); 345 acquireReadLock(); 346 try { 347 contains = map.containsKey(key); 348 } finally { 349 releaseReadLock(); 350 getSession().endOperationProfile(SessionProfiler.CACHE); 351 } 352 } else { 353 contains = map.containsKey(key); 354 } 355 356 return contains; 357 } 358 359 362 public Vector getAllFromIdentityMap(Expression selectionCriteria, Class theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean shouldReturnInvalidatedObjects) { 363 ClassDescriptor descriptor = getSession().getDescriptor(theClass); 364 getSession().startOperationProfile(SessionProfiler.CACHE); 365 Vector objects = null; 366 try { 367 Expression selectionCriteriaClone = selectionCriteria; 368 369 if ((selectionCriteria != null) && (selectionCriteriaClone.getBuilder().getSession() == null)) { 371 selectionCriteriaClone = (Expression)selectionCriteria.clone(); 372 selectionCriteriaClone.getBuilder().setSession(getSession()); 373 selectionCriteriaClone.getBuilder().setQueryClass(theClass); 374 } 375 objects = new Vector(); 376 IdentityMap map = getIdentityMap(descriptor); 377 378 long currentTimeInMillis = System.currentTimeMillis(); 380 for (Enumeration cacheEnum = map.keys(); cacheEnum.hasMoreElements();) { 381 CacheKey key = (CacheKey)cacheEnum.nextElement(); 382 if ((key.getObject() == null) || (!shouldReturnInvalidatedObjects && getSession().getDescriptor(theClass).getCacheInvalidationPolicy().isInvalidated(key, currentTimeInMillis))) { 383 continue; 384 } 385 Object object = key.getObject(); 386 387 if (object == null) { 389 continue; 390 } 391 392 if ((object.getClass() == theClass) || (theClass.isInstance(object))) { 394 if (selectionCriteriaClone == null) { 395 objects.addElement(object); 396 getSession().incrementProfile(SessionProfiler.CacheHits); 397 } else { 398 try { 399 if (selectionCriteriaClone.doesConform(object, getSession(), (AbstractRecord)translationRow, valueHolderPolicy)) { 400 objects.addElement(object); 401 getSession().incrementProfile(SessionProfiler.CacheHits); 402 } 403 } catch (QueryException queryException) { 404 if (queryException.getErrorCode() == QueryException.MUST_INSTANTIATE_VALUEHOLDERS) { 405 if (valueHolderPolicy.shouldIgnoreIndirectionExceptionReturnConformed()) { 406 objects.addElement(object); 407 getSession().incrementProfile(SessionProfiler.CacheHits); 408 } else if (valueHolderPolicy.shouldThrowIndirectionException()) { 409 throw queryException; 410 } 411 } else { 412 throw queryException; 413 } 414 } 415 } 416 } 417 } 418 } finally { 419 getSession().endOperationProfile(SessionProfiler.CACHE); 420 } 421 return objects; 422 } 423 424 431 public CacheKey getCacheKeyForObject(Vector primaryKey, Class myClass, ClassDescriptor descriptor) { 432 IdentityMap map = getIdentityMap(descriptor); 433 CacheKey cacheKey = null; 434 if (isCacheAccessPreCheckRequired()) { 435 getSession().startOperationProfile(SessionProfiler.CACHE); 436 acquireReadLock(); 437 try { 438 cacheKey = map.getCacheKey(primaryKey); 439 } finally { 440 releaseReadLock(); 441 getSession().endOperationProfile(SessionProfiler.CACHE); 442 } 443 } else { 444 cacheKey = map.getCacheKey(primaryKey); 445 } 446 return cacheKey; 447 } 448 449 454 public ConcurrencyManager getCacheMutex() { 455 return cacheMutex; 456 } 457 458 462 public Vector getClassesRegistered() { 463 Enumeration classes = getIdentityMaps().keys(); 464 Vector results = new Vector(getIdentityMaps().size()); 465 while (classes.hasMoreElements()) { 466 results.add(((Class )classes.nextElement()).getName()); 467 } 468 return results; 469 } 470 471 475 public Object getFromIdentityMap(Object object) { 476 ClassDescriptor descriptor = getSession().getDescriptor(object); 477 Vector primaryKey = descriptor.getObjectBuilder().extractPrimaryKeyFromObject(object, getSession()); 478 return getFromIdentityMap(primaryKey, object.getClass(), descriptor); 479 } 480 481 484 public Object getFromIdentityMap(Vector key, Class theClass, ClassDescriptor descriptor) { 485 return getFromIdentityMap(key, theClass, true, descriptor); 486 } 487 488 492 public Object getFromIdentityMap(Vector key, Class theClass, boolean shouldReturnInvalidatedObjects, ClassDescriptor descriptor) { 493 if (key == null) { 494 return null; 495 } 496 497 for (int index = 0; index < key.size(); index++) { 499 if (key.elementAt(index) == null) { 500 return null; 501 } 502 } 503 504 CacheKey cacheKey; 505 IdentityMap map = getIdentityMap(descriptor); 506 Object domainObject = null; 507 if (isCacheAccessPreCheckRequired()) { 508 getSession().startOperationProfile(SessionProfiler.CACHE); 509 acquireReadLock(); 510 try { 511 cacheKey = map.getCacheKey(key); 512 } finally { 513 releaseReadLock(); 514 } 515 } else { 516 cacheKey = map.getCacheKey(key); 517 } 518 519 if ((cacheKey != null) && (shouldReturnInvalidatedObjects || !getSession().getDescriptor(theClass).getCacheInvalidationPolicy().isInvalidated(cacheKey, System.currentTimeMillis()))) { 520 try { 522 cacheKey.acquireReadLock(); 523 } finally { 524 cacheKey.releaseReadLock(); 525 } 526 try { 528 cacheKey.acquireReadLock(); 529 domainObject = cacheKey.getObject(); 530 } finally { 531 cacheKey.releaseReadLock(); 532 } 533 domainObject = checkForInheritance(domainObject, theClass); 535 } 536 537 if (isCacheAccessPreCheckRequired()) { 538 getSession().endOperationProfile(SessionProfiler.CACHE); 539 if (domainObject == null) { 540 getSession().incrementProfile(SessionProfiler.CacheMisses); 541 } else { 542 getSession().incrementProfile(SessionProfiler.CacheHits); 543 } 544 } 545 546 return domainObject; 547 } 548 549 public Object getFromIdentityMap(Expression selectionCriteria, Class theClass, Record translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean conforming, boolean shouldReturnInvalidatedObjects, ClassDescriptor descriptor) { 550 UnitOfWorkImpl unitOfWork = (conforming) ? (UnitOfWorkImpl)getSession() : null; 551 getSession().startOperationProfile(SessionProfiler.CACHE); 552 try { 553 Expression selectionCriteriaClone = selectionCriteria; 554 555 if ((selectionCriteria != null) && (selectionCriteriaClone.getBuilder().getSession() == null)) { 557 selectionCriteriaClone = (Expression)selectionCriteria.clone(); 558 selectionCriteriaClone.getBuilder().setSession(getSession()); 559 selectionCriteriaClone.getBuilder().setQueryClass(theClass); 560 } 561 IdentityMap map = getIdentityMap(descriptor); 562 563 long currentTimeInMillis = System.currentTimeMillis(); 565 for (Enumeration cacheEnum = map.keys(); cacheEnum.hasMoreElements();) { 566 CacheKey key = (CacheKey)cacheEnum.nextElement(); 567 if (!shouldReturnInvalidatedObjects && descriptor.getCacheInvalidationPolicy().isInvalidated(key, currentTimeInMillis)) { 568 continue; 569 } 570 Object object = key.getObject(); 571 572 if (object == null) { 574 continue; 575 } 576 577 if ((object.getClass() == theClass) || (theClass.isInstance(object))) { 579 if (selectionCriteriaClone == null) { 580 if (!(conforming && unitOfWork.isObjectDeleted(object))) { 582 getSession().incrementProfile(SessionProfiler.CacheHits); 583 return object; 584 } 585 } 586 587 try { 589 if (selectionCriteriaClone.doesConform(object, getSession(), (AbstractRecord)translationRow, valueHolderPolicy)) { 590 if (!(conforming && unitOfWork.isObjectDeleted(object))) { 592 getSession().incrementProfile(SessionProfiler.CacheHits); 593 return object; 594 } 595 } 596 } catch (QueryException queryException) { 597 if (queryException.getErrorCode() == QueryException.MUST_INSTANTIATE_VALUEHOLDERS) { 598 if (valueHolderPolicy.shouldIgnoreIndirectionExceptionReturnConformed()) { 599 if (!(conforming && unitOfWork.isObjectDeleted(object))) { 601 getSession().incrementProfile(SessionProfiler.CacheHits); 602 return object; 603 } 604 } else if (valueHolderPolicy.shouldIgnoreIndirectionExceptionReturnNotConformed()) { 605 } else { 607 throw queryException; 608 } 609 } else { 610 throw queryException; 611 } 612 } 613 } 614 } 615 } finally { 616 getSession().endOperationProfile(SessionProfiler.CACHE); 617 } 618 return null; 619 } 620 621 625 public Object getFromIdentityMapWithDeferredLock(Vector key, Class theClass, boolean shouldReturnInvalidatedObjects, ClassDescriptor descriptor) { 626 if (key == null) { 627 getSession().incrementProfile(SessionProfiler.CacheMisses); 628 return null; 629 } 630 631 for (int index = 0; index < key.size(); index++) { 633 if (key.elementAt(index) == null) { 634 getSession().incrementProfile(SessionProfiler.CacheMisses); 635 return null; 636 } 637 } 638 639 IdentityMap map = getIdentityMap(descriptor); 640 CacheKey cacheKey; 641 Object domainObject = null; 642 if (isCacheAccessPreCheckRequired()) { 643 getSession().startOperationProfile(SessionProfiler.CACHE); 644 acquireReadLock(); 645 try { 646 cacheKey = map.getCacheKey(key); 647 } finally { 648 releaseReadLock(); 649 } 650 } else { 651 cacheKey = map.getCacheKey(key); 652 } 653 654 if ((cacheKey != null) && (shouldReturnInvalidatedObjects || !descriptor.getCacheInvalidationPolicy().isInvalidated(cacheKey, System.currentTimeMillis()))) { 655 cacheKey.acquireDeferredLock(); 656 domainObject = cacheKey.getObject(); 657 cacheKey.releaseDeferredLock(); 658 } 659 660 domainObject = checkForInheritance(domainObject, theClass); 662 663 if (isCacheAccessPreCheckRequired()) { 664 getSession().endOperationProfile(SessionProfiler.CACHE); 665 if (domainObject == null) { 666 getSession().incrementProfile(SessionProfiler.CacheMisses); 667 } else { 668 getSession().incrementProfile(SessionProfiler.CacheHits); 669 } 670 } 671 672 return domainObject; 673 } 674 675 679 public IdentityMap getIdentityMap(ClassDescriptor descriptor) { 680 IdentityMap identityMap; 681 682 if (descriptor.hasInheritance()) { 685 descriptor = descriptor.getInheritancePolicy().getRootParentDescriptor(); 686 } 687 Class descriptorClass = descriptor.getJavaClass(); 688 689 synchronized (this) { 692 IdentityMap tempMap = this.lastAccessedIdentityMap; 694 if ((tempMap != null) && (this.lastAccessedIdentityMapClass == descriptorClass)) { 695 return tempMap; 696 } 697 698 identityMap = (IdentityMap)getIdentityMaps().get(descriptorClass); 700 if (identityMap == null) { 701 identityMap = buildNewIdentityMap(descriptor); 702 getIdentityMaps().put(descriptorClass, identityMap); 703 } 704 this.lastAccessedIdentityMap = identityMap; 705 this.lastAccessedIdentityMapClass = descriptorClass; 706 } 707 return identityMap; 708 } 709 710 protected Hashtable getIdentityMaps() { 711 return identityMaps; 712 } 713 714 719 public Enumeration getIdentityMapClasses() { 720 return identityMaps.keys(); 721 } 722 723 protected Vector getKey(Object domainObject) { 724 return getSession().keyFromObject(domainObject); 725 } 726 727 protected AbstractSession getSession() { 728 return session; 729 } 730 731 735 public Object getWrapper(Vector primaryKey, Class theClass) { 736 ClassDescriptor descriptor = getSession().getDescriptor(theClass); 737 IdentityMap map = getIdentityMap(descriptor); 738 Object wrapper; 739 if (isCacheAccessPreCheckRequired()) { 740 getSession().startOperationProfile(SessionProfiler.CACHE); 741 acquireReadLock(); 742 try { 743 wrapper = map.getWrapper(primaryKey); 744 } finally { 745 releaseReadLock(); 746 } 747 getSession().endOperationProfile(SessionProfiler.CACHE); 748 } else { 749 wrapper = map.getWrapper(primaryKey); 750 } 751 return wrapper; 752 } 753 754 758 public WriteLockManager getWriteLockManager() { 759 synchronized (this) { 762 if (this.writeLockManager == null) { 763 this.writeLockManager = new WriteLockManager(); 764 } 765 } 766 return this.writeLockManager; 767 } 768 769 772 public Object getWriteLockValue(Vector primaryKey, Class domainClass, ClassDescriptor descriptor) { 773 IdentityMap map = getIdentityMap(descriptor); 774 Object value; 775 if (isCacheAccessPreCheckRequired()) { 776 getSession().startOperationProfile(SessionProfiler.CACHE); 777 acquireReadLock(); 778 try { 779 value = map.getWriteLockValue(primaryKey); 780 } finally { 781 releaseReadLock(); 782 } 783 getSession().endOperationProfile(SessionProfiler.CACHE); 784 } else { 785 value = map.getWriteLockValue(primaryKey); 786 } 787 return value; 788 } 789 790 794 public void initializeIdentityMap(Class theClass) throws TopLinkException { 795 ClassDescriptor descriptor = getSession().getDescriptor(theClass); 796 797 if (descriptor == null) { 798 throw ValidationException.missingDescriptor(String.valueOf(theClass)); 799 } 800 if (descriptor.isChildDescriptor()) { 801 throw ValidationException.childDescriptorsDoNotHaveIdentityMap(); 802 } 803 804 Class javaClass = descriptor.getJavaClass(); 806 807 if (javaClass == lastAccessedIdentityMapClass) { 809 clearLastAccessedIdentityMap(); 810 } 811 IdentityMap identityMap = buildNewIdentityMap(descriptor); 812 getIdentityMaps().put(javaClass, identityMap); 813 } 814 815 public void initializeIdentityMaps() { 816 clearLastAccessedIdentityMap(); 817 setIdentityMaps(new Hashtable()); 818 clearQueryCache(); 819 } 820 821 826 public void printIdentityMap(Class businessClass) { 827 String cr = Helper.cr(); 828 ClassDescriptor descriptor = getSession().getDescriptor(businessClass); 829 int cacheCounter = 0; 830 StringWriter writer = new StringWriter(); 831 if (descriptor.isAggregateDescriptor()) { 832 return; } 834 835 IdentityMap map = getIdentityMap(descriptor); 836 writer.write(LoggingLocalization.buildMessage("identitymap_for", new Object [] { cr, Helper.getShortClassName(map.getClass()), Helper.getShortClassName(businessClass) })); 837 if (descriptor.hasInheritance()) { 838 if (descriptor.getInheritancePolicy().isRootParentDescriptor()) { 839 writer.write(LoggingLocalization.buildMessage("includes")); 840 Vector childDescriptors; 841 childDescriptors = descriptor.getInheritancePolicy().getChildDescriptors(); 842 if ((childDescriptors != null) && (childDescriptors.size() != 0)) { Enumeration enum2 = childDescriptors.elements(); 844 writer.write(Helper.getShortClassName((Class )((ClassDescriptor)enum2.nextElement()).getJavaClass())); 845 while (enum2.hasMoreElements()) { 846 writer.write(", " + Helper.getShortClassName((Class )((ClassDescriptor)enum2.nextElement()).getJavaClass())); 847 } 848 } 849 writer.write(")"); 850 } 851 } 852 853 for (Enumeration enumtr = map.keys(); enumtr.hasMoreElements();) { 854 oracle.toplink.essentials.internal.identitymaps.CacheKey cacheKey = (oracle.toplink.essentials.internal.identitymaps.CacheKey)enumtr.nextElement(); 855 Object object = cacheKey.getObject(); 856 if (businessClass.isInstance(object)) { 857 cacheCounter++; 858 if (object == null) { 859 writer.write(LoggingLocalization.buildMessage("key_object_null", new Object [] { cr, cacheKey.getKey(), "\t" })); 860 } else { 861 writer.write(LoggingLocalization.buildMessage("key_identity_hash_code_object", new Object [] { cr, cacheKey.getKey(), "\t", String.valueOf(System.identityHashCode(object)), object })); 862 } 863 } 864 } 865 writer.write(LoggingLocalization.buildMessage("elements", new Object [] { cr, String.valueOf(cacheCounter) })); 866 getSession().log(SessionLog.SEVERE, SessionLog.CACHE, writer.toString(), null, null, false); 867 } 868 869 874 public void printIdentityMaps() { 875 for (Iterator iterator = getSession().getDescriptors().keySet().iterator(); 876 iterator.hasNext();) { 877 Class businessClass = (Class )iterator.next(); 878 ClassDescriptor descriptor = getSession().getDescriptor(businessClass); 879 if (descriptor.hasInheritance()) { 880 if (descriptor.getInheritancePolicy().isRootParentDescriptor()) { 881 printIdentityMap(businessClass); 882 } 883 } else { 884 printIdentityMap(businessClass); 885 } 886 } 887 } 888 889 894 public void printLocks() { 895 StringWriter writer = new StringWriter(); 896 HashMap threadCollection = new HashMap(); 897 writer.write(TraceLocalization.buildMessage("lock_writer_header", (Object [])null) + Helper.cr()); 898 Iterator idenityMapsIterator = this.session.getIdentityMapAccessorInstance().getIdentityMapManager().getIdentityMaps().values().iterator(); 899 while (idenityMapsIterator.hasNext()) { 900 IdentityMap idenityMap = (IdentityMap)idenityMapsIterator.next(); 901 idenityMap.collectLocks(threadCollection); 902 } 903 Object [] parameters = new Object [1]; 904 for (Iterator threads = threadCollection.keySet().iterator(); threads.hasNext();) { 905 Thread activeThread = (Thread )threads.next(); 906 parameters[0] = activeThread.getName(); 907 writer.write(TraceLocalization.buildMessage("active_thread", parameters) + Helper.cr()); 908 for (Iterator cacheKeys = ((HashSet)threadCollection.get(activeThread)).iterator(); 909 cacheKeys.hasNext();) { 910 CacheKey cacheKey = (CacheKey)cacheKeys.next(); 911 parameters[0] = cacheKey.getObject(); 912 writer.write(TraceLocalization.buildMessage("locked_object", parameters) + Helper.cr()); 913 parameters[0] = new Integer (cacheKey.getMutex().getDepth()); 914 writer.write(TraceLocalization.buildMessage("depth", parameters) + Helper.cr()); 915 } 916 DeferredLockManager deferredLockManager = ConcurrencyManager.getDeferredLockManager(activeThread); 917 if (deferredLockManager != null) { 918 for (Iterator deferredLocks = deferredLockManager.getDeferredLocks().iterator(); 919 deferredLocks.hasNext();) { 920 ConcurrencyManager lock = (ConcurrencyManager)deferredLocks.next(); 921 parameters[0] = lock.getOwnerCacheKey().getObject(); 922 writer.write(TraceLocalization.buildMessage("deferred_locks", parameters) + Helper.cr()); 923 } 924 } 925 } 926 writer.write(Helper.cr() + TraceLocalization.buildMessage("lock_writer_footer", (Object [])null) + Helper.cr()); 927 getSession().log(SessionLog.FINEST, SessionLog.CACHE, writer.toString(), null, null, false); 928 } 929 930 935 public void printLocks(Class theClass) { 936 ClassDescriptor descriptor = getSession().getDescriptor(theClass); 937 StringWriter writer = new StringWriter(); 938 HashMap threadCollection = new HashMap(); 939 writer.write(TraceLocalization.buildMessage("lock_writer_header", (Object [])null) + Helper.cr()); 940 IdentityMap identityMap = getIdentityMap(descriptor); 941 identityMap.collectLocks(threadCollection); 942 943 Object [] parameters = new Object [1]; 944 for (Iterator threads = threadCollection.keySet().iterator(); threads.hasNext();) { 945 Thread activeThread = (Thread )threads.next(); 946 parameters[0] = activeThread.getName(); 947 writer.write(TraceLocalization.buildMessage("active_thread", parameters) + Helper.cr()); 948 for (Iterator cacheKeys = ((HashSet)threadCollection.get(activeThread)).iterator(); 949 cacheKeys.hasNext();) { 950 CacheKey cacheKey = (CacheKey)cacheKeys.next(); 951 parameters[0] = cacheKey.getObject(); 952 writer.write(TraceLocalization.buildMessage("locked_object", parameters) + Helper.cr()); 953 parameters[0] = new Integer (cacheKey.getMutex().getDepth()); 954 writer.write(TraceLocalization.buildMessage("depth", parameters) + Helper.cr()); 955 } 956 DeferredLockManager deferredLockManager = ConcurrencyManager.getDeferredLockManager(activeThread); 957 if (deferredLockManager != null) { 958 for (Iterator deferredLocks = deferredLockManager.getDeferredLocks().iterator(); 959 deferredLocks.hasNext();) { 960 ConcurrencyManager lock = (ConcurrencyManager)deferredLocks.next(); 961 parameters[0] = lock.getOwnerCacheKey().getObject(); 962 writer.write(TraceLocalization.buildMessage("deferred_locks", parameters) + Helper.cr()); 963 } 964 } 965 } 966 writer.write(Helper.cr() + TraceLocalization.buildMessage("lock_writer_footer", (Object [])null) + Helper.cr()); 967 getSession().log(SessionLog.FINEST, SessionLog.CACHE, writer.toString(), null, null, false); 968 } 969 970 975 public CacheKey putInIdentityMap(Object domainObject, Vector keys, Object writeLockValue, long readTime, ClassDescriptor descriptor) { 976 ObjectBuilder builder = descriptor.getObjectBuilder(); 977 Object implementation = builder.unwrapObject(domainObject, getSession()); 978 979 IdentityMap map = getIdentityMap(descriptor); 980 CacheKey cacheKey; 981 982 if (isCacheAccessPreCheckRequired()) { 983 getSession().startOperationProfile(SessionProfiler.CACHE); 984 acquireReadLock(); 986 try { 987 cacheKey = map.put(keys, implementation, writeLockValue, readTime); 988 } finally { 989 releaseReadLock(); 990 } 991 getSession().endOperationProfile(SessionProfiler.CACHE); 992 } else { 993 cacheKey = map.put(keys, implementation, writeLockValue, readTime); 994 } 995 return cacheKey; 996 } 997 998 1001 protected void releaseReadLock() { 1002 if (getSession().getDatasourceLogin().shouldSynchronizedReadOnWrite()) { 1003 getCacheMutex().releaseReadLock(); 1004 } 1005 } 1006 1007 1012 public void releaseWriteLock() { 1013 if (getSession().getDatasourceLogin().shouldSynchronizedReadOnWrite() || getSession().getDatasourceLogin().shouldSynchronizeWrites()) { 1014 getCacheMutex().release(); 1015 } 1016 } 1017 1018 1021 public Object removeFromIdentityMap(Vector key, Class domainClass, ClassDescriptor descriptor) { 1022 IdentityMap map = getIdentityMap(descriptor); 1023 Object value; 1024 1025 if (isCacheAccessPreCheckRequired()) { 1026 getSession().startOperationProfile(SessionProfiler.CACHE); 1027 acquireReadLock(); 1029 try { 1030 value = map.remove(key); 1031 } finally { 1032 releaseReadLock(); 1033 } 1034 getSession().endOperationProfile(SessionProfiler.CACHE); 1035 } else { 1036 value = map.remove(key); 1037 } 1038 return value; 1039 } 1040 1041 1046 protected void setCacheMutex(ConcurrencyManager cacheMutex) { 1047 this.cacheMutex = cacheMutex; 1048 } 1049 1050 public void setIdentityMaps(Hashtable identityMaps) { 1051 clearLastAccessedIdentityMap(); 1052 this.identityMaps = identityMaps; 1053 } 1054 1055 protected void setSession(AbstractSession session) { 1056 this.session = session; 1057 } 1058 1059 1063 public void setWrapper(Vector primaryKey, Class theClass, Object wrapper) { 1064 ClassDescriptor descriptor = getSession().getDescriptor(theClass); 1065 IdentityMap map = getIdentityMap(descriptor); 1066 1067 if (isCacheAccessPreCheckRequired()) { 1068 getSession().startOperationProfile(SessionProfiler.CACHE); 1069 acquireReadLock(); 1071 try { 1072 map.setWrapper(primaryKey, wrapper); 1073 } finally { 1074 releaseReadLock(); 1075 } 1076 getSession().endOperationProfile(SessionProfiler.CACHE); 1077 } else { 1078 map.setWrapper(primaryKey, wrapper); 1079 } 1080 } 1081 1082 1085 public void setWriteLockValue(Vector primaryKey, Class theClass, Object writeLockValue) { 1086 ClassDescriptor descriptor = getSession().getDescriptor(theClass); 1087 IdentityMap map = getIdentityMap(descriptor); 1088 1089 if (isCacheAccessPreCheckRequired()) { 1090 getSession().startOperationProfile(SessionProfiler.CACHE); 1091 acquireReadLock(); 1093 try { 1094 map.setWriteLockValue(primaryKey, writeLockValue); 1095 } finally { 1096 releaseReadLock(); 1097 } 1098 getSession().endOperationProfile(SessionProfiler.CACHE); 1099 } else { 1100 map.setWriteLockValue(primaryKey, writeLockValue); 1101 } 1102 } 1103 1104 1111 private Object checkForInheritance(Object domainObject, Class superClass) { 1112 if ((domainObject != null) && ((domainObject.getClass() != superClass) && (!superClass.isInstance(domainObject)))) { 1113 ClassDescriptor descriptor = getSession().getDescriptor(superClass); 1115 1116 if (descriptor.hasInheritance() && descriptor.getInheritancePolicy().getUseDescriptorsToValidateInheritedObjects()) { 1117 if (descriptor.getInheritancePolicy().getSubclassDescriptor(domainObject.getClass()) == null) { 1120 return null; 1121 } 1122 1123 return domainObject; 1125 } 1126 1127 return null; 1129 } 1130 1131 return domainObject; 1133 } 1134} 1135 | Popular Tags |