use of org.expath.pkg.repo.PackageException in project exist by eXist-db.
the class AppRestoreUtils method checkApps.
/**
* Inspects the apps contained in the backup against installed apps in the database
* and return a set of symbolic backup paths pointing to the collection of those
* apps for which newer versions are installed within the database. The returned
* paths may then be ignored during a restore.
*
* The method attempts to be fail safe to make sure even bad backups can be restored. Errors
* reading package descriptors are thus only logged and should not abort the process.
*
* @param broker the broker used for reading the backup and retrieving the expath repo
* @param descriptors a queue of backup descriptors to inspect
* @return a set of paths for which newer versions exist in the database. may be empty.
*/
public static Set<String> checkApps(final DBBroker broker, final Deque<BackupDescriptor> descriptors) {
final List<AppDetail> apps = getAppsFromBackup(broker, descriptors);
final Set<String> paths = new HashSet<>();
final Optional<ExistRepository> repo = broker.getBrokerPool().getExpathRepo();
if (repo.isPresent()) {
for (final AppDetail app : apps) {
final Packages packages = repo.get().getParentRepo().getPackages(app.name);
if (packages != null) {
final Package latest = packages.latest();
try {
final Semver version = Semver.parse(latest.getVersion());
if (version.compareTo(app.version) > 0) {
paths.add(app.path);
}
} catch (PackageException e) {
LOG.warn("Invalid semver in expath repository for {}", app.name, e);
}
}
}
}
return paths;
}
use of org.expath.pkg.repo.PackageException in project exist by eXist-db.
the class AppRestoreUtils method getAppsFromSubColl.
private static void getAppsFromSubColl(final List<AppDetail> result, final XMLReaderPool parserPool, final BackupDescriptor descriptor) {
final List<String> collections = getSubcollectionNames(parserPool, descriptor);
for (final String collection : collections) {
final BackupDescriptor app = descriptor.getChildBackupDescriptor(collection);
final InputSource is = app.getInputSource("expath-pkg.xml");
if (is != null) {
XMLReader reader = null;
try {
reader = parserPool.borrowXMLReader();
reader.setContentHandler(new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (PKG_NAMESPACE.equals(uri) && "package".equals(localName)) {
final String version = attributes.getValue("version");
final String name = attributes.getValue("name");
if (StringUtils.isEmpty(version) || StringUtils.isEmpty(name)) {
LOG.warn("Invalid package descriptor for {}", app.getSymbolicPath());
return;
}
try {
final AppDetail detail = new AppDetail(app.getSymbolicPath(), name, Semver.parse(version));
result.add(detail);
} catch (PackageException e) {
LOG.warn("Invalid semver found while parsing {}", app.getSymbolicPath());
}
}
}
});
reader.parse(is);
} catch (IOException | SAXException e) {
LOG.warn("Parse exception while parsing {}", app.getSymbolicPath("expath-pkg.xml", false));
} finally {
if (reader != null) {
parserPool.returnXMLReader(reader);
}
}
}
}
}
use of org.expath.pkg.repo.PackageException in project exist by eXist-db.
the class Deploy method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (!context.getSubject().hasDbaRole())
throw new XPathException(this, EXPathErrorCode.EXPDY003, "Permission denied. You need to be a member " + "of the dba group to use repo:deploy/undeploy");
final String pkgName = args[0].getStringValue();
try {
Deployment deployment = new Deployment();
final Optional<String> target;
if (isCalledAs("deploy")) {
String userTarget = null;
if (getArgumentCount() == 2) {
userTarget = args[1].getStringValue();
}
try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
target = deployment.deploy(context.getBroker(), transaction, pkgName, context.getRepository(), userTarget);
transaction.commit();
}
} else if (isCalledAs("install-and-deploy")) {
String version = null;
final String repoURI;
if (getArgumentCount() == 3) {
version = args[1].getStringValue();
repoURI = args[2].getStringValue();
} else {
repoURI = args[1].getStringValue();
}
try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
target = installAndDeploy(transaction, pkgName, version, repoURI);
transaction.commit();
}
} else if (isCalledAs("install-and-deploy-from-db")) {
String repoURI = null;
if (getArgumentCount() == 2) {
repoURI = args[1].getStringValue();
}
try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
target = installAndDeployFromDb(transaction, pkgName, repoURI);
transaction.commit();
}
} else {
try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
target = deployment.undeploy(context.getBroker(), transaction, pkgName, context.getRepository());
transaction.commit();
}
}
target.orElseThrow(() -> new XPathException("expath repository is not available."));
return statusReport(target);
} catch (PackageException e) {
throw new XPathException(this, EXPathErrorCode.EXPDY001, e.getMessage(), args[0], e);
} catch (IOException e) {
throw new XPathException(this, ErrorCodes.FOER0000, "Caught IO error while deploying expath archive", args[0], e);
} catch (TransactionException e) {
throw new XPathException(this, ErrorCodes.FOER0000, "Caught transaction error while deploying expath archive", args[0], e);
}
}
use of org.expath.pkg.repo.PackageException in project exist by eXist-db.
the class Deploy method installAndDeploy.
private Optional<String> installAndDeploy(final Txn transaction, final String pkgName, final String version, final String repoURI) throws XPathException {
try {
final RepoPackageLoader loader = new RepoPackageLoader(repoURI);
final Deployment deployment = new Deployment();
final XarSource xar = loader.load(pkgName, new PackageLoader.Version(version, false));
if (xar != null) {
return deployment.installAndDeploy(context.getBroker(), transaction, xar, loader);
}
return Optional.empty();
} catch (final MalformedURLException e) {
throw new XPathException(this, EXPathErrorCode.EXPDY005, "Malformed URL: " + repoURI);
} catch (final PackageException | IOException e) {
LOG.error(e.getMessage(), e);
throw new XPathException(this, EXPathErrorCode.EXPDY007, e.getMessage());
}
}
use of org.expath.pkg.repo.PackageException in project exist by eXist-db.
the class Deploy method installAndDeployFromDb.
private Optional<String> installAndDeployFromDb(final Txn transaction, final String path, final String repoURI) throws XPathException {
final XmldbURI docPath = XmldbURI.createInternal(path);
try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(docPath, LockMode.READ_LOCK)) {
if (lockedDoc == null) {
throw new XPathException(this, EXPathErrorCode.EXPDY001, path + " no such .xar", new StringValue(path));
}
final DocumentImpl doc = lockedDoc.getDocument();
if (doc.getResourceType() != DocumentImpl.BINARY_FILE) {
throw new XPathException(this, EXPathErrorCode.EXPDY001, path + " is not a valid .xar", new StringValue(path));
}
RepoPackageLoader loader = null;
if (repoURI != null) {
loader = new RepoPackageLoader(repoURI);
}
final XarSource xarSource = new BinaryDocumentXarSource(context.getBroker().getBrokerPool(), transaction, (BinaryDocument) doc);
final Deployment deployment = new Deployment();
return deployment.installAndDeploy(context.getBroker(), transaction, xarSource, loader);
} catch (PackageException | IOException | PermissionDeniedException e) {
LOG.error(e.getMessage(), e);
throw new XPathException(this, EXPathErrorCode.EXPDY007, "Package installation failed: " + e.getMessage(), new StringValue(e.getMessage()));
}
}
Aggregations