1 19 package org.fjank.jcache; 20 21 import java.io.File ; 22 import java.io.Serializable ; 23 import java.util.Iterator ; 24 import javax.util.jcache.Attributes; 25 import javax.util.jcache.CacheException; 26 import javax.util.jcache.CacheFullException; 27 import javax.util.jcache.CacheLoader; 28 import javax.util.jcache.CacheLogger; 29 import javax.util.jcache.DiskCacheException; 30 import javax.util.jcache.NotARetrievableObjectException; 31 import javax.util.jcache.ObjectNotFoundException; 32 import org.fjank.jcache.persistence.DiskCache; 33 34 35 42 public final class CacheAccessImpl2 { 43 44 private boolean valid = true; 45 46 private CacheRegion region; 47 private Class lastException; 48 private String lastMessage; 49 50 56 CacheAccessImpl2(final CacheRegion aRegion) { 57 this.region = aRegion; 58 } 59 60 70 public void cancelResponse() { 71 if (!isValid()) { 72 return; 73 } 74 } 75 76 81 private boolean isValid() { 82 if (this.valid) { 83 return true; 84 } 85 return false; 86 } 87 88 92 public void close() { 93 if (!isValid()) { 94 return; 95 } 96 region = null; 97 this.valid = false; 98 } 99 100 106 public void defineGroup(final String name) { 107 if (!isValid()) { 108 return; 109 } 110 defineGroupImpl(name, null, null); 111 } 112 113 120 public void defineGroup(final String name, final Attributes attributes) { 121 if (!isValid()) { 122 return; 123 } 124 defineGroupImpl(name, null, attributes); 125 } 126 127 135 public void defineGroup(final String name, final String group) { 136 if (!isValid()) { 137 return; 138 } 139 defineGroupImpl(name, group, null); 140 } 141 142 150 public void defineGroup(final String name, final String groupName, final Attributes attributes) { 151 if (!isValid()) { 152 return; 153 } 154 defineGroupImpl(name, groupName, attributes); 155 } 156 157 163 private void defineGroupImpl(final String name, final String groupName, final Attributes attributes) { 164 if (name == null) { 165 return; 166 } 167 if (name.equals("")) { 168 return; 169 } 170 CacheGroup group; 171 if (groupName == null) { 172 group = region; 173 } else { 174 group = region.getGroup(groupName); 175 if (group == null) { 176 return; 177 } 178 } 179 group.put(new CacheGroup(name, new AttributesImpl(attributes))); 180 } 181 182 195 public void defineObject(final Object name, final Attributes attributes) { 196 if (!isValid()) { 197 return; 198 } 199 putImpl(name, null, attributes, NullObject.getInstance()); 200 } 201 202 211 public void defineObject(final Object name, final String group, final Attributes attributes) { 212 if (!isValid()) { 213 return; 214 } 215 putImpl(name, group, attributes, NullObject.getInstance()); 216 } 217 218 225 public void destroy() { 226 if (!isValid()) { 227 return; 228 } 229 if (region.getName() != null) { 230 destroy(region.getName()); 231 } else { 232 region.destroy(); 233 } 234 close(); 235 } 236 237 245 public void destroy(final Object name) { 246 if (!isValid()) { 247 return; 248 } 249 CacheImpl.getCache(true).destroyRegion(name); 250 } 251 252 270 public Object get(final Object name) { 271 if (!isValid()) { 272 return null; 273 } 274 return getImpl(name, null, null); 275 } 276 277 290 public Object get(final Object name, final Object arguments) { 291 if (!isValid()) { 292 return null; 293 } 294 return getImpl(name, null, arguments); 295 } 296 297 302 private Object getImpl(final Object name, final String group, final Object arguments) { 303 CacheObject objHandle = getCacheObject(name, group); 304 if (objHandle == null) { 305 return null; 306 } 307 return getTrueObject(objHandle, arguments); 308 } 309 310 private Object getTrueObject(final CacheObject objHandle, final Object arguments) { 311 if (objHandle.needsLoading(arguments)) { 312 Attributes attribs = objHandle.getAttributes(); 313 CacheLoader loader = attribs.getLoader(); 314 Object retrievedObject; 315 try { 316 retrievedObject = loader.load(objHandle, arguments); 317 if (retrievedObject == null) { 318 throw new ObjectNotFoundException("The returned object from the CacheLoader " + loader.getClass().getName() + " was null."); 319 } 320 if (retrievedObject instanceof CacheOutputStream) { 321 334 return ((CacheOutputStream) retrievedObject).getStreamObject().getInputStream(); 335 } else if (retrievedObject instanceof File ) { 336 340 return ((File ) retrievedObject).getAbsolutePath(); 341 } 342 attribs.setCreateTime(System.currentTimeMillis()); 343 } catch (CacheException e) { 344 this.lastException = e.getClass(); 345 this.lastMessage = e.getMessage(); 346 return null; 347 } 348 CacheGroup group = objHandle.getGroup(); 349 CacheObject newRef = new CacheObject(objHandle.getKey(), retrievedObject, group, region, CacheImpl.getCache().getReferenceQueue()); 350 group.replace(objHandle.getKey(), newRef); 351 return newRef.get(); 352 } 353 return objHandle.get(); 354 } 355 356 363 private CacheObject getCacheObject(final Object name, final String group) { 364 Object object = null; 365 CacheGroup parentCacheObject = region; 367 if (group != null) { 368 parentCacheObject = region.getGroup(group); 369 if (parentCacheObject == null) { 370 return null; 371 } 372 } 373 if (parentCacheObject.contains(name)) { 374 object = parentCacheObject.get(name); 375 } else if (name instanceof Serializable ) { 376 try { 378 DiskCache diskCache = CacheImpl.getCache().getDiskCache(); 379 if (diskCache != null) { 380 object = diskCache.getObject((Serializable ) name); 381 if (object == null) { 382 return null; 383 } 384 } 385 } catch (DiskCacheException e) { 386 this.lastException = DiskCacheException.class; 387 this.lastMessage = e.getMessage(); 388 return null; 389 } 390 } 391 if (object == null) { 392 return null; 393 } 394 if (object instanceof CacheGroup) { 395 this.lastException = NotARetrievableObjectException.class; 396 this.lastMessage = ("This object is a group and cannot be retrieved."); 397 return null; 398 } 399 CacheObject objHandle = (CacheObject) object; 400 return objHandle; 401 } 402 403 418 public Object get(final Object name, final String group, final Object arguments) { 419 if (!isValid()) { 420 return null; 421 } 422 return getImpl(name, group, arguments); 423 } 424 425 432 public Attributes getAttributes() { 433 if (!isValid()) { 434 return null; 435 } 436 return region.getAttributes(); 437 } 438 439 448 public Attributes getAttributes(final Object name) { 449 if (!isValid()) { 450 return null; 451 } 452 if (name == null) { 453 return null; 454 } 455 Object obj = region.get(name); 456 if (obj == null) { 457 return null; 458 } 459 if (obj instanceof CacheObject) { 460 return ((CacheObject) obj).getAttributes(); 461 } else if (obj instanceof CacheGroup) { 462 return ((CacheGroup) obj).getAttributes(); 463 } 464 this.lastException = CacheException.class; 465 this.lastMessage = "The object " + name + " is not valid. (" + obj.getClass().getName() + ')'; 466 return null; 467 } 468 469 491 public boolean getOwnership(final Object name, final int timeout) { 492 if (!isValid()) { 493 return false; 494 } 495 return false; 496 } 497 498 502 public void invalidate() { 503 if (!isValid()) { 504 return; 505 } 506 region.invalidate(); 507 } 509 510 522 public void invalidate(final Object name) { 523 if (!isValid()) { 524 return; 525 } 526 invalidateWithoutDistribution(name); 527 if (CacheImpl.getCache().isDistributed()) { 528 CacheImpl.getCache().getDistributionEngine().cacheObjectInvalidated(getRegionName(), (Serializable ) name); 529 } 530 } 531 532 private void invalidateWithoutDistribution(final Object name) { 533 if (!region.contains(name)) { 534 return; 535 } 536 Object object = region.get(name); 537 if (object == null) { 538 return; 539 } 540 if (object instanceof CacheGroup) { 541 CacheGroup group = (CacheGroup) object; 542 group.invalidate(); 543 } else { 544 if (object instanceof CacheObject) { 546 CacheObject obj = (CacheObject) object; 547 obj.invalidate(); 548 } 549 } 550 } 551 552 560 public boolean isPresent(final Object name) { 561 if (!isValid()) { 562 return false; 563 } 564 return region.contains(name); 565 } 566 567 577 public void preLoad(final Object name) { 578 if (!isValid()) { 579 return; 580 } 581 preLoad(name, null); 582 } 583 584 592 public void preLoad(final Object name, final Object arguments) { 593 if (!isValid()) { 594 return; 595 } 596 preLoad(name, null, arguments); 597 } 598 599 609 public void preLoad(final Object name, final String group, final Object arguments) { 610 if (!isValid()) { 611 return; 612 } 613 if (name == null) { 614 return; 616 } 617 final CacheLogger logger = CacheImpl.getCache().getAttributes().getLogger(); 618 try { 619 final CacheObject obj = getCacheObject(name, group); 620 final CacheLoader loader = obj.getAttributes().getLoader(); 621 if (loader == null) { 622 throw new ObjectNotFoundException("The object has no CacheLoader associated with it."); 623 } 624 Runnable runnable = new Runnable () { 625 public void run() { 626 try { 627 loader.load(obj, arguments); 628 } catch (CacheException e) { 629 if (logger != null) { 630 logger.log("The object was not found in the cache.", e); 631 } 632 } 633 } 634 }; 635 CacheImpl.getCache().getExecPool().execute(runnable); 636 } catch (ObjectNotFoundException e) { 637 if (logger != null) { 638 logger.log("The object was not found in the cache.", e); 639 } 640 } catch (InterruptedException e) { 641 if (logger != null) { 642 logger.log("Some strange concurrency issue have occured.", e); 643 } 644 } 645 } 646 647 659 public void put(final Object name, final Attributes attributes, final Object object) { 660 if (!isValid()) { 661 return; 662 } 663 putImpl(name, null, attributes, object); 664 } 665 666 678 public void put(final Object name, final String group, final Attributes attributes, final Object object) { 679 putImpl(name, group, attributes, object); 680 } 681 682 685 private int getCurrentObjectCount() { 686 int count = 0; 687 count += CacheImpl.getCache().getRegion().getObjectCount(); 688 Iterator regions = CacheImpl.getCache().userRegionNames(); 689 while (regions.hasNext()) { 690 count += CacheImpl.getCache().getRegion(regions.next()).getObjectCount(); 691 } 692 return count; 693 } 694 695 704 public CacheException getException(boolean cached) { 705 if (lastException == null) { 706 return null; 707 } 708 try { 709 CacheException ex = (CacheException) lastException.newInstance(); 710 ex.setMessage(lastMessage); 711 this.lastException = null; 712 this.lastMessage = null; 713 return ex; 714 } catch (InstantiationException e) { 715 throw new IllegalStateException (e.getMessage() + ":" + lastMessage); 716 } catch (IllegalAccessException e) { 717 throw new IllegalStateException (e.getMessage() + ":" + lastMessage); 718 } 719 } 720 721 730 private boolean putImpl(final Object name, final String group, final Attributes attributes, final Object object) { 731 CacheGroup objGr = region; 732 if (group != null) { 733 objGr = region.getGroup(group); 734 if (objGr == null) { 735 this.lastException = ObjectNotFoundException.class; 736 this.lastMessage = "The object " + group + " is not present in this cache."; 737 return false; 738 } 739 } 740 CacheObject o = new CacheObject(name, object, objGr, region, CacheImpl.getCache().getReferenceQueue()); 741 o.setAttributes(attributes); 742 int maxObjects = CacheImpl.getCache().getAttributes().getMaxObjects(); 743 int currentObjectCount = getCurrentObjectCount(); 744 if (currentObjectCount >= maxObjects) { 745 DiskCache diskCache = CacheImpl.getCache().getDiskCache(); 746 if (diskCache == null) { 747 this.lastException = CacheFullException.class; 749 this.lastMessage = "The maximum number of objects in the cache has been reached."; 750 return false; 751 } 752 boolean updated = diskCache.update(o); 753 if (!updated) { 754 this.lastException = CacheFullException.class; 755 this.lastMessage = "The maximum size for the diskCache has been reached."; 756 } 757 return updated; 758 } 759 if ((attributes != null) && (attributes.getSize() != 0)) { 760 if ((region.getCurrentSize() + attributes.getSize()) > (CacheImpl.getCache().getAttributes().getMemoryCacheSize() * 1024 * 1024)) { 761 DiskCache diskCache = CacheImpl.getCache().getDiskCache(); 762 if (diskCache == null) { 763 this.lastException = CacheFullException.class; 765 this.lastMessage = "The maximum size for the memory cache has been reached."; 766 return false; 767 } 768 boolean updated = diskCache.update(o); 769 if (!updated) { 770 this.lastException = CacheFullException.class; 771 this.lastMessage = "The maximum size for the diskCache has been reached."; 772 } 773 return updated; 774 } 775 } 776 objGr.put(name, o, object); 777 return true; 778 } 779 780 793 public boolean put(final Object name, final Object object) { 794 if (!isValid()) { 795 return false; 796 } 797 return putImpl(name, null, null, object); 798 } 799 800 811 public void put(Object name, String group, Object object) { 812 if (!isValid()) { 813 return; 814 } 815 putImpl(name, group, null, object); 816 } 817 818 825 public void releaseOwnership() { 826 if (!isValid()) { 827 return; 828 } 829 } 830 831 846 public Object replace(Object name, Object object) { 847 if (!isValid()) { 848 return null; 849 } 850 if (name == null) 851 return null; 852 if (object == null) 853 return null; 854 Object replacedObject = replaceWithoutDistribution(name, object); 855 if (CacheImpl.getCache().isDistributed()) { 856 CacheImpl.getCache().getDistributionEngine().cacheObjectUpdated(getRegionName(), null, (Serializable ) name, (Serializable ) object); 857 } 858 return replacedObject; 859 } 860 861 865 public Object replaceWithoutDistribution(Object name, Object object) { 866 if (!isValid()) { 867 return null; 868 } 869 return region.replace(name, new CacheObject(name, object, region, region, CacheImpl.getCache().getReferenceQueue())); 870 } 871 872 882 public Object replace(Object name, String group, Object object) { 883 if (!isValid()) { 884 return null; 885 } 886 if (name == null) 887 return null; 888 if (object == null) 889 return null; 890 if (group == null) 891 return null; 892 Object replacedObject = replaceWithoutDistribution(name, group, object); 893 if (CacheImpl.getCache().isDistributed()) { 894 CacheImpl.getCache().getDistributionEngine().cacheObjectUpdated(getRegionName(), group, (Serializable ) name, (Serializable ) object); 895 } 896 return replacedObject; 897 } 898 899 903 public Object replaceWithoutDistribution(Object name, String group, Object object) { 904 if (!isValid()) { 905 return null; 906 } 907 CacheGroup cachegroup = (CacheGroup) region.get(group); 908 if (cachegroup == null) 909 return null; 910 return cachegroup.replace(name, new CacheObject(name, object, region, region, CacheImpl.getCache().getReferenceQueue())); 911 } 912 913 925 public void resetAttributes(Attributes attributes) { 926 if (!isValid()) { 927 return; 928 } 929 Attributes att = region.getAttributes(); 930 att.reset(); 931 att.applyAttributes(attributes); 932 } 933 934 941 public void resetAttributes(Object name, Attributes attributes) { 942 if (!isValid()) { 943 return; 944 } 945 Attributes att = CacheImpl.getCache(true).getRegion(name).getAttributes(); 946 att.reset(); 947 att.applyAttributes(attributes); 948 } 949 950 958 public void save() { 959 if (!isValid()) { 960 return; 961 } 962 saveImpl(region); 963 } 964 965 972 private void saveImpl(CacheRegion region) { 973 } 974 975 985 public void save(Object name) { 986 if (!isValid()) { 987 return; 988 } 989 saveImpl(CacheImpl.getCache().getRegion(name)); 990 } 991 992 1005 public void waitForResponse(int timeout) { 1006 if (!isValid()) { 1007 return; 1008 } 1009 } 1010 1011 1016 public CacheRegion getRegion() { 1017 return region; 1018 } 1019 1020 private String getRegionName() { 1021 return region.getName() == null ? null : region.getName().toString(); 1022 } 1023} | Popular Tags |