Search in sources :

Example 1 with BrokerPool

use of org.exist.storage.BrokerPool 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 2 with BrokerPool

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

the class Configurator method save.

public static DocumentImpl save(final Configurable instance, final DBBroker broker, final Collection collection, final XmldbURI uri) throws IOException, ConfigurationException {
    final StringWriter writer = new StringWriter();
    final SAXSerializer serializer = new SAXSerializer(writer, null);
    try {
        serializer.startDocument();
        serialize(instance, serializer);
        serializer.endDocument();
    } catch (final SAXException saxe) {
        throw new ConfigurationException(saxe.getMessage(), saxe);
    }
    final String data = writer.toString();
    if (data == null || data.length() == 0) {
        return null;
    }
    FullXmldbURI fullURI = null;
    final BrokerPool pool = broker.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    LOG.info("Storing configuration {}/{}", collection.getURI(), uri);
    final SecurityManager securityManager = pool.getSecurityManager();
    try {
        final Subject systemSubject = securityManager.getSystemSubject();
        broker.pushSubject(systemSubject);
        Txn txn = broker.getCurrentTransaction();
        final boolean txnInProgress = txn != null;
        if (!txnInProgress) {
            txn = transact.beginTransaction();
        }
        try {
            txn.acquireCollectionLock(() -> pool.getLockManager().acquireCollectionWriteLock(collection.getURI()));
            fullURI = getFullURI(pool, collection.getURI().append(uri));
            saving.add(fullURI);
            final Permission systemResourcePermission = PermissionFactory.getDefaultResourcePermission(pool.getSecurityManager());
            systemResourcePermission.setOwner(systemSubject);
            systemResourcePermission.setGroup(systemSubject.getDefaultGroup());
            systemResourcePermission.setMode(Permission.DEFAULT_SYSTEM_RESOURCE_PERM);
            broker.storeDocument(txn, uri, new StringInputSource(data), MimeType.XML_TYPE, null, null, systemResourcePermission, null, null, collection);
            broker.saveCollection(txn, collection);
            if (!txnInProgress) {
                transact.commit(txn);
            }
        } catch (final EXistException | PermissionDeniedException | SAXException | LockException e) {
            if (!txnInProgress) {
                transact.abort(txn);
            }
            throw e;
        } finally {
            if (!txnInProgress) {
                txn.close();
            }
        }
        saving.remove(fullURI);
        broker.flush();
        broker.sync(Sync.MAJOR);
        return collection.getDocument(broker, uri.lastSegment());
    } catch (final EXistException | PermissionDeniedException | SAXException | LockException e) {
        LOG.error(e);
        if (fullURI != null) {
            saving.remove(fullURI);
        }
        throw new IOException(e);
    } finally {
        broker.popSubject();
    }
}
Also used : SecurityManager(org.exist.security.SecurityManager) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) SAXException(org.xml.sax.SAXException) StringInputSource(org.exist.util.StringInputSource) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LockException(org.exist.util.LockException) TransactionManager(org.exist.storage.txn.TransactionManager) FullXmldbURI(org.exist.xmldb.FullXmldbURI) SAXSerializer(org.exist.util.serializer.SAXSerializer) BrokerPool(org.exist.storage.BrokerPool)

Example 3 with BrokerPool

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

the class Launcher method registerObserver.

private void registerObserver() {
    try {
        final BrokerPool pool = BrokerPool.getInstance();
        final Optional<ExistRepository> repo = pool.getExpathRepo();
        if (repo.isPresent()) {
            repo.get().addObserver(this);
            repo.get().addObserver(utilityPanel);
        } else {
            System.err.println("EXPath repository is not available.");
        }
    } catch (final EXistException e) {
        System.err.println("Failed to register as observer for package manager events");
        e.printStackTrace();
    }
}
Also used : EXistException(org.exist.EXistException) BrokerPool(org.exist.storage.BrokerPool) ExistRepository(org.exist.repo.ExistRepository)

Example 4 with BrokerPool

use of org.exist.storage.BrokerPool 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 5 with BrokerPool

use of org.exist.storage.BrokerPool 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

BrokerPool (org.exist.storage.BrokerPool)381 DBBroker (org.exist.storage.DBBroker)300 Txn (org.exist.storage.txn.Txn)180 Sequence (org.exist.xquery.value.Sequence)157 Test (org.junit.Test)115 XQuery (org.exist.xquery.XQuery)105 Collection (org.exist.collections.Collection)71 StringInputSource (org.exist.util.StringInputSource)66 TransactionManager (org.exist.storage.txn.TransactionManager)61 Source (org.exist.source.Source)43 StringSource (org.exist.source.StringSource)40 CompiledXQuery (org.exist.xquery.CompiledXQuery)38 Path (java.nio.file.Path)22 XmldbURI (org.exist.xmldb.XmldbURI)21 XPathException (org.exist.xquery.XPathException)21 Properties (java.util.Properties)20 LockedDocument (org.exist.dom.persistent.LockedDocument)20 InputSource (org.xml.sax.InputSource)20 IOException (java.io.IOException)19 XQueryContext (org.exist.xquery.XQueryContext)19