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