Search in sources :

Example 11 with Collection

use of org.xmldb.api.base.Collection in project exist by eXist-db.

the class Main method restoreWithGui.

private static void restoreWithGui(final String username, final String password, final Optional<String> dbaPassword, final Path f, final XmldbURI uri, boolean overwriteApps) {
    final GuiRestoreServiceTaskListener listener = new GuiRestoreServiceTaskListener();
    listener.info("Connecting ...");
    final Callable<Void> callable = () -> {
        try {
            final Collection collection = DatabaseManager.getCollection(uri.toString(), username, password);
            final EXistRestoreService service = (EXistRestoreService) collection.getService("RestoreService", "1.0");
            service.restore(f.toAbsolutePath().toString(), dbaPassword.orElse(null), listener, overwriteApps);
            listener.enableDismissDialogButton();
            if (JOptionPane.showConfirmDialog(null, "Would you like to rebuild the application repository?\nThis is only necessary if application packages were restored.", "Rebuild App Repository?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                System.out.println("Rebuilding application repository ...");
                try {
                    final Collection root = DatabaseManager.getCollection(uri.toString(), username, dbaPassword.orElse(password));
                    ClientFrame.repairRepository(root);
                    listener.info("Application repository rebuilt successfully.");
                } catch (final XMLDBException e) {
                    reportError(e);
                    listener.info("Rebuilding application repository failed!");
                }
            }
        } catch (final Exception e) {
            // $NON-NLS-1$
            ClientFrame.showErrorMessage(e.getMessage(), null);
        } finally {
            if (listener.hasProblems()) {
                ClientFrame.showErrorMessage(listener.getAllProblems(), null);
            }
        }
        return null;
    };
    final ExecutorService executor = Executors.newSingleThreadExecutor(new NamedThreadFactory(null, null, "backup.restore-with-gui"));
    final Future<Void> future = executor.submit(callable);
    while (!future.isDone() && !future.isCancelled()) {
        try {
            future.get(100, TimeUnit.MILLISECONDS);
        } catch (final InterruptedException | TimeoutException ie) {
        } catch (final ExecutionException ee) {
            break;
        }
    }
}
Also used : NamedThreadFactory(org.exist.util.NamedThreadFactory) XMLDBException(org.xmldb.api.base.XMLDBException) StartException(org.exist.start.StartException) XMLDBException(org.xmldb.api.base.XMLDBException) IOException(java.io.IOException) Collection(org.xmldb.api.base.Collection)

Example 12 with Collection

use of org.xmldb.api.base.Collection in project exist by eXist-db.

the class CreateBackupDialog method getAllCollections.

private void getAllCollections(final Collection collection, final Vector<String> collections) throws XMLDBException {
    collections.add(collection.getName());
    final String[] childCollections = collection.listChildCollections();
    Collection child = null;
    for (final String childCollection : childCollections) {
        try {
            child = collection.getChildCollection(childCollection);
        } catch (final XMLDBException xmldbe) {
            if (xmldbe.getCause() instanceof PermissionDeniedException) {
                continue;
            } else {
                throw xmldbe;
            }
        } catch (final Exception npe) {
            System.out.println("Corrupted resource/collection skipped: " + child != null ? child.getName() != null ? child.getName() : "unknown" : "unknown");
            continue;
        }
        try {
            getAllCollections(child, collections);
        } catch (final Exception ee) {
            System.out.println("Corrupted resource/collection skipped: " + child != null ? child.getName() != null ? child.getName() : "unknown" : "unknown");
            continue;
        }
    }
}
Also used : Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) PermissionDeniedException(org.exist.security.PermissionDeniedException) XMLDBException(org.xmldb.api.base.XMLDBException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 13 with Collection

use of org.xmldb.api.base.Collection in project exist by eXist-db.

the class IndexDialog method getCollections.

// THIS IS A COPY FROM ClientFrame
// TODO: share this code between the two classes
private ArrayList getCollections(Collection root, ArrayList collectionsList) throws XMLDBException {
    collectionsList.add(new PrettyXmldbURI(XmldbURI.create(root.getName())));
    final String[] childCollections = root.listChildCollections();
    Collection child;
    for (String childCollection : childCollections) {
        child = root.getChildCollection(childCollection);
        getCollections(child, collectionsList);
    }
    return collectionsList;
}
Also used : Collection(org.xmldb.api.base.Collection)

Example 14 with Collection

use of org.xmldb.api.base.Collection in project exist by eXist-db.

the class AbstractXMLDBTask method mkcol.

protected final Collection mkcol(final Collection rootCollection, final String baseURI, String path, final String relPath) throws XMLDBException {
    CollectionManagementService mgtService;
    Collection current = rootCollection;
    Collection collection;
    String token;
    // /TODO : use dedicated function in XmldbURI
    final StringTokenizer tokenizer = new StringTokenizer(relPath, "/");
    while (tokenizer.hasMoreTokens()) {
        token = tokenizer.nextToken();
        if (path != null) {
            path = path + "/" + token;
        } else {
            path = "/" + token;
        }
        log("Get collection " + baseURI + path, Project.MSG_DEBUG);
        collection = DatabaseManager.getCollection(baseURI + path, user, password);
        if (collection == null) {
            log("Create collection management service for collection " + current.getName(), Project.MSG_DEBUG);
            mgtService = (CollectionManagementService) current.getService("CollectionManagementService", "1.0");
            log("Create child collection " + token, Project.MSG_DEBUG);
            current = mgtService.createCollection(token);
            log("Created collection " + current.getName() + '.', Project.MSG_DEBUG);
        } else {
            current = collection;
        }
    }
    return (current);
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) StringTokenizer(java.util.StringTokenizer) Collection(org.xmldb.api.base.Collection)

Example 15 with Collection

use of org.xmldb.api.base.Collection 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

Collection (org.xmldb.api.base.Collection)345 XMLResource (org.xmldb.api.modules.XMLResource)140 Test (org.junit.Test)115 Resource (org.xmldb.api.base.Resource)111 UserManagementService (org.exist.xmldb.UserManagementService)91 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)85 BinaryResource (org.xmldb.api.modules.BinaryResource)80 XMLDBException (org.xmldb.api.base.XMLDBException)68 ResourceSet (org.xmldb.api.base.ResourceSet)55 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)48 XPathQueryService (org.xmldb.api.modules.XPathQueryService)31 EXistResource (org.exist.xmldb.EXistResource)25 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)20 Before (org.junit.Before)20 URISyntaxException (java.net.URISyntaxException)18 Path (java.nio.file.Path)18 InputStream (java.io.InputStream)17 BuildException (org.apache.tools.ant.BuildException)14 XmldbURI (org.exist.xmldb.XmldbURI)13 Account (org.exist.security.Account)10