Search in sources :

Example 6 with RestXqService

use of org.exquery.restxq.RestXqService in project exist by eXist-db.

the class ExistXqueryRegistry method reexamineModulesWithResolvedDependencies.

/**
 * Gets the modules that have a missing dependency
 * on the module indicated by compiledModuleURI
 * and attempts to re-compile them and register their
 * services
 */
private void reexamineModulesWithResolvedDependencies(final DBBroker broker, final String compiledModuleUri) {
    final Set<String> dependants;
    synchronized (missingDependencies) {
        final Set<String> deps = missingDependencies.get(compiledModuleUri);
        if (deps != null) {
            dependants = new HashSet<>(deps);
        } else {
            dependants = new HashSet<>();
        }
    }
    for (final String dependant : dependants) {
        try {
            final DocumentImpl dependantModule = broker.getResource(XmldbURI.create(dependant), Permission.READ);
            /*
                 * This null check is needed, as a dependency module may have been renamed,
                 * and so is no longer accessible under its old URI.
                 *
                 * However if its dependant module (compiledModuleUri) compiles
                 * (which it must have for this function to be invoked)
                 * then we can assume that the dependant module references the new
                 * module dependency (in the case of a module move/rename)
                 * or the dependency has been removed
                 */
            if (dependantModule != null) {
                LOG.info("Missing dependency '{}' has been added to the database, re-examining '{}'...", compiledModuleUri, dependant);
                final List<RestXqService> services = findServices(broker, dependantModule);
                LOG.info("Discovered {} resource functions for {}", services.size(), dependant);
                registerServices(broker, services);
            } else {
                LOG.info("Dependant '{}' has been resolved. Dependency on: {} was removed", compiledModuleUri, dependant);
                // we need to remove dependant from the dependenciesTree of dependant
                removeDependency(dependant, compiledModuleUri);
            }
        } catch (final PermissionDeniedException | ExQueryException pde) {
            LOG.error(pde.getMessage(), pde);
        }
        // remove the resolve dependencies from the missing dependencies
        removeMissingDependency(compiledModuleUri, dependant);
    }
}
Also used : RestXqService(org.exquery.restxq.RestXqService) ExQueryException(org.exquery.ExQueryException) PermissionDeniedException(org.exist.security.PermissionDeniedException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 7 with RestXqService

use of org.exquery.restxq.RestXqService in project exist by eXist-db.

the class RegistryFunctions method findService.

private RestXqService findService(final Iterator<RestXqService> services, final SignatureDetail signatureDetail) {
    RestXqService result = null;
    if (services != null) {
        while (services.hasNext()) {
            final RestXqService service = services.next();
            final org.exquery.xquery.FunctionSignature signature = service.getResourceFunction().getFunctionSignature();
            if (signature.getName().equals(signatureDetail.name) && signature.getArgumentCount() == signatureDetail.arity) {
                result = service;
                break;
            }
        }
    }
    return result;
}
Also used : RestXqService(org.exquery.restxq.RestXqService)

Example 8 with RestXqService

use of org.exquery.restxq.RestXqService in project exist by eXist-db.

the class RegistryFunctions method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final ExistXqueryRegistry xqueryRegistry = ExistXqueryRegistry.getInstance();
    final RestXqServiceRegistry registry = RestXqServiceRegistryManager.getRegistry(getContext().getBroker().getBrokerPool());
    Sequence result = Sequence.EMPTY_SEQUENCE;
    context.pushDocumentContext();
    try {
        if (isCalledAs(qnRegisterModule.getLocalPart())) {
            final XmldbURI moduleUri = args[0].toJavaObject(XmldbURI.class);
            final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
            if (xqueryRegistry.isXquery(module)) {
                try {
                    final List<RestXqService> resourceFunctions = xqueryRegistry.findServices(getContext().getBroker(), module);
                    xqueryRegistry.registerServices(getContext().getBroker(), resourceFunctions);
                    result = (NodeValue) org.exist.extensions.exquery.restxq.impl.xquery.RegistryFunctions.serializeRestXqServices(context.getDocumentBuilder(), resourceFunctions).getDocumentElement();
                } catch (final ExQueryException e) {
                    LOG.warn(e.getMessage(), e);
                    result = Sequence.EMPTY_SEQUENCE;
                }
            } else {
                result = Sequence.EMPTY_SEQUENCE;
            }
        } else if (isCalledAs(qnDeregisterModule.getLocalPart())) {
            final XmldbURI moduleUri = args[0].toJavaObject(XmldbURI.class);
            final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
            if (xqueryRegistry.isXquery(module)) {
                final List<RestXqService> deregisteringServices = new ArrayList<>();
                for (final RestXqService service : registry) {
                    if (XmldbURI.create(service.getResourceFunction().getXQueryLocation()).equals(moduleUri)) {
                        deregisteringServices.add(service);
                    }
                }
                xqueryRegistry.deregisterServices(getContext().getBroker(), moduleUri);
                result = (NodeValue) org.exist.extensions.exquery.restxq.impl.xquery.RegistryFunctions.serializeRestXqServices(context.getDocumentBuilder(), deregisteringServices).getDocumentElement();
            } else {
                result = Sequence.EMPTY_SEQUENCE;
            }
        } else if (isCalledAs(qnFindResourceFunctions.getLocalPart())) {
            final XmldbURI moduleUri = args[0].toJavaObject(XmldbURI.class);
            final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
            if (xqueryRegistry.isXquery(module)) {
                try {
                    final List<RestXqService> resourceFunctions = xqueryRegistry.findServices(getContext().getBroker(), module);
                    xqueryRegistry.deregisterServices(getContext().getBroker(), moduleUri);
                    result = (NodeValue) org.exist.extensions.exquery.restxq.impl.xquery.RegistryFunctions.serializeRestXqServices(context.getDocumentBuilder(), resourceFunctions).getDocumentElement();
                } catch (final ExQueryException e) {
                    LOG.warn(e.getMessage(), e);
                    result = Sequence.EMPTY_SEQUENCE;
                }
            } else {
                result = Sequence.EMPTY_SEQUENCE;
            }
        } else if (isCalledAs(qnRegisterResourceFunction.getLocalPart())) {
            final XmldbURI moduleUri = args[0].toJavaObject(XmldbURI.class);
            final String resourceFunctionIdentifier = args[1].getStringValue();
            final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
            if (xqueryRegistry.isXquery(module)) {
                final SignatureDetail signatureDetail = extractSignatureDetail(resourceFunctionIdentifier);
                if (signatureDetail != null) {
                    try {
                        final RestXqService serviceToRegister = findService(xqueryRegistry.findServices(getContext().getBroker(), module).iterator(), signatureDetail);
                        if (serviceToRegister != null) {
                            xqueryRegistry.registerServices(context.getBroker(), Collections.singletonList(serviceToRegister));
                            result = BooleanValue.TRUE;
                        } else {
                            result = BooleanValue.FALSE;
                        }
                    } catch (final ExQueryException e) {
                        LOG.warn(e.getMessage(), e);
                        result = BooleanValue.FALSE;
                    }
                } else {
                    result = BooleanValue.FALSE;
                }
            } else {
                result = Sequence.EMPTY_SEQUENCE;
            }
        } else if (isCalledAs(qnDeregisterResourceFunction.getLocalPart())) {
            // TODO
            final String resourceFunctionIdentifier = args[1].getStringValue();
            final SignatureDetail signatureDetail = extractSignatureDetail(resourceFunctionIdentifier);
            if (signatureDetail != null) {
                final RestXqService serviceToDeregister = findService(xqueryRegistry.registered(context.getBroker()), signatureDetail);
                if (serviceToDeregister != null) {
                    xqueryRegistry.deregisterService(context.getBroker(), serviceToDeregister);
                    result = BooleanValue.TRUE;
                } else {
                    result = BooleanValue.FALSE;
                }
            } else {
                result = BooleanValue.FALSE;
            }
        } else if (isCalledAs(qnInvalidModules.getLocalPart())) {
            result = serializeInvalidQueries(xqueryRegistry.getInvalidQueries());
        } else if (isCalledAs(qnMissingDependencies.getLocalPart())) {
            result = serializeMissingDependencies(xqueryRegistry.getMissingDependencies());
        } else if (isCalledAs(qnDependencies.getLocalPart())) {
            result = serializeDependenciesTree(xqueryRegistry.getDependenciesTree());
        }
        return result;
    } catch (final PermissionDeniedException pde) {
        throw new XPathException(this, pde.getMessage(), pde);
    } finally {
        context.popDocumentContext();
    }
}
Also used : ExQueryException(org.exquery.ExQueryException) XPathException(org.exist.xquery.XPathException) RestXqServiceRegistry(org.exquery.restxq.RestXqServiceRegistry) ExistXqueryRegistry(org.exist.extensions.exquery.restxq.impl.ExistXqueryRegistry) DocumentImpl(org.exist.dom.persistent.DocumentImpl) RestXqService(org.exquery.restxq.RestXqService) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 9 with RestXqService

use of org.exquery.restxq.RestXqService in project exist by eXist-db.

the class RegistryFunctions method serializeRestXqServices.

/**
 * Serializes RESTXQ Services to an XML description
 *
 * @param builder The receiver for the serialization
 * @param services The services to describe
 *
 * @return The XML Document constructed from serializing the
 * services to the MemTreeBuilder
 */
public static Document serializeRestXqServices(final MemTreeBuilder builder, final Iterable<RestXqService> services) {
    builder.startDocument();
    builder.startElement(RESOURCE_FUNCTIONS, null);
    for (final RestXqService service : services) {
        final ResourceFunction resourceFn = service.getResourceFunction();
        serializeResourceFunction(builder, resourceFn);
    }
    builder.endElement();
    builder.endDocument();
    return builder.getDocument();
}
Also used : RestXqService(org.exquery.restxq.RestXqService) ResourceFunction(org.exquery.restxq.ResourceFunction)

Aggregations

RestXqService (org.exquery.restxq.RestXqService)9 ExQueryException (org.exquery.ExQueryException)5 URI (java.net.URI)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 EXistException (org.exist.EXistException)2 DocumentImpl (org.exist.dom.persistent.DocumentImpl)2 DBBroker (org.exist.storage.DBBroker)2 ResourceFunction (org.exquery.restxq.ResourceFunction)2 LineNumberReader (java.io.LineNumberReader)1 PrintWriter (java.io.PrintWriter)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ServletException (javax.servlet.ServletException)1 TriggerException (org.exist.collections.triggers.TriggerException)1 ExistXqueryRegistry (org.exist.extensions.exquery.restxq.impl.ExistXqueryRegistry)1 AnnotationAdapter (org.exist.extensions.exquery.restxq.impl.adapters.AnnotationAdapter)1 HttpServletRequestAdapter (org.exist.extensions.exquery.restxq.impl.adapters.HttpServletRequestAdapter)1