All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.etlunit.feature.informatica.util.PmCmdProcess Maven / Gradle / Ivy

There is a newer version: 1.6.9
Show newest version
package org.etlunit.feature.informatica.util;

import org.etlunit.ProcessDescription;
import org.etlunit.ProcessFacade;
import org.etlunit.RuntimeSupport;
import org.etlunit.io.Expectorator;
import org.etlunit.io.FileBuilder;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.Map;

public class PmCmdProcess
{
	public static final String PMCMD = "pmcmd>";
	public static final String NEWLINE = "\r\n";
	private Expectorator expectorator;
	private final ProcessFacade processFacade;

	public static final long DEFAULT_TIMEOUT = 60000L;

	public PmCmdProcess(
		File pmRep,
		RuntimeSupport runtimeSupport,
		Map environment,
		String identifier,
		File workingDir
	) throws Exception {
		ProcessDescription pd = new ProcessDescription(pmRep.getAbsolutePath());
		pd.getEnvironment().putAll(environment);

		File output = new FileBuilder(runtimeSupport.getTempDirectory())
			.subdir("process_log").subdir("informatica").mkdirs().name("pmcmd-" + identifier + ".out").file();
		pd.output(output).workingDirectory(workingDir);

		try {
			processFacade = runtimeSupport.execute(pd);
			processFacade.waitForStreams();

			expectorator = new Expectorator(
					new BufferedReader(processFacade.getReader()),
					processFacade.getWriter());

			// wait for the first pmcmd prompt
			expectorator.expect(PMCMD, DEFAULT_TIMEOUT);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public String send(String line) throws IOException {
		String resp = send(line, DEFAULT_TIMEOUT);

		return resp;
	}

	public String send(String line, long timeout) throws IOException {
		if (expectorator == null)
		{
			throw new IllegalStateException("Pmcmd client disposed");
		}

		try
		{
			String response = expectorator.sayAndExpect(line + NEWLINE, PMCMD, timeout);

			return response;
		}
		catch (InterruptedException e)
		{
			throw new IllegalStateException("Process timed out:" + line, e);
		}
	}

	public void dispose() throws IOException
	{
		if (expectorator == null)
		{
			throw new IllegalStateException("Pmcmd client disposed");
		}

		expectorator.say("exit");

		processFacade.kill();

		expectorator = null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy