Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/wsplgen/WSPLGENCallable.java @ 3468:f37e7e8907cb
merged flys-artifacts/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:39 +0200 |
parents | 453d2d0c4258 |
children | b87073a05f9d |
comparison
equal
deleted
inserted
replaced
3387:5ffad8bde8ad | 3468:f37e7e8907cb |
---|---|
1 package de.intevation.flys.wsplgen; | |
2 | |
3 import java.io.File; | |
4 import java.io.IOException; | |
5 import java.util.concurrent.Callable; | |
6 | |
7 import org.apache.log4j.Logger; | |
8 | |
9 import de.intevation.artifacts.CallContext; | |
10 import de.intevation.flys.artifacts.model.map.WSPLGENJob; | |
11 | |
12 | |
13 /** | |
14 * A Callable that is used to start and observe an external Process for WSPLGEN. | |
15 * | |
16 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
17 */ | |
18 public class WSPLGENCallable implements Callable { | |
19 | |
20 public static final String WSPLGEN_PARAMETER_FILE = | |
21 "wsplgen.par"; | |
22 | |
23 public static final String WSPLGEN_BIN_PATH = | |
24 System.getProperty("wsplgen.bin.path"); | |
25 | |
26 | |
27 private Logger logger = Logger.getLogger(WSPLGENCallable.class); | |
28 | |
29 private Process process; | |
30 | |
31 protected Scheduler scheduler; | |
32 | |
33 protected WSPLGENJob job; | |
34 | |
35 protected JobObserver logObserver; | |
36 protected ProblemObserver errorObserver; | |
37 | |
38 | |
39 public WSPLGENCallable(Scheduler scheduler, WSPLGENJob job) { | |
40 this.scheduler = scheduler; | |
41 this.job = job; | |
42 this.logObserver = new JobObserver(job); | |
43 this.errorObserver = new ProblemObserver(job); | |
44 } | |
45 | |
46 | |
47 @Override | |
48 public WSPLGENJob call() { | |
49 File dir = job.getWorkingDir(); | |
50 File parameter = new File(dir, WSPLGEN_PARAMETER_FILE); | |
51 | |
52 String[] args = new String[] { | |
53 WSPLGEN_BIN_PATH, | |
54 "-PAR=\"" + parameter.getAbsolutePath() + "\"" | |
55 }; | |
56 | |
57 execute(args, dir); | |
58 | |
59 return job; | |
60 } | |
61 | |
62 | |
63 protected void execute(String[] args, File dir) { | |
64 logger.info("Start JobExecutor for artifact: " + dir.getName()); | |
65 | |
66 String errorMsg = null; | |
67 | |
68 try { | |
69 synchronized (this) { | |
70 process = Runtime.getRuntime().exec(args, null, dir); | |
71 | |
72 logObserver.setInputStream(process.getInputStream()); | |
73 errorObserver.setInputStream(process.getErrorStream()); | |
74 | |
75 logObserver.start(); | |
76 errorObserver.start(); | |
77 | |
78 try { | |
79 process.waitFor(); | |
80 } | |
81 catch (InterruptedException ie) { | |
82 logger.warn("WSPLGEN job interrupted: " + ie.getMessage()); | |
83 } | |
84 | |
85 try { | |
86 logObserver.join(); | |
87 errorObserver.join(); | |
88 } | |
89 catch (InterruptedException iee) { /* do nothing */ } | |
90 | |
91 logger.info("WSPLGEN exit value: " + process.exitValue()); | |
92 logger.info( | |
93 "WSPLGEN throw " + | |
94 errorObserver.numErrors() + " errors."); | |
95 logger.info( | |
96 "WSPLGEN throw " + | |
97 errorObserver.numWarnings() + " warnings."); | |
98 | |
99 if (process.exitValue() < 2 && errorObserver.numErrors() == 0) { | |
100 FacetCreator fc = job.getFacetCreator(); | |
101 fc.createWSPLGENFacet(); | |
102 fc.finish(); | |
103 } | |
104 | |
105 job.getCallContext().afterBackground(CallContext.STORE); | |
106 | |
107 scheduler.removeJob(getJob().getArtifact().identifier()); | |
108 | |
109 return; | |
110 } | |
111 } | |
112 catch (SecurityException se) { | |
113 logger.error(se); | |
114 } | |
115 catch (IOException ioe) { | |
116 logger.error(ioe); | |
117 } | |
118 catch (NullPointerException npe) { | |
119 logger.error(npe, npe); | |
120 } | |
121 catch (IndexOutOfBoundsException ioobe) { | |
122 logger.error(ioobe, ioobe); | |
123 } | |
124 } | |
125 | |
126 | |
127 public void cancelWSPLGEN() { | |
128 if (process != null) { | |
129 logger.debug("Cancel running WSPLGEN process."); | |
130 process.destroy(); | |
131 } | |
132 } | |
133 | |
134 | |
135 public WSPLGENJob getJob() { | |
136 return job; | |
137 } | |
138 } | |
139 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |