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 = 1L;

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

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

	@Override
	public String logInfo() {
		StringBuffer log = new StringBuffer("ShellProcessor\n");
		log.append(aSystemCall);
		log.append("\nError: ");
		log.append(aErrors);
		log.append("\n");
		return log.toString();
	}

}
