private <R> R readResource(
      DBBroker broker, Resource resource, DatabaseItemReader<DocumentImpl, R> reader)
      throws XMLDBException, PermissionDeniedException {

    DocumentImpl document = null;
    try {
      document = ((AbstractEXistResource) resource).openDocument(broker, Lock.READ_LOCK);

      return reader.read(document);

    } finally {
      if (document != null) {
        ((AbstractEXistResource) resource).closeDocument(document, Lock.READ_LOCK);
      }
    }
  }
  private <R> R readCollection(
      DBBroker broker,
      XmldbURI collectionURI,
      DatabaseItemReader<org.exist.collections.Collection, R> reader)
      throws XMLDBException, PermissionDeniedException {
    org.exist.collections.Collection coll = null;

    try {
      coll = broker.openCollection(collectionURI, Lock.READ_LOCK);
      if (coll == null) {
        throw new XMLDBException(
            ErrorCodes.INVALID_COLLECTION, "Collection " + collectionURI.toString() + " not found");
      }

      return reader.read(coll);

    } finally {
      if (coll != null) {
        coll.release(Lock.READ_LOCK);
      }
    }
  }