Search in sources :

Example 6 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class Util method compileQuery.

static CompiledXQuery compileQuery(final DBBroker broker, final XQuery xqueryService, final XQueryPool xqueryPool, final Source query) throws PermissionDeniedException, XPathException, IOException {
    CompiledXQuery compiled = xqueryPool.borrowCompiledXQuery(broker, query);
    XQueryContext context;
    if (compiled == null) {
        context = new XQueryContext(broker.getBrokerPool());
    } else {
        context = compiled.getContext();
        context.prepareForReuse();
    }
    if (compiled == null) {
        compiled = xqueryService.compile(context, query);
    } else {
        compiled.getContext().updateContext(context);
        context.getWatchDog().reset();
    }
    return compiled;
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext)

Example 7 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class JNDIModule method retrieveJNDIContext.

/**
 * Retrieves a previously stored Connection from the Context of an XQuery
 *
 * @param context 		The Context of the XQuery containing the JNDI Context
 * @param ctxID 		The ID of the JNDI Context to retrieve from the Context of the XQuery
 *
 * @return the JNDI context
 */
public static final Context retrieveJNDIContext(XQueryContext context, long ctxID) {
    Context jndiContext = null;
    // get the existing connections map from the context
    HashMap contexts = (HashMap) context.getAttribute(JNDIModule.JNDICONTEXTS_VARIABLE);
    if (contexts != null) {
        jndiContext = (Context) contexts.get(ctxID);
    }
    return (jndiContext);
}
Also used : Context(javax.naming.Context) XQueryContext(org.exist.xquery.XQueryContext) HashMap(java.util.HashMap)

Example 8 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class JNDIModule method closeJNDIContext.

/**
 * Closes a specified JNDI Context for the specified XQueryContext
 *
 * @param context 	The context to close JNDI Contexts for
 * @param ctxID 			The ID of the JNDI Context to retrieve from the Context of the XQuery
 * @param contexts 			The contexts hashmap
 */
private static final void closeJNDIContext(XQueryContext context, long ctxID, HashMap contexts) {
    Context ctx = null;
    if (contexts != null) {
        ctx = (Context) contexts.get(ctxID);
        if (ctx != null) {
            try {
                // close the connection
                ctx.close();
                // remove it from the connections map
                contexts.remove(ctxID);
            } catch (NamingException ne) {
                LOG.error("Unable to close JNDI Context", ne);
            }
        }
    }
}
Also used : Context(javax.naming.Context) XQueryContext(org.exist.xquery.XQueryContext) NamingException(javax.naming.NamingException)

Example 9 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class LocalXPathQueryService method execute.

private ResourceSet execute(final LocalXmldbFunction<Source> sourceOp) throws XMLDBException {
    return withDb((broker, transaction) -> {
        final long start = System.currentTimeMillis();
        final Source source = sourceOp.apply(broker, transaction);
        final XmldbURI[] docs = new XmldbURI[] { XmldbURI.create(collection.getName(broker, transaction)) };
        final XQuery xquery = brokerPool.getXQueryService();
        final XQueryPool pool = brokerPool.getXQueryPool();
        XQueryContext context;
        CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
        if (compiled == null) {
            context = new XQueryContext(broker.getBrokerPool());
        } else {
            context = compiled.getContext();
            context.prepareForReuse();
        }
        context.setStaticallyKnownDocuments(docs);
        if (variableDecls.containsKey(Debuggee.PREFIX + ":session")) {
            context.declareVariable(Debuggee.SESSION, variableDecls.get(Debuggee.PREFIX + ":session"));
            variableDecls.remove(Debuggee.PREFIX + ":session");
        }
        setupContext(source, context);
        if (compiled == null) {
            compiled = xquery.compile(context, source);
        }
        try {
            final Sequence result = xquery.execute(broker, compiled, null, properties);
            if (LOG.isDebugEnabled()) {
                LOG.debug("query took {} ms.", System.currentTimeMillis() - start);
            }
            final Properties resourceSetProperties = new Properties(properties);
            resourceSetProperties.setProperty(EXistOutputKeys.XDM_SERIALIZATION, "yes");
            return result != null ? new LocalResourceSet(user, brokerPool, collection, resourceSetProperties, result, null) : null;
        } finally {
            compiled.getContext().runCleanupTasks();
            pool.returnCompiledXQuery(source, compiled);
        }
    });
}
Also used : XQueryPool(org.exist.storage.XQueryPool) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) FileSource(org.exist.source.FileSource)

Example 10 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class LocalXPathQueryService method compileAndCheck.

private Either<XPathException, CompiledExpression> compileAndCheck(final DBBroker broker, final Txn transaction, final String query) throws XMLDBException {
    final long start = System.currentTimeMillis();
    final XQuery xquery = broker.getBrokerPool().getXQueryService();
    final XQueryContext context = new XQueryContext(broker.getBrokerPool());
    try {
        setupContext(null, context);
        final CompiledExpression expr = xquery.compile(context, query);
        if (LOG.isDebugEnabled()) {
            LOG.debug("compilation took {}", System.currentTimeMillis() - start);
        }
        return Either.Right(expr);
    } catch (final PermissionDeniedException e) {
        throw new XMLDBException(ErrorCodes.PERMISSION_DENIED, e.getMessage(), e);
    } catch (final IllegalArgumentException e) {
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
    } catch (final XPathException e) {
        return Either.Left(e);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Aggregations

XQueryContext (org.exist.xquery.XQueryContext)70 CompiledXQuery (org.exist.xquery.CompiledXQuery)37 Sequence (org.exist.xquery.value.Sequence)34 XQuery (org.exist.xquery.XQuery)33 DBBroker (org.exist.storage.DBBroker)24 Test (org.junit.Test)23 XPathException (org.exist.xquery.XPathException)18 BrokerPool (org.exist.storage.BrokerPool)17 IOException (java.io.IOException)11 Source (org.exist.source.Source)10 XQueryPool (org.exist.storage.XQueryPool)10 Node (org.w3c.dom.Node)10 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)8 AnyURIValue (org.exist.xquery.value.AnyURIValue)8 URI (java.net.URI)7 StringValue (org.exist.xquery.value.StringValue)7 InputSource (org.xml.sax.InputSource)7 EXistException (org.exist.EXistException)6 QName (org.exist.dom.QName)6