use of org.exist.xmldb.FullXmldbURI 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.xmldb.FullXmldbURI in project exist by eXist-db.
the class Configurator method parse.
public static Configuration parse(final Configurable instance, final DBBroker broker, final Collection collection, final XmldbURI fileURL) throws ConfigurationException {
final FullXmldbURI key = getFullURI(broker.getBrokerPool(), collection.getURI().append(fileURL));
Configuration conf = hotConfigs.get(key);
if (conf != null) {
return conf;
}
// XXX: locking required
DocumentImpl document;
try {
document = collection.getDocument(broker, fileURL);
} catch (final PermissionDeniedException pde) {
throw new ConfigurationException(pde.getMessage(), pde);
}
if (document == null) {
if (broker.isReadOnly()) {
// create in memory document & configuration
try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
final Writer writer = new OutputStreamWriter(os)) {
final SAXSerializer serializer = new SAXSerializer(writer, null);
serializer.startDocument();
serialize(instance, serializer);
serializer.endDocument();
if (os.size() == 0) {
return null;
}
return parse(os.toInputStream());
} catch (final IOException | SAXException e) {
throw new ConfigurationException(e.getMessage(), e);
}
}
try {
document = save(instance, broker, collection, fileURL);
} catch (final IOException e) {
LOG.error(e.getMessage(), e);
// TODO : throw exception ? -pb
return null;
}
}
if (document == null) {
// possibly on corrupted database, find better solution (recovery flag?)
return null;
}
final Element confElement = document.getDocumentElement();
if (confElement == null) {
// possibly on corrupted database, find better solution (recovery flag?)
return null;
}
conf = new ConfigurationImpl(confElement);
hotConfigs.put(key, conf);
return conf;
}
use of org.exist.xmldb.FullXmldbURI in project exist by eXist-db.
the class Configurator method clear.
public static synchronized void clear(final Database db) {
for (final Entry<FullXmldbURI, Configuration> entry : hotConfigs.entrySet()) {
final FullXmldbURI uri = entry.getKey();
if (uri.getInstanceName().equals(db.getId())) {
final Configuration conf = entry.getValue();
if (conf instanceof ConfigurationImpl) {
((ConfigurationImpl) conf).configuredObjectReference = null;
}
}
hotConfigs.remove(uri);
}
}
use of org.exist.xmldb.FullXmldbURI in project exist by eXist-db.
the class Configurator method parse.
public static Configuration parse(final BrokerPool pool, final DocumentImpl document) {
if (document == null) {
return null;
}
Configuration conf;
final FullXmldbURI key = getFullURI(pool, document.getURI());
conf = hotConfigs.get(key);
if (conf != null) {
return conf;
}
final Element confElement = document.getDocumentElement();
if (confElement == null) {
// possibly on corrupted database, find better solution (recovery flag?)
return null;
}
conf = new ConfigurationImpl(confElement);
hotConfigs.put(key, conf);
return conf;
}
Aggregations