changeset 1147:e7d5452a7381

Added a 'ProblemObserver' that analyses WSPLGEN's error stream for errors and warnings. flys-artifacts/trunk@2677 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 08 Sep 2011 11:24:27 +0000
parents 66d98964f2df
children 302461d5d071
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/wsplgen/JobExecutor.java flys-artifacts/src/main/java/de/intevation/flys/wsplgen/JobObserver.java flys-artifacts/src/main/java/de/intevation/flys/wsplgen/ProblemObserver.java
diffstat 4 files changed, 144 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Sep 08 11:22:35 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Sep 08 11:24:27 2011 +0000
@@ -1,3 +1,16 @@
+2011-09-08  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/wsplgen/JobObserver.java: Small
+	  adjustments that makes it easier to subclass this observer.
+
+	* src/main/java/de/intevation/flys/wsplgen/ProblemObserver.java: New. A
+	  sublcass of JobObserver which analyses the WSPLGEN output for errors and
+	  warnings.
+
+	* src/main/java/de/intevation/flys/wsplgen/JobExecutor.java: Use
+	  JobObserver and ProblemObserver to track the whole WSPLGEN output and
+	  print number of errors/warnings to log output.
+
 2011-09-08  Felix Wolfsteller <felix.wolfsteller@intevation.de>
 
 	Added translation of w_differences.
--- a/flys-artifacts/src/main/java/de/intevation/flys/wsplgen/JobExecutor.java	Thu Sep 08 11:22:35 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/wsplgen/JobExecutor.java	Thu Sep 08 11:24:27 2011 +0000
@@ -1,6 +1,5 @@
 package de.intevation.flys.wsplgen;
 
-import java.io.InputStream;
 import java.io.IOException;
 import java.io.File;
 
@@ -28,12 +27,14 @@
 
     protected WSPLGENJob job;
 
-    protected JobObserver observer;
+    protected JobObserver     logObserver;
+    protected ProblemObserver errorObserver;
 
 
     public JobExecutor(WSPLGENJob job) {
-        this.job      = job;
-        this.observer = new JobObserver(job);
+        this.job           = job;
+        this.logObserver   = new JobObserver(job);
+        this.errorObserver = new ProblemObserver(job);
     }
 
 
@@ -46,11 +47,11 @@
             "-PAR=\"" + parameter.getAbsolutePath() + "\""
         };
 
-        execute(args, dir, observer);
+        execute(args, dir);
     }
 
 
-    public void execute(String[] args, File dir, JobObserver observer) {
+    protected void execute(String[] args, File dir) {
         logger.info("Start JobExecutor for artifact: " + dir.getName());
 
         String errorMsg = null;
@@ -59,10 +60,11 @@
             synchronized (this) {
                 process = Runtime.getRuntime().exec(args, null, dir);
 
-                InputStream out = process.getInputStream();
-                observer.setInputStream(out);
+                logObserver.setInputStream(process.getInputStream());
+                errorObserver.setInputStream(process.getErrorStream());
 
-                observer.start();
+                logObserver.start();
+                errorObserver.start();
 
                 try {
                     process.waitFor();
@@ -72,13 +74,20 @@
                 }
 
                 try {
-                    observer.join();
+                    logObserver.join();
+                    errorObserver.join();
                 }
                 catch (InterruptedException iee) { /* do nothing */ }
 
                 job.getCallContext().afterBackground(CallContext.STORE);
 
                 logger.info("WSPLGEN exit value: " + process.exitValue());
+                logger.info(
+                    "WSPLGEN throw " +
+                    errorObserver.numErrors() + " errors.");
+                logger.info(
+                    "WSPLGEN throw " +
+                    errorObserver.numWarnings() + " warnings.");
 
                 MapfileGenerator.getInstance().update();
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/wsplgen/JobObserver.java	Thu Sep 08 11:22:35 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/wsplgen/JobObserver.java	Thu Sep 08 11:24:27 2011 +0000
@@ -49,14 +49,12 @@
         this.copy = Boolean.parseBoolean(WSPLGEN_LOG_OUTPUT);
 
         patterns = new Pattern[STEPS.length];
-
-        prepareRegexes();
     }
 
 
-    private void prepareRegexes() {
+    protected void prepareRegexes() {
         for (int num = STEPS.length, i = 0; i < num; i++) {
-            patterns[i] = Pattern.compile(STEPS[i]);
+            patterns[i] = Pattern.compile(STEPS[i], Pattern.DOTALL);
         }
     }
 
@@ -67,8 +65,10 @@
 
 
     public void run() {
+        logger.debug("Start observation...");
+        prepareRegexes();
+
         try {
-
             BufferedReader reader =
                 new BufferedReader(
                     new InputStreamReader(in, WSPLGEN_ENCODING));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/wsplgen/ProblemObserver.java	Thu Sep 08 11:24:27 2011 +0000
@@ -0,0 +1,107 @@
+package de.intevation.flys.wsplgen;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.artifacts.model.WSPLGENJob;
+
+
+public class ProblemObserver extends JobObserver {
+
+    private static Logger logger = Logger.getLogger(ProblemObserver.class);
+
+
+    public static final Pattern WSPLGEN_ERROR_START = Pattern.compile(
+        ".*->Fehler\\s*\\((\\d+)\\).*",
+        Pattern.DOTALL);
+
+    public static final Pattern WSPLGEN_ERROR_END = Pattern.compile(
+        ".*<-Fehler .*",
+        Pattern.DOTALL);
+
+    public static final Pattern WSPLGEN_WARNING_START = Pattern.compile(
+        ".*->Warnung\\s*\\((\\d+)\\).*",
+        Pattern.DOTALL);
+
+    public static final Pattern WSPLGEN_WARNING_END = Pattern.compile(
+        ".*<-Warnung .*",
+        Pattern.DOTALL);
+
+
+    protected int error;
+    protected int warning;
+
+    protected Map<Integer, String> errorMsg;
+    protected Map<Integer, String> warningMsg;
+
+
+    public ProblemObserver(WSPLGENJob job) {
+        super(job);
+        error      = -1;
+        warning    = -1;
+        errorMsg   = new HashMap<Integer, String>();
+        warningMsg = new HashMap<Integer, String>();
+    }
+
+
+    public void run() {
+        logger.debug("Start observation...");
+
+        super.run();
+    }
+
+
+    @Override
+    protected void prepareRegexes() {
+        // do nothing
+    }
+
+
+    @Override
+    protected void update(String log) {
+        Matcher startError = WSPLGEN_ERROR_START.matcher(log);
+        if (startError.matches()) {
+            error = Integer.parseInt(startError.group(1));
+            return;
+        }
+
+        Matcher endError = WSPLGEN_ERROR_END.matcher(log);
+        if (endError.matches()) {
+            error = -1;
+        }
+
+        if (error > 0) {
+            errorMsg.put(new Integer(error), log);
+        }
+
+        Matcher startWarning = WSPLGEN_WARNING_START.matcher(log);
+        if (startWarning.matches()) {
+            warning = Integer.parseInt(startWarning.group(1));
+            return;
+        }
+
+        Matcher endWarning = WSPLGEN_WARNING_END.matcher(log);
+        if (endWarning.matches()) {
+            warning = -1;
+        }
+
+        if (warning > 0) {
+            warningMsg.put(new Integer(warning), log);
+        }
+    }
+
+
+    public int numErrors() {
+        return errorMsg.size();
+    }
+
+
+    public int numWarnings() {
+        return warningMsg.size();
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=5 fenc=utf-8 :

http://dive4elements.wald.intevation.org