view flys-artifacts/src/main/java/de/intevation/flys/wsplgen/WSPLGENCallable.java @ 4241:49cb65d5932d

Improved the historical discharge calculation. The calculation now creates new HistoricalWQKms (new subclass of WQKms). Those WQKms are used to create new facets from (new) type 'HistoricalDischargeCurveFacet'. The chart generator is improved to support those facets.
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 24 Oct 2012 14:34:35 +0200
parents 453d2d0c4258
children b87073a05f9d
line wrap: on
line source
package de.intevation.flys.wsplgen;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;

import org.apache.log4j.Logger;

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


/**
 * A Callable that is used to start and observe an external Process for WSPLGEN.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class WSPLGENCallable implements Callable {

    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(WSPLGENCallable.class);

    private Process process;

    protected Scheduler scheduler;

    protected WSPLGENJob job;

    protected JobObserver     logObserver;
    protected ProblemObserver errorObserver;


    public WSPLGENCallable(Scheduler scheduler, WSPLGENJob job) {
        this.scheduler     = scheduler;
        this.job           = job;
        this.logObserver   = new JobObserver(job);
        this.errorObserver = new ProblemObserver(job);
    }


    @Override
    public WSPLGENJob call() {
        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);

        return job;
    }


    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.warn("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);

                scheduler.removeJob(getJob().getArtifact().identifier());

                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);
        }
    }


    public void cancelWSPLGEN() {
        if (process != null) {
            logger.debug("Cancel running WSPLGEN process.");
            process.destroy();
        }
    }


    public WSPLGENJob getJob() {
        return job;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org