view flys-artifacts/src/main/java/de/intevation/flys/wsplgen/JobExecutor.java @ 3807:d73c43798a99 pre2.6-2011-11-04

merged flys-backend/pre2.6-2011-11-04
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:51 +0200
parents ef2300b450bf
children
line wrap: on
line source
package de.intevation.flys.wsplgen;

import java.io.IOException;
import java.io.File;

import org.apache.log4j.Logger;

import de.intevation.artifacts.CallContext;

import de.intevation.flys.artifacts.model.WSPLGENJob;


public class JobExecutor {

    public static final String WSPLGEN_PARAMETER_FILE =
        "wsplgen.par";

    public static final String WSPLGEN_BIN_PATH =
        System.getProperty("wsplgen.bin.path");


    private Logger logger = Logger.getLogger(JobExecutor.class);

    private Process process;

    protected WSPLGENJob job;

    protected JobObserver     logObserver;
    protected ProblemObserver errorObserver;


    public JobExecutor(WSPLGENJob job) {
        this.job           = job;
        this.logObserver   = new JobObserver(job);
        this.errorObserver = new ProblemObserver(job);
    }


    public void execute() {
        File dir       = job.getWorkingDir();
        File parameter = new File(dir, WSPLGEN_PARAMETER_FILE);

        String[] args = new String[] {
            WSPLGEN_BIN_PATH,
            "-PAR=\"" + parameter.getAbsolutePath() + "\""
        };

        execute(args, dir);
    }


    protected void execute(String[] args, File dir) {
        logger.info("Start JobExecutor for artifact: " + dir.getName());

        String errorMsg = null;

        try {
            synchronized (this) {
                process = Runtime.getRuntime().exec(args, null, dir);

                logObserver.setInputStream(process.getInputStream());
                errorObserver.setInputStream(process.getErrorStream());

                logObserver.start();
                errorObserver.start();

                try {
                    process.waitFor();
                }
                catch (InterruptedException ie) {
                    logger.error("WSPLGEN job interrupted: " + ie.getMessage());
                }

                try {
                    logObserver.join();
                    errorObserver.join();
                }
                catch (InterruptedException iee) { /* do nothing */ }

                logger.info("WSPLGEN exit value: " + process.exitValue());
                logger.info(
                    "WSPLGEN throw " +
                    errorObserver.numErrors() + " errors.");
                logger.info(
                    "WSPLGEN throw " +
                    errorObserver.numWarnings() + " warnings.");

                if (process.exitValue() < 2 && errorObserver.numErrors() == 0) {
                    FacetCreator fc = job.getFacetCreator();
                    fc.createWSPLGENFacet();
                    fc.finish();
                }

                job.getCallContext().afterBackground(CallContext.STORE);

                return;
            }
        }
        catch (SecurityException se) {
            logger.error(se);
        }
        catch (IOException ioe) {
            logger.error(ioe);
        }
        catch (NullPointerException npe) {
            logger.error(npe, npe);
        }
        catch (IndexOutOfBoundsException ioobe) {
            logger.error(ioobe, ioobe);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org