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();
}
}
Aggregations