Search in sources :

Example 6 with Package

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;
}
Also used : XPathException(org.exist.xquery.XPathException) StreamSource(javax.xml.transform.stream.StreamSource) URISyntaxException(java.net.URISyntaxException) Package(org.expath.pkg.repo.Package) IOException(java.io.IOException) URI(java.net.URI) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) FileSystemResolver(org.expath.pkg.repo.FileSystemStorage.FileSystemResolver)

Example 7 with Package

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;
}
Also used : XPathException(org.exist.xquery.XPathException) Sequence(org.exist.xquery.value.Sequence) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI) URI(java.net.URI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) ExistPkgInfo(org.exist.repo.ExistPkgInfo) ExistRepository(org.exist.repo.ExistRepository) TransactionException(org.exist.storage.txn.TransactionException) LockedDocument(org.exist.dom.persistent.LockedDocument) Package(org.expath.pkg.repo.Package) BatchUserInteraction(org.expath.pkg.repo.tui.BatchUserInteraction) ExistRepository(org.exist.repo.ExistRepository)

Example 8 with Package

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;
}
Also used : Storage(org.expath.pkg.repo.Storage) XPathException(org.exist.xquery.XPathException) Packages(org.expath.pkg.repo.Packages) StreamSource(javax.xml.transform.stream.StreamSource) PackageException(org.expath.pkg.repo.PackageException) Package(org.expath.pkg.repo.Package) ExistRepository(org.exist.repo.ExistRepository)

Example 9 with Package

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();
}
Also used : Path(java.nio.file.Path) DependencyVersion(org.expath.pkg.repo.deps.DependencyVersion) Txn(org.exist.storage.txn.Txn) java.util(java.util) BufferedInputStream(java.io.BufferedInputStream) QName(org.exist.dom.QName) SequenceIterator(org.exist.xquery.value.SequenceIterator) PermissionDeniedException(org.exist.security.PermissionDeniedException) org.exist.xquery(org.exist.xquery) DirectoryStream(java.nio.file.DirectoryStream) JarEntry(java.util.jar.JarEntry) org.exist.dom.memtree(org.exist.dom.memtree) Collection(org.exist.collections.Collection) UnixStylePermission(org.exist.security.UnixStylePermission) XmldbURI(org.exist.xmldb.XmldbURI) Attributes(org.xml.sax.Attributes) JarInputStream(java.util.jar.JarInputStream) EXistException(org.exist.EXistException) DocUtils(org.exist.xquery.util.DocUtils) DateTimeValue(org.exist.xquery.value.DateTimeValue) SystemProperties(org.exist.SystemProperties) Path(java.nio.file.Path) Permission(org.exist.security.Permission) Nullable(javax.annotation.Nullable) BatchUserInteraction(org.expath.pkg.repo.tui.BatchUserInteraction) PermissionFactory(org.exist.security.PermissionFactory) InputSource(org.xml.sax.InputSource) Files(java.nio.file.Files) GroupAider(org.exist.security.internal.aider.GroupAider) Type(org.exist.xquery.value.Type) FileSource(org.exist.source.FileSource) IOException(java.io.IOException) UserAider(org.exist.security.internal.aider.UserAider) Either(com.evolvedbinary.j8fu.Either) org.expath.pkg.repo(org.expath.pkg.repo) Logger(org.apache.logging.log4j.Logger) Element(org.w3c.dom.Element) Stream(java.util.stream.Stream) DBBroker(org.exist.storage.DBBroker) SAXException(org.xml.sax.SAXException) org.exist.util(org.exist.util) Sequence(org.exist.xquery.value.Sequence) TriggerException(org.exist.collections.triggers.TriggerException) LogManager(org.apache.logging.log4j.LogManager) Package(org.expath.pkg.repo.Package) AttrList(org.exist.util.serializer.AttrList) InputStream(java.io.InputStream) Package(org.expath.pkg.repo.Package) IOException(java.io.IOException)

Example 10 with Package

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);
    }
}
Also used : DependencyVersion(org.expath.pkg.repo.deps.DependencyVersion) Txn(org.exist.storage.txn.Txn) java.util(java.util) BufferedInputStream(java.io.BufferedInputStream) QName(org.exist.dom.QName) SequenceIterator(org.exist.xquery.value.SequenceIterator) PermissionDeniedException(org.exist.security.PermissionDeniedException) org.exist.xquery(org.exist.xquery) DirectoryStream(java.nio.file.DirectoryStream) JarEntry(java.util.jar.JarEntry) org.exist.dom.memtree(org.exist.dom.memtree) Collection(org.exist.collections.Collection) UnixStylePermission(org.exist.security.UnixStylePermission) XmldbURI(org.exist.xmldb.XmldbURI) Attributes(org.xml.sax.Attributes) JarInputStream(java.util.jar.JarInputStream) EXistException(org.exist.EXistException) DocUtils(org.exist.xquery.util.DocUtils) DateTimeValue(org.exist.xquery.value.DateTimeValue) SystemProperties(org.exist.SystemProperties) Path(java.nio.file.Path) Permission(org.exist.security.Permission) Nullable(javax.annotation.Nullable) BatchUserInteraction(org.expath.pkg.repo.tui.BatchUserInteraction) PermissionFactory(org.exist.security.PermissionFactory) InputSource(org.xml.sax.InputSource) Files(java.nio.file.Files) GroupAider(org.exist.security.internal.aider.GroupAider) Type(org.exist.xquery.value.Type) FileSource(org.exist.source.FileSource) IOException(java.io.IOException) UserAider(org.exist.security.internal.aider.UserAider) Either(com.evolvedbinary.j8fu.Either) org.expath.pkg.repo(org.expath.pkg.repo) Logger(org.apache.logging.log4j.Logger) Element(org.w3c.dom.Element) Stream(java.util.stream.Stream) DBBroker(org.exist.storage.DBBroker) SAXException(org.xml.sax.SAXException) org.exist.util(org.exist.util) Sequence(org.exist.xquery.value.Sequence) TriggerException(org.exist.collections.triggers.TriggerException) LogManager(org.apache.logging.log4j.LogManager) Package(org.expath.pkg.repo.Package) AttrList(org.exist.util.serializer.AttrList) InputStream(java.io.InputStream) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

Package (org.expath.pkg.repo.Package)13 IOException (java.io.IOException)6 Path (java.nio.file.Path)5 BatchUserInteraction (org.expath.pkg.repo.tui.BatchUserInteraction)5 URI (java.net.URI)4 Txn (org.exist.storage.txn.Txn)4 Either (com.evolvedbinary.j8fu.Either)3 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3 DirectoryStream (java.nio.file.DirectoryStream)3 Files (java.nio.file.Files)3 java.util (java.util)3 JarEntry (java.util.jar.JarEntry)3 JarInputStream (java.util.jar.JarInputStream)3 Stream (java.util.stream.Stream)3 Nullable (javax.annotation.Nullable)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 EXistException (org.exist.EXistException)3 SystemProperties (org.exist.SystemProperties)3