1 23 package org.apache.slide.store; 24 25 26 import java.util.Enumeration; 27 import java.util.Vector; 28 import org.apache.slide.common.ServiceAccessException; 29 import org.apache.slide.common.Uri; 30 import org.apache.slide.common.UriPath; 31 import org.apache.slide.content.NodeProperty; 32 import org.apache.slide.content.NodeRevisionContent; 33 import org.apache.slide.content.NodeRevisionDescriptor; 34 import org.apache.slide.content.NodeRevisionDescriptors; 35 import org.apache.slide.content.NodeRevisionNumber; 36 import org.apache.slide.content.RevisionAlreadyExistException; 37 import org.apache.slide.content.RevisionDescriptorNotFoundException; 38 import org.apache.slide.content.RevisionNotFoundException; 39 import org.apache.slide.lock.LockTokenNotFoundException; 40 import org.apache.slide.lock.NodeLock; 41 import org.apache.slide.security.NodePermission; 42 import org.apache.slide.store.ExtendedStore; 43 import org.apache.slide.store.NodeStore; 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.Configuration; 48 import org.apache.slide.util.XMLValue; 49 import org.jdom.Element; 50 51 58 public class BindingStore extends ExtendedStore { 59 60 public ObjectNode retrieveObject(Uri uri) 62 throws ServiceAccessException, ObjectNotFoundException { 63 64 if (uri instanceof ResourceId) { 65 return super.retrieveObject(uri); 66 } 67 else { 68 return doRetrieveObjectNode(uri); 69 } 70 } 71 72 public void storeObject(Uri uri, ObjectNode object) 74 throws ServiceAccessException, ObjectNotFoundException { 75 76 if (uri instanceof ResourceId) { 77 super.storeObject(uri, object); 78 } 79 else { 80 ResourceId resourceId = obtainResourceId(uri); 81 ObjectNode objectClone = object.cloneObject(); 82 objectClone.setUri(resourceId.getUuri()); resourceId.getStore().storeObject(resourceId, objectClone); 84 } 85 } 86 87 public void createObject(Uri uri, ObjectNode object) 89 throws ServiceAccessException, ObjectAlreadyExistsException { 90 91 if (uri instanceof ResourceId) { 92 super.createObject(uri, object); 93 } 94 else { 95 ResourceId resourceId = ResourceId.createNew(uri); 96 object.setUuri(resourceId.getUuri()); 97 ObjectNode objectClone = object.cloneObject(); 98 objectClone.setUri(resourceId.getUuri()); cacheResolve(uri, resourceId); 100 resourceId.getStore().createObject(resourceId, objectClone); 101 } 102 } 103 104 public void removeObject(Uri uri, ObjectNode object) 106 throws ServiceAccessException, ObjectNotFoundException { 107 108 if (uri instanceof ResourceId) { 109 super.removeObject(uri, object); 110 } 111 else { 112 ResourceId resourceId = obtainResourceId(uri); 113 ObjectNode objectClone = object.cloneObject(); 114 objectClone.setUri(resourceId.getUuri()); cacheResolve(uri, null); 116 resourceId.getStore().removeObject(resourceId, objectClone); 117 } 118 } 119 120 public void grantPermission(Uri uri, NodePermission permission) 122 throws ServiceAccessException { 123 124 if (uri instanceof ResourceId) { 125 super.grantPermission(uri, permission); 126 } 127 else { 128 try { 129 ResourceId resourceId = obtainResourceId(uri); 130 NodePermission permissionClone = permission.cloneObject(); 131 permissionClone.setObject(resourceId.getUuri()); resourceId.getStore().grantPermission(resourceId, permissionClone); 133 } 134 catch (ObjectNotFoundException e) { 135 throw new ServiceAccessException(this, e); 136 } 137 } 138 } 139 140 public void revokePermission(Uri uri, NodePermission permission) 142 throws ServiceAccessException { 143 144 if (uri instanceof ResourceId) { 145 super.revokePermission(uri, permission); 146 } 147 else { 148 try { 149 ResourceId resourceId = obtainResourceId(uri); 150 NodePermission permissionClone = permission.cloneObject(); 151 permissionClone.setObject(resourceId.getUuri()); resourceId.getStore().revokePermission(resourceId, permissionClone); 153 } 154 catch (ObjectNotFoundException e) { 155 throw new ServiceAccessException(this, e); 156 } 157 } 158 } 159 160 public void revokePermissions(Uri uri) 162 throws ServiceAccessException { 163 164 if (uri instanceof ResourceId) { 165 super.revokePermissions(uri); 166 } 167 else { 168 try { 169 ResourceId resourceId = obtainResourceId(uri); 170 resourceId.getStore().revokePermissions(resourceId); 171 } 172 catch (ObjectNotFoundException e) { 173 throw new ServiceAccessException(this, e); 174 } 175 } 176 } 177 178 public Enumeration enumeratePermissions(Uri uri) 180 throws ServiceAccessException { 181 182 if (uri instanceof ResourceId) { 183 return super.enumeratePermissions(uri); 184 } 185 else { 186 try { 187 ResourceId resourceId = obtainResourceId(uri); 188 Enumeration permissions = resourceId.getStore().enumeratePermissions(resourceId); 189 Vector result = new Vector(); 190 while (permissions.hasMoreElements()) { 191 NodePermission p = ((NodePermission)permissions.nextElement()).cloneObject(); 192 p.setObject(uri.toString()); result.add(p); 194 } 195 return result.elements(); 196 } 197 catch (ObjectNotFoundException e) { 198 throw new ServiceAccessException(this, e); 199 } 200 } 201 } 202 203 public void putLock(Uri uri, NodeLock lock) 205 throws ServiceAccessException { 206 207 if (uri instanceof ResourceId) { 208 super.putLock(uri, lock); 209 } 210 else { 211 try { 212 ResourceId resourceId = obtainResourceId(uri); 213 NodeLock lockClone = lock.cloneObject(); 214 lockClone.setObjectUri(resourceId.getUuri()); resourceId.getStore().putLock(resourceId, lockClone); 216 } 217 catch (ObjectNotFoundException e) { 218 throw new ServiceAccessException(this, e); 219 } 220 } 221 } 222 223 public void renewLock(Uri uri, NodeLock lock) 225 throws ServiceAccessException, LockTokenNotFoundException { 226 227 if (uri instanceof ResourceId) { 228 super.renewLock(uri, lock); 229 } 230 else { 231 try { 232 ResourceId resourceId = obtainResourceId(uri); 233 NodeLock lockClone = lock.cloneObject(); 234 lockClone.setObjectUri(resourceId.getUuri()); resourceId.getStore().renewLock(resourceId, lockClone); 236 } 237 catch (ObjectNotFoundException e) { 238 throw new ServiceAccessException(this, e); 239 } 240 } 241 } 242 243 public void removeLock(Uri uri, NodeLock lock) 245 throws ServiceAccessException, LockTokenNotFoundException { 246 247 if (uri instanceof ResourceId) { 248 super.removeLock(uri, lock); 249 } 250 else { 251 try { 252 ResourceId resourceId = obtainResourceId(uri); 253 NodeLock lockClone = lock.cloneObject(); 254 lockClone.setObjectUri(resourceId.getUuri()); resourceId.getStore().removeLock(resourceId, lockClone); 256 } 257 catch (ObjectNotFoundException e) { 258 throw new ServiceAccessException(this, e); 259 } 260 } 261 } 262 263 public void killLock(Uri uri, NodeLock lock) 265 throws ServiceAccessException, LockTokenNotFoundException { 266 267 if (uri instanceof ResourceId) { 268 super.killLock(uri, lock); 269 } 270 else { 271 try { 272 ResourceId resourceId = obtainResourceId(uri); 273 NodeLock lockClone = lock.cloneObject(); 274 lockClone.setObjectUri(resourceId.getUuri()); resourceId.getStore().killLock(resourceId, lockClone); 276 } 277 catch (ObjectNotFoundException e) { 278 throw new ServiceAccessException(this, e); 279 } 280 } 281 } 282 283 public Enumeration enumerateLocks(Uri uri) 285 throws ServiceAccessException { 286 287 if (uri instanceof ResourceId) { 288 return super.enumerateLocks(uri); 289 } 290 else { 291 try { 292 ResourceId resourceId = obtainResourceId(uri); 293 Enumeration locks = resourceId.getStore().enumerateLocks(resourceId); 294 Vector result = new Vector(); 295 while (locks.hasMoreElements()) { 296 NodeLock l = ((NodeLock)locks.nextElement()).cloneObject(); 297 l.setObjectUri(uri.toString()); result.add(l); 299 } 300 return result.elements(); 301 } 302 catch (ObjectNotFoundException e) { 303 throw new ServiceAccessException(this, e); 304 } 305 } 306 } 307 308 public NodeRevisionDescriptors retrieveRevisionDescriptors(Uri uri) 310 throws ServiceAccessException, RevisionDescriptorNotFoundException { 311 312 if (uri instanceof ResourceId) { 313 return super.retrieveRevisionDescriptors(uri); 314 } 315 else { 316 try { 317 ResourceId resourceId = obtainResourceId(uri); 318 NodeRevisionDescriptors nrdsClone = 319 resourceId.getStore().retrieveRevisionDescriptors(resourceId).cloneObject(); 320 nrdsClone.setUri(uri.toString()); 321 return nrdsClone; 322 } 323 catch (ObjectNotFoundException e) { 324 throw new ServiceAccessException(this, e); 326 } 327 } 328 } 329 330 public void createRevisionDescriptors 332 (Uri uri, NodeRevisionDescriptors revisionDescriptors) 333 throws ServiceAccessException { 334 335 if (uri instanceof ResourceId) { 336 super.createRevisionDescriptors(uri, revisionDescriptors); 337 } 338 else { 339 try { 340 ResourceId resourceId = obtainResourceId(uri); 341 NodeRevisionDescriptors nrdsClone = revisionDescriptors.cloneObject(); 342 nrdsClone.setUri(resourceId.getUuri()); resourceId.getStore().createRevisionDescriptors(resourceId, nrdsClone); 344 } 345 catch (ObjectNotFoundException e) { 346 throw new ServiceAccessException(this, e); 347 } 348 } 349 } 350 351 public void storeRevisionDescriptors 353 (Uri uri, NodeRevisionDescriptors revisionDescriptors) 354 throws ServiceAccessException, RevisionDescriptorNotFoundException { 355 356 if (uri instanceof ResourceId) { 357 super.storeRevisionDescriptors(uri, revisionDescriptors); 358 } 359 else { 360 try { 361 ResourceId resourceId = obtainResourceId(uri); 362 NodeRevisionDescriptors nrdsClone = revisionDescriptors.cloneObject(); 363 nrdsClone.setUri(resourceId.getUuri()); resourceId.getStore().storeRevisionDescriptors(resourceId, nrdsClone); 365 } 366 catch (ObjectNotFoundException e) { 367 throw new ServiceAccessException(this, e); 369 } 370 } 371 } 372 373 public void removeRevisionDescriptors(Uri uri) 375 throws ServiceAccessException { 376 377 if (uri instanceof ResourceId) { 378 super.removeRevisionDescriptors(uri); 379 } 380 else { 381 try { 382 ResourceId resourceId = obtainResourceId(uri); 383 resourceId.getStore().removeRevisionDescriptors(resourceId); 384 } 385 catch (ObjectNotFoundException e) { 386 throw new ServiceAccessException(this, e); 387 } 388 } 389 } 390 391 public NodeRevisionDescriptor retrieveRevisionDescriptor 393 (Uri uri, NodeRevisionNumber revisionNumber) 394 throws ServiceAccessException, RevisionDescriptorNotFoundException { 395 396 if (uri instanceof ResourceId) { 397 return super.retrieveRevisionDescriptor(uri, revisionNumber); 398 } 399 else { 400 try { 401 ObjectNode objectNode = doRetrieveObjectNode(uri); 402 ResourceId resourceId = obtainResourceId(uri); 403 NodeRevisionDescriptor nrd = resourceId.getStore().retrieveRevisionDescriptor(resourceId, revisionNumber); 404 nrd.setProperty("resource-id", resourceId.asXml()); 405 nrd.setProperty("parent-set", getXmlParentSet(uri, objectNode)); 406 return nrd; 407 } 408 catch (ObjectNotFoundException e) { 409 throw new ServiceAccessException(this, e); 411 } 412 } 413 } 414 415 public String getXmlParentSet(Uri uri, ObjectNode objectNode) throws ServiceAccessException, ObjectNotFoundException { 416 ResourceId resourceId = obtainResourceId(uri); 417 boolean useBinding = Configuration.useBinding(this) && !resourceId.isStoreRoot(); 421 422 XMLValue result = new XMLValue(); 423 Enumeration parentBindings = objectNode.enumerateParentBindings(); 424 while (parentBindings.hasMoreElements()) { 425 ObjectNode.Binding parentBinding = (ObjectNode.Binding) parentBindings.nextElement(); 426 Element parentElm = new Element("parent", NodeProperty.NamespaceCache.DEFAULT_NAMESPACE); 427 Element hrefElm = new Element("href", NodeProperty.NamespaceCache.DEFAULT_NAMESPACE); 428 String parentUriStr = new UriPath(objectNode.getUri()).parent().toString(); 429 Uri parentUri = new Uri(uri.getToken(), uri.getNamespace(), parentUriStr); 430 String uriStr; 431 if (useBinding) { 432 ResourceId parentResourceId = ResourceId.create(parentUri, parentBinding.getUuri()); 433 uriStr = getFirstMapping(parentResourceId); 434 } 435 else { 436 uriStr = parentUriStr; 437 } 438 hrefElm.setText(uriStr); 439 parentElm.addContent(hrefElm); 440 Element segmentElm = new Element("segment", NodeProperty.NamespaceCache.DEFAULT_NAMESPACE); 441 segmentElm.setText(parentBinding.getName()); 442 parentElm.addContent(segmentElm); 443 result.add(parentElm); 444 } 445 446 return result.toString(); 447 } 448 449 private String getFirstMapping(ResourceId resourceId) throws ServiceAccessException, ObjectNotFoundException { 450 String result = ""; 451 while (!resourceId.isStoreRoot()) { 452 ObjectNode objectNode = resourceId.getStore().retrieveObject(resourceId); 453 Enumeration enum = objectNode.enumerateParentBindings(); 454 if (!enum.hasMoreElements()) { 455 throw new IllegalStateException(); 456 } 457 ObjectNode.Binding parentBinding = (ObjectNode.Binding) enum.nextElement(); 458 String parentSegment = parentBinding.getName(); 459 result = "/" + parentSegment + result; 460 resourceId = ResourceId.create(resourceId, parentBinding.getUuri()); 461 } 462 return "/".equals(resourceId.getScope().toString()) 463 ? result 464 : resourceId.getScope()+result; 465 } 466 467 public void createRevisionDescriptor 469 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 470 throws ServiceAccessException { 471 472 if (uri instanceof ResourceId) { 473 super.createRevisionDescriptor(uri, revisionDescriptor); 474 } 475 else { 476 try { 477 revisionDescriptor.removeProperty("resource-id"); 478 revisionDescriptor.removeProperty("parent-set"); 479 ResourceId resourceId = obtainResourceId(uri); 480 resourceId.getStore().createRevisionDescriptor(resourceId, revisionDescriptor); 481 } 482 catch (ObjectNotFoundException e) { 483 throw new ServiceAccessException(this, e); 484 } 485 } 486 } 487 488 public void storeRevisionDescriptor 490 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 491 throws ServiceAccessException, RevisionDescriptorNotFoundException { 492 493 if (uri instanceof ResourceId) { 494 super.storeRevisionDescriptor(uri, revisionDescriptor); 495 } 496 else { 497 try { 498 revisionDescriptor.removeProperty("resource-id"); 499 revisionDescriptor.removeProperty("parent-set"); 500 ResourceId resourceId = obtainResourceId(uri); 501 resourceId.getStore().storeRevisionDescriptor(resourceId, revisionDescriptor); 502 } 503 catch (ObjectNotFoundException e) { 504 throw new ServiceAccessException(this, e); 506 } 507 } 508 } 509 510 public void removeRevisionDescriptor(Uri uri, NodeRevisionNumber number) 512 throws ServiceAccessException { 513 514 if (uri instanceof ResourceId) { 515 super.removeRevisionDescriptor(uri, number); 516 } 517 else { 518 try { 519 ResourceId resourceId = obtainResourceId(uri); 520 resourceId.getStore().removeRevisionDescriptor(resourceId, number); 521 } 522 catch (ObjectNotFoundException e) { 523 throw new ServiceAccessException(this, e); 524 } 525 } 526 } 527 528 public NodeRevisionContent retrieveRevisionContent 530 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 531 throws ServiceAccessException, RevisionNotFoundException { 532 533 if (uri instanceof ResourceId) { 534 return super.retrieveRevisionContent(uri, revisionDescriptor); 535 } 536 else { 537 try { 538 ResourceId resourceId = obtainResourceId(uri); 539 return resourceId.getStore().retrieveRevisionContent(resourceId, revisionDescriptor); 540 } 541 catch (ObjectNotFoundException e) { 542 throw new ServiceAccessException(this, e); 544 } 545 } 546 } 547 548 public void createRevisionContent 550 (Uri uri, NodeRevisionDescriptor revisionDescriptor, 551 NodeRevisionContent revisionContent) 552 throws ServiceAccessException, RevisionAlreadyExistException { 553 554 if (uri instanceof ResourceId) { 555 super.createRevisionContent(uri, revisionDescriptor, revisionContent); 556 } 557 else { 558 try { 559 ResourceId resourceId = obtainResourceId(uri); 560 resourceId.getStore().createRevisionContent(resourceId, revisionDescriptor, revisionContent); 561 } 562 catch (ObjectNotFoundException e) { 563 throw new ServiceAccessException(this, e); 564 } 565 } 566 } 567 568 public void storeRevisionContent 570 (Uri uri, NodeRevisionDescriptor revisionDescriptor, 571 NodeRevisionContent revisionContent) 572 throws ServiceAccessException, RevisionNotFoundException { 573 574 if (uri instanceof ResourceId) { 575 super.storeRevisionContent(uri, revisionDescriptor, revisionContent); 576 } 577 else { 578 try { 579 ResourceId resourceId = obtainResourceId(uri); 580 resourceId.getStore().storeRevisionContent(resourceId, revisionDescriptor, revisionContent); 581 } 582 catch (ObjectNotFoundException e) { 583 throw new ServiceAccessException(this, e); 585 } 586 } 587 } 588 589 public void removeRevisionContent 591 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 592 throws ServiceAccessException { 593 594 if (uri instanceof ResourceId) { 595 super.removeRevisionContent(uri, revisionDescriptor); 596 } 597 else { 598 try { 599 ResourceId resourceId = obtainResourceId(uri); 600 resourceId.getStore().removeRevisionContent(resourceId, revisionDescriptor); 601 } 602 catch (ObjectNotFoundException e) { 603 throw new ServiceAccessException(this, e); 604 } 605 } 606 } 607 608 610 613 public NodeStore getNodeStore() { 614 return nodeStore; 615 } 616 617 621 public boolean useBinding() { 622 return !"false".equalsIgnoreCase((String)getParameter("useBinding")); 623 } 624 625 private ObjectNode doRetrieveObjectNode(Uri uri) 626 throws ServiceAccessException, ObjectNotFoundException { 627 628 ObjectNode result; 629 if (Configuration.useBinding(this)) { 630 result = doResolve(uri); 631 result.setUuri(result.getUri()); 632 result.setUri(uri.toString()); } else { 634 ResourceId resourceId = ResourceId.create(uri, uri.toString()); 635 result = resourceId.getStore().retrieveObject(resourceId); 636 result.setUuri(result.getUri()); 637 } 638 return result; 639 } 640 641 644 private ObjectNode doResolve(Uri uri) throws ObjectNotFoundException, ServiceAccessException { 645 ResourceId resourceId = checkResolveCache(uri); 647 648 if (resourceId == null) { 649 UriPath uriPath = new UriPath(uri.toString()); 650 String[] segments = uriPath.tokens(); 651 String rootUriStr = uri.getScope().toString(); 652 if (!"/".equals(rootUriStr)) { 653 rootUriStr += "/"; 654 } 655 String currentUriStr = rootUriStr; 656 Uri currentUri = new Uri(uri.getToken(), uri.getNamespace(), currentUriStr); 657 ResourceId currentResourceId = ResourceId.create(currentUri, currentUriStr); 658 int start = new UriPath(rootUriStr).tokens().length; 659 660 for (int i = start; i < segments.length; i++) { 661 664 ObjectNode objectNode = currentResourceId.getStore().retrieveObject(currentResourceId); 665 objectNode.setUri(currentUriStr); 666 currentUriStr = uriPath.subUriPath(0, i + 1).toString(); 667 currentUri = new Uri(uri.getToken(), uri.getNamespace(), currentUriStr); 668 String currentUuri = objectNode.getBindingUuri(segments[i]); 669 if (currentUuri == null) { 670 throw new ObjectNotFoundException(currentUriStr); 671 } 672 currentResourceId = ResourceId.create(currentUri, currentUuri); 673 } 674 resourceId = currentResourceId; 675 676 cacheResolve(uri, resourceId); 678 } 680 return resourceId.getStore().retrieveObject(resourceId); 681 } 682 683 private void cacheResolve(Uri uri, ResourceId resourceId) { 684 if (uri.getToken() != null) { 685 uri.getToken().cacheResolve(uri, resourceId); 686 } 688 } 692 693 private ResourceId checkResolveCache(Uri uri) { 694 ResourceId resourceId = null; 695 if (uri.getToken() != null) { 696 resourceId = uri.getToken().checkResolveCache(uri); 697 } 698 return resourceId; 699 } 700 701 private ResourceId obtainResourceId(Uri uri) throws ServiceAccessException, ObjectNotFoundException { 702 ObjectNode objectNode = doRetrieveObjectNode(uri); 703 ResourceId resourceId = ResourceId.create(uri, objectNode.getUuri()); 704 return resourceId; 705 } 706 } 707 708 709 | Popular Tags |