Search in sources :

Example 1 with FileSystemBackupDescriptor

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

the class ListBackups method eval.

public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (!context.getEffectiveUser().hasDbaRole()) {
        throw new XPathException("You must be a DBA to list available backups");
    }
    final String exportDir = args[0].getStringValue();
    Path dir = Paths.get(exportDir);
    if (!dir.isAbsolute()) {
        dir = ((Path) context.getBroker().getConfiguration().getProperty(BrokerPool.PROPERTY_DATA_DIR)).resolve(exportDir);
    }
    context.pushDocumentContext();
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        final int nodeNr = builder.startElement(DIRECTORY_ELEMENT, null);
        if (Files.isDirectory(dir) && Files.isReadable(dir)) {
            final Pattern pattern = Pattern.compile(BackupDirectory.FILE_REGEX);
            final Matcher matcher = pattern.matcher("");
            final List<Path> files = FileUtils.list(dir);
            for (final Path file : files) {
                matcher.reset(FileUtils.fileName(file));
                if (matcher.matches()) {
                    final BackupDescriptor descriptor;
                    try {
                        if (FileUtils.fileName(file).endsWith(".zip")) {
                            descriptor = new ZipArchiveBackupDescriptor(file);
                        } else {
                            final Path descriptorFile = file.resolve("db").resolve(BackupDescriptor.COLLECTION_DESCRIPTOR);
                            descriptor = new FileSystemBackupDescriptor(file, descriptorFile);
                        }
                        final Properties properties = descriptor.getProperties();
                        if (properties != null) {
                            final AttributesImpl attrs = new AttributesImpl();
                            attrs.addAttribute("", "file", "file", "CDATA", FileUtils.fileName(file));
                            builder.startElement(BACKUP_ELEMENT, attrs);
                            for (final Object o : properties.keySet()) {
                                final String key = o.toString();
                                builder.startElement(new QName(key, Namespaces.EXIST_NS, ""), null);
                                builder.characters((String) properties.get(key));
                                builder.endElement();
                            }
                            builder.endElement();
                        }
                    } catch (final IOException e) {
                    }
                }
            }
        }
        builder.endElement();
        return (builder.getDocument().getNode(nodeNr));
    } catch (final IOException ioe) {
        throw new XPathException(this, ioe);
    } finally {
        context.popDocumentContext();
    }
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) ZipArchiveBackupDescriptor(org.exist.backup.ZipArchiveBackupDescriptor) Matcher(java.util.regex.Matcher) QName(org.exist.dom.QName) IOException(java.io.IOException) Properties(java.util.Properties) FileSystemBackupDescriptor(org.exist.backup.FileSystemBackupDescriptor) ZipArchiveBackupDescriptor(org.exist.backup.ZipArchiveBackupDescriptor) BackupDescriptor(org.exist.backup.BackupDescriptor) AttributesImpl(org.xml.sax.helpers.AttributesImpl) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) FileSystemBackupDescriptor(org.exist.backup.FileSystemBackupDescriptor)

Aggregations

IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Properties (java.util.Properties)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 BackupDescriptor (org.exist.backup.BackupDescriptor)1 FileSystemBackupDescriptor (org.exist.backup.FileSystemBackupDescriptor)1 ZipArchiveBackupDescriptor (org.exist.backup.ZipArchiveBackupDescriptor)1 QName (org.exist.dom.QName)1 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1