Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/wsplgen/JobObserver.java @ 3818:dc18457b1cef
merged flys-artifacts/pre2.7-2012-03-16
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:59 +0200 |
parents | a470b7c3b165 |
children | 453d2d0c4258 |
comparison
equal
deleted
inserted
replaced
2456:60ab1054069d | 3818:dc18457b1cef |
---|---|
1 package de.intevation.flys.wsplgen; | |
2 | |
3 import java.io.BufferedReader; | |
4 import java.io.InputStream; | |
5 import java.io.InputStreamReader; | |
6 import java.io.IOException; | |
7 import java.util.regex.Matcher; | |
8 import java.util.regex.Pattern; | |
9 | |
10 import org.apache.log4j.Logger; | |
11 | |
12 import de.intevation.flys.artifacts.model.CalculationMessage; | |
13 import de.intevation.flys.artifacts.model.WSPLGENJob; | |
14 | |
15 | |
16 public class JobObserver extends Thread { | |
17 | |
18 private static Logger logger = Logger.getLogger(JobObserver.class); | |
19 | |
20 | |
21 public static final String WSPLGEN_ENCODING = | |
22 "ISO-8859-1"; | |
23 | |
24 public static final String WSPLGEN_LOG_OUTPUT = | |
25 System.getProperty("wsplgen.log.output", "false"); | |
26 | |
27 public static final String[] STEPS = { | |
28 ".*<-Auswertung der Kommandozeilen-Parameter beendet.*", | |
29 ".*->Laden des DGM in Datei '.*' gestartet.*", | |
30 ".*->Triangulierung der Knoten gestartet.*", | |
31 ".*->Anpassung der Elemente an Dämme und Gräben gestartet.*", | |
32 ".*<-WSPLGEN Version .* beendet.*" | |
33 }; | |
34 | |
35 | |
36 protected WSPLGENJob job; | |
37 | |
38 protected InputStream in; | |
39 | |
40 protected Pattern[] patterns; | |
41 | |
42 protected int len; | |
43 | |
44 protected boolean copy; | |
45 | |
46 | |
47 public JobObserver(WSPLGENJob job) { | |
48 this.job = job; | |
49 this.len = 0; | |
50 this.copy = Boolean.parseBoolean(WSPLGEN_LOG_OUTPUT); | |
51 | |
52 patterns = new Pattern[STEPS.length]; | |
53 } | |
54 | |
55 | |
56 protected void prepareRegexes() { | |
57 for (int num = STEPS.length, i = 0; i < num; i++) { | |
58 patterns[i] = Pattern.compile(STEPS[i], Pattern.DOTALL); | |
59 } | |
60 } | |
61 | |
62 | |
63 public void setInputStream(InputStream in) { | |
64 this.in = in; | |
65 } | |
66 | |
67 | |
68 public void run() { | |
69 logger.debug("Start observation..."); | |
70 prepareRegexes(); | |
71 | |
72 try { | |
73 BufferedReader reader = | |
74 new BufferedReader( | |
75 new InputStreamReader(in, WSPLGEN_ENCODING)); | |
76 | |
77 String line = null; | |
78 | |
79 while ((line = reader.readLine()) != null) { | |
80 if (copy) { | |
81 logger.debug(line); | |
82 } | |
83 | |
84 update(line); | |
85 } | |
86 } | |
87 catch (IOException ioe) { | |
88 logger.warn("Observation canceled: " + ioe.getMessage()); | |
89 } | |
90 } | |
91 | |
92 | |
93 protected void update(String log) { | |
94 for (int num = patterns.length, i = 0; i < num; i++) { | |
95 Matcher m = patterns[i].matcher(log); | |
96 | |
97 if (m.matches()) { | |
98 job.getCallContext().addBackgroundMessage( | |
99 new CalculationMessage(num, i+1, log)); | |
100 | |
101 logger.info("Finished step " + (i+1) + " / " + num); | |
102 } | |
103 } | |
104 } | |
105 } | |
106 // vim:set ts=4 sw=4 si et sta sts=5 fenc=utf-8 : |