}
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
DocumentImpl doc = null;
try {
// Retrieve Lucene
LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker()
.getIndexController().getWorkerByIndexId(LuceneIndex.ID);
if (isCalledAs("index")) {
// Get first parameter, this is the document
String path = args[0].itemAt(0).getStringValue();
// Retrieve document from database
doc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(path), Lock.READ_LOCK);
// Verify the document actually exists
if (doc == null) {
throw new XPathException("Document " + path + " does not exist.");
}
boolean flush = args.length == 2 || args[2].effectiveBooleanValue();
// Note: code order is important here,
index.setDocument(doc, StreamListener.STORE);
index.setMode(StreamListener.STORE);
// Get 'solr' node from second parameter
NodeValue descriptor = (NodeValue) args[1].itemAt(0);
// Pas document and index instructions to indexer
index.indexNonXML(descriptor);
if (flush) {
// Make sure things are written
index.writeNonXML();
}
} else {
// "close"
index.writeNonXML();
}
} catch (Exception ex) { // PermissionDeniedException
logger.error(ex.getMessage(), ex);
throw new XPathException(ex);
} finally {
if (doc != null) {
doc.getUpdateLock().release(Lock.READ_LOCK);
}
}
// Return nothing [status would be nice]
return Sequence.EMPTY_SEQUENCE;