use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class GetRunningXQueries method getRunningXQueries.
private Sequence getRunningXQueries() throws XPathException {
Sequence xmlResponse = null;
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("xqueries", NAMESPACE_URI, PREFIX), null);
// Add all the running xqueries
final XQueryWatchDog[] watchdogs = getContext().getBroker().getBrokerPool().getProcessMonitor().getRunningXQueries();
for (XQueryWatchDog watchdog : watchdogs) {
final XQueryContext context = watchdog.getContext();
getRunningXQuery(builder, context, watchdog);
}
builder.endElement();
xmlResponse = (NodeValue) builder.getDocument().getDocumentElement();
return (xmlResponse);
} finally {
context.popDocumentContext();
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class KillRunningXQuery method killXQuery.
private void killXQuery(Sequence[] args) throws XPathException {
int id = 0;
long waittime = 0;
// determine the query id to kill
if (args.length == 1) {
if (!args[0].isEmpty()) {
id = ((NumericValue) args[0].itemAt(0)).getInt();
}
}
// determine the wait time
if (args.length == 2) {
if (!args[1].isEmpty()) {
waittime = ((NumericValue) args[1].itemAt(0)).getLong();
}
}
if (id != 0) {
final XQueryWatchDog[] watchdogs = getContext().getBroker().getBrokerPool().getProcessMonitor().getRunningXQueries();
for (XQueryWatchDog watchdog : watchdogs) {
final XQueryContext context = watchdog.getContext();
if (id == context.hashCode()) {
if (!watchdog.isTerminating()) {
watchdog.kill(waittime);
}
break;
}
}
}
}
use of org.exist.xquery.XQueryContext 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);
}
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class Shared method getStreamSource.
public static StreamSource getStreamSource(Item item, XQueryContext context) throws XPathException, IOException {
final StreamSource streamSource = new StreamSource();
if (item.getType() == Type.JAVA_OBJECT) {
LOG.debug("Streaming Java object");
final Object obj = ((JavaObjectValue) item).getObject();
if (!(obj instanceof File)) {
throw new XPathException("Passed java object should be a File");
}
final File inputFile = (File) obj;
final InputStream is = new FileInputStream(inputFile);
streamSource.setInputStream(is);
streamSource.setSystemId(inputFile.toURI().toURL().toString());
} else if (item.getType() == Type.ANY_URI) {
LOG.debug("Streaming xs:anyURI");
// anyURI provided
String url = item.getStringValue();
// Fix URL
if (url.startsWith("/")) {
url = "xmldb:exist://" + url;
}
final InputStream is = new URL(url).openStream();
streamSource.setInputStream(is);
streamSource.setSystemId(url);
} else if (item.getType() == Type.ELEMENT || item.getType() == Type.DOCUMENT) {
LOG.debug("Streaming element or document node");
if (item instanceof NodeProxy) {
final NodeProxy np = (NodeProxy) item;
final String url = "xmldb:exist://" + np.getOwnerDocument().getBaseURI();
LOG.debug("Document detected, adding URL {}", url);
streamSource.setSystemId(url);
}
// Node provided
final DBBroker broker = context.getBroker();
final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
final Serializer serializer = broker.borrowSerializer();
try {
fn.accept(serializer);
} finally {
broker.returnSerializer(serializer);
}
};
final NodeValue node = (NodeValue) item;
final InputStream is = new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
streamSource.setInputStream(is);
} else if (item.getType() == Type.BASE64_BINARY || item.getType() == Type.HEX_BINARY) {
LOG.debug("Streaming base64 binary");
final BinaryValue binary = (BinaryValue) item;
final byte[] data = binary.toJavaObject(byte[].class);
final InputStream is = new UnsynchronizedByteArrayInputStream(data);
streamSource.setInputStream(is);
if (item instanceof Base64BinaryDocument) {
final Base64BinaryDocument b64doc = (Base64BinaryDocument) item;
final String url = "xmldb:exist://" + b64doc.getUrl();
LOG.debug("Base64BinaryDocument detected, adding URL {}", url);
streamSource.setSystemId(url);
}
} else {
LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
}
return streamSource;
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class XQueryCompiler method compile.
public static CompiledXQuery compile(final DBBroker broker, final DocumentImpl document) throws RestXqServiceCompilationException {
try {
if (document instanceof BinaryDocument) {
if (document.getMimeType().equals(XQUERY_MIME_TYPE)) {
// compile the query
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
final DBSource source = new DBSource(broker, (BinaryDocument) document, true);
// set the module load path for any module imports that are relative
context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI_PREFIX + source.getDocumentPath().removeLastSegment());
return broker.getBrokerPool().getXQueryService().compile(context, source);
} else {
throw new RestXqServiceCompilationException("Invalid mimetype '" + document.getMimeType() + "' for XQuery: " + document.getURI().toString());
}
} else {
throw new RestXqServiceCompilationException("Invalid document location for XQuery: " + document.getURI().toString());
}
} catch (XPathException xpe) {
throw new RestXqServiceCompilationException("Unable to compile XQuery: " + document.getURI().toString() + ": " + xpe.getMessage(), xpe);
} catch (IOException ioe) {
throw new RestXqServiceCompilationException("Unable to access XQuery: " + document.getURI().toString() + ": " + ioe.getMessage(), ioe);
} catch (PermissionDeniedException pde) {
throw new RestXqServiceCompilationException("Permission to access XQuery denied: " + document.getURI().toString() + ": " + pde.getMessage(), pde);
}
}
Aggregations