use of org.exist.storage.txn.TransactionException in project exist by eXist-db.
the class NativeBroker method getBinaryResource.
@Override
public InputStream getBinaryResource(final BinaryDocument blob) throws IOException {
// TODO(AR) how best to get the transaction?
try (final Txn transaction = continueOrBeginTransaction()) {
final InputStream is = getBinaryResource(transaction, blob);
transaction.commit();
return is;
} catch (final TransactionException e) {
throw new IOException(e.getMessage(), e);
}
}
use of org.exist.storage.txn.TransactionException in project exist by eXist-db.
the class MetadataFunctions method extractMetadataFromLocalResource.
private Sequence extractMetadataFromLocalResource(final XmldbURI docUri) throws XPathException {
try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(docUri, LockMode.READ_LOCK)) {
if (lockedDoc != null && lockedDoc.getDocument() instanceof BinaryDocument) {
final BinaryDocument binDoc = (BinaryDocument) lockedDoc.getDocument();
final BrokerPool pool = context.getBroker().getBrokerPool();
final BlobStore blobStore = pool.getBlobStore();
try (final Txn transaction = pool.getTransactionManager().beginTransaction()) {
final Sequence result = blobStore.with(transaction, binDoc.getBlobId(), blobFile -> TaggedTryUnchecked(XPathException.class, () -> exifToolExtract(blobFile))).get();
transaction.commit();
return result;
}
} else {
throw new XPathException(this, "The binary document at " + docUri.toString() + " cannot be found.");
}
} catch (PermissionDeniedException | IOException | TransactionException e) {
throw new XPathException(this, "Could not access binary document: " + e.getMessage(), e);
}
}
use of org.exist.storage.txn.TransactionException in project exist by eXist-db.
the class Deploy method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (!context.getSubject().hasDbaRole())
throw new XPathException(this, EXPathErrorCode.EXPDY003, "Permission denied. You need to be a member " + "of the dba group to use repo:deploy/undeploy");
final String pkgName = args[0].getStringValue();
try {
Deployment deployment = new Deployment();
final Optional<String> target;
if (isCalledAs("deploy")) {
String userTarget = null;
if (getArgumentCount() == 2) {
userTarget = args[1].getStringValue();
}
try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
target = deployment.deploy(context.getBroker(), transaction, pkgName, context.getRepository(), userTarget);
transaction.commit();
}
} else if (isCalledAs("install-and-deploy")) {
String version = null;
final String repoURI;
if (getArgumentCount() == 3) {
version = args[1].getStringValue();
repoURI = args[2].getStringValue();
} else {
repoURI = args[1].getStringValue();
}
try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
target = installAndDeploy(transaction, pkgName, version, repoURI);
transaction.commit();
}
} else if (isCalledAs("install-and-deploy-from-db")) {
String repoURI = null;
if (getArgumentCount() == 2) {
repoURI = args[1].getStringValue();
}
try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
target = installAndDeployFromDb(transaction, pkgName, repoURI);
transaction.commit();
}
} else {
try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
target = deployment.undeploy(context.getBroker(), transaction, pkgName, context.getRepository());
transaction.commit();
}
}
target.orElseThrow(() -> new XPathException("expath repository is not available."));
return statusReport(target);
} catch (PackageException e) {
throw new XPathException(this, EXPathErrorCode.EXPDY001, e.getMessage(), args[0], e);
} catch (IOException e) {
throw new XPathException(this, ErrorCodes.FOER0000, "Caught IO error while deploying expath archive", args[0], e);
} catch (TransactionException e) {
throw new XPathException(this, ErrorCodes.FOER0000, "Caught transaction error while deploying expath archive", args[0], e);
}
}
use of org.exist.storage.txn.TransactionException in project exist by eXist-db.
the class EXistURIResolver method databaseSource.
private Source databaseSource(final String path) throws TransformerException {
final XmldbURI uri = XmldbURI.create(path);
final DBBroker broker = db.getActiveBroker();
final DocumentImpl doc;
try {
doc = broker.getResource(uri, Permission.READ);
if (doc == null) {
LOG.error("Document {} not found", path);
throw new TransformerException("Resource " + path + " not found in database.");
}
final Source source;
if (doc instanceof BinaryDocument) {
/*
* NOTE: this is extremely unpleasant as we let a reference to the blob file
* escape from the closure into the StreamSource. This means that the file could have been deleted
* by time the user comes to access the StreamSource, however this was also
* the case with eXist-db's previous design, and due to the lack of resource
* management of the StreamSource class, there is little we can do to improve
* the situation - AR.
*/
try (final Txn transaction = broker.getBrokerPool().getTransactionManager().beginTransaction()) {
source = broker.withBinaryFile(transaction, (BinaryDocument) doc, p -> {
final StreamSource source1 = new StreamSource(p.toFile());
source1.setSystemId(p.toUri().toString());
return source1;
});
transaction.commit();
return source;
}
} else {
source = new EXistDbSource(broker, doc);
source.setSystemId(uri.toASCIIString());
return source;
}
} catch (final PermissionDeniedException | TransactionException | IOException e) {
throw new TransformerException(e.getMessage(), e);
}
}
use of org.exist.storage.txn.TransactionException in project exist by eXist-db.
the class RestoreHandler method restoreDeletedEntry.
private void restoreDeletedEntry(final Attributes atts) {
final String name = atts.getValue("name");
final String type = atts.getValue("type");
if ("collection".equals(type)) {
try {
try (final Txn transaction = beginTransaction();
final Collection collection = broker.openCollection(currentCollectionUri.append(name), Lock.LockMode.WRITE_LOCK)) {
if (collection != null) {
final boolean triggersEnabled = broker.isTriggersEnabled();
try {
broker.setTriggersEnabled(false);
broker.removeCollection(transaction, collection);
} finally {
// restore triggers enabled setting
broker.setTriggersEnabled(triggersEnabled);
}
}
transaction.commit();
}
} catch (final PermissionDeniedException | IOException | TriggerException | TransactionException e) {
listener.warn("Failed to remove deleted collection: " + name + ": " + e.getMessage());
}
} else if ("resource".equals(type)) {
final XmldbURI docName = XmldbURI.create(name);
try (final Txn transaction = beginTransaction();
final Collection collection = broker.openCollection(currentCollectionUri.append(name), Lock.LockMode.WRITE_LOCK);
final LockedDocument lockedDocument = collection.getDocumentWithLock(broker, docName, Lock.LockMode.WRITE_LOCK)) {
// Check that the document exists
if (lockedDocument != null) {
final boolean triggersEnabled = broker.isTriggersEnabled();
try {
broker.setTriggersEnabled(false);
final boolean xmlType = !(lockedDocument.getDocument() instanceof BinaryDocument);
if (xmlType) {
collection.removeXMLResource(transaction, broker, docName);
} else {
collection.removeBinaryResource(transaction, broker, docName);
}
} finally {
// restore triggers enabled setting
broker.setTriggersEnabled(triggersEnabled);
}
}
transaction.commit();
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
} catch (final PermissionDeniedException | TransactionException | TriggerException | LockException | IOException e) {
listener.warn("Failed to remove deleted resource: " + name + ": " + e.getMessage());
}
}
}
Aggregations