use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class LocalCollectionManagementService method moveResource.
@Override
public void moveResource(final XmldbURI src, final XmldbURI dest, final XmldbURI name) throws XMLDBException {
final XmldbURI srcPath = resolve(src);
final XmldbURI destPath = dest == null ? srcPath.removeLastSegment() : resolve(dest);
final XmldbURI newName;
if (name == null) {
newName = srcPath.lastSegment();
} else {
newName = name;
}
withDb((broker, transaction) -> modify(broker, transaction, srcPath.removeLastSegment()).apply((sourceCol, b1, t1) -> {
try (final LockedDocument lockedSource = sourceCol.getDocumentWithLock(b1, srcPath.lastSegment(), Lock.LockMode.WRITE_LOCK)) {
final DocumentImpl source = lockedSource == null ? null : lockedSource.getDocument();
if (source == null) {
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
sourceCol.close();
throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE, "Resource " + srcPath + " not found");
}
return modify(b1, t1, destPath).apply((destinationCol, b2, t2) -> {
try (final ManagedDocumentLock lockedDestination = b2.getBrokerPool().getLockManager().acquireDocumentWriteLock(destinationCol.getURI().append(newName))) {
b2.moveResource(t2, source, destinationCol, newName);
// NOTE: early release of Collection locks inline with Asymmetrical Locking scheme
destinationCol.close();
sourceCol.close();
}
return null;
});
}
}));
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class LocalCollection method getResource.
Resource getResource(final DBBroker broker, final Txn transaction, final XmldbURI idURI) throws XMLDBException {
return this.<Resource>read(broker, transaction).apply((collection, broker1, transaction1) -> {
try (final LockedDocument lockedDocument = collection.getDocumentWithLock(broker1, idURI, LockMode.READ_LOCK)) {
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
final DocumentImpl document = lockedDocument == null ? null : lockedDocument.getDocument();
if (document == null) {
LOG.warn("Resource {} not found", idURI);
return null;
}
final Resource r;
switch(document.getResourceType()) {
case DocumentImpl.XML_FILE:
r = new LocalXMLResource(user, brokerPool, this, idURI);
break;
case DocumentImpl.BINARY_FILE:
r = new LocalBinaryResource(user, brokerPool, this, idURI);
break;
default:
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Unknown resource type");
}
((AbstractEXistResource) r).setMimeType(document.getMimeType());
return r;
}
});
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class CollectionStoreTest method store.
@Test
public void store() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
try (final Collection col = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI)) {
broker.storeDocument(transaction, TEST_XML_DOC_URI, new StringInputSource(TEST_XML_DOC), MimeType.XML_TYPE, col);
broker.saveCollection(transaction, col);
}
try (final Collection col = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.READ_LOCK)) {
try (final LockedDocument lockedDoc = col.getDocumentWithLock(broker, TEST_XML_DOC_URI, LockMode.READ_LOCK)) {
// NOTE: early release of collection lock inline with async locking
col.close();
if (lockedDoc != null) {
final Source expected = Input.fromString(TEST_XML_DOC).build();
final Source actual = Input.fromDocument(lockedDoc.getDocument()).build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
}
}
}
transaction.commit();
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class CollectionStoreTest method storeBinary.
private void storeBinary(final PreserveType preserveOnCopy) throws EXistException, PermissionDeniedException, IOException, TriggerException, LockException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
try (final Collection col = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI)) {
final byte[] bin = TEST_BIN_DOC.getBytes(UTF_8);
try (final InputStream is = new UnsynchronizedByteArrayInputStream(bin)) {
final int docId = broker.getNextResourceId(transaction);
final BinaryDocument binDoc = col.addBinaryResource(transaction, broker, new BinaryDocument(broker.getBrokerPool(), col, docId, TEST_BIN_DOC_URI), is, "text/plain", bin.length, null, null, preserveOnCopy);
assertNotNull(binDoc);
}
broker.saveCollection(transaction, col);
}
try (final Collection col = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.READ_LOCK)) {
try (final LockedDocument lockedDoc = col.getDocumentWithLock(broker, TEST_BIN_DOC_URI, LockMode.READ_LOCK)) {
// NOTE: early release of collection lock inline with async locking
col.close();
if (lockedDoc != null) {
assertTrue(lockedDoc.getDocument() instanceof BinaryDocument);
final BinaryDocument doc = (BinaryDocument) lockedDoc.getDocument();
final Try<String, IOException> docContent = broker.withBinaryFile(transaction, doc, is -> Try.TaggedTryUnchecked(IOException.class, () -> new String(Files.readAllBytes(is), UTF_8)));
assertEquals(TEST_BIN_DOC, docContent.get());
}
}
}
transaction.commit();
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class StylesheetResolverAndCompiler method templates.
public <E extends Exception> Templates templates(DBBroker broker, XSLTErrorsListener<E> errorListener) throws E, TransformerConfigurationException, IOException, PermissionDeniedException, SAXException {
if (uri.startsWith(XmldbURI.EMBEDDED_SERVER_URI_PREFIX)) {
final String docPath = uri.substring(XmldbURI.EMBEDDED_SERVER_URI_PREFIX.length());
try (final LockedDocument lockedDocument = broker.getXMLResource(XmldbURI.create(docPath), LockMode.READ_LOCK)) {
if (lockedDocument == null) {
throw new IOException("XSL stylesheet not found: " + docPath);
}
final DocumentImpl doc = lockedDocument.getDocument();
if (templates == null || doc.getLastModified() > lastModified) {
if (LOG.isDebugEnabled()) {
LOG.debug("compiling stylesheet {}", doc.getURI());
}
templates = compileTemplates(broker, doc, errorListener);
lastModified = doc.getLastModified();
}
}
} else {
final URL url = new URL(uri);
final URLConnection connection = url.openConnection();
long modified = connection.getLastModified();
if (templates == null || modified > lastModified || modified == 0) {
if (LOG.isDebugEnabled()) {
LOG.debug("compiling stylesheet {}", url);
}
try (final InputStream is = connection.getInputStream()) {
templates = factory(broker.getBrokerPool(), errorListener).newTemplates(new StreamSource(is));
}
}
lastModified = modified;
}
return templates;
}
Aggregations