<%= "We've got '" + configUrl + "'." %>
<%= "We've got a session in workspace '" + jcrSession.getWorkspace().getName() + "'." %>
<%
String fileName = "myfile.csv";
String fileData = "SYMBOL,FULLNAME,PRICE\nBA,Boeing Company,58.11\nORCL,Oracle Corporation,147.12";
//String fileMime = "application/octet-stream";
GregorianCalendar lastModified = new GregorianCalendar(2011, 01, 13, 21, 59);
// clean up directory files/ first
try {
rn.getNode("files").removeShare();
if (jcrSession.hasPendingChanges()) {
out.println("we have pending changes, saving..");
}
jcrSession.save();
if (!jcrSession.hasPendingChanges()) {
out.print(" now changes are saved");
}
} catch (javax.jcr.PathNotFoundException pnfe) {
//ignore
}
%>
<%
Node dirNode = rn.addNode("files", "nt:folder");
Node fileNode = rn.addNode("files/" + fileName, "nt:file");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
//resNode.setProperty("jcr:mimeType", fileMime);
//resNode.setProperty("jcr:encoding", "");
resNode.setProperty("jcr:data", jcrSession.getValueFactory().createValue(new ByteArrayInputStream(
fileData.getBytes())));
resNode.setProperty("jcr:lastModified", lastModified);
if (jcrSession.hasPendingChanges()) {
out.println("we have pending changes, saving..");
}
//Before the session save, register a listener that will be notified when the sequencer finishes
final JspWriter writer = out;
final CountDownLatch sequencingLatch = new CountDownLatch(1);
final long sequencingStart = System.currentTimeMillis();
ObservationManager observationManager = jcrSession.getWorkspace().getObservationManager();
observationManager.addEventListener(new EventListener() {
@Override
public void onEvent( EventIterator events ) {
try {
long sequecingDurationMillis = System.currentTimeMillis() - sequencingStart;
writer.println("Sequencing duration: " + sequecingDurationMillis + " milliseconds");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
sequencingLatch.countDown();
}
}
}, Event.Sequencing.NODE_SEQUENCED, null, true, null, null, false);
jcrSession.save();
if (!jcrSession.hasPendingChanges()) {
out.print(" now changes are saved");
}
//block and wait max 5 seconds for the sequencing to finish
out.println();
out.println("Waiting for sequencing to finish...");
if (!sequencingLatch.await(5, TimeUnit.SECONDS)) {
out.println("Sequencing never finished...something went wrong");
}
%>
<%
//ByteArrayOutputStream wsAsXml = new ByteArrayOutputStream();
//jcrSession.exportSystemView("/" + fileName + "/", wsAsXml, false, false);
//wsAsXml.close();
//out.println(wsAsXml.toString());
String sqlExpression = "SELECT * FROM [nt:file] where name() like '" + fileName + "')";
QueryManager qm = jcrSession.getWorkspace().getQueryManager();
Query query = qm.createQuery(sqlExpression, Query.JCR_SQL2);
QueryResult qr = query.execute();
out.println("Below see our file(s) in the workspace:");
// you may also use NodeIterator
Iterator it = qr.getNodes();
// wait up to 15 seconds for indexes to get synced, for
// rationale see http://issues.jboss.org/browse/MODE-1063
long checkStart = System.currentTimeMillis();
for (int i = 0; ; i++) {
if (it.hasNext()) {
out.println("INFO: waited for index sync " + (System.currentTimeMillis() - checkStart) + " milliseconds");
break;
}
if (i == 30) {
throw new ServletException("15 seconds not enough to get query indexes in sync");
}
Thread.sleep(5000);
qr = query.execute();
it = qr.getNodes();
}
while (it.hasNext()) {
out.print("* ");
Node n = it.next();
out.println(n.getName() + ":" + n.getIdentifier());
String content = n.getNode("jcr:content").getProperty("jcr:data").getString();
out.println(content);
if (!content.matches(fileData)) {
out.println("!!! file content and what we see in repo does not match !!!");
}
}
%>
<%
String ftsExpression = "SELECT * FROM [nt:base] WHERE CONTAINS( [nt:base].*, 'Boeing' )";
Query ftsQuery = qm.createQuery(ftsExpression, Query.JCR_SQL2);
QueryResult ftsResult = ftsQuery.execute();
// Iterator ir = null;
Iterator ir = qr.getNodes();
out.println("Full text search query result below:");
// wait for data to be sequenced
checkStart = System.currentTimeMillis();
for (int i = 0; ; i++) {
// ir = ftsResult.getRows();
// out.println("iteration: " + i);
if (ir.hasNext()) {
out.println(
"INFO: waited for sequencing and persisting " + (System.currentTimeMillis() - checkStart) + " miliseconds");
break;
}
if (i == 100) {
throw new ServletException("10 seconds not enough to sequence the file and persist the additional nodes");
}
Thread.sleep(100);
ftsResult = ftsQuery.execute();
ir = qr.getNodes();
}
while (ir.hasNext()) {
Node n = ir.next();
out.println("name:" + n.getName() + ", id:" + n.getIdentifier());
}
%>
<%
out.println("deleting our file(s)..");
it = qr.getNodes();
while (it.hasNext()) {
Node n = it.next();
n.remove();
}
checkStart = System.currentTimeMillis();
dirNode.removeShare(); // remove the directory node also
jcrSession.save();
// lets make sure file(s) gone
it = query.execute().getNodes();
if (it.hasNext()) {
out.println("file(s) not deleted!?");
} else {
out.println("file(s) gone");
}
// lets make sure sequenced data is gone
for (int i = 0; ; i++) {
ir = ftsQuery.execute().getRows();
if (!ir.hasNext()) {
out.println("sequenced data gone in " + (System.currentTimeMillis() - checkStart) + " milliseconds");
break;
}
if (i == 100) {
out.println("sequenced data not deleted after 10 seconds!?");
break;
}
}
%>