use of org.expath.pkg.repo.Package in project exist by eXist-db.
the class ExistRepository method resolveXQueryModule.
/**
* Resolve an XQuery Module.
*
* @param namespace the namespace of the module
* @return the path to the module, or null
*
* @throws XPathException with:
* XQST0046 if the namespace URI is invalid
* XQST0059 if an error occurs loading the module
*/
public Path resolveXQueryModule(final String namespace) throws XPathException {
final URI uri;
try {
uri = new URI(namespace);
} catch (final URISyntaxException ex) {
throw new XPathException(ErrorCodes.XQST0046, "Invalid URI: " + namespace, ex);
}
for (final Packages pp : myParent.listPackages()) {
final Package pkg = pp.latest();
// FIXME: Rely on having a file system storage, that's probably a bad design!
final FileSystemResolver resolver = (FileSystemResolver) pkg.getResolver();
final ExistPkgInfo info = (ExistPkgInfo) pkg.getInfo("exist");
if (info != null) {
final String f = info.getXQuery(uri);
if (f != null) {
return resolver.resolveComponentAsFile(f);
}
}
// declared here to be used in catch
String sysid = null;
Source src = null;
try {
src = pkg.resolve(namespace, URISpace.XQUERY);
if (src != null) {
sysid = src.getSystemId();
return Paths.get(new URI(sysid));
}
} catch (final URISyntaxException ex) {
throw new XPathException(ErrorCodes.XQST0046, "Error parsing the URI of the query library: " + sysid, ex);
} catch (final PackageException ex) {
throw new XPathException(ErrorCodes.XQST0059, "Error resolving the query library: " + namespace, ex);
} finally {
if (src != null && src instanceof StreamSource) {
final StreamSource streamSource = ((StreamSource) src);
try {
if (streamSource.getInputStream() != null) {
streamSource.getInputStream().close();
} else if (streamSource.getReader() != null) {
streamSource.getReader().close();
}
} catch (final IOException e) {
LOG.warn("Unable to close pkg source: {}", e.getMessage(), e);
}
}
}
}
return null;
}
use of org.expath.pkg.repo.Package in project exist by eXist-db.
the class InstallFunction method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
Sequence removed = BooleanValue.FALSE;
boolean force = true;
UserInteractionStrategy interact = new BatchUserInteraction();
String pkgOrPath = args[0].getStringValue();
Optional<ExistRepository> repo = getContext().getRepository();
try {
if (repo.isPresent()) {
Repository parent_repo = repo.get().getParentRepo();
Package pkg;
if (isCalledAs("install")) {
// download .xar from a URI
URI uri = _getURI(pkgOrPath);
pkg = parent_repo.installPackage(uri, force, interact);
repo.get().reportAction(ExistRepository.Action.INSTALL, pkg.getName());
} else {
// .xar is stored as a binary resource
try (final LockedDocument lockedDoc = getBinaryDoc(pkgOrPath);
final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
final DocumentImpl doc = lockedDoc.getDocument();
LOG.debug("Installing file: {}", doc.getURI());
pkg = parent_repo.installPackage(new BinaryDocumentXarSource(context.getBroker().getBrokerPool(), transaction, (BinaryDocument) doc), force, interact);
repo.get().reportAction(ExistRepository.Action.INSTALL, pkg.getName());
transaction.commit();
}
}
ExistPkgInfo info = (ExistPkgInfo) pkg.getInfo("exist");
if (info != null && !info.getJars().isEmpty())
ClasspathHelper.updateClasspath(context.getBroker().getBrokerPool(), pkg);
// TODO: expath libs do not provide a way to see if there were any XQuery modules installed at all
context.getBroker().getBrokerPool().getXQueryPool().clear();
removed = BooleanValue.TRUE;
} else {
throw new XPathException("expath repository not available");
}
} catch (PackageException | TransactionException ex) {
logger.error(ex.getMessage(), ex);
return removed;
// /TODO: _repo.removePackage seems to throw PackageException
// throw new XPathException("Problem installing package " + pkg + " in expath repository, check that eXist-db has access permissions to expath repository file directory ", ex);
} catch (XPathException xpe) {
logger.error(xpe.getMessage());
return removed;
}
return removed;
}
use of org.expath.pkg.repo.Package in project exist by eXist-db.
the class GetResource method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
String pkgName = args[0].getStringValue();
String path = args[1].getStringValue();
Optional<ExistRepository> repo = context.getRepository();
if (repo.isPresent()) {
try {
for (Packages pp : repo.get().getParentRepo().listPackages()) {
final Package pkg = pp.latest();
if (pkg.getName().equals(pkgName)) {
try {
// FileSystemResolver already returns an input stream
StreamSource source = (StreamSource) pkg.getResolver().resolveResource(path);
return Base64BinaryDocument.getInstance(context, source.getInputStream());
} catch (Storage.NotExistException e) {
throw new XPathException(ErrorCodes.FODC0002, "Resource " + path + " does not exist.", e);
}
}
}
} catch (PackageException e) {
throw new XPathException(this, ErrorCodes.FOER0000, "Caught package error while reading expath package");
}
} else {
throw new XPathException("expath repository not available");
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.expath.pkg.repo.Package in project exist by eXist-db.
the class Deployment method undeploy.
public Optional<String> undeploy(final DBBroker broker, final Txn transaction, final String pkgName, final Optional<ExistRepository> repo) throws PackageException {
final Optional<Path> maybePackageDir = getPackageDir(pkgName, repo);
if (!maybePackageDir.isPresent()) {
// fails silently if package dir is not found?
return Optional.empty();
}
final Path packageDir = maybePackageDir.get();
final Optional<Package> pkg = getPackage(pkgName, repo);
final DocumentImpl repoXML;
try {
repoXML = getRepoXML(broker, packageDir);
} catch (PackageException e) {
if (pkg.isPresent()) {
uninstall(broker, transaction, pkg.get(), Optional.empty());
}
throw new PackageException("Failed to remove package from database " + "due to error in repo.xml: " + e.getMessage(), e);
}
if (repoXML != null) {
try {
final Optional<ElementImpl> cleanup = findElement(repoXML, CLEANUP_ELEMENT);
if (cleanup.isPresent()) {
runQuery(broker, null, packageDir, cleanup.get().getStringValue(), pkgName, QueryPurpose.UNDEPLOY);
}
final Optional<ElementImpl> target = findElement(repoXML, TARGET_COLL_ELEMENT);
if (pkg.isPresent()) {
uninstall(broker, transaction, pkg.get(), target);
}
return target.map(e -> Optional.ofNullable(e.getStringValue())).orElseGet(() -> Optional.of(getTargetFallback(pkg.get()).getCollectionPath()));
} catch (final XPathException | IOException e) {
throw new PackageException("Error found while processing repo.xml: " + e.getMessage(), e);
}
} else {
// we still may need to remove the copy of the package from /db/system/repo
if (pkg.isPresent()) {
uninstall(broker, transaction, pkg.get(), Optional.empty());
}
}
return Optional.empty();
}
use of org.expath.pkg.repo.Package in project exist by eXist-db.
the class Deployment method uninstall.
/**
* Delete the target collection of the package. If there's no repo.xml descriptor,
* target will be null.
*
* @param pkg
* @param target
* @throws PackageException
*/
private void uninstall(final DBBroker broker, final Txn transaction, final Package pkg, final Optional<ElementImpl> target) throws PackageException {
// determine target collection
final Optional<String> targetPath = target.map(ElementImpl::getStringValue).filter(s -> !s.isEmpty());
final XmldbURI targetCollection = targetPath.map(s -> XmldbURI.create(getTargetCollection(broker, s))).orElseGet(() -> getTargetFallback(pkg));
try {
Collection collection = broker.getOrCreateCollection(transaction, targetCollection);
if (collection != null) {
broker.removeCollection(transaction, collection);
}
if (target != null) {
final XmldbURI configCollection = XmldbURI.CONFIG_COLLECTION_URI.append(targetCollection);
collection = broker.getOrCreateCollection(transaction, configCollection);
if (collection != null) {
broker.removeCollection(transaction, collection);
}
}
} catch (final PermissionDeniedException | IOException | TriggerException e) {
LOG.error("Exception occurred while removing package.", e);
}
}
Aggregations