private void modifyMetadata(ModifyMetadata method) throws IOException {
// if (initialized) {return;}
DBBroker broker = null;
BrokerPool db = null;
try {
try {
db = BrokerPool.getInstance();
broker = db.get(null);
} catch (final EXistException e) {
throw new IOException(e);
}
final TransactionManager tm = db.getTransactionManager();
Txn txn = null;
try {
//collection
if (uri.endsWith("/")) {
collection = broker.getCollection(uri);
if (collection == null)
{throw new IOException("Resource not found: "+uri);}
//resource
} else {
resource = broker.getXMLResource(uri, Lock.READ_LOCK);
if (resource == null) {
//may be, it's collection ... checking ...
collection = broker.getCollection(uri);
if (collection == null) {
throw new IOException("Resource not found: "+uri);
}
txn = tm.beginTransaction();
method.modify(collection);
broker.saveCollection(txn, collection);
tm.commit(txn);
} else {
collection = resource.getCollection();
txn = tm.beginTransaction();
method.modify(resource);
broker.storeMetadata(txn, resource);
tm.commit(txn);
}
}
} catch (final IOException e) {
if (txn != null) {
tm.abort(txn);
}
throw e;
} catch (final Exception e) {
if (txn != null) {
tm.abort(txn);
}
throw new IOException(e);
} finally {
tm.close(txn);
if (resource != null)
{resource.getUpdateLock().release(Lock.READ_LOCK);}
}
} finally {
if (db != null)
{db.release(broker);}
}
initialized = true;
}