use of org.exist.storage.lock.ManagedCollectionLock 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.lock.ManagedCollectionLock in project exist by eXist-db.
the class LocalCollectionManagementService method createCollection.
@Override
public Collection createCollection(final XmldbURI name, final Date created) throws XMLDBException {
final XmldbURI collName = resolve(name);
withDb((broker, transaction) -> {
try {
final org.exist.collections.Collection coll = broker.getOrCreateCollection(transaction, collName, Optional.ofNullable(created).map(c -> new Tuple2<>(null, c.getTime())));
try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collName)) {
broker.saveCollection(transaction, coll);
}
return null;
} catch (final LockException | TriggerException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
});
return new LocalCollection(user, brokerPool, collection, collName);
}
use of org.exist.storage.lock.ManagedCollectionLock in project exist by eXist-db.
the class Txn method acquireCollectionLock.
public void acquireCollectionLock(final SupplierE<ManagedCollectionLock, LockException> fnLockAcquire) throws LockException {
final ManagedCollectionLock lock = fnLockAcquire.get();
locksHeld.add(new LockInfo(lock, lock::close));
}
use of org.exist.storage.lock.ManagedCollectionLock in project exist by eXist-db.
the class RpcConnection method createCollection.
private boolean createCollection(final XmldbURI collUri, final Date created) throws PermissionDeniedException, EXistException {
withDb((broker, transaction) -> {
Collection current = broker.getCollection(collUri);
if (current != null) {
return true;
}
current = broker.getOrCreateCollection(transaction, collUri, Optional.ofNullable(created).map(c -> new Tuple2<>(null, c.getTime())));
try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collUri)) {
broker.saveCollection(transaction, current);
}
return null;
});
LOG.info("collection {} has been created", collUri);
return true;
}
use of org.exist.storage.lock.ManagedCollectionLock in project exist by eXist-db.
the class EmbeddedBinariesTest method storeBinaryFile.
@Override
protected void storeBinaryFile(final XmldbURI filePath, final byte[] content) throws Exception {
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
try (final ManagedCollectionLock collectionLock = brokerPool.getLockManager().acquireCollectionWriteLock(filePath.removeLastSegment())) {
final Collection collection = broker.getOrCreateCollection(transaction, filePath.removeLastSegment());
broker.storeDocument(transaction, filePath.lastSegment(), new StringInputSource(content), MimeType.BINARY_TYPE, collection);
broker.saveCollection(transaction, collection);
}
transaction.commit();
}
}
Aggregations