use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class CopyResourceTest method checkAttributes.
private void checkAttributes(final XmldbURI docName, final String expectedOwner, final String expectedGroup, final int expectedMode, final Matcher<Long> expectedCreated, final Matcher<Long> expectedLastModified) throws EXistException, PermissionDeniedException {
final BrokerPool pool = existWebServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final LockedDocument lockedDoc = broker.getXMLResource(TEST_COLLECTION_URI.append(docName), LockMode.READ_LOCK)) {
final DocumentImpl doc = lockedDoc.getDocument();
final Permission permission = doc.getPermissions();
assertEquals("Owner value was not expected", expectedOwner, permission.getOwner().getName());
assertEquals("Group value was not expected", expectedGroup, permission.getGroup().getName());
assertEquals("Mode value was not expected", expectedMode, permission.getMode());
assertThat("Created value is not correct", doc.getCreated(), expectedCreated);
assertThat("LastModified value is not correct", doc.getLastModified(), expectedLastModified);
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class RecoveryTest2 method read.
@Test
public void read() throws EXistException, DatabaseConfigurationException, PermissionDeniedException, SAXException, IOException {
BrokerPool.FORCE_CORRUPTION = false;
BrokerPool pool = startDb();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
assertNotNull(broker);
final Serializer serializer = broker.borrowSerializer();
try (final LockedDocument lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append("terms-eng.xml"), LockMode.READ_LOCK)) {
assertNotNull("Document should not be null", lockedDoc);
String data = serializer.serialize(lockedDoc.getDocument());
assertNotNull(data);
} finally {
broker.returnSerializer(serializer);
}
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class RemoveTest method doUpdate.
@Override
protected void doUpdate(final DBBroker broker, final TransactionManager transact, final MutableDocumentSet docs) throws ParserConfigurationException, IOException, SAXException, LockException, XPathException, PermissionDeniedException, EXistException {
final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
assertNotNull(proc);
try (final Txn transaction = transact.beginTransaction()) {
// append some new element to records
for (int i = 1; i <= 50; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:append select=\"/products\">" + " <product>" + " <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>" + " <description>Product " + i + "</description>" + " <price>" + (i * 2.5) + "</price>" + " <stock>" + (i * 10) + "</stock>" + " </product>" + " </xu:append>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
}
transact.commit(transaction);
}
final Serializer serializer = broker.borrowSerializer();
try (final LockedDocument lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(TestConstants.TEST_XML_URI), LockMode.READ_LOCK)) {
assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/test.xml' should not be null", lockedDoc);
final String data = serializer.serialize(lockedDoc.getDocument());
} finally {
broker.returnSerializer(serializer);
}
// the following transaction will not be committed and thus undone during recovery
final Txn transaction = transact.beginTransaction();
// remove elements
for (int i = 1; i <= 25; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:remove select=\"/products/product[last()]\"/>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
}
// DO NOT COMMIT TRANSACTION
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class ResourceTest method read.
private void read(final BrokerPool pool) throws EXistException, PermissionDeniedException, IOException, LockException, TriggerException {
final TransactionManager transact = pool.getTransactionManager();
byte[] data = null;
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
final XmldbURI docPath = TestConstants.TEST_COLLECTION_URI.append(DOCUMENT_NAME_URI);
try (final LockedDocument lockedDoc = broker.getXMLResource(docPath, LockMode.READ_LOCK)) {
// if document is not present, null is returned
if (lockedDoc == null) {
fail("Binary document '" + docPath + " does not exist.");
} else {
final BinaryDocument binDoc = (BinaryDocument) lockedDoc.getDocument();
try (final InputStream is = broker.getBinaryResource(transaction, binDoc)) {
data = new byte[(int) binDoc.getContentLength()];
is.read(data);
}
}
}
try (final Collection collection = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
try (final LockedDocument lockedDoc = broker.getXMLResource(docPath, LockMode.WRITE_LOCK)) {
collection.removeBinaryResource(transaction, broker, lockedDoc.getDocument());
broker.saveCollection(transaction, collection);
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
}
}
transact.commit(transaction);
}
assertEquals(0, data.length);
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class AuditTrailSessionListener method executeXQuery.
private void executeXQuery(String xqueryResourcePath) {
if (xqueryResourcePath != null && xqueryResourcePath.length() > 0) {
xqueryResourcePath = xqueryResourcePath.trim();
try {
final BrokerPool pool = BrokerPool.getInstance();
final Subject sysSubject = pool.getSecurityManager().getSystemSubject();
try (final DBBroker broker = pool.get(Optional.of(sysSubject))) {
if (broker == null) {
LOG.error("Unable to retrieve DBBroker for {}", sysSubject.getName());
return;
}
final XmldbURI pathUri = XmldbURI.create(xqueryResourcePath);
try (final LockedDocument lockedResource = broker.getXMLResource(pathUri, LockMode.READ_LOCK)) {
final Source source;
if (lockedResource != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Resource [{}] exists.", xqueryResourcePath);
}
source = new DBSource(broker, (BinaryDocument) lockedResource.getDocument(), true);
} else {
LOG.error("Resource [{}] does not exist.", xqueryResourcePath);
return;
}
final XQuery xquery = pool.getXQueryService();
if (xquery == null) {
LOG.error("broker unable to retrieve XQueryService");
return;
}
final XQueryPool xqpool = pool.getXQueryPool();
CompiledXQuery compiled = xqpool.borrowCompiledXQuery(broker, source);
final XQueryContext context;
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
context.setStaticallyKnownDocuments(new XmldbURI[] { pathUri });
context.setBaseURI(new AnyURIValue(pathUri.toString()));
if (compiled == null) {
compiled = xquery.compile(context, source);
} else {
compiled.getContext().updateContext(context);
context.getWatchDog().reset();
}
final Properties outputProperties = new Properties();
try {
final long startTime = System.currentTimeMillis();
final Sequence result = xquery.execute(broker, compiled, null, outputProperties);
final long queryTime = System.currentTimeMillis() - startTime;
if (LOG.isTraceEnabled()) {
LOG.trace("XQuery execution results: {} in {}ms.", result.toString(), queryTime);
}
} finally {
context.runCleanupTasks();
xqpool.returnCompiledXQuery(source, compiled);
}
}
}
} catch (final Exception e) {
LOG.error("Exception while executing [{}] script", xqueryResourcePath, e);
}
}
}
Aggregations