| 1 23 24 package org.apache.slide.store; 25 26 import java.util.Enumeration ; 27 import java.util.Hashtable ; 28 29 import javax.transaction.Transaction ; 30 import javax.transaction.xa.XAException ; 31 import javax.transaction.xa.Xid ; 32 33 import org.apache.slide.authenticate.CredentialsToken; 34 import org.apache.slide.common.AbstractSimpleService; 35 import org.apache.slide.common.Namespace; 36 import org.apache.slide.common.NamespaceAccessToken; 37 import org.apache.slide.common.Scope; 38 import org.apache.slide.common.Service; 39 import org.apache.slide.common.ServiceAccessException; 40 import org.apache.slide.common.ServiceConnectionFailedException; 41 import org.apache.slide.common.ServiceDisconnectionFailedException; 42 import org.apache.slide.common.ServiceInitializationFailedException; 43 import org.apache.slide.common.ServiceParameterErrorException; 44 import org.apache.slide.common.ServiceParameterMissingException; 45 import org.apache.slide.common.ServiceResetFailedException; 46 import org.apache.slide.common.SlideToken; 47 import org.apache.slide.common.Uri; 48 import org.apache.slide.content.NodeRevisionContent; 49 import org.apache.slide.content.NodeRevisionDescriptor; 50 import org.apache.slide.content.NodeRevisionDescriptors; 51 import org.apache.slide.content.NodeRevisionNumber; 52 import org.apache.slide.content.RevisionAlreadyExistException; 53 import org.apache.slide.content.RevisionDescriptorNotFoundException; 54 import org.apache.slide.content.RevisionNotFoundException; 55 import org.apache.slide.lock.LockTokenNotFoundException; 56 import org.apache.slide.lock.NodeLock; 57 import org.apache.slide.security.NodePermission; 58 import org.apache.slide.structure.ObjectAlreadyExistsException; 59 import org.apache.slide.structure.ObjectNode; 60 import org.apache.slide.structure.ObjectNotFoundException; 61 import org.apache.slide.util.Messages; 62 import org.apache.slide.util.logger.Logger; 63 64 69 public abstract class AbstractStore extends AbstractSimpleService 70 implements Store { 71 72 73 75 76 78 79 82 protected NodeStore nodeStore; 83 84 85 88 protected SecurityStore securityStore; 89 90 91 94 protected LockStore lockStore; 95 96 97 100 protected RevisionDescriptorsStore revisionDescriptorsStore; 101 102 103 106 protected RevisionDescriptorStore revisionDescriptorStore; 107 108 109 112 protected ContentStore contentStore; 113 114 117 protected IndexStore propertiesIndexer; 118 119 122 protected IndexStore contentIndexer; 123 124 127 protected SequenceStore sequenceStore = null; 128 129 132 protected Service resourceManagers[] = new Service[0]; 133 134 135 136 private String name; 138 139 140 143 public void setName(String name) { 144 this.name = name; 145 } 146 147 148 149 152 public String getName() { 153 return this.name; 154 } 155 156 157 158 161 public void setScope(Scope scope) { 162 super.setScope(scope); 163 for (int i = 0; i < resourceManagers.length; i++) { 164 resourceManagers[i].setScope(scope); 165 } 166 } 167 168 169 170 172 173 176 public void setNamespace(Namespace namespace) { 177 178 super.setNamespace(namespace); 179 180 for (int i = 0; i < resourceManagers.length; i++) { 181 resourceManagers[i].setNamespace(namespace); 182 } 183 184 } 185 186 187 protected Hashtable parameters = null; 188 189 197 public void setParameters(Hashtable parameters) 198 throws ServiceParameterErrorException, 199 ServiceParameterMissingException { 200 this.parameters = parameters; 201 } 202 203 public Object getParameter (Object key) { 204 return parameters.get (key); 205 } 206 207 212 public void connect(CredentialsToken crdtoken) 213 throws ServiceConnectionFailedException { 214 215 for (int i = 0; i < resourceManagers.length; i++) { 216 resourceManagers[i].connect(crdtoken); 217 } 218 219 } 220 221 222 223 228 public void connect() 229 throws ServiceConnectionFailedException { 230 231 for (int i = 0; i < resourceManagers.length; i++) { 232 resourceManagers[i].connect(); 233 } 234 235 } 236 237 238 243 public void disconnect() 244 throws ServiceDisconnectionFailedException { 245 246 for (int i = 0; i < resourceManagers.length; i++) { 247 resourceManagers[i].disconnect(); 248 } 249 250 } 251 252 253 259 public void initialize(NamespaceAccessToken token) 260 throws ServiceInitializationFailedException { 261 262 super.initialize(token); 263 264 for (int i = 0; i < resourceManagers.length; i++) { 265 resourceManagers[i].initialize(token); 266 } 267 268 } 269 270 271 276 public void reset() 277 throws ServiceResetFailedException { 278 279 for (int i = 0; i < resourceManagers.length; i++) { 280 resourceManagers[i].reset(); 281 } 282 283 } 284 285 286 292 public boolean isConnected() 293 throws ServiceAccessException { 294 295 for (int i = 0; i < resourceManagers.length; i++) { 296 if (!resourceManagers[i].isConnected()) 297 return false; 298 } 299 return true; 300 301 } 302 303 304 306 307 321 public void commit(Xid xid, boolean onePhase) 322 throws XAException { 323 super.commit(xid, onePhase); 324 } 325 326 327 337 public void end(Xid xid, int flags) 338 throws XAException { 339 super.end(xid, flags); 340 } 341 342 343 351 public void forget(Xid xid) 352 throws XAException { 353 super.forget(xid); 354 } 355 356 357 370 public int prepare(Xid xid) 371 throws XAException { 372 return super.prepare(xid); 373 } 374 375 376 383 public void rollback(Xid xid) 384 throws XAException { 385 super.rollback(xid); 386 } 387 388 389 399 public void start(Xid xid, int flags) 400 throws XAException { 401 super.start(xid, flags); 402 } 403 404 405 407 408 411 public void setNodeStore(NodeStore nodeStore) { 412 if (nodeStore == null) { 413 throw new IllegalArgumentException ("Nodestore must not be null"); 414 } 415 this.nodeStore = nodeStore; 416 addResourceManager(this.nodeStore); 417 } 418 419 420 423 public void setSecurityStore(SecurityStore securityStore) { 424 if (securityStore == null) { 425 throw new IllegalArgumentException ("Securitystore must not be null"); 426 } 427 this.securityStore = securityStore; 428 addResourceManager(this.securityStore); 429 } 430 431 432 435 public void setLockStore(LockStore lockStore) { 436 if (lockStore == null) { 437 throw new IllegalArgumentException ("Lockstore must not be null"); 438 } 439 this.lockStore = lockStore; 440 addResourceManager(this.lockStore); 441 } 442 443 444 447 public void setRevisionDescriptorsStore 448 (RevisionDescriptorsStore revisionDescriptorsStore) { 449 if (revisionDescriptorsStore == null) { 450 throw new IllegalArgumentException ("Revisiondescriptorsstore must not be null"); 451 } 452 this.revisionDescriptorsStore = revisionDescriptorsStore; 453 addResourceManager(this.revisionDescriptorsStore); 454 } 455 456 457 460 public void setRevisionDescriptorStore 461 (RevisionDescriptorStore revisionDescriptorStore) { 462 if (revisionDescriptorStore == null) { 463 throw new IllegalArgumentException ("Revisiondescriptorstore must not be null"); 464 } 465 this.revisionDescriptorStore = revisionDescriptorStore; 466 addResourceManager(this.revisionDescriptorStore); 467 } 468 469 470 473 public void setContentStore(ContentStore contentStore) { 474 if (contentStore == null) { 475 476 } 477 this.contentStore = contentStore; 478 addResourceManager(this.contentStore); 479 } 480 481 482 488 public void setPropertiesIndexer (IndexStore propertiesIndexer) { 489 if (propertiesIndexer == null) { 490 throw new IllegalArgumentException ("PropertiesIndexer must not be null"); 491 } 492 this.propertiesIndexer = propertiesIndexer; 493 addResourceManager(this.propertiesIndexer); 494 495 } 496 497 498 504 public void setContentIndexer (IndexStore contentIndexer) { 505 if (contentIndexer == null) { 506 throw new IllegalArgumentException ("ContentIndexer must not be null"); 507 } 508 this.contentIndexer = contentIndexer; 509 addResourceManager(this.contentIndexer); 510 511 } 512 513 519 public IndexStore getContentIndexer () { 520 return contentIndexer; 521 } 522 523 529 public IndexStore getPropertiesIndexer () { 530 return propertiesIndexer; 531 } 532 533 536 public void setSequenceStore(SequenceStore store) { 537 sequenceStore = store; 538 } 539 540 544 547 public boolean isSequenceSupported() { 548 return (sequenceStore != null && sequenceStore.isSequenceSupported()); 549 } 550 551 554 public boolean sequenceExists(String sequenceName) throws ServiceAccessException { 555 if (!isSequenceSupported()) { 556 throw new ServiceAccessException(this, "Sequences not supported"); 557 } 558 return sequenceStore.sequenceExists(sequenceName); 559 } 560 561 564 public boolean createSequence(String sequenceName) throws ServiceAccessException { 565 if (!isSequenceSupported()) { 566 throw new ServiceAccessException(this, "Sequences not supported"); 567 } 568 return sequenceStore.createSequence(sequenceName); 569 } 570 571 574 public long nextSequenceValue(String sequenceName) throws ServiceAccessException { 575 if (!isSequenceSupported()) { 576 throw new ServiceAccessException(this, "Sequences not supported"); 577 } 578 return sequenceStore.nextSequenceValue(sequenceName); 579 } 580 581 588 public ObjectNode retrieveObject(Uri uri) 589 throws ServiceAccessException, ObjectNotFoundException { 590 ObjectNode objectNode = null; 591 if (isForceStoreEnlistment(uri)) { 592 enlist(nodeStore); 593 try { 594 objectNode = nodeStore.retrieveObject(uri); 595 } catch (ServiceAccessException e) { 596 delist(nodeStore, false); 597 throw e; 598 } catch (ObjectNotFoundException e) { 599 delist(nodeStore); 602 throw e; 603 } catch (Throwable t) { 604 delist(nodeStore, false); 605 throw new ServiceAccessException(nodeStore, t); 607 } 608 delist(nodeStore); 609 } else { 610 try { 611 objectNode = nodeStore.retrieveObject(uri); 612 } catch (ServiceAccessException e) { 613 throw e; 614 } catch (ObjectNotFoundException e) { 615 throw e; 616 } catch (Throwable t) { 617 throw new ServiceAccessException(nodeStore, t); 619 } 620 } 621 objectNode.validate(uri.toString()); 622 return objectNode; 623 } 624 625 626 633 public void storeObject(Uri uri, ObjectNode object) 634 throws ServiceAccessException, ObjectNotFoundException { 635 ObjectNode tempObject = object.cloneObject(); 636 tempObject.validate(uri.toString()); 637 enlist(nodeStore); 638 try { 639 nodeStore.storeObject(uri, tempObject); 640 } catch (ServiceAccessException e) { 641 delist(nodeStore, false); 642 throw e; 643 } catch (ObjectNotFoundException e) { 644 delist(nodeStore); 645 throw e; 646 } catch (Throwable t) { 647 delist(nodeStore, false); 648 throw new ServiceAccessException(contentStore, t); 650 } 651 delist(nodeStore); 652 } 653 654 655 664 public void createObject(Uri uri, ObjectNode object) 665 throws ServiceAccessException, ObjectAlreadyExistsException { 666 ObjectNode tempObject = object.cloneObject(); 667 tempObject.validate(uri.toString()); 668 enlist(nodeStore); 669 try { 670 nodeStore.createObject(uri, tempObject); 671 if (useBinding()) { 672 String uuri = tempObject.getUuri(); 673 if (uuri == null) { 674 throw new IllegalStateException (); 675 } 676 object.setUuri(uuri); 677 } else { 678 object.setUuri(tempObject.getUri()); 679 } 680 } catch (ServiceAccessException e) { 681 delist(nodeStore, false); 682 throw e; 683 } catch (ObjectAlreadyExistsException e) { 684 delist(nodeStore); 685 throw e; 686 } catch (Throwable t) { 687 delist(nodeStore, false); 688 throw new ServiceAccessException(nodeStore, t); 690 } 691 delist(nodeStore); 692 } 693 694 695 702 public void removeObject(Uri uri, ObjectNode object) 703 throws ServiceAccessException, ObjectNotFoundException { 704 object.validate(uri.toString()); 705 enlist(nodeStore); 706 try { 707 nodeStore.removeObject(uri, object); 708 } catch (ServiceAccessException e) { 709 delist(nodeStore, false); 710 throw e; 711 } catch (ObjectNotFoundException e) { 712 delist(nodeStore); 713 throw e; 714 } catch (Throwable t) { 715 delist(nodeStore, false); 716 throw new ServiceAccessException(nodeStore, t); 718 } 719 delist(nodeStore); 720 } 721 722 723 729 public void grantPermission(Uri uri, NodePermission permission) 730 throws ServiceAccessException { 731 NodePermission tempPermission = permission.cloneObject(); 732 tempPermission.validate(uri.toString()); 733 enlist(securityStore); 734 try { 735 securityStore.grantPermission(uri, tempPermission); 736 } catch (ServiceAccessException e) { 737 delist(securityStore, false); 738 throw e; 739 } catch (Throwable t) { 740 delist(securityStore, false); 741 throw new ServiceAccessException(securityStore, t); 743 } 744 delist(securityStore); 745 } 746 747 748 754 public void revokePermission(Uri uri, NodePermission permission) 755 throws ServiceAccessException { 756 permission.validate(uri.toString()); 757 enlist(securityStore); 758 try { 759 securityStore.revokePermission(uri, permission); 760 } catch (ServiceAccessException e) { 761 delist(securityStore, false); 762 throw e; 763 } catch (Throwable t) { 764 delist(securityStore, false); 765 throw new ServiceAccessException(securityStore, t); 767 } 768 delist(securityStore); 769 } 770 771 772 778 public void revokePermissions(Uri uri) 779 throws ServiceAccessException { 780 enlist(securityStore); 781 try { 782 securityStore.revokePermissions(uri); 783 } catch (ServiceAccessException e) { 784 delist(securityStore, false); 785 throw e; 786 } catch (Throwable t) { 787 delist(securityStore, false); 788 throw new ServiceAccessException(securityStore, t); 790 } 791 delist(securityStore); 792 } 793 794 795 803 public Enumeration enumeratePermissions(Uri uri) 804 throws ServiceAccessException { 805 if (isForceStoreEnlistment(uri)) { 807 enlist(securityStore); 808 Enumeration permissions = null; 809 try { 810 permissions = securityStore.enumeratePermissions(uri); 811 } catch (ServiceAccessException e) { 812 delist(securityStore, false); 813 throw e; 814 } catch (Throwable t) { 815 delist(securityStore, false); 816 throw new ServiceAccessException 818 (securityStore, t); 819 } 820 delist(securityStore); 821 return permissions; 822 } else { 823 try { 824 return securityStore.enumeratePermissions(uri); 825 } catch (ServiceAccessException e) { 826 throw e; 827 } catch (Throwable t) { 828 throw new ServiceAccessException 830 (securityStore, t); 831 } 832 } 833 } 834 835 836 842 public void putLock(Uri uri, NodeLock lock) 843 throws ServiceAccessException { 844 lock.validate(uri.toString()); 845 enlist(lockStore); 846 try { 847 lockStore.putLock(uri, lock); 848 } catch (ServiceAccessException e) { 849 delist(lockStore, false); 850 throw e; 851 } catch (Throwable t) { 852 delist(lockStore, false); 853 throw new ServiceAccessException(lockStore, t); 855 } 856 delist(lockStore); 857 } 858 859 860 867 public void renewLock(Uri uri, NodeLock lock) 868 throws ServiceAccessException, LockTokenNotFoundException { 869 lock.validate(uri.toString()); 870 enlist(lockStore); 871 try { 872 lockStore.renewLock(uri, lock); 873 } catch (ServiceAccessException e) { 874 delist(lockStore, false); 875 throw e; 876 } catch (LockTokenNotFoundException e) { 877 delist(lockStore); 878 throw e; 879 } catch (Throwable t) { 880 delist(lockStore, false); 881 throw new ServiceAccessException(lockStore, t); 883 } 884 delist(lockStore); 885 } 886 887 888 |