Search in sources :

Example 1 with ConsoleRestoreServiceTaskListener

use of org.exist.xmldb.ConsoleRestoreServiceTaskListener in project exist by eXist-db.

the class RestoreTask method execute.

@Override
public void execute() throws BuildException {
    if (uri == null) {
        throw (new BuildException("You have to specify an XMLDB collection URI"));
    }
    if ((dir == null) && (dirSet == null) && (zipFile == null)) {
        throw (new BuildException("Missing required argument: either dir, dirset or file required"));
    }
    if ((dir != null) && !Files.isReadable(dir)) {
        final String msg = "Cannot read restore file: " + dir.toAbsolutePath().toString();
        if (failonerror) {
            throw (new BuildException(msg));
        } else {
            log(msg, Project.MSG_ERR);
        }
    } else {
        registerDatabase();
        try {
            if (dir != null) {
                log("Restoring from " + dir.toAbsolutePath().toString(), Project.MSG_INFO);
                final Path file = dir.resolve("__contents__.xml");
                if (!Files.exists(file)) {
                    final String msg = "Could not find file " + file.toAbsolutePath().toString();
                    if (failonerror) {
                        throw (new BuildException(msg));
                    } else {
                        log(msg, Project.MSG_ERR);
                    }
                } else {
                    final RestoreServiceTaskListener listener = new ConsoleRestoreServiceTaskListener();
                    final Collection collection = DatabaseManager.getCollection(uri, user, password);
                    final EXistRestoreService service = (EXistRestoreService) collection.getService("RestoreService", "1.0");
                    service.restore(file.normalize().toAbsolutePath().toString(), restorePassword, listener, overwriteApps);
                }
            } else if (dirSet != null) {
                final DirectoryScanner scanner = dirSet.getDirectoryScanner(getProject());
                scanner.scan();
                final String[] includedFiles = scanner.getIncludedFiles();
                log("Found " + includedFiles.length + " files.\n");
                for (final String included : includedFiles) {
                    dir = scanner.getBasedir().toPath().resolve(included);
                    final Path contentsFile = dir.resolve("__contents__.xml");
                    if (!Files.exists(contentsFile)) {
                        final String msg = "Did not found file " + contentsFile.toAbsolutePath().toString();
                        if (failonerror) {
                            throw (new BuildException(msg));
                        } else {
                            log(msg, Project.MSG_ERR);
                        }
                    } else {
                        log("Restoring from " + contentsFile.toAbsolutePath().toString() + " ...\n");
                        // TODO subdirectories as sub-collections?
                        final RestoreServiceTaskListener listener = new ConsoleRestoreServiceTaskListener();
                        final Collection collection = DatabaseManager.getCollection(uri, user, password);
                        final EXistRestoreService service = (EXistRestoreService) collection.getService("RestoreService", "1.0");
                        service.restore(contentsFile.normalize().toAbsolutePath().toString(), restorePassword, listener, overwriteApps);
                    }
                }
            } else if (zipFile != null) {
                log("Restoring from " + zipFile.toAbsolutePath().toString(), Project.MSG_INFO);
                if (!Files.exists(zipFile)) {
                    final String msg = "File not found: " + zipFile.toAbsolutePath().toString();
                    if (failonerror) {
                        throw (new BuildException(msg));
                    } else {
                        log(msg, Project.MSG_ERR);
                    }
                } else {
                    final RestoreServiceTaskListener listener = new ConsoleRestoreServiceTaskListener();
                    final Collection collection = DatabaseManager.getCollection(uri, user, password);
                    final EXistRestoreService service = (EXistRestoreService) collection.getService("RestoreService", "1.0");
                    service.restore(zipFile.normalize().toAbsolutePath().toString(), restorePassword, listener, overwriteApps);
                }
            }
        } catch (final Exception e) {
            e.printStackTrace();
            final String msg = "Exception during restore: " + e.getMessage();
            if (failonerror) {
                throw new BuildException(msg, e);
            } else {
                log(msg, e, Project.MSG_ERR);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) EXistRestoreService(org.exist.xmldb.EXistRestoreService) ConsoleRestoreServiceTaskListener(org.exist.xmldb.ConsoleRestoreServiceTaskListener) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) Collection(org.xmldb.api.base.Collection) BuildException(org.apache.tools.ant.BuildException) ConsoleRestoreServiceTaskListener(org.exist.xmldb.ConsoleRestoreServiceTaskListener) RestoreServiceTaskListener(org.exist.xmldb.RestoreServiceTaskListener) BuildException(org.apache.tools.ant.BuildException)

Aggregations

Path (java.nio.file.Path)1 BuildException (org.apache.tools.ant.BuildException)1 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)1 ConsoleRestoreServiceTaskListener (org.exist.xmldb.ConsoleRestoreServiceTaskListener)1 EXistRestoreService (org.exist.xmldb.EXistRestoreService)1 RestoreServiceTaskListener (org.exist.xmldb.RestoreServiceTaskListener)1 Collection (org.xmldb.api.base.Collection)1