use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class UserXQueryJob method execute.
@Override
public final void execute(final JobExecutionContext jec) throws JobExecutionException {
final JobDataMap jobDataMap = jec.getJobDetail().getJobDataMap();
// TODO why are these values not used from the class members?
final String xqueryResource = (String) jobDataMap.get(XQUERY_SOURCE);
final Subject user = (Subject) jobDataMap.get(ACCOUNT);
final BrokerPool pool = (BrokerPool) jobDataMap.get(DATABASE);
final Properties params = (Properties) jobDataMap.get(PARAMS);
final boolean unschedule = ((Boolean) jobDataMap.get(UNSCHEDULE));
// if invalid arguments then abort
if ((pool == null) || (xqueryResource == null) || (user == null)) {
abort("BrokerPool or XQueryResource or User was null!");
}
try (final DBBroker broker = pool.get(Optional.of(user))) {
if (xqueryResource.indexOf(':') > 0) {
final Source source = SourceFactory.getSource(broker, "", xqueryResource, true);
if (source != null) {
executeXQuery(pool, broker, source, params);
return;
}
} else {
final XmldbURI pathUri = XmldbURI.create(xqueryResource);
try (final LockedDocument lockedResource = broker.getXMLResource(pathUri, LockMode.READ_LOCK)) {
if (lockedResource != null) {
final Source source = new DBSource(broker, (BinaryDocument) lockedResource.getDocument(), true);
executeXQuery(pool, broker, source, params);
return;
}
}
}
LOG.warn("XQuery User Job not found: {}, job not scheduled", xqueryResource);
} catch (final EXistException ee) {
abort("Could not get DBBroker!");
} catch (final PermissionDeniedException pde) {
abort("Permission denied for the scheduling user: " + user.getName() + "!");
} catch (final XPathException xpe) {
abort("XPathException in the Job: " + xpe.getMessage() + "!", unschedule);
} catch (final IOException e) {
abort("Could not load XQuery: " + e.getMessage());
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class LocalCollection method removeResource.
@Override
public void removeResource(final Resource res) throws XMLDBException {
if (res == null) {
return;
}
final XmldbURI resURI;
try {
resURI = XmldbURI.xmldbUriFor(res.getId());
} catch (final URISyntaxException e) {
throw new XMLDBException(ErrorCodes.INVALID_URI, e);
}
modify().apply((collection, broker, transaction) -> {
// Check that the document exists
try (final LockedDocument lockedDocument = collection.getDocumentWithLock(broker, resURI, LockMode.WRITE_LOCK)) {
if (lockedDocument == null) {
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Resource " + resURI + " not found");
}
if (XMLResource.RESOURCE_TYPE.equals(res.getResourceType())) {
collection.removeXMLResource(transaction, broker, resURI);
} else {
collection.removeBinaryResource(transaction, broker, resURI);
}
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
}
return null;
});
this.needsSync = true;
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class LocalXUpdateQueryService method updateResource.
@Override
public long updateResource(final String id, final String commands) throws XMLDBException {
return this.<Long>withDb((broker, transaction) -> {
final long start = System.currentTimeMillis();
final MutableDocumentSet docs = this.<MutableDocumentSet>read(broker, transaction, collection.getPathURI()).apply((collection, broker1, transaction1) -> {
MutableDocumentSet d = new DefaultDocumentSet();
if (id == null) {
d = collection.allDocs(broker1, d, true);
} else {
try {
final XmldbURI resourceURI = XmldbURI.xmldbUriFor(id);
try (final LockedDocument lockedDocument = collection.getDocumentWithLock(broker1, resourceURI, Lock.LockMode.READ_LOCK)) {
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
final DocumentImpl doc = lockedDocument == null ? null : lockedDocument.getDocument();
if (doc == null) {
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Resource not found: " + id);
}
d.add(doc);
}
} catch (final URISyntaxException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
}
return d;
});
try (final Reader reader = new StringReader(commands)) {
if (processor == null) {
processor = new XUpdateProcessor(broker, docs);
} else {
processor.setBroker(broker);
processor.setDocumentSet(docs);
}
final Modification[] modifications = processor.parse(new InputSource(reader));
long mods = 0;
for (Modification modification : modifications) {
mods += modification.process(transaction);
broker.flush();
}
if (LOG.isDebugEnabled()) {
LOG.debug("xupdate took {}ms.", System.currentTimeMillis() - start);
}
return mods;
} catch (final ParserConfigurationException | SAXException | LockException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
} finally {
if (processor != null) {
processor.reset();
}
}
});
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class LocalCollectionManagementService method copyResource.
private void copyResource(final XmldbURI src, final XmldbURI dest, final XmldbURI name, final PreserveType preserve) 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) -> read(broker, transaction, srcPath.removeLastSegment()).apply((sourceCol, b1, t1) -> {
try (final LockedDocument lockedSource = sourceCol.getDocumentWithLock(b1, srcPath.lastSegment(), Lock.LockMode.READ_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))) {
try {
b2.copyResource(t2, source, destinationCol, newName, preserve);
// NOTE: early release of Collection locks inline with Asymmetrical Locking scheme
destinationCol.close();
sourceCol.close();
return null;
} catch (final EXistException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "failed to copy resource " + srcPath, e);
}
}
});
}
}));
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class Eval method loadQueryFromURI.
/**
* @param expr
* @throws XPathException
* @throws NullPointerException
* @throws IllegalArgumentException
*/
private Source loadQueryFromURI(final Item expr) throws XPathException, NullPointerException, IllegalArgumentException {
final String location = expr.getStringValue();
Source querySource = null;
if (location.indexOf(':') < 0 || location.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
try {
XmldbURI locationUri = XmldbURI.xmldbUriFor(location);
// be added.
if (location.indexOf('/') < 0 || location.startsWith(".")) {
final XmldbURI moduleLoadPathUri = XmldbURI.xmldbUriFor(context.getModuleLoadPath());
locationUri = moduleLoadPathUri.resolveCollectionPath(locationUri);
}
try (final LockedDocument lockedSourceDoc = context.getBroker().getXMLResource(locationUri.toCollectionPathURI(), LockMode.READ_LOCK)) {
final DocumentImpl sourceDoc = lockedSourceDoc == null ? null : lockedSourceDoc.getDocument();
if (sourceDoc == null) {
throw new XPathException(this, "source for module " + location + " not found in database");
}
if (sourceDoc.getResourceType() != DocumentImpl.BINARY_FILE || !"application/xquery".equals(sourceDoc.getMetadata().getMimeType())) {
throw new XPathException(this, "source for module " + location + " is not an XQuery or " + "declares a wrong mime-type");
}
querySource = new DBSource(context.getBroker(), (BinaryDocument) sourceDoc, true);
} catch (final PermissionDeniedException e) {
throw new XPathException(this, "permission denied to read module source from " + location);
}
} catch (final URISyntaxException e) {
throw new XPathException(this, e);
}
} else {
// No. Load from file or URL
try {
// TODO: use URIs to ensure proper resolution of relative locations
querySource = SourceFactory.getSource(context.getBroker(), context.getModuleLoadPath(), location, true);
if (querySource == null) {
throw new XPathException(this, "source for query at " + location + " not found");
}
} catch (final MalformedURLException e) {
throw new XPathException(this, "source location for query at " + location + " should be a valid URL: " + e.getMessage());
} catch (final IOException e) {
throw new XPathException(this, "source for query at " + location + " not found: " + e.getMessage());
} catch (final PermissionDeniedException e) {
throw new XPathException(this, "Permission denied to access query at " + location + " : " + e.getMessage());
}
}
return querySource;
}
Aggregations