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);
}
}
Aggregations