Search in sources :

Example 1 with DBBroker

use of org.exist.storage.DBBroker in project exist by eXist-db.

the class ExportGUI method exportDB.

// GEN-LAST:event_btnConfSelectActionPerformed
private void exportDB(final String exportTarget, final List<ErrorReport> errorList) {
    if (!startDB()) {
        return;
    }
    try {
        final SystemExport.StatusCallback callback = new SystemExport.StatusCallback() {

            public void startCollection(final String path) {
                progress.setString(path);
            }

            public void startDocument(final String name, final int current, final int count) {
                progress.setString(name);
                progress.setValue(progress.getValue() + 1);
            }

            public void error(final String message, final Throwable exception) {
                displayMessage(message);
                if (exception != null) {
                    displayMessage(exception.toString());
                }
                displayMessage("---------------------------------------------------");
            }
        };
        progress.setIndeterminate(false);
        progress.setValue(0);
        progress.setStringPainted(true);
        progress.setMinimum(0);
        progress.setMaximum(documentCount);
        Object[] selected = directAccessBtn.getSelectedObjects();
        final boolean directAccess = (selected != null) && (selected[0] != null);
        selected = incrementalBtn.getSelectedObjects();
        final boolean incremental = (selected != null) && (selected[0] != null);
        selected = zipBtn.getSelectedObjects();
        final boolean zip = (selected != null) && (selected[0] != null);
        displayMessage("Starting export ...");
        final long start = System.currentTimeMillis();
        try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
            final Txn transaction = pool.getTransactionManager().beginTransaction()) {
            final SystemExport sysexport = new SystemExport(broker, transaction, callback, null, directAccess);
            final Path file = sysexport.export(exportTarget, incremental, zip, errorList);
            transaction.commit();
            final long end = System.currentTimeMillis();
            displayMessage("Export to " + file.toAbsolutePath().toString() + " completed successfully.");
            displayMessage("Export took " + (end - start) + "ms.");
        } catch (final EXistException e) {
            System.err.println("ERROR: Failed to retrieve database broker: " + e.getMessage());
        }
    } finally {
        progress.setString("");
        progress.setValue(0);
        currentTask.setText(" ");
    }
}
Also used : Path(java.nio.file.Path) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) DBBroker(org.exist.storage.DBBroker)

Example 2 with DBBroker

use of org.exist.storage.DBBroker in project exist by eXist-db.

the class ExportMain method process.

private static void process(final ParsedArguments arguments) {
    final boolean verbose = getBool(arguments, verboseArg);
    final boolean noCheck = getBool(arguments, noCheckArg);
    final boolean checkDocs = getBool(arguments, checkDocsArg);
    final boolean direct = getBool(arguments, directAccessArg);
    boolean export = getBool(arguments, exportArg);
    final boolean noExport = getBool(arguments, noExportArg);
    if (noExport) {
        export = false;
    }
    final boolean incremental = getBool(arguments, incrementalArg);
    boolean zip = getBool(arguments, zipArg);
    final boolean noZip = getBool(arguments, noZipArg);
    if (noZip) {
        zip = false;
    }
    final Optional<Path> dbConfig = getOpt(arguments, configArg).map(File::toPath);
    final Path exportTarget = arguments.get(outputDirArg).toPath();
    final BrokerPool pool = startDB(dbConfig);
    if (pool == null) {
        System.exit(SystemExitCodes.CATCH_ALL_GENERAL_ERROR_EXIT_CODE);
    }
    // return value
    int retval = 0;
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        List<ErrorReport> errors = null;
        if (!noCheck) {
            final ConsistencyCheck checker = new ConsistencyCheck(broker, transaction, direct, checkDocs);
            errors = checker.checkAll(new CheckCallback());
        }
        if (errors != null && !errors.isEmpty()) {
            System.err.println("ERRORS FOUND.");
            retval = 1;
        } else {
            System.out.println("No errors.");
        }
        if (export) {
            if (!Files.exists(exportTarget)) {
                Files.createDirectories(exportTarget);
            } else if (!Files.isDirectory(exportTarget)) {
                System.err.println("Output dir already exists and is a file: " + exportTarget.toAbsolutePath().toString());
                System.exit(SystemExitCodes.INVALID_ARGUMENT_EXIT_CODE);
            }
            final SystemExport sysexport = new SystemExport(broker, transaction, new Callback(verbose), null, direct);
            sysexport.export(exportTarget.toAbsolutePath().toString(), incremental, zip, errors);
        }
        transaction.commit();
    } catch (final EXistException e) {
        System.err.println("ERROR: Failed to retrieve database broker: " + e.getMessage());
        retval = SystemExitCodes.NO_BROKER_EXIT_CODE;
    } catch (final TerminatedException e) {
        System.err.println("WARN: Export was terminated by db.");
        retval = SystemExitCodes.TERMINATED_EARLY_EXIT_CODE;
    } catch (final PermissionDeniedException pde) {
        System.err.println("ERROR: Failed to retrieve database data: " + pde.getMessage());
        retval = SystemExitCodes.PERMISSION_DENIED_EXIT_CODE;
    } catch (final IOException ioe) {
        System.err.println("ERROR: Failed to retrieve database data: " + ioe.getMessage());
        retval = SystemExitCodes.IO_ERROR_EXIT_CODE;
    } finally {
        BrokerPool.stopAll(false);
    }
    System.exit(retval);
}
Also used : Path(java.nio.file.Path) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) IOException(java.io.IOException) DBBroker(org.exist.storage.DBBroker) PermissionDeniedException(org.exist.security.PermissionDeniedException) File(java.io.File) BrokerPool(org.exist.storage.BrokerPool) TerminatedException(org.exist.xquery.TerminatedException)

Example 3 with DBBroker

use of org.exist.storage.DBBroker in project exist by eXist-db.

the class Launcher method checkInstalledApps.

private void checkInstalledApps() {
    try {
        final BrokerPool pool = BrokerPool.getInstance();
        try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
            final XQuery xquery = pool.getXQueryService();
            final Sequence pkgs = xquery.execute(broker, "repo:list()", null);
            for (final SequenceIterator i = pkgs.iterate(); i.hasNext(); ) {
                final ExistRepository.Notification notification = new ExistRepository.Notification(ExistRepository.Action.INSTALL, i.nextItem().getStringValue());
                final Optional<ExistRepository> expathRepo = pool.getExpathRepo();
                if (expathRepo.isPresent()) {
                    update(expathRepo.get(), notification);
                    utilityPanel.update(expathRepo.get(), notification);
                }
                expathRepo.orElseThrow(() -> new EXistException("EXPath repository is not available."));
            }
        }
    } catch (final EXistException | XPathException | PermissionDeniedException e) {
        System.err.println("Failed to check installed packages: " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) EXistException(org.exist.EXistException) DBBroker(org.exist.storage.DBBroker) SequenceIterator(org.exist.xquery.value.SequenceIterator) PermissionDeniedException(org.exist.security.PermissionDeniedException) BrokerPool(org.exist.storage.BrokerPool) ExistRepository(org.exist.repo.ExistRepository)

Example 4 with DBBroker

use of org.exist.storage.DBBroker in project exist by eXist-db.

the class Profiler method isLogEnabled.

public final boolean isLogEnabled() {
    try {
        final DBBroker broker = db.getActiveBroker();
        final Boolean globalProp = (Boolean) broker.getConfiguration().getProperty(CONFIG_PROPERTY_TRACELOG);
        return logEnabled || (globalProp != null && globalProp);
    } catch (Throwable t) {
        log.debug("Ignored exception: {}", t.getMessage());
        return logEnabled;
    }
}
Also used : DBBroker(org.exist.storage.DBBroker)

Example 5 with DBBroker

use of org.exist.storage.DBBroker in project exist by eXist-db.

the class SecurityManagerTest method setup.

@BeforeClass
public static void setup() throws EXistException, PermissionDeniedException {
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    final SecurityManager securityManager = brokerPool.getSecurityManager();
    // create the personal group
    final Group group = new GroupAider(TEST_GROUP_NAME);
    group.setMetadataValue(EXistSchemaType.DESCRIPTION, "Personal group for " + TEST_GROUP_NAME);
    try (final DBBroker broker = brokerPool.get(Optional.of(securityManager.getSystemSubject()))) {
        securityManager.addGroup(broker, group);
        // create the account
        final Account user = new UserAider(TEST_USER_NAME);
        user.setPassword(TEST_USER_NAME);
        user.addGroup(TEST_GROUP_NAME);
        securityManager.addAccount(user);
        // add the new account as a manager of their personal group
        final Group personalGroup = securityManager.getGroup(TEST_GROUP_NAME);
        personalGroup.addManager(securityManager.getAccount(TEST_USER_NAME));
        securityManager.updateGroup(personalGroup);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) GroupAider(org.exist.security.internal.aider.GroupAider) UserAider(org.exist.security.internal.aider.UserAider) BrokerPool(org.exist.storage.BrokerPool) BeforeClass(org.junit.BeforeClass)

Aggregations

DBBroker (org.exist.storage.DBBroker)467 BrokerPool (org.exist.storage.BrokerPool)303 Txn (org.exist.storage.txn.Txn)219 Sequence (org.exist.xquery.value.Sequence)185 Test (org.junit.Test)170 XQuery (org.exist.xquery.XQuery)107 Collection (org.exist.collections.Collection)93 TransactionManager (org.exist.storage.txn.TransactionManager)70 EXistException (org.exist.EXistException)66 StringInputSource (org.exist.util.StringInputSource)66 PermissionDeniedException (org.exist.security.PermissionDeniedException)44 Source (org.exist.source.Source)42 StringSource (org.exist.source.StringSource)41 XmldbURI (org.exist.xmldb.XmldbURI)41 CompiledXQuery (org.exist.xquery.CompiledXQuery)39 IOException (java.io.IOException)38 QName (org.exist.dom.QName)37 LockedDocument (org.exist.dom.persistent.LockedDocument)36 Database (org.exist.Database)35 XPathException (org.exist.xquery.XPathException)30