Search in sources :

Example 1 with SystemImport

use of org.exist.backup.SystemImport in project exist by eXist-db.

the class FnImport method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (!context.getSubject().hasDbaRole()) {
        throw (new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to kill a running xquery"));
    }
    final String dirOrFile = args[0].getStringValue();
    String adminPass = null;
    if (args[1].hasOne()) {
        adminPass = args[1].getStringValue();
    }
    String adminPassAfter = null;
    if (args[2].hasOne()) {
        adminPassAfter = args[2].getStringValue();
    }
    MemTreeBuilder builder = null;
    context.pushDocumentContext();
    try {
        if (NAME.equals(getName())) {
            builder = context.getDocumentBuilder();
            builder.startDocument();
            builder.startElement(IMPORT_ELEMENT, null);
        }
        try {
            final SystemImport restore = new SystemImport(context.getDatabase());
            final RestoreListener listener = new XMLRestoreListener(builder);
            restore.restore(org.exist.security.SecurityManager.DBA_USER, adminPass, adminPassAfter, Paths.get(dirOrFile), listener);
        } catch (final Exception e) {
            throw new XPathException(this, "restore failed with exception: " + e.getMessage(), e);
        }
        if (builder == null) {
            return Sequence.EMPTY_SEQUENCE;
        } else {
            builder.endElement();
            builder.endDocument();
            return (NodeValue) builder.getDocument().getDocumentElement();
        }
    } finally {
        if (builder != null) {
            context.popDocumentContext();
        }
    }
}
Also used : SystemImport(org.exist.backup.SystemImport) NodeValue(org.exist.xquery.value.NodeValue) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) XPathException(org.exist.xquery.XPathException) RestoreListener(org.exist.backup.restore.listener.RestoreListener) AbstractRestoreListener(org.exist.backup.restore.listener.AbstractRestoreListener) XPathException(org.exist.xquery.XPathException)

Aggregations

SystemImport (org.exist.backup.SystemImport)1 AbstractRestoreListener (org.exist.backup.restore.listener.AbstractRestoreListener)1 RestoreListener (org.exist.backup.restore.listener.RestoreListener)1 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)1 XPathException (org.exist.xquery.XPathException)1 NodeValue (org.exist.xquery.value.NodeValue)1