1 23 24 package org.apache.slide.store; 25 26 import java.util.Enumeration ; 27 import java.util.Vector ; 28 29 import org.apache.slide.common.NamespaceAccessToken; 30 import org.apache.slide.common.Service; 31 import org.apache.slide.common.ServiceAccessException; 32 import org.apache.slide.common.ServiceInitializationFailedException; 33 import org.apache.slide.common.Uri; 34 import org.apache.slide.content.NodeRevisionContent; 35 import org.apache.slide.content.NodeRevisionDescriptor; 36 import org.apache.slide.content.NodeRevisionDescriptors; 37 import org.apache.slide.content.NodeRevisionNumber; 38 import org.apache.slide.content.RevisionAlreadyExistException; 39 import org.apache.slide.content.RevisionDescriptorNotFoundException; 40 import org.apache.slide.content.RevisionNotFoundException; 41 import org.apache.slide.lock.LockTokenNotFoundException; 42 import org.apache.slide.lock.NodeLock; 43 import org.apache.slide.security.NodePermission; 44 import org.apache.slide.structure.ObjectAlreadyExistsException; 45 import org.apache.slide.structure.ObjectNode; 46 import org.apache.slide.structure.ObjectNotFoundException; 47 import org.apache.slide.util.HashMapObjectCache; 48 import org.apache.slide.util.ObjectCache; 49 50 56 public class StandardStore extends AbstractStore { 57 58 59 61 62 65 public StandardStore() { 66 super(); 67 68 objectsCache = new HashMapObjectCache(1000, 10000, 0.95); 69 permissionsCache = new HashMapObjectCache(1000, 10000, 0.95); 70 locksCache = new HashMapObjectCache(1000, 10000, 0.95); 71 descriptorsCache = new HashMapObjectCache(1000, 10000, 0.95); 72 descriptorCache = new HashMapObjectCache(1000, 10000, 0.8); 73 } 74 75 77 78 81 protected ObjectCache objectsCache; 82 83 84 87 protected ObjectCache permissionsCache; 88 89 90 93 protected ObjectCache locksCache; 94 95 96 99 protected ObjectCache descriptorsCache; 100 101 102 105 protected ObjectCache descriptorCache; 106 107 108 110 111 117 public void initialize(NamespaceAccessToken token) 118 throws ServiceInitializationFailedException { 119 super.initialize(token); 120 } 121 122 123 125 126 133 public ObjectNode retrieveObject(Uri uri) 134 throws ServiceAccessException, ObjectNotFoundException { 135 if (nodeStore.cacheResults()) { 136 Object tempObject = objectsCache.get(uri.toString()); 137 if (tempObject != null) { 138 return ((ObjectNode) tempObject).cloneObject(); 139 } else { 140 ObjectNode objectNode = nodeStore.retrieveObject(uri); 141 objectNode.validate(uri.toString()); 142 objectsCache.put(uri.toString(), objectNode.cloneObject()); 143 return objectNode; 144 } 145 } else { 146 return super.retrieveObject(uri); 147 } 148 } 149 150 151 158 public void storeObject(Uri uri, ObjectNode object) 159 throws ServiceAccessException, ObjectNotFoundException { 160 super.storeObject(uri, object); 161 if (nodeStore.cacheResults()) { 162 objectsCache.put(uri.toString(), object.cloneObject()); 163 } 164 } 165 166 167 176 public void createObject(Uri uri, ObjectNode object) 177 throws ServiceAccessException, ObjectAlreadyExistsException { 178 super.createObject(uri, object); 179 if (nodeStore.cacheResults()) { 180 objectsCache.put(uri.toString(), object.cloneObject()); 181 } 182 } 183 184 185 192 public void removeObject(Uri uri, ObjectNode object) 193 throws ServiceAccessException, ObjectNotFoundException { 194 super.removeObject(uri, object); 195 if (nodeStore.cacheResults()) { 196 objectsCache.remove(uri.toString()); 197 } 198 } 199 200 201 207 public void grantPermission(Uri uri, NodePermission permission) 208 throws ServiceAccessException { 209 super.grantPermission(uri, permission); 210 if (securityStore.cacheResults()) { 211 NodePermission tempPermission = permission.cloneObject(); 212 Object value = permissionsCache.get(uri.toString()); 213 if (value == null) { 214 enumeratePermissions(uri); 216 value = permissionsCache.get(uri.toString()); 218 if (value == null) { 219 Vector permissionsVector = new Vector (); 222 permissionsVector.addElement(tempPermission); 223 permissionsCache.put(uri.toString(), permissionsVector); 224 } 225 } else { 226 Vector permissionsVector = (Vector ) value; 227 permissionsVector.addElement(tempPermission); 228 } 229 } 230 } 231 232 233 239 public void revokePermission(Uri uri, NodePermission permission) 240 throws ServiceAccessException { 241 super.revokePermission(uri, permission); 242 if (securityStore.cacheResults()) { 243 Object value = permissionsCache.get(uri.toString()); 244 Vector permissionsVector = null; 245 if (value != null) { 246 permissionsVector = (Vector ) value; 247 permissionsVector.removeElement(permission); 248 } 249 } 250 } 251 252 253 259 public void revokePermissions(Uri uri) 260 throws ServiceAccessException { 261 super.revokePermissions(uri); 262 if (securityStore.cacheResults()) { 263 Object value = permissionsCache.get(uri.toString()); 264 Vector permissionsVector = null; 265 if (value != null) { 266 permissionsVector = (Vector ) value; 267 permissionsVector.removeAllElements(); 268 } 269 } 270 } 271 272 273 281 public Enumeration enumeratePermissions(Uri uri) 282 throws ServiceAccessException { 283 if (securityStore.cacheResults()) { 285 Object value = permissionsCache.get(uri.toString()); 286 Vector permissionsVector = null; 287 if (value != null) { 288 permissionsVector = (Vector ) value; 289 } else { 290 permissionsVector = new Vector (); 291 Enumeration tempEnum = securityStore.enumeratePermissions(uri); 292 while (tempEnum.hasMoreElements()) { 293 NodePermission tempPermission = 294 (NodePermission) tempEnum.nextElement(); 295 tempPermission.validate(uri.toString()); 296 permissionsVector.addElement(tempPermission); 297 } 298 permissionsCache.put(uri.toString(), permissionsVector); 299 } 300 return ((Vector ) permissionsVector.clone()).elements(); 301 } else { 302 return super.enumeratePermissions(uri); 303 } 304 } 305 306 307 313 public void putLock(Uri uri, NodeLock lock) 314 throws ServiceAccessException { 315 super.putLock(uri, lock); 316 if (lockStore.cacheResults()) { 317 Object value = locksCache.get(uri.toString()); 318 Vector locksVector = null; 319 if (value == null) { 320 locksVector = new Vector (); 321 locksCache.put(uri.toString(), locksVector); 322 } else { 323 locksVector = (Vector ) value; 324 } 325 locksVector.addElement(lock.cloneObject()); 326 } 327 } 328 329 330 337 public void renewLock(Uri uri, NodeLock lock) 338 throws ServiceAccessException, LockTokenNotFoundException { 339 super.renewLock(uri, lock); 340 if (lockStore.cacheResults()) { 341 Object value = locksCache.get(uri.toString()); 342 Vector locksVector = null; 343 if (value != null) { 344 locksVector = (Vector ) value; 345 boolean wasPresent = locksVector.removeElement(lock); 346 if (!wasPresent) { 347 throw new LockTokenNotFoundException(lock); 348 } 349 locksVector.addElement(lock.cloneObject()); 350 } 351 } 352 } 353 354 355 362 public void removeLock(Uri uri, NodeLock lock) 363 throws ServiceAccessException, LockTokenNotFoundException { 364 super.removeLock(uri, lock); 365 if (lockStore.cacheResults()) { 366 Object value = locksCache.get(uri.toString()); 367 Vector locksVector = null; 368 if (value != null) { 369 locksVector = (Vector ) value; 370 boolean wasPresent = locksVector.removeElement(lock); 371 if (!wasPresent) { 372 throw new LockTokenNotFoundException(lock); 373 } 374 } 375 } 376 } 377 378 379 386 public void killLock(Uri uri, NodeLock lock) 387 throws ServiceAccessException, LockTokenNotFoundException { 388 super.killLock(uri, lock); 389 if (lockStore.cacheResults()) { 390 Object value = locksCache.get(uri.toString()); 391 Vector locksVector = null; 392 if (value != null) { 393 locksVector = (Vector ) value; 394 boolean wasPresent = locksVector.removeElement(lock); 395 if (!wasPresent) { 396 throw new LockTokenNotFoundException(lock); 397 } 398 } 399 } 400 } 401 402 403 411 public Enumeration enumerateLocks(Uri uri) 412 throws ServiceAccessException { 413 if (lockStore.cacheResults()) { 414 Object value = locksCache.get(uri.toString()); 415 Vector locksVector = null; 416 if (value == null) { 417 locksVector = new Vector (); 418 Enumeration lockList = lockStore.enumerateLocks(uri); 419 while (lockList.hasMoreElements()) { 420 NodeLock tempLock = (NodeLock) lockList.nextElement(); 421 tempLock.validate(uri.toString()); 422 locksVector.addElement(tempLock); 423 } 424 locksCache.put(uri.toString(), locksVector); 425 } else { 426 locksVector = (Vector ) value; 427 } 428 return ((Vector ) locksVector.clone()).elements(); 429 } else { 430 return super.enumerateLocks(uri); 431 } 432 } 433 434 435 443 public NodeRevisionDescriptors retrieveRevisionDescriptors(Uri uri) 444 throws ServiceAccessException, RevisionDescriptorNotFoundException { 445 if (revisionDescriptorsStore.cacheResults()) { 446 Object tempObject = descriptorsCache.get(uri.toString()); 447 if (tempObject != null) { 448 return ((NodeRevisionDescriptors) tempObject).cloneObject(); 449 } else { 450 NodeRevisionDescriptors revisionDescriptors = 451 revisionDescriptorsStore.retrieveRevisionDescriptors(uri); 452 revisionDescriptors = revisionDescriptors.cloneObject(); 453 descriptorsCache.put(uri.toString(), 454 revisionDescriptors); 455 revisionDescriptors.validate(uri.toString()); 456 return revisionDescriptors; 457 } 458 } else { 459 return super.retrieveRevisionDescriptors(uri); 460 } 461 } 462 463 464 471 public void createRevisionDescriptors 472 (Uri uri, NodeRevisionDescriptors revisionDescriptors) 473 throws ServiceAccessException { 474 super.createRevisionDescriptors(uri, revisionDescriptors); 475 if (revisionDescriptorsStore.cacheResults()) { 476 descriptorsCache.put(uri.toString(), 477 revisionDescriptors.cloneObject()); 478 } 479 } 480 481 482 491 public void storeRevisionDescriptors 492 (Uri uri, NodeRevisionDescriptors revisionDescriptors) 493 throws ServiceAccessException, RevisionDescriptorNotFoundException { 494 super.storeRevisionDescriptors(uri, revisionDescriptors); 495 if (revisionDescriptorsStore.cacheResults()) { 496 descriptorsCache.put(uri.toString(), 497 revisionDescriptors.cloneObject()); 498 } 499 } 500 501 502 508 public void removeRevisionDescriptors(Uri uri) 509 throws ServiceAccessException { 510 if (revisionDescriptorsStore.cacheResults()) { 511 descriptorsCache.remove(uri.toString()); 512 } 513 super.removeRevisionDescriptors(uri); 514 } 515 516 517 523 public NodeRevisionDescriptor retrieveRevisionDescriptor 524 (Uri uri, NodeRevisionNumber revisionNumber) 525 throws ServiceAccessException, RevisionDescriptorNotFoundException { 526 if (revisionDescriptorStore.cacheResults()) { 527 Object result = descriptorCache.get(uri + "-" + revisionNumber); 528 if (result != null) { 529 return ((NodeRevisionDescriptor) result).cloneObject(); 530 } else { 531 NodeRevisionDescriptor revisionDescriptor = 532 revisionDescriptorStore 533 .retrieveRevisionDescriptor(uri, revisionNumber) 534 .cloneObject(); 535 revisionDescriptor.validate(); 536 descriptorCache.put(uri + "-" + revisionNumber, 537 revisionDescriptor); 538 return revisionDescriptor; 539 } 540 } else { 541 return super.retrieveRevisionDescriptor(uri, revisionNumber); 542 } 543 } 544 545 546 553 public void createRevisionDescriptor 554 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 555 throws ServiceAccessException { 556 super.createRevisionDescriptor(uri, revisionDescriptor); 557 if (revisionDescriptorStore.cacheResults()) { 558 descriptorCache.put(uri + "-" 559 + revisionDescriptor.getRevisionNumber(), 560 revisionDescriptor.cloneObject()); 561 } 562 } 563 564 565 574 public void storeRevisionDescriptor 575 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 576 throws ServiceAccessException, RevisionDescriptorNotFoundException { 577 super.storeRevisionDescriptor(uri, revisionDescriptor); 578 if (revisionDescriptorStore.cacheResults()) { 579 String key = uri + "-" + revisionDescriptor.getRevisionNumber(); 580 descriptorCache.put(key, revisionDescriptor.cloneObject()); 581 } 582 } 583 584 585 592 public void removeRevisionDescriptor(Uri uri, NodeRevisionNumber number) 593 throws ServiceAccessException { 594 super.removeRevisionDescriptor(uri, number); 595 if (revisionDescriptorStore.cacheResults()) { 596 descriptorCache.remove(uri + "-" + number); 597 } 598 } 599 600 601 607 public NodeRevisionContent retrieveRevisionContent 608 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 609 throws ServiceAccessException, RevisionNotFoundException { 610 return super.retrieveRevisionContent(uri, revisionDescriptor); 611 } 612 613 614 621 public void createRevisionContent 622 (Uri uri, NodeRevisionDescriptor revisionDescriptor, 623 NodeRevisionContent revisionContent) 624 throws ServiceAccessException, RevisionAlreadyExistException { 625 super.createRevisionContent(uri, revisionDescriptor, revisionContent); 626 } 627 628 629 636 public void storeRevisionContent 637 (Uri uri, NodeRevisionDescriptor revisionDescriptor, 638 NodeRevisionContent revisionContent) 639 throws ServiceAccessException, RevisionNotFoundException { 640 super.storeRevisionContent(uri, revisionDescriptor, revisionContent); 641 } 642 643 644 650 public void removeRevisionContent(Uri uri, 651 NodeRevisionDescriptor revisionDescriptor) 652 throws ServiceAccessException { 653 super.removeRevisionContent(uri, revisionDescriptor); 654 } 655 656 657 661 public String toString() { 662 return getName() + "(" + getClass().getName() + ")"; 663 } 664 665 666 668 669 672 protected void delist(Service service, boolean success) 673 throws ServiceAccessException { 674 if (!success) { 675 resetCaches(); 678 } 679 super.delist(service, success); 680 } 681 682 683 686 protected void resetCaches() { 687 objectsCache.clear(); 688 permissionsCache.clear(); 689 locksCache.clear(); 690 descriptorsCache.clear(); 691 descriptorCache.clear(); 692 } 693 694 695 } 696 | Popular Tags |