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

csip.Executable Maven / Gradle / Ivy

Go to download

The Cloud Services Integration Platform is a SoA implementation to offer a Model-as-a-Service framework, Application Programming Interface, deployment infrastructure, and service implementations for environmental modeling.

There is a newer version: 2.6.30
Show newest version
/*
 * $Id: Executable.java 9e04709703fa 2020-04-27 od $
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2019, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Interface to manage an executable.
 *
 * @author od
 */
public interface Executable {

  public static final int TIMED_OUT = 1024;

  /**
   * Receives output for stderr or stdout. Allows to parse the process output
   * during execution as it becomes available.
   */
  interface StdHandler {

    /**
     * The output string. This is not a single line! Could be multiple lines. Be
     * careful what you are doing here. Do not spend too much time in this
     * method.
     *
     * @param out the current multi-line output string. Output lines are always
     * complete.
     */
    void handle(String out);
  }


  /**
   * Get the name of the executable.
   * @return the name of the executable
   */
  String getName();


  /**
   * Set the executable arguments.
   *
   * @param args the executable arguments
   */
  void setArguments(Object... args);


  /**
   * Add additional arguments.
   * @param args the command line arguments.
   */
  void addArguments(Object... args);


  /**
   * Get the executables arguments
   * @return the arguments
   */
  Object[] getArguments();


  /**
   * Get the current environment map
   *
   * @return the environment.
   */
  Map environment();


  /**
   * Redirect stdout to a file in the workspace.
   *
   * @param filename the filename to use relative to the workspace.
   * @throws IOException if redirection fails.
   */
  void redirectOutput(String filename) throws IOException;


  /**
   * Redirect the output to a string writer
   * @param w the StringWriter
   * @throws IOException if redirection fails.
   */
  void redirectOutput(StringWriter w) throws IOException;


  /**
   * Redirect stderr to a file in the workspace.
   *
   * @param filename the filename to use relative to the workspace.
   * @throws java.io.IOException if redirection fails.
   */
  void redirectError(String filename) throws IOException;


  /**
   * Redirect the error stream to a string writer
   * @param w the StringWriter
   * @throws IOException if redirection fails
   */
  void redirectError(StringWriter w) throws IOException;


  /**
   * redirect to default files and append. Call this before repeated execution.
   *
   * @throws IOException if redirection fails
   */
  void redirectDefaults() throws IOException;


  /**
   * Set the timeout.
   *
   * @param time the amount of the time unit.
   * @param unit the TimeUnit
   */
  void setTimeout(long time, TimeUnit unit);


  /**
   * run the executable.
   *
   * @return 0 if successful, !=0 otherwise
   * @throws java.io.IOException if an IO error occurs.
   */
  int exec() throws IOException;


  /**
   * Get the File for stdio. The File is unique for each instance of this class.
   * name : 'executable name'-'hash code'-stdout.exe
   *
   * @return The file with the output.
   */
  File stdout();


  /**
   * Get the File for stderr. The File is unique for each instance of this
   * class. name : 'executable name'-'hash code'-stdout.exe
   *
   * @return The
   */
  File stderr();


  /**
   * Handle the current output from stdout.
   *
   * @param handler the handler object. Pass 'null' for removing an existing
   * handler.
   */
  void setStdoutHandler(StdHandler handler);


  /**
   * Handle the current output from stderr.
   *
   * @param handler the handler object. Pass 'null' for removing an existing
   * handler.
   */
  void setStderrHandler(StdHandler handler);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy