package de.hsadmin.core.qserv;


/**
 * A ShellProcessor encapsulates a shell command as a Processor.
 * 
 * As such, a client can prepare shell commands to be processed on a server.
 * 
 * @author mi
 */
public class ShellProcessor extends AbstractProcessor {

	private static final long serialVersionUID = -649045174380048818L;

	private String aSystemCall;
	private String aInput;
	private String aOutput;
	private String aErrors;

	public ShellProcessor(String aSystemCall, String aInput) {
		this.aSystemCall = aSystemCall;
		this.aInput = aInput;
	}

	public ShellProcessor(String aSystemCall) {
		this.aSystemCall = aSystemCall;
		this.aInput = null;
	}

	public Object process() throws ProcessorException {
		try {
			aOutput = CommandShell.execute(aSystemCall, aInput);
			return aOutput;
		} catch (ShellException aExc) {
			aErrors = aExc.getMessage();
			aExc.printStackTrace(System.err); // Logging
			throw new ProcessorException(aExc);
		}
	}

	/**
	 * @return Returns stderr as a String.
	 */
	public String getErrors() {
		return aErrors;
	}

	/**
	 * @return Returns stdout as a String.
	 */
	public String getOutput() {
		return aOutput;
	}

}
