use of org.exist.util.StringInputSource in project exist by eXist-db.
the class CollectionConfigurationManager method addConfiguration.
/**
* Add a new collection configuration. The XML document is passed as a
* string.
*
* @param txn The transaction that will hold the WRITE locks until they are
* released by commit()/abort()
* @param broker the eXist-db broker
* @param collection the collection to which the configuration applies.
* @param config the xconf document as a String.
* @throws CollectionConfigurationException if config is invalid
*/
public void addConfiguration(final Txn txn, final DBBroker broker, final Collection collection, final String config) throws CollectionConfigurationException {
try {
final XmldbURI path = CONFIG_COLLECTION_URI.append(collection.getURI());
final Collection confCol = broker.getOrCreateCollection(txn, path);
if (confCol == null) {
throw new CollectionConfigurationException("Failed to create config collection: " + path);
}
XmldbURI configurationDocumentName = null;
// Replaces the current configuration file if there is one
final CollectionConfiguration conf = getConfiguration(collection);
if (conf != null) {
configurationDocumentName = conf.getDocName();
if (configurationDocumentName != null) {
LOG.warn("Replacing current configuration file '{}'", configurationDocumentName);
}
}
if (configurationDocumentName == null) {
configurationDocumentName = CollectionConfiguration.DEFAULT_COLLECTION_CONFIG_FILE_URI;
}
broker.saveCollection(txn, confCol);
broker.storeDocument(txn, configurationDocumentName, new StringInputSource(config), MimeType.XML_TYPE, confCol);
// broker.sync(Sync.MAJOR_SYNC);
} catch (final CollectionConfigurationException e) {
throw e;
} catch (final Exception e) {
throw new CollectionConfigurationException("Failed to store collection configuration: " + e.getMessage(), e);
}
}
use of org.exist.util.StringInputSource 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.util.StringInputSource in project exist by eXist-db.
the class AbstractUpdateTest method init.
protected DocumentImpl init(final DBBroker broker, final TransactionManager transact) throws PermissionDeniedException, IOException, SAXException, LockException, EXistException {
DocumentImpl doc = null;
try (final Txn transaction = transact.beginTransaction()) {
final Collection root = broker.getOrCreateCollection(transaction, TEST_COLLECTION_URI);
broker.saveCollection(transaction, root);
final Collection test = broker.getOrCreateCollection(transaction, TEST_COLLECTION_URI.append("test2"));
broker.saveCollection(transaction, test);
broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(TEST_XML), MimeType.XML_TYPE, test);
doc = test.getDocument(broker, XmldbURI.create("test.xml"));
// TODO : unlock the collection here ?
transact.commit(transaction);
}
return doc;
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class NodeTest method setUp.
@BeforeClass
public static void setUp() throws Exception {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
root = broker.getOrCreateCollection(transaction, XmldbURI.create(XmldbURI.ROOT_COLLECTION + "/test"));
assertNotNull(root);
broker.saveCollection(transaction, root);
broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(XML), MimeType.XML_TYPE, root);
transact.commit(transaction);
}
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class IndexerTest2 method storeDoc.
private static void storeDoc() throws PermissionDeniedException, IOException, EXistException, SAXException, LockException, AuthenticationException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager txnMgr = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().authenticate("admin", "")));
final Txn txn = txnMgr.beginTransaction()) {
try (final Collection collection = broker.getOrCreateCollection(txn, TestConstants.TEST_COLLECTION_URI)) {
broker.storeDocument(txn, TestConstants.TEST_XML_URI2, new StringInputSource(XML), MimeType.XML_TYPE, collection);
broker.flush();
broker.saveCollection(txn, collection);
}
txnMgr.commit(txn);
}
}
Aggregations