use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class LocalCollection method getResource.
Resource getResource(final DBBroker broker, final Txn transaction, final XmldbURI idURI) throws XMLDBException {
return this.<Resource>read(broker, transaction).apply((collection, broker1, transaction1) -> {
try (final LockedDocument lockedDocument = collection.getDocumentWithLock(broker1, idURI, LockMode.READ_LOCK)) {
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
final DocumentImpl document = lockedDocument == null ? null : lockedDocument.getDocument();
if (document == null) {
LOG.warn("Resource {} not found", idURI);
return null;
}
final Resource r;
switch(document.getResourceType()) {
case DocumentImpl.XML_FILE:
r = new LocalXMLResource(user, brokerPool, this, idURI);
break;
case DocumentImpl.BINARY_FILE:
r = new LocalBinaryResource(user, brokerPool, this, idURI);
break;
default:
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Unknown resource type");
}
((AbstractEXistResource) r).setMimeType(document.getMimeType());
return r;
}
});
}
use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class LocalResourceSet method getMembersAsResource.
@Override
public Resource getMembersAsResource() throws XMLDBException {
return this.<Resource>withDb((broker, transaction) -> {
final Serializer serializer = broker.borrowSerializer();
final SAXSerializer handler = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
final StringWriter writer = new StringWriter();
handler.setOutput(writer, outputProperties);
try {
// configure the serializer
collection.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
serializer.setProperties(outputProperties);
serializer.setUser(user);
serializer.setSAXHandlers(handler, handler);
// serialize results
handler.startDocument();
handler.startPrefixMapping("exist", Namespaces.EXIST_NS);
final AttributesImpl attribs = new AttributesImpl();
attribs.addAttribute("", "hitCount", "hitCount", "CDATA", Integer.toString(resources.size()));
handler.startElement(Namespaces.EXIST_NS, "result", "exist:result", attribs);
Item current;
char[] value;
for (Object resource : resources) {
current = (Item) resource;
if (Type.subTypeOf(current.getType(), Type.NODE)) {
((NodeValue) current).toSAX(broker, handler, outputProperties);
} else {
value = current.toString().toCharArray();
handler.characters(value, 0, value.length);
}
}
handler.endElement(Namespaces.EXIST_NS, "result", "exist:result");
handler.endPrefixMapping("exist");
handler.endDocument();
final Resource res = new LocalXMLResource(user, brokerPool, collection, XmldbURI.EMPTY_URI);
res.setContent(writer.toString());
return res;
} catch (final SAXException e) {
throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, "serialization error", e);
} finally {
SerializerPool.getInstance().returnObject(handler);
broker.returnSerializer(serializer);
}
});
}
use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class AbstractLocal method withDb.
/**
* Higher-order-function for performing an XMLDB operation on
* the database
*
* @param dbOperation The operation to perform on the database
* @param <R> The return type of the operation
*
* @return the result of the operation
*
* @throws XMLDBException if an error occurs when executing the operation.
*/
protected <R> R withDb(final LocalXmldbFunction<R> dbOperation) throws XMLDBException {
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(user));
final Txn transaction = transaction().apply(broker)) {
try {
final R result = dbOperation.apply(broker, transaction);
transaction.commit();
return result;
} catch (final XMLDBException | EXistException e) {
transaction.abort();
throw e;
}
} catch (final EXistException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
}
use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class DatabaseImpl method configure.
/**
* In embedded mode: configure the database instance
*
* @throws XMLDBException Description of the Exception
*/
private void configure(final String instanceName) throws XMLDBException {
try {
final Configuration config = new Configuration(configuration, Optional.empty());
if (dataDir != null) {
config.setProperty(BrokerPool.PROPERTY_DATA_DIR, Paths.get(dataDir));
}
if (journalDir != null) {
config.setProperty(Journal.PROPERTY_RECOVERY_JOURNAL_DIR, Paths.get(journalDir));
}
BrokerPool.configure(instanceName, 1, 5, config);
if (shutdown != null) {
BrokerPool.getInstance(instanceName).registerShutdownListener(shutdown);
}
currentInstanceName = instanceName;
} catch (final Exception e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "configuration error: " + e.getMessage(), e);
}
}
use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.
the class DatabaseImpl method getCollection.
/**
* @deprecated {@link org.exist.xmldb.DatabaseImpl#getCollection(org.exist.xmldb.XmldbURI, java.lang.String, java.lang.String)}
*/
@Deprecated
@Override
public Collection getCollection(final String uri, final String user, final String password) throws XMLDBException {
try {
// Ugly workaround for non-URI compliant collection names (most likely IRIs)
final String newURIString = XmldbURI.recoverPseudoURIs(uri);
// Remember that DatabaseManager (provided in xmldb.jar) trims the leading "xmldb:" !!!
// ... prepend it to have a real xmldb URI again...
final XmldbURI xmldbURI = XmldbURI.xmldbUriFor(XmldbURI.XMLDB_URI_PREFIX + newURIString);
return getCollection(xmldbURI, user, password);
} catch (final URISyntaxException e) {
// ... even in the error message
throw new XMLDBException(ErrorCodes.INVALID_DATABASE, "xmldb URI is not well formed: " + XmldbURI.XMLDB_URI_PREFIX + uri);
}
}
Aggregations