ingo@1127: package de.intevation.flys.wsplgen;
ingo@1127: 
ingo@1127: import java.io.BufferedReader;
ingo@3302: import java.io.IOException;
ingo@1127: import java.io.InputStream;
ingo@1127: import java.io.InputStreamReader;
ingo@1127: import java.util.regex.Matcher;
ingo@1127: import java.util.regex.Pattern;
ingo@1127: 
ingo@1127: import org.apache.log4j.Logger;
ingo@1127: 
ingo@1157: import de.intevation.flys.artifacts.model.CalculationMessage;
ingo@3302: import de.intevation.flys.artifacts.model.map.WSPLGENJob;
ingo@1127: 
ingo@1127: 
ingo@1127: public class JobObserver extends Thread {
ingo@1127: 
ingo@1127:     private static Logger logger = Logger.getLogger(JobObserver.class);
ingo@1127: 
ingo@1127: 
ingo@1127:     public static final String WSPLGEN_ENCODING =
ingo@1127:         "ISO-8859-1";
ingo@1127: 
ingo@1127:     public static final String WSPLGEN_LOG_OUTPUT =
ingo@1127:         System.getProperty("wsplgen.log.output", "false");
ingo@1127: 
ingo@1127:     public static final String[] STEPS = {
ingo@1127:         ".*<-Auswertung der Kommandozeilen-Parameter beendet.*",
ingo@1127:         ".*->Laden des DGM in Datei '.*' gestartet.*",
ingo@1127:         ".*->Triangulierung der Knoten gestartet.*",
ingo@1127:         ".*->Anpassung der Elemente an Dämme und Gräben gestartet.*",
ingo@1127:         ".*<-WSPLGEN Version .* beendet.*"
ingo@1127:     };
ingo@1127: 
ingo@1127: 
ingo@1127:     protected WSPLGENJob job;
ingo@1127: 
ingo@1127:     protected InputStream in;
ingo@1127: 
ingo@1127:     protected Pattern[] patterns;
ingo@1127: 
ingo@1127:     protected int len;
ingo@1127: 
ingo@1127:     protected boolean copy;
ingo@1127: 
ingo@1127: 
ingo@1127:     public JobObserver(WSPLGENJob job) {
ingo@1127:         this.job  = job;
ingo@1127:         this.len  = 0;
ingo@1127:         this.copy = Boolean.parseBoolean(WSPLGEN_LOG_OUTPUT);
ingo@1127: 
ingo@1127:         patterns = new Pattern[STEPS.length];
ingo@1127:     }
ingo@1127: 
ingo@1127: 
ingo@1147:     protected void prepareRegexes() {
ingo@1127:         for (int num = STEPS.length, i = 0; i < num; i++) {
ingo@1147:             patterns[i] = Pattern.compile(STEPS[i], Pattern.DOTALL);
ingo@1127:         }
ingo@1127:     }
ingo@1127: 
ingo@1127: 
ingo@1127:     public void setInputStream(InputStream in) {
ingo@1127:         this.in = in;
ingo@1127:     }
ingo@1127: 
ingo@1127: 
ingo@1127:     public void run() {
ingo@1147:         logger.debug("Start observation...");
ingo@1147:         prepareRegexes();
ingo@1147: 
ingo@1127:         try {
ingo@1127:             BufferedReader reader =
ingo@1127:                 new BufferedReader(
ingo@1127:                     new InputStreamReader(in, WSPLGEN_ENCODING));
ingo@1127: 
ingo@1127:             String line = null;
ingo@1127: 
ingo@1127:             while ((line = reader.readLine()) != null) {
ingo@1127:                 if (copy) {
ingo@1127:                     logger.debug(line);
ingo@1127:                 }
ingo@1127: 
ingo@1127:                 update(line);
ingo@1127:             }
ingo@1127:         }
ingo@1127:         catch (IOException ioe) {
ingo@1127:             logger.warn("Observation canceled: " + ioe.getMessage());
ingo@1127:         }
ingo@1127:     }
ingo@1127: 
ingo@1127: 
ingo@1127:     protected void update(String log) {
ingo@1127:         for (int num = patterns.length, i = 0; i < num; i++) {
ingo@1127:             Matcher m = patterns[i].matcher(log);
ingo@1127: 
ingo@1127:             if (m.matches()) {
ingo@1157:                 job.getCallContext().addBackgroundMessage(
ingo@1157:                     new CalculationMessage(num, i+1, log));
ingo@1157: 
ingo@1127:                 logger.info("Finished step " + (i+1) + " / " + num);
ingo@1127:             }
ingo@1127:         }
ingo@1127:     }
ingo@1127: }
ingo@1127: // vim:set ts=4 sw=4 si et sta sts=5 fenc=utf-8 :