Search in sources :

Example 1 with FunctionSignature

use of org.exquery.xquery3.FunctionSignature in project exist by eXist-db.

the class RestXqServiceRegistryPersistence method updateRegistryOnDisk.

private synchronized void updateRegistryOnDisk(final RestXqService restXqService, final UpdateAction updateAction) {
    // we can ignore the change in service provided to this function as args, as we just write the details of all
    // services to disk, overwriting the old registry
    final Optional<Path> optTmpNewRegistry = getRegistryFile(true);
    if (!optTmpNewRegistry.isPresent()) {
        log.error("Could not save RESTXQ Registry to disk!");
    } else {
        final Path tmpNewRegistry = optTmpNewRegistry.get();
        log.info("Preparing new RESTXQ registry on disk: {}", tmpNewRegistry.toAbsolutePath().toString());
        try {
            try (final PrintWriter writer = new PrintWriter(Files.newBufferedWriter(tmpNewRegistry, StandardOpenOption.TRUNCATE_EXISTING))) {
                writer.println(VERSION_LABEL + LABEL_SEP + REGISTRY_FILE_VERSION);
                // get details of RESTXQ functions in XQuery modules
                final Map<URI, List<FunctionSignature>> xqueryServices = new HashMap<>();
                for (final RestXqService service : getRegistry()) {
                    List<FunctionSignature> fnNames = xqueryServices.get(service.getResourceFunction().getXQueryLocation());
                    if (fnNames == null) {
                        fnNames = new ArrayList<>();
                    }
                    fnNames.add(service.getResourceFunction().getFunctionSignature());
                    xqueryServices.put(service.getResourceFunction().getXQueryLocation(), fnNames);
                }
                // iterate and save to disk
                for (final Entry<URI, List<FunctionSignature>> xqueryServiceFunctions : xqueryServices.entrySet()) {
                    writer.print(xqueryServiceFunctions.getKey() + FIELD_SEP);
                    final List<FunctionSignature> fnSigs = xqueryServiceFunctions.getValue();
                    for (final FunctionSignature fnSig : fnSigs) {
                        writer.print(qnameToClarkNotation(fnSig.getName()) + ARITY_SEP + fnSig.getArgumentCount());
                    }
                    writer.println();
                }
            }
            final Optional<Path> optRegistry = getRegistryFile(false);
            if (optRegistry.isPresent()) {
                final Path registry = optRegistry.get();
                // replace the original registry with the new registry
                final Path localTmpNewRegistry = Files.copy(tmpNewRegistry, registry.getParent().resolve(tmpNewRegistry.getFileName()));
                Files.move(localTmpNewRegistry, registry, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
                log.info("Replaced RESTXQ registry: {} -> {}", FileUtils.fileName(tmpNewRegistry), FileUtils.fileName(registry));
            } else {
                throw new IOException("Unable to retrieve existing RESTXQ registry");
            }
        } catch (final IOException ioe) {
            log.error(ioe.getMessage(), ioe);
        } finally {
            TemporaryFileManager.getInstance().returnTemporaryFile(tmpNewRegistry);
        }
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) URI(java.net.URI) FunctionSignature(org.exquery.xquery3.FunctionSignature) RestXqService(org.exquery.restxq.RestXqService) PrintWriter(java.io.PrintWriter)

Aggregations

IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 RestXqService (org.exquery.restxq.RestXqService)1 FunctionSignature (org.exquery.xquery3.FunctionSignature)1