use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.
the class CollectionConfigurationManager method checkRootCollectionConfig.
/**
* Create a stored default configuration document for the root collection
*
* @param broker The broker which will do the operation
* @throws EXistException if something goes wrong
* @throws PermissionDeniedException if user does not have sufficient rights
*/
public void checkRootCollectionConfig(DBBroker broker) throws EXistException, PermissionDeniedException {
// Copied from the legacy conf.xml in order to make the test suite work
// TODO : backward compatibility could be ensured by copying the
// relevant parts of conf.xml
final String configuration = "<collection xmlns=\"http://exist-db.org/collection-config/1.0\">" + " <index>" + " </index>" + "</collection>";
final TransactionManager transact = broker.getDatabase().getTransactionManager();
try (final Txn txn = transact.beginTransaction()) {
try (final Collection collection = broker.openCollection(XmldbURI.ROOT_COLLECTION_URI, LockMode.READ_LOCK)) {
if (collection == null) {
transact.abort(txn);
throw new EXistException("collection " + XmldbURI.ROOT_COLLECTION_URI + " not found!");
}
final CollectionConfiguration conf = getConfiguration(collection);
if (conf != null) {
// it
if (conf.getDocName() != null) {
transact.abort(txn);
return;
}
}
// Configure the root collection
addConfiguration(txn, broker, collection, configuration);
LOG.info("Configured '{}'", collection.getURI());
}
transact.commit(txn);
} catch (final CollectionConfigurationException e) {
throw new EXistException(e.getMessage());
}
}
use of org.exist.storage.txn.TransactionManager 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.txn.TransactionManager in project exist by eXist-db.
the class XQueryStartupTrigger method createAutostartCollection.
/**
* Create autostart collection when not existent
*
* @param broker The exist-db broker
*/
private void createAutostartCollection(DBBroker broker) {
LOG.info("Creating {}", AUTOSTART_COLLECTION);
final TransactionManager txnManager = broker.getBrokerPool().getTransactionManager();
try (final Txn txn = txnManager.beginTransaction()) {
XmldbURI newCollection = XmldbURI.create(AUTOSTART_COLLECTION, true);
// Create collection
final Collection created = broker.getOrCreateCollection(txn, newCollection);
// Set ownership and mode
PermissionFactory.chown(broker, created, Optional.of(SecurityManager.SYSTEM), Optional.of(SecurityManager.DBA_GROUP));
PermissionFactory.chmod(broker, created, Optional.of(Permission.DEFAULT_SYSTEM_SECURITY_COLLECTION_PERM), Optional.empty());
broker.saveCollection(txn, created);
broker.flush();
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Finished creation of collection");
}
} catch (Throwable ex) {
LOG.error(ex);
}
}
use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.
the class ElementImpl method insertBefore.
@Override
public Node insertBefore(final Node newChild, final Node refChild) throws DOMException {
if (refChild == null) {
return appendChild(newChild);
} else if (!(refChild instanceof IStoredNode)) {
throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
}
final org.exist.dom.NodeListImpl nl = new org.exist.dom.NodeListImpl();
nl.add(newChild);
final TransactionManager transact = ownerDocument.getBrokerPool().getTransactionManager();
try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
final Txn transaction = transact.beginTransaction()) {
insertBefore(transaction, nl, refChild);
broker.storeXMLResource(transaction, getOwnerDocument());
transact.commit(transaction);
return refChild.getPreviousSibling();
} catch (final TransactionException e) {
throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, e.getMessage());
} catch (final EXistException e) {
LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
}
return null;
}
use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.
the class ElementImpl method appendChild.
@Override
public Node appendChild(final Node newChild) throws DOMException {
if (newChild.getNodeType() != Node.DOCUMENT_NODE && newChild.getOwnerDocument() != null && newChild.getOwnerDocument() != ownerDocument) {
throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Owning document IDs do not match");
}
if (newChild == this) {
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Cannot append an element to itself");
}
if (newChild.getNodeType() == DOCUMENT_NODE) {
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "A Document Node may not be appended to an element");
}
if (newChild.getNodeType() == DOCUMENT_TYPE_NODE) {
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "A Document Type Node may not be appended to an element");
}
if (newChild instanceof IStoredNode) {
final NodeId newChildId = ((IStoredNode) newChild).getNodeId();
if (newChildId != null && getNodeId().isDescendantOf(newChildId)) {
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "The node to append is one of this node's ancestors");
}
}
final TransactionManager transact = ownerDocument.getBrokerPool().getTransactionManager();
final org.exist.dom.NodeListImpl nl = new org.exist.dom.NodeListImpl();
nl.add(newChild);
try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
final Txn transaction = transact.beginTransaction()) {
appendChildren(transaction, nl, 0);
broker.storeXMLResource(transaction, getOwnerDocument());
// bugID 3419602
transact.commit(transaction);
return getLastChild();
} catch (final Exception e) {
throw new DOMException(DOMException.INVALID_STATE_ERR, e.getMessage());
}
}
Aggregations