ingo@1127: package de.intevation.flys.wsplgen; ingo@1127: ingo@1127: import java.io.IOException; ingo@1127: import java.io.File; ingo@1127: ingo@1127: import org.apache.log4j.Logger; ingo@1127: ingo@1128: import de.intevation.artifacts.CallContext; ingo@1128: ingo@1127: import de.intevation.flys.artifacts.model.WSPLGENJob; ingo@1127: ingo@1127: ingo@1127: public class JobExecutor { ingo@1127: ingo@1127: public static final String WSPLGEN_PARAMETER_FILE = ingo@1127: "wsplgen.par"; ingo@1127: ingo@1127: public static final String WSPLGEN_BIN_PATH = ingo@1127: System.getProperty("wsplgen.bin.path"); ingo@1127: ingo@1127: ingo@1127: private Logger logger = Logger.getLogger(JobExecutor.class); ingo@1127: ingo@1127: private Process process; ingo@1127: ingo@1127: protected WSPLGENJob job; ingo@1127: ingo@1147: protected JobObserver logObserver; ingo@1147: protected ProblemObserver errorObserver; ingo@1127: ingo@1127: ingo@1127: public JobExecutor(WSPLGENJob job) { ingo@1147: this.job = job; ingo@1147: this.logObserver = new JobObserver(job); ingo@1147: this.errorObserver = new ProblemObserver(job); ingo@1127: } ingo@1127: ingo@1127: ingo@1127: public void execute() { ingo@1127: File dir = job.getWorkingDir(); ingo@1127: File parameter = new File(dir, WSPLGEN_PARAMETER_FILE); ingo@1127: ingo@1127: String[] args = new String[] { ingo@1127: WSPLGEN_BIN_PATH, ingo@1127: "-PAR=\"" + parameter.getAbsolutePath() + "\"" ingo@1127: }; ingo@1127: ingo@1147: execute(args, dir); ingo@1127: } ingo@1127: ingo@1127: ingo@1147: protected void execute(String[] args, File dir) { ingo@1127: logger.info("Start JobExecutor for artifact: " + dir.getName()); ingo@1127: ingo@1127: String errorMsg = null; ingo@1127: ingo@1127: try { ingo@1127: synchronized (this) { ingo@1127: process = Runtime.getRuntime().exec(args, null, dir); ingo@1127: ingo@1147: logObserver.setInputStream(process.getInputStream()); ingo@1147: errorObserver.setInputStream(process.getErrorStream()); ingo@1127: ingo@1147: logObserver.start(); ingo@1147: errorObserver.start(); ingo@1127: ingo@1127: try { ingo@1127: process.waitFor(); ingo@1127: } ingo@1127: catch (InterruptedException ie) { ingo@1127: logger.error("WSPLGEN job interrupted: " + ie.getMessage()); ingo@1127: } ingo@1127: ingo@1127: try { ingo@1147: logObserver.join(); ingo@1147: errorObserver.join(); ingo@1127: } ingo@1127: catch (InterruptedException iee) { /* do nothing */ } ingo@1127: ingo@1127: logger.info("WSPLGEN exit value: " + process.exitValue()); ingo@1147: logger.info( ingo@1147: "WSPLGEN throw " + ingo@1147: errorObserver.numErrors() + " errors."); ingo@1147: logger.info( ingo@1147: "WSPLGEN throw " + ingo@1147: errorObserver.numWarnings() + " warnings."); ingo@1127: ingo@1650: if (process.exitValue() < 2 && errorObserver.numErrors() == 0) { ingo@1650: FacetCreator fc = job.getFacetCreator(); ingo@1650: fc.createWSPLGENFacet(); ingo@1650: fc.finish(); ingo@1650: } ingo@1650: ingo@1650: job.getCallContext().afterBackground(CallContext.STORE); ingo@1130: ingo@1127: return; ingo@1127: } ingo@1127: } ingo@1127: catch (SecurityException se) { ingo@1128: logger.error(se); ingo@1127: } ingo@1127: catch (IOException ioe) { ingo@1128: logger.error(ioe); ingo@1127: } ingo@1127: catch (NullPointerException npe) { ingo@1128: logger.error(npe, npe); ingo@1127: } ingo@1127: catch (IndexOutOfBoundsException ioobe) { ingo@1128: logger.error(ioobe, ioobe); ingo@1127: } ingo@1127: } ingo@1127: } ingo@1127: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :