Search in sources :

Example 1 with XarFileSource

use of org.expath.pkg.repo.XarFileSource in project exist by eXist-db.

the class AutoDeploymentTrigger method execute.

@Override
public void execute(final DBBroker sysBroker, final Txn transaction, final Map<String, List<? extends Object>> params) {
    final boolean ignoreAutodeploySystemProperty = Optional.ofNullable(getFirstParamValue(params, IGNORE_AUTODEPLOY_SYSTEM_PROPERTY_PARAM, v -> Boolean.valueOf(v.toString()))).orElse(false);
    if (!ignoreAutodeploySystemProperty) {
        // do not execute if the system property exist.autodeploy=off
        final String property = System.getProperty(AUTODEPLOY_PROPERTY, "on");
        if (property.equalsIgnoreCase("off")) {
            return;
        }
    }
    Path autodeployDir = Optional.ofNullable(System.getProperty(AUTODEPLOY_DIRECTORY_PROPERTY)).map(Paths::get).orElse(null);
    if (autodeployDir == null) {
        final String dir = getFirstParamValue(params, AUTODEPLOY_DIRECTORY_PARAM, Object::toString);
        if (dir != null) {
            autodeployDir = Paths.get(dir);
        } else {
            final Optional<Path> homeDir = sysBroker.getConfiguration().getExistHome();
            autodeployDir = FileUtils.resolve(homeDir, AUTODEPLOY_DIRECTORY);
        }
    }
    if (!Files.isReadable(autodeployDir) && Files.isDirectory(autodeployDir)) {
        LOG.warn("Unable to read autodeploy directory: {}", autodeployDir);
        return;
    }
    try (Stream<Path> xarsStream = Files.find(autodeployDir, 1, (path, attrs) -> (!attrs.isDirectory()) && FileUtils.fileName(path).endsWith(".xar")).sorted(Comparator.comparing(Path::getFileName))) {
        final List<Path> xars = xarsStream.collect(Collectors.toList());
        LOG.info("Scanning autodeploy directory. Found {} app packages.", xars.size());
        final Deployment deployment = new Deployment();
        // build a map with uri -> file so we can resolve dependencies
        final Map<String, Path> packages = new HashMap<>();
        for (final Path xar : xars) {
            try {
                final Optional<String> name = deployment.getNameFromDescriptor(sysBroker, new XarFileSource(xar));
                if (name.isPresent()) {
                    packages.put(name.get(), xar);
                } else {
                    LOG.error("No descriptor name for: {}", xar.toAbsolutePath().toString());
                }
            } catch (final IOException | PackageException e) {
                LOG.error("Caught exception while reading app package {}", xar.toAbsolutePath().toString(), e);
            }
        }
        final PackageLoader loader = (name, version) -> {
            // TODO: enforce version check
            final Path p = packages.get(name);
            if (p == null) {
                return null;
            }
            return new XarFileSource(p);
        };
        for (final Path xar : xars) {
            try {
                deployment.installAndDeploy(sysBroker, transaction, new XarFileSource(xar), loader, false);
            } catch (final PackageException | IOException e) {
                LOG.error("Exception during deployment of app {}: {}", FileUtils.fileName(xar), e.getMessage(), e);
                sysBroker.getBrokerPool().reportStatus("An error occurred during app deployment: " + e.getMessage());
            }
        }
    } catch (final IOException ioe) {
        LOG.error(ioe);
    }
}
Also used : Path(java.nio.file.Path) Txn(org.exist.storage.txn.Txn) Files(java.nio.file.Files) StartupTrigger(org.exist.storage.StartupTrigger) IOException(java.io.IOException) HashMap(java.util.HashMap) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) FileUtils(org.exist.util.FileUtils) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) PackageException(org.expath.pkg.repo.PackageException) Paths(java.nio.file.Paths) DBBroker(org.exist.storage.DBBroker) Map(java.util.Map) XarFileSource(org.expath.pkg.repo.XarFileSource) Optional(java.util.Optional) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) XarFileSource(org.expath.pkg.repo.XarFileSource) HashMap(java.util.HashMap) IOException(java.io.IOException) PackageException(org.expath.pkg.repo.PackageException)

Aggregations

IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Nullable (javax.annotation.Nullable)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 DBBroker (org.exist.storage.DBBroker)1 StartupTrigger (org.exist.storage.StartupTrigger)1 Txn (org.exist.storage.txn.Txn)1 FileUtils (org.exist.util.FileUtils)1 PackageException (org.expath.pkg.repo.PackageException)1