X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fplugins%2Forg.argeo.slc.client.ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fclient%2Fui%2Feditors%2FProcessLogPage.java;h=46e50b6f0face84914935ff89b6d252877482bb2;hb=9b2422e7198df6f34282a805058dd5f497417318;hp=00ad3e48ed43493b791ba1c4109b5842fbe71250;hpb=94f8c90b4eb50398388b6ee2ebb4e19c8abdee01;p=gpl%2Fargeo-slc.git diff --git a/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/editors/ProcessLogPage.java b/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/editors/ProcessLogPage.java index 00ad3e48e..46e50b6f0 100644 --- a/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/editors/ProcessLogPage.java +++ b/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/editors/ProcessLogPage.java @@ -3,8 +3,22 @@ package org.argeo.slc.client.ui.editors; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.List; +import java.util.SortedMap; +import java.util.TreeMap; +import javax.jcr.Node; +import javax.jcr.NodeIterator; +import javax.jcr.RepositoryException; +import javax.jcr.Workspace; +import javax.jcr.observation.Event; +import javax.jcr.observation.EventListener; +import javax.jcr.query.Query; + +import org.argeo.eclipse.ui.jcr.AsyncUiEventListener; +import org.argeo.slc.SlcException; import org.argeo.slc.execution.ExecutionStep; +import org.argeo.slc.jcr.SlcNames; +import org.argeo.slc.jcr.SlcTypes; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; @@ -27,8 +41,46 @@ public class ProcessLogPage extends FormPage { */ private StringBuffer beforeTextInit = new StringBuffer(""); - public ProcessLogPage(FormEditor editor) { + private Node processNode; + /** + * optimization field: we compute once the length of the path to slc:log so + * that we can easily substring the relative path of logs. + */ + private Integer logPathLength; + + public ProcessLogPage(FormEditor editor, Node processNode) { super(editor, ID, "Log"); + this.processNode = processNode; + + EventListener listener = new LogListener(editor.getSite().getPage() + .getWorkbenchWindow().getWorkbench().getDisplay()); + + try { + String logBasePath = processNode.getPath() + '/' + SlcNames.SLC_LOG; + logPathLength = logBasePath.length(); + + Workspace ws = processNode.getSession().getWorkspace(); + + String statement = "SELECT * FROM [" + + SlcTypes.SLC_LOG_ENTRY + + "] as logEntry" + + " WHERE ISDESCENDANTNODE('" + + logBasePath + + "')" + + " ORDER BY logEntry.[slc:timestamp] ASC, NAME(logEntry) ASC"; + StringBuffer buf = new StringBuffer(""); + NodeIterator it = ws.getQueryManager() + .createQuery(statement, Query.JCR_SQL2).execute() + .getNodes(); + while (it.hasNext()) + appendLogEntry(buf, it.nextNode()); + beforeTextInit = new StringBuffer(buf.toString()); + // text.setText(buf.toString()); + ws.getObservationManager().addEventListener(listener, + Event.NODE_ADDED, logBasePath, true, null, null, false); + } catch (RepositoryException e) { + throw new SlcException("Cannot register listener", e); + } } @Override @@ -39,7 +91,7 @@ public class ProcessLogPage extends FormPage { text = tk.createText(parent, "", SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); text.setEditable(false); - + // transfer the existing buffer the first time if (beforeTextInit.length() > 0) { text.append(beforeTextInit.toString()); @@ -49,27 +101,51 @@ public class ProcessLogPage extends FormPage { } -// @Override -// protected synchronized void createFormContent(IManagedForm mf) { -// ScrolledForm form = mf.getForm(); -// form.setExpandHorizontal(true); -// form.setExpandVertical(true); -// // form.setText("Log"); -// FillLayout mainLayout = new FillLayout(); -// form.getBody().setLayout(mainLayout); -// -// FormToolkit tk = getManagedForm().getToolkit(); -// text = tk.createText(form.getBody(), "", SWT.MULTI | SWT.H_SCROLL -// | SWT.V_SCROLL); -// text.setEditable(false); -// // transfer the existing buffer the first time -// if (beforeTextInit.length() > 0) { -// text.append(beforeTextInit.toString()); -// // clear buffer -// beforeTextInit.setLength(0); -// } -// } + // @Override + // protected synchronized void createFormContent(IManagedForm mf) { + // ScrolledForm form = mf.getForm(); + // form.setExpandHorizontal(true); + // form.setExpandVertical(true); + // // form.setText("Log"); + // FillLayout mainLayout = new FillLayout(); + // form.getBody().setLayout(mainLayout); + // + // FormToolkit tk = getManagedForm().getToolkit(); + // text = tk.createText(form.getBody(), "", SWT.MULTI | SWT.H_SCROLL + // | SWT.V_SCROLL); + // text.setEditable(false); + // // transfer the existing buffer the first time + // if (beforeTextInit.length() > 0) { + // text.append(beforeTextInit.toString()); + // // clear buffer + // beforeTextInit.setLength(0); + // } + // } + protected void appendLogEntry(StringBuffer buf, Node logEntry) + throws RepositoryException { + // +1 in order to remove the first slash + String relPath = logEntry.getPath().substring(logPathLength + 1); + //System.out.println("relPath=" + relPath); + int firstSlashIndex = relPath.indexOf('/'); + int lastSlashIndex = relPath.lastIndexOf('/'); + String thread = relPath.substring(0, firstSlashIndex); + String location = relPath.substring(firstSlashIndex, lastSlashIndex); + + // String date = dateFormat.format(logEntry + // .getProperty(SlcNames.SLC_TIMESTAMP).getDate().getTime()); + String date = logEntry.getProperty(SlcNames.SLC_TIMESTAMP).getString(); + buf.append(date).append(' '); + String type = logEntry.getPrimaryNodeType().getName().substring(7); + buf.append(type).append('\t'); + // buf.append(thread).append('\t'); + // buf.append(location).append('\t'); + buf.append(logEntry.getProperty(SlcNames.SLC_MESSAGE).getString()); + buf.append('\n'); + + } + + /** @deprecated */ public synchronized void addSteps(List steps) { final StringBuffer buf = new StringBuffer(""); for (ExecutionStep step : steps) { @@ -107,4 +183,38 @@ public class ProcessLogPage extends FormPage { text.setFocus(); } + /** JCR event listener notifying when new nodes are added */ + private class LogListener extends AsyncUiEventListener { + + public LogListener(Display display) { + super(display); + } + + @Override + protected void onEventInUiThread(List events) + throws RepositoryException { + // since we use batch save, order is not guaranteed + // so we need to reorder, according to log line number for the time + // being + SortedMap nodes = new TreeMap(); + + for (Event evt : events) { + Node newNode = ProcessLogPage.this.processNode.getSession() + .getNode(evt.getPath()); + if (newNode.isNodeType(SlcTypes.SLC_LOG_ENTRY)) { + nodes.put(Long.parseLong(newNode.getName()), newNode); + } + } + + StringBuffer buf = new StringBuffer(""); + for (Node logEntry : nodes.values()) { + appendLogEntry(buf, logEntry); + } + + if (text != null) + text.append(buf.toString()); + else + beforeTextInit.append(buf); + } + } }