tmf: Bug 489971: Premature processing of payload in custom parser
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomTxtTrace.java
index 4175d074fe871550d7f3558b3e1b532488806903..2cecbaa36eeb5093810a7ef11802273ea24bc15d 100644 (file)
@@ -13,6 +13,8 @@
 
 package org.eclipse.tracecompass.tmf.core.parsers.custom;
 
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
@@ -35,10 +37,13 @@ import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.InputLine;
+import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
+import org.eclipse.tracecompass.tmf.core.signal.TmfTraceRangeUpdatedSignal;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
@@ -108,6 +113,11 @@ public class CustomTxtTrace extends TmfTrace implements ITmfPersistentlyIndexabl
     @Override
     public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType) throws TmfTraceException {
         super.initTrace(resource, path, eventType);
+        initFile();
+    }
+
+    private void initFile() throws TmfTraceException {
+        closeFile();
         try {
             fFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
         } catch (IOException e) {
@@ -118,6 +128,10 @@ public class CustomTxtTrace extends TmfTrace implements ITmfPersistentlyIndexabl
     @Override
     public synchronized void dispose() {
         super.dispose();
+        closeFile();
+    }
+
+    private void closeFile() {
         if (fFile != null) {
             try {
                 fFile.close();
@@ -234,7 +248,7 @@ public class CustomTxtTrace extends TmfTrace implements ITmfPersistentlyIndexabl
         final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
         final CustomTxtEvent event = parse(context);
         if (event != null) {
-            updateAttributes(savedContext, event.getTimestamp());
+            updateAttributes(savedContext, event);
             context.increaseRank();
         }
         return event;
@@ -283,7 +297,7 @@ public class CustomTxtTrace extends TmfTrace implements ITmfPersistentlyIndexabl
                         }
                     }
                 } else {
-                    if (countMap.get(currentInput) >= currentInput.getMinCount()) {
+                    if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMinCount()) {
                         final List<InputLine> nextInputs = currentInput.getNextInputs(countMap);
                         if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {
                             for (final InputLine input : getFirstLines()) {
@@ -306,7 +320,7 @@ public class CustomTxtTrace extends TmfTrace implements ITmfPersistentlyIndexabl
                                 if (countMap.get(currentInput) == null) {
                                     countMap.put(currentInput, 1);
                                 } else {
-                                    countMap.put(currentInput, countMap.get(currentInput) + 1);
+                                    countMap.put(currentInput, checkNotNull(countMap.get(currentInput)) + 1);
                                 }
                                 Iterator<InputLine> iter = countMap.keySet().iterator();
                                 while (iter.hasNext()) {
@@ -318,7 +332,7 @@ public class CustomTxtTrace extends TmfTrace implements ITmfPersistentlyIndexabl
                                 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
                                     currentInput = currentInput.childrenInputs.get(0);
                                     countMap.put(currentInput, 0);
-                                } else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
+                                } else if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMaxCount()) {
                                     if (currentInput.getNextInputs(countMap).size() > 0) {
                                         currentInput = currentInput.getNextInputs(countMap).get(0);
                                         if (countMap.get(currentInput) == null) {
@@ -344,11 +358,11 @@ public class CustomTxtTrace extends TmfTrace implements ITmfPersistentlyIndexabl
                         final Matcher matcher = currentInput.getPattern().matcher(line);
                         if (matcher.matches()) {
                             event.processGroups(currentInput, matcher);
-                            countMap.put(currentInput, countMap.get(currentInput) + 1);
+                            countMap.put(currentInput, checkNotNull(countMap.get(currentInput)) + 1);
                             if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
                                 currentInput = currentInput.childrenInputs.get(0);
                                 countMap.put(currentInput, 0);
-                            } else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
+                            } else if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMaxCount()) {
                                 if (currentInput.getNextInputs(countMap).size() > 0) {
                                     currentInput = currentInput.getNextInputs(countMap).get(0);
                                     if (countMap.get(currentInput) == null) {
@@ -366,7 +380,7 @@ public class CustomTxtTrace extends TmfTrace implements ITmfPersistentlyIndexabl
                                 }
                             }
                         }
-                        ((StringBuffer) event.getContent().getValue()).append("\n").append(line); //$NON-NLS-1$
+                        ((StringBuffer) event.getContentValue()).append("\n").append(line); //$NON-NLS-1$
                     }
                 }
                 rawPos = fFile.getFilePointer();
@@ -428,10 +442,19 @@ public class CustomTxtTrace extends TmfTrace implements ITmfPersistentlyIndexabl
             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CustomTrace_FileNotFound + ": " + path); //$NON-NLS-1$
         }
         int confidence = 0;
+        try {
+            if (!TmfTraceUtils.isText(file)) {
+                return new TraceValidationStatus(confidence, Activator.PLUGIN_ID);
+            }
+        } catch (IOException e) {
+            Activator.logError("Error validating file: " + path, e); //$NON-NLS-1$
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
+        }
         try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
             int lineCount = 0;
             double matches = 0.0;
             String line = rafile.getNextLine();
+
             while ((line != null) && (lineCount++ < MAX_LINES)) {
                 for (InputLine inputLine : fDefinition.inputs) {
                     Matcher matcher = inputLine.getPattern().matcher(line);
@@ -539,4 +562,22 @@ public class CustomTxtTrace extends TmfTrace implements ITmfPersistentlyIndexabl
         }
         return traceTypeId;
     }
+
+    @TmfSignalHandler
+    @Override
+    public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal) {
+        if (signal.getTrace() == this) {
+            try {
+                synchronized (this) {
+                    // Reset the file handle in case it has reached the end of the
+                    // file already. Otherwise, it will not be able to read new data
+                    // pass the previous end.
+                    initFile();
+                }
+            } catch (TmfTraceException e) {
+                Activator.logError(e.getLocalizedMessage(), e);
+            }
+        }
+        super.traceRangeUpdated(signal);
+    }
 }
This page took 0.026361 seconds and 5 git commands to generate.