final Subject currentUser = getSubject();
//elevate getUser() to DBA_USER
setSubject(pool.getSecurityManager().getSystemSubject());
//start a transaction
final TransactionManager transact = pool.getTransactionManager();
final Txn transaction = transact.beginTransaction();
//create a name for the temporary document
final XmldbURI docName = XmldbURI.create(MessageDigester.md5(Thread.currentThread().getName() + Long.toString(System.currentTimeMillis()), false) + ".xml");
//get the temp collection
Collection temp = openCollection(XmldbURI.TEMP_COLLECTION_URI, Lock.WRITE_LOCK);
boolean created = false;
try {
//if no temp collection
if(temp == null) {
//creates temp collection (with write lock)
temp = createTempCollection(transaction);
if(temp == null) {
LOG.warn("Failed to create temporary collection");
//TODO : emergency exit?
}
created = true;
}
//create a temporary document
final DocumentImpl targetDoc = new DocumentImpl(pool, temp, docName);
targetDoc.getPermissions().setMode(Permission.DEFAULT_TEMPORARY_DOCUMENT_PERM);
final long now = System.currentTimeMillis();
final DocumentMetadata metadata = new DocumentMetadata();
metadata.setLastModified(now);
metadata.setCreated(now);
targetDoc.setMetadata(metadata);
targetDoc.setDocId(getNextResourceId(transaction, temp));
//index the temporary document
final DOMIndexer indexer = new DOMIndexer(this, transaction, doc, targetDoc); //NULL transaction, so temporary fragment is not journalled - AR
indexer.scan();
indexer.store();
//store the temporary document
temp.addDocument(transaction, this, targetDoc); //NULL transaction, so temporary fragment is not journalled - AR
// unlock the temp collection
if(transaction == null) {
temp.getLock().release(Lock.WRITE_LOCK);
} else if(!created) {
transaction.registerLock(temp.getLock(), Lock.WRITE_LOCK);
}
//NULL transaction, so temporary fragment is not journalled - AR
storeXMLResource(transaction, targetDoc);
flush();
closeDocument();