use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class SystemImportHandler method restoreResourceEntry.
private DeferredPermission restoreResourceEntry(final Attributes atts) throws SAXException {
@Nullable final String skip = atts.getValue("skip");
// Don't process entries which should be skipped
if (skip != null && !"no".equals(skip)) {
return new SkippedEntryDeferredPermission();
}
@Nullable final String name = atts.getValue("name");
if (name == null) {
throw new SAXException("Resource requires a name attribute");
}
final boolean xmlType = Optional.ofNullable(atts.getValue("type")).filter(s -> s.equals("XMLResource")).isPresent();
final String owner = getAttr(atts, "owner", SecurityManager.SYSTEM);
final String group = getAttr(atts, "group", SecurityManager.DBA_GROUP);
final String perms = getAttr(atts, "mode", "644");
final String filename = getAttr(atts, "filename", name);
@Nullable final String mimeTypeStr = atts.getValue("mimetype");
@Nullable final String dateCreatedStr = atts.getValue("created");
@Nullable final String dateModifiedStr = atts.getValue("modified");
@Nullable final String publicId = atts.getValue("publicid");
@Nullable final String systemId = atts.getValue("systemid");
@Nullable final String nameDocType = atts.getValue("namedoctype");
MimeType mimeType = null;
if (mimeTypeStr != null) {
mimeType = MimeTable.getInstance().getContentType(mimeTypeStr);
}
if (mimeType == null) {
mimeType = xmlType ? MimeType.XML_TYPE : MimeType.BINARY_TYPE;
}
Date dateCreated = null;
if (dateCreatedStr != null) {
try {
dateCreated = new DateTimeValue(dateCreatedStr).getDate();
} catch (final XPathException xpe) {
listener.warn("Illegal creation date. Ignoring date...");
}
}
Date dateModified = null;
if (dateModifiedStr != null) {
try {
dateModified = new DateTimeValue(dateModifiedStr).getDate();
} catch (final XPathException xpe) {
listener.warn("Illegal modification date. Ignoring date...");
}
}
final DocumentType docType;
if (publicId != null || systemId != null) {
docType = new DocumentTypeImpl(nameDocType, publicId, systemId);
} else {
docType = null;
}
final XmldbURI docUri;
if (version >= STRICT_URI_VERSION) {
docUri = XmldbURI.create(name);
} else {
try {
docUri = URIUtils.encodeXmldbUriFor(name);
} catch (final URISyntaxException e) {
final String msg = "Could not parse document name into a URI: " + e.getMessage();
listener.error(msg);
LOG.error(msg, e);
return new SkippedEntryDeferredPermission();
}
}
try (final EXistInputSource is = descriptor.getInputSource(filename)) {
if (is == null) {
final String msg = "Failed to restore resource '" + name + "'\nfrom file '" + descriptor.getSymbolicPath(name, false) + "'.\nReason: Unable to obtain its EXistInputSource";
listener.warn(msg);
throw new RuntimeException(msg);
}
try (final Txn transaction = beginTransaction()) {
broker.storeDocument(transaction, docUri, is, mimeType, dateCreated, dateModified, null, docType, null, currentCollection);
try (final LockedDocument doc = currentCollection.getDocumentWithLock(broker, docUri, Lock.LockMode.READ_LOCK)) {
rh.startDocumentRestore(doc.getDocument(), atts);
}
transaction.commit();
final DeferredPermission deferredPermission;
if (name.startsWith(XmldbURI.SYSTEM_COLLECTION)) {
// prevents restore of a backup from changing system collection resource ownership
deferredPermission = new ResourceDeferredPermission(listener, currentCollection.getURI().append(name), SecurityManager.SYSTEM, SecurityManager.DBA_GROUP, Integer.parseInt(perms, 8));
} else {
deferredPermission = new ResourceDeferredPermission(listener, currentCollection.getURI().append(name), owner, group, Integer.parseInt(perms, 8));
}
try (final LockedDocument doc = currentCollection.getDocumentWithLock(broker, docUri, Lock.LockMode.READ_LOCK)) {
rh.endDocumentRestore(doc.getDocument());
}
listener.restoredResource(name);
return deferredPermission;
} catch (final Exception e) {
throw new IOException(e);
}
} catch (final Exception e) {
listener.warn("Failed to restore resource '" + name + "'\nfrom file '" + descriptor.getSymbolicPath(name, false) + "'.\nReason: " + e.getMessage());
LOG.error(e.getMessage(), e);
return new SkippedEntryDeferredPermission();
}
}
use of org.exist.storage.txn.Txn 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(" ");
}
}
use of org.exist.storage.txn.Txn 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.txn.Txn in project exist by eXist-db.
the class RestoreHandler method restoreCollectionEntry.
private DeferredPermission restoreCollectionEntry(final Attributes atts) throws SAXException {
final String name = atts.getValue("name");
if (name == null) {
throw new SAXException("Collection requires a name attribute");
}
final String owner = getAttr(atts, "owner", SecurityManager.SYSTEM);
final String group = getAttr(atts, "group", SecurityManager.DBA_GROUP);
final String mode = getAttr(atts, "mode", "644");
final String created = atts.getValue("created");
final String strVersion = atts.getValue("version");
if (strVersion != null) {
try {
this.version = Integer.parseInt(strVersion);
} catch (final NumberFormatException nfe) {
final String msg = "Could not parse version number for Collection '" + name + "', defaulting to version 0";
listener.warn(msg);
LOG.warn(msg);
this.version = 0;
}
}
try {
listener.createdCollection(name);
final XmldbURI collUri;
if (version >= STRICT_URI_VERSION) {
collUri = XmldbURI.create(name);
} else {
try {
collUri = URIUtils.encodeXmldbUriFor(name);
} catch (final URISyntaxException e) {
listener.warn("Could not parse document name into a URI: " + e.getMessage());
return new SkippedEntryDeferredPermission();
}
}
if (version >= BLOB_STORE_VERSION) {
this.deduplicateBlobs = Boolean.parseBoolean(atts.getValue("deduplicate-blobs"));
} else {
this.deduplicateBlobs = false;
}
final LockManager lockManager = broker.getBrokerPool().getLockManager();
try (final Txn transaction = beginTransaction();
final ManagedCollectionLock colLock = lockManager.acquireCollectionWriteLock(collUri)) {
Collection collection = broker.getCollection(collUri);
if (collection == null) {
final Tuple2<Permission, Long> creationAttributes = Tuple(null, getDateFromXSDateTimeStringForItem(created, name).getTime());
collection = broker.getOrCreateCollection(transaction, collUri, Optional.of(creationAttributes));
broker.saveCollection(transaction, collection);
}
transaction.commit();
this.currentCollectionUri = collection.getURI();
}
final DeferredPermission deferredPermission;
if (name.startsWith(XmldbURI.SYSTEM_COLLECTION)) {
// prevents restore of a backup from changing System collection ownership
deferredPermission = new CollectionDeferredPermission(listener, currentCollectionUri, SecurityManager.SYSTEM, SecurityManager.DBA_GROUP, Integer.parseInt(mode, 8));
} else {
deferredPermission = new CollectionDeferredPermission(listener, currentCollectionUri, owner, group, Integer.parseInt(mode, 8));
}
return deferredPermission;
} catch (final IOException | LockException | TransactionException | PermissionDeniedException e) {
final String msg = "An unrecoverable error occurred while restoring collection '" + name + "': " + e.getMessage() + ". Aborting restore!";
LOG.error(msg, e);
listener.warn(msg);
throw new SAXException(msg, e);
}
}
use of org.exist.storage.txn.Txn 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();
}
}
Aggregations