}
public boolean createNewFile() throws IOException {
DBBroker broker = null;
BrokerPool db = null;
TransactionManager tm;
try {
try {
db = BrokerPool.getInstance();
broker = db.get(null);
} catch (final EXistException e) {
throw new IOException(e);
}
// if (!uri.startsWith("/db"))
// uri = XmldbURI.DB.append(uri);
//
try {
if (uri.endsWith("/"))
{throw new IOException("It collection, but should be resource: "+uri);}
} catch (final Exception e) {
throw new IOException(e);
}
final XmldbURI collectionURI = uri.removeLastSegment();
collection = broker.getCollection(collectionURI);
if (collection == null)
{throw new IOException("Collection not found: "+collectionURI);}
final XmldbURI fileName = uri.lastSegment();
// try {
// resource = broker.getXMLResource(uri, Lock.READ_LOCK);
// } catch (final PermissionDeniedException e1) {
// } finally {
// if (resource != null) {
// resource.getUpdateLock().release(Lock.READ_LOCK);
// collection = resource.getCollection();
// initialized = true;
//
// return false;
// }
// }
//
try {
resource = broker.getResource(uri, Lock.READ_LOCK);
} catch (final PermissionDeniedException e1) {
} finally {
if (resource != null) {
resource.getUpdateLock().release(Lock.READ_LOCK);
collection = resource.getCollection();
initialized = true;
return false;
}
}
MimeType mimeType = MimeTable.getInstance().getContentTypeFor(fileName);
if (mimeType == null) {
mimeType = MimeType.BINARY_TYPE;
}
tm = db.getTransactionManager();
final Txn transaction = tm.beginTransaction();
InputStream is = null;
try {
if (mimeType.isXMLType()) {
// store as xml resource
final String str = "<empty/>";
final IndexInfo info = collection.validateXMLResource(transaction, broker, fileName, str);
info.getDocument().getMetadata().setMimeType(mimeType.getName());
info.getDocument().getPermissions().setMode(DEFAULT_RESOURCE_PERM);
collection.store(transaction, broker, info, str, false);
} else {
// store as binary resource
is = new ByteArrayInputStream("".getBytes(UTF_8));
final BinaryDocument blob = new BinaryDocument(db, collection, fileName);
blob.getPermissions().setMode(DEFAULT_RESOURCE_PERM);
collection.addBinaryResource(transaction, broker, blob, is,
mimeType.getName(), 0L , new Date(), new Date());
}
tm.commit(transaction);
} catch (final Exception e) {
tm.abort(transaction);
throw new IOException(e);
} finally {
tm.close(transaction);
closeFile(is);
if (resource != null)
{resource.getUpdateLock().release(Lock.READ_LOCK);}
}