1 package org.jahia.services.webdav.stores; 2 3 import org.apache.slide.common.*; 4 import org.apache.slide.content.*; 5 import org.apache.slide.lock.LockTokenNotFoundException; 6 import org.apache.slide.lock.NodeLock; 7 import org.apache.slide.security.NodePermission; 8 import org.apache.slide.store.AbstractStore; 9 import org.apache.slide.structure.ObjectAlreadyExistsException; 10 import org.apache.slide.structure.ObjectNode; 11 import org.apache.slide.structure.ObjectNotFoundException; 12 import org.jahia.services.cache.Cache; 13 import org.jahia.services.cache.CacheEntry; 14 import org.jahia.services.cache.CacheFactory; 15 16 import javax.transaction.xa.Xid ; 17 import javax.transaction.xa.XAException ; 18 import java.util.Date ; 19 import java.util.Enumeration ; 20 import java.util.Vector ; 21 22 29 public class CachedStore extends AbstractStore { 30 public static final String CACHE_NAME = "WebdavCache"; 31 32 private static final int TEMP_CACHE_DELAY = 5000; 33 34 final private org.apache.log4j.Logger logger = 35 org.apache.log4j.Logger.getLogger (CachedStore.class); 36 37 39 40 43 public CachedStore () { 44 super (); 45 } 46 47 49 protected Cache internalCache; 50 51 52 54 55 62 public void initialize (NamespaceAccessToken token) 63 throws ServiceInitializationFailedException { 64 super.initialize (token); 65 try { 66 internalCache = CacheFactory.createCache (CACHE_NAME); 67 68 } catch (Exception e) { 69 logger.warn ("Error while creating cache for slide store", e); 70 throw new ServiceInitializationFailedException (this, e); 71 } 72 logger.debug ("New Cache [" + CACHE_NAME + "] successfully instanciated!"); 73 } 74 75 76 78 79 89 public ObjectNode retrieveObject (Uri uri) 90 throws ServiceAccessException, ObjectNotFoundException { 91 if (nodeStore.cacheResults ()) { 92 Object tempObject = internalCache.get ( 93 "o" + uri.getNamespace ().getName () + uri.toString ()); 94 if (tempObject != null) { 95 return ((ObjectNode) tempObject).cloneObject (); 96 } else { 97 ObjectNode objectNode = super.retrieveObject (uri); 98 objectNode.validate (uri.toString ()); 99 if (!isForceStoreEnlistment (uri)) { 100 cacheEntry ("o", (JahiaDescriptorsStore) nodeStore, uri, objectNode); 101 } 102 return objectNode; 103 } 104 } else { 105 return super.retrieveObject (uri); 106 } 107 } 108 109 110 120 public void storeObject (Uri uri, ObjectNode object) 121 throws ServiceAccessException, ObjectNotFoundException { 122 super.storeObject (uri, object); 123 if (nodeStore.cacheResults ()) { 124 cacheEntry ("o", (JahiaDescriptorsStore) nodeStore, uri, object.cloneObject ()); 125 flushParentFromCache("o", uri); 126 } 127 } 128 129 130 142 public void createObject (Uri uri, ObjectNode object) 143 throws ServiceAccessException, ObjectAlreadyExistsException { 144 super.createObject (uri, object); 145 if (nodeStore.cacheResults ()) { 146 cacheEntry ("o", (JahiaDescriptorsStore) nodeStore, uri, object.cloneObject ()); 147 flushParentFromCache("o", uri); 148 } 149 } 150 151 161 public void removeObject (Uri uri, ObjectNode object) 162 throws ServiceAccessException, ObjectNotFoundException { 163 super.removeObject (uri, object); 164 if (nodeStore.cacheResults ()) { 165 internalCache.remove ("o" + uri.getNamespace ().getName () + uri.toString ()); 166 flushParentFromCache("o", uri); 167 } 168 } 169 170 public void removeObjectFromCache (Uri uri) { 171 if (nodeStore.cacheResults ()) { 172 internalCache.remove ("o" + uri.getNamespace ().getName () + uri.toString ()); 173 } 174 } 175 176 177 185 public void grantPermission (Uri uri, NodePermission permission) 186 throws ServiceAccessException { 187 super.grantPermission (uri, permission); 188 if (securityStore.cacheResults ()) { 189 NodePermission tempPermission = permission.cloneObject (); 190 Object value = internalCache.get ( 191 "p" + uri.getNamespace ().getName () + uri.toString ()); 192 if (value == null) { 193 enumeratePermissions (uri); 195 value = internalCache.get ("p" + uri.getNamespace ().getName () + uri.toString ()); 197 if (value == null) { 198 Vector permissionsVector = new Vector (); 201 permissionsVector.addElement (tempPermission); 202 if (!isForceStoreEnlistment (uri)) { 203 cacheEntry ("p", (JahiaDescriptorsStore) securityStore, uri, 204 permissionsVector); 205 } 206 } 207 } else { 208 Vector permissionsVector = (Vector ) value; 209 permissionsVector.addElement (tempPermission); 210 internalCache.put ("p" + uri.getNamespace ().getName () + uri.toString (), value); 211 } 212 } 213 } 214 215 216 224 public void revokePermission (Uri uri, NodePermission permission) 225 throws ServiceAccessException { 226 super.revokePermission (uri, permission); 227 if (securityStore.cacheResults ()) { 228 internalCache.remove ( 229 "p" + uri.getNamespace ().getName () + uri.toString ()); 230 } 231 } 232 233 234 242 public void revokePermissions (Uri uri) 243 throws ServiceAccessException { 244 super.revokePermissions (uri); 245 if (securityStore.cacheResults ()) { 246 Object value = internalCache.get ( 247 "p" + uri.getNamespace ().getName () + uri.toString ()); 248 Vector permissionsVector = null; 249 if (value != null) { 250 permissionsVector = (Vector ) value; 251 permissionsVector.removeAllElements (); 252 internalCache.put("p" + uri.getNamespace ().getName () + uri.toString (), value); 253 } 254 } 255 } 256 257 258 266 public Enumeration enumeratePermissions (Uri uri) 267 throws ServiceAccessException { 268 if (securityStore.cacheResults ()) { 270 Object value = internalCache.get ( 271 "p" + uri.getNamespace ().getName () + uri.toString ()); 272 Vector permissionsVector = null; 273 if (value != null) { 274 permissionsVector = new Vector ((Vector ) value); 275 return permissionsVector.elements (); 276 } else { 277 permissionsVector = new Vector (); 278 Enumeration tempEnum = super.enumeratePermissions (uri); 279 while (tempEnum.hasMoreElements ()) { 280 NodePermission tempPermission = 281 (NodePermission) tempEnum.nextElement (); 282 tempPermission.validate (uri.toString ()); 283 permissionsVector.addElement (tempPermission); 284 } 285 if (!isForceStoreEnlistment (uri)) { 286 cacheEntry ("p", (JahiaDescriptorsStore) securityStore, uri, 287 permissionsVector); 288 } 289 return ((Vector ) permissionsVector.clone ()).elements (); 290 } 291 } else { 292 return super.enumeratePermissions (uri); 293 } 294 } 295 296 297 305 public void putLock (Uri uri, NodeLock lock) 306 throws ServiceAccessException { 307 super.putLock (uri, lock); 308 if (lockStore.cacheResults ()) { 309 Object value = internalCache.get ( 310 "l" + uri.getNamespace ().getName () + uri.toString ()); 311 Vector locksVector = null; 312 if (value == null) { 313 locksVector = new Vector (); 314 cacheEntry ("l", (JahiaDescriptorsStore) lockStore, uri, locksVector); 315 } else { 316 locksVector = (Vector ) value; 317 } 318 locksVector.addElement (lock.cloneObject ()); 319 cacheEntry ("l", (JahiaDescriptorsStore) lockStore, uri, locksVector); 320 } 321 } 322 323 324 334 public void renewLock (Uri uri, NodeLock lock) 335 throws ServiceAccessException, LockTokenNotFoundException { 336 super.renewLock (uri, lock); 337 if (lockStore.cacheResults ()) { 338 Object value = internalCache.get ( 339 "l" + uri.getNamespace ().getName () + uri.toString ()); 340 Vector locksVector = null; 341 if (value != null) { 342 locksVector = (Vector ) value; 343 boolean wasPresent = locksVector.removeElement (lock); 344 if (!wasPresent) { 345 throw new LockTokenNotFoundException (lock); 346 } 347 locksVector.addElement (lock.cloneObject ()); 348 internalCache.put ( 349 "l" + uri.getNamespace ().getName () + uri.toString (), value); 350 } 351 } 352 } 353 354 355 365 public void removeLock (Uri uri, NodeLock lock) 366 throws ServiceAccessException, LockTokenNotFoundException { 367 super.removeLock (uri, lock); 368 if (lockStore.cacheResults ()) { 369 Object value = internalCache.get ( 370 "l" + uri.getNamespace ().getName () + uri.toString ()); 371 Vector locksVector = null; 372 if (value != null) { 373 locksVector = (Vector ) value; 374 boolean wasPresent = locksVector.removeElement (lock); 375 if (!wasPresent) { 376 throw new LockTokenNotFoundException (lock); 377 } 378 internalCache.put ( 379 "l" + uri.getNamespace ().getName () + uri.toString (), value); 380 } 381 } 382 } 383 384 385 395 public void killLock (Uri uri, NodeLock lock) 396 throws ServiceAccessException, LockTokenNotFoundException { 397 super.killLock (uri, lock); 398 if (lockStore.cacheResults ()) { 399 Object value = internalCache.get ( 400 "l" + uri.getNamespace ().getName () + uri.toString ()); 401 Vector locksVector = null; 402 if (value != null) { 403 locksVector = (Vector ) value; 404 boolean wasPresent = locksVector.removeElement (lock); 405 if (!wasPresent) { 406 throw new LockTokenNotFoundException (lock); 407 } 408 internalCache.put ( 409 "l" + uri.getNamespace ().getName () + uri.toString (), value); 410 } 411 } 412 } 413 414 415 425 public Enumeration enumerateLocks (Uri uri) 426 throws ServiceAccessException { 427 if (lockStore.cacheResults ()) { 428 Object value = internalCache.get ( 429 "l" + uri.getNamespace ().getName () + uri.toString ()); 430 Vector locksVector = null; 431 if (value == null) { 432 locksVector = new Vector (); 433 Enumeration lockList = super.enumerateLocks (uri); 434 while (lockList.hasMoreElements ()) { 435 NodeLock tempLock = (NodeLock) lockList.nextElement (); 436 tempLock.validate (uri.toString ()); 437 locksVector.addElement (tempLock); 438 } 439 if (!isForceStoreEnlistment (uri)) { 440 cacheEntry ("l", (JahiaDescriptorsStore) lockStore, uri, locksVector); 441 } 442 } else { 443 locksVector = (Vector ) value; 444 } 445 return ((Vector ) locksVector.clone ()).elements (); 446 } else { 447 return super.enumerateLocks (uri); 448 } 449 } 450 451 452 463 public NodeRevisionDescriptors retrieveRevisionDescriptors (Uri uri) 464 throws ServiceAccessException, RevisionDescriptorNotFoundException { 465 if (revisionDescriptorsStore.cacheResults ()) { 466 Object tempObject = internalCache.get ( 467 "d" + uri.getNamespace ().getName () + uri.toString ()); 468 if (tempObject != null) { 469 return ((NodeRevisionDescriptors) tempObject).cloneObject (); 470 } else { 471 NodeRevisionDescriptors revisionDescriptors = 472 super.retrieveRevisionDescriptors (uri); 473 revisionDescriptors = revisionDescriptors.cloneObject (); 474 if (!isForceStoreEnlistment (uri)) { 475 cacheEntry ("d", (JahiaDescriptorsStore) revisionDescriptorsStore, uri, 476 revisionDescriptors); 477 } 478 revisionDescriptors.validate (uri.toString ()); 479 return revisionDescriptors; 480 } 481 } else { 482 return super.retrieveRevisionDescriptors (uri); 483 } 484 } 485 486 487 496 public void createRevisionDescriptors 497 (Uri uri, NodeRevisionDescriptors revisionDescriptors) 498 throws ServiceAccessException { 499 super.createRevisionDescriptors (uri, revisionDescriptors); 500 if (revisionDescriptorsStore.cacheResults ()) { 501 cacheEntry ("d", (JahiaDescriptorsStore) revisionDescriptorsStore, uri, 502 revisionDescriptors.cloneObject ()); 503 } 504 } 505 506 507 519 public void storeRevisionDescriptors 520 (Uri uri, NodeRevisionDescriptors revisionDescriptors) 521 throws ServiceAccessException, RevisionDescriptorNotFoundException { 522 super.storeRevisionDescriptors (uri, revisionDescriptors); 523 if (revisionDescriptorsStore.cacheResults ()) { 524 cacheEntry ("d", (JahiaDescriptorsStore) revisionDescriptorsStore, uri, 525 revisionDescriptors.cloneObject ()); 526 } 527 } 528 529 530 538 public void removeRevisionDescriptors (Uri uri) 539 throws ServiceAccessException { 540 if (revisionDescriptorsStore.cacheResults ()) { 541 internalCache.remove ("d" + uri.getNamespace ().getName () + uri.toString ()); 542 } 543 super.removeRevisionDescriptors (uri); 544 } 545 546 547 553 public NodeRevisionDescriptor retrieveRevisionDescriptor 554 (Uri uri, NodeRevisionNumber revisionNumber) 555 throws ServiceAccessException, RevisionDescriptorNotFoundException { 556 if (revisionDescriptorStore.cacheResults ()) { 557 Object result = internalCache.get ( 558 "D" + revisionNumber + "-" + uri.getNamespace ().getName () + uri); 559 if (result != null) { 560 return ((NodeRevisionDescriptor) result).cloneObject (); 561 } else { 562 NodeRevisionDescriptor revisionDescriptor = 563 super 564 .retrieveRevisionDescriptor (uri, revisionNumber) 565 .cloneObject (); 566 revisionDescriptor.validate (); 567 if (!isForceStoreEnlistment (uri)) { 568 cacheEntry ("D" + revisionNumber + "-", 569 (JahiaDescriptorsStore) revisionDescriptorStore, uri, 570 revisionDescriptor); 571 } 572 return revisionDescriptor; 573 } 574 } else { 575 return super.retrieveRevisionDescriptor (uri, revisionNumber); 576 } 577 } 578 579 580 589 public void createRevisionDescriptor 590 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 591 throws ServiceAccessException { 592 super.createRevisionDescriptor (uri, revisionDescriptor); 593 if (revisionDescriptorStore.cacheResults ()) { 594 cacheEntry ("D" + revisionDescriptor.getRevisionNumber () + "-", 595 (JahiaDescriptorsStore) revisionDescriptorStore, uri, 596 revisionDescriptor.cloneObject ()); 597 } 598 } 599 600 601 613 public void storeRevisionDescriptor 614 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 615 throws ServiceAccessException, RevisionDescriptorNotFoundException { 616 super.storeRevisionDescriptor (uri, revisionDescriptor); 617 if (revisionDescriptorStore.cacheResults ()) { 618 cacheEntry ("D" + revisionDescriptor.getRevisionNumber () + "-", 619 (JahiaDescriptorsStore) revisionDescriptorStore, uri, 620 revisionDescriptor.cloneObject ()); 621 } 622 } 623 624 625 634 public void removeRevisionDescriptor (Uri uri, NodeRevisionNumber number) 635 throws ServiceAccessException { 636 super.removeRevisionDescriptor (uri, number); 637 if (revisionDescriptorStore.cacheResults ()) { 638 internalCache.remove ("D" + number + "-" + uri.getNamespace ().getName () + uri); 639 } 640 } 641 642 643 649 public NodeRevisionContent retrieveRevisionContent 650 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 651 throws ServiceAccessException, RevisionNotFoundException { 652 return super.retrieveRevisionContent (uri, revisionDescriptor); 653 } 654 655 656 663 public void createRevisionContent 664 (Uri uri, NodeRevisionDescriptor revisionDescriptor, 665 NodeRevisionContent revisionContent) 666 throws ServiceAccessException, RevisionAlreadyExistException { 667 super.createRevisionContent (uri, revisionDescriptor, revisionContent); 668 } 669 670 671 678 public void storeRevisionContent 679 (Uri uri, NodeRevisionDescriptor revisionDescriptor, 680 NodeRevisionContent revisionContent) 681 throws ServiceAccessException, RevisionNotFoundException { 682 super.storeRevisionContent (uri, revisionDescriptor, revisionContent); 683 } 684 685 686 692 public void removeRevisionContent (Uri uri, 693 NodeRevisionDescriptor revisionDescriptor) 694 throws ServiceAccessException { 695 super.removeRevisionContent (uri, revisionDescriptor); 696 } 697 698 699 702 public String toString () { 703 return getName () + "(" + getClass ().getName () + ")"; 704 } 705 706 707 709 710 713 protected void resetCaches () { 714 internalCache.flush (); 715 } 716 717 private void cacheEntry (String keyPrefix, JahiaDescriptorsStore store, Uri uri, 718 Object object) { 719 try { 720 PathItem pathItem = store.getPathItem(uri); 721 if ((pathItem.type & JahiaDescriptorsStore.TEMP_CACHEABLE) != 0) { 722 CacheEntry cEntry = new CacheEntry (object); 723 cEntry.setExpirationDate (new Date (System.currentTimeMillis () + TEMP_CACHE_DELAY)); 724 internalCache.putCacheEntry ( 725 keyPrefix + uri.getNamespace ().getName () + uri.toString (), cEntry, true); 726 } else if ((pathItem.type & JahiaDescriptorsStore.UNCACHEABLE) == 0) 727 internalCache.put (keyPrefix + uri.getNamespace ().getName () + uri.toString (), 728 object); 729 } catch (ServiceAccessException e) { 730 } 731 } 732 733 private void flushParentFromCache(String keyPrefix, Uri uri) { 734 if ("o".equals(keyPrefix)) { 735 Uri parentUri = uri.getParentUri(); 737 if (parentUri != null) { 738 logger.debug("Flusing parent entry : [" + "o" + 739 parentUri.getNamespace().getName() + 740 parentUri.toString() + "]"); 741 internalCache.remove("o" + parentUri.getNamespace().getName() + 742 parentUri.toString()); 743 } 744 } 745 } 746 747 public void rollback(Xid xid) throws XAException { 748 resetCaches(); 749 super.rollback(xid); 750 } 751 752 protected void enlist(Service service) throws ServiceAccessException { 753 if (service instanceof JahiaDescriptorsStore) { 754 super.enlist(this); 755 } 756 super.enlist(service); 757 } 758 759 protected void delist(Service service, boolean b) throws ServiceAccessException { 760 super.delist(service, b); 761 if (service instanceof JahiaDescriptorsStore) { 762 super.delist(this, b); 763 } 764 } 765 766 } 767 | Popular Tags |