comparison flys-artifacts/src/main/java/de/intevation/flys/wsplgen/WSPLGENCallable.java @ 1970:368040e5c400

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

http://dive4elements.wald.intevation.org