package com.waldura.exec; import java.io.IOException; /** * Represents the execution of an external processes. * Descendants will extend this class in order to provide process execution * from specific platforms or shell/command-line environments. * * @author Renaud Waldura */ public abstract class Exec { /** * The exit value returned from the getExitValue() method * if the process execution has not yet completed. */ public static final int NOT_COMPLETED = 255; /** * The field of the Command property. * * @see getCommand() */ private final String[] command; /** * The field of the Wait property. * * @see getWait() */ private boolean wait = true; /** * The field of the Verbose property. * * @see getVerbose() */ private boolean verbose = false; /** * The field of the ExitValue proeprty. * * @see ExitValue() */ private int exitValue = Exec.NOT_COMPLETED; /** * Private constructor. */ protected Exec(String[] command) { this.command = command; } /** * The first item of the array has the name of the executable, * and the remaining items have the arguments to be passed to the running process. */ public String[] getCommand() { return command; } /** * Tells this class whether the execute method should wait for the process * to finish executing or if it should return right away. */ public boolean getWait() { return wait; } /** * Tells this class whether the execute method should redirect * the standard and error outputs of the process to be executed * to the respective outputs of this process (the one that is running the subprocess). */ public boolean getVerbose() { return verbose; } /** * Returns the exit value of the process execution previously spawned. *

* It will return the value of the Exec.NOT_COMPLETED constant * if the process has not yet completed. *

* If you are not waiting for the process to finish, the exit value might * not be updated with the actual exit value of the process (implementors * do not have to do it). * * @return The exit value of the process execution. */ public int getExitValue() { return exitValue; } /** * The setter of the Wait property. * * @see #getWait() */ public void setWait(boolean newValue) { wait = newValue; } /** * The setter of the Verbose property. * * @see #getVerbose() */ public void setVerbose(boolean newValue) { verbose = newValue; } /** * The setter of the ExitValue property. *

* This setter is a protected method because only the descendants of this class should change * this property in the overriden execute() method. * * @see #getExitValue() */ protected void setExitValue(int newValue) { exitValue = newValue; } /** * Executes the process specified by the Command property, * and waits for its completion if the Wait property is * set to true; otherwise, returns right away and leaves the process executing. * * @throws IOException Unable to execute the process or error during the process execution. */ public abstract void execute() throws IOException; /** * Returns an instance of this class prepared to execute the process * specified by the command. Note: if the platform we are * currently running on is unknown to us, we default to trying * the {@link CertifiedBourneExec CertifiedBourneExec} (the Unix launcher). * * @param command see {@link #getCommand() getCommand} */ public static Exec newInstance(String command[]) { final Exec exec; if (command.length > 0 && command[0].endsWith(".class")) { exec = new JavaClassExec(command); } else if (Platform.getCurrent().isWindows()) { exec = new CertifiedExitValExec(command); } else if (Platform.getCurrent().isMacintosh()) { if (Platform.getCurrent().isMacOSX()) { exec = new MacOSXExec(command); } else { exec = new CertifiedExitValExec(command); } } else if (Platform.getCurrent().isUnix()) { exec = new CertifiedBourneExec(command); } else { // unknown platform; try the CBE exec = new CertifiedBourneExec(command); } return exec; } }