use of org.exist.security.Subject 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