Executor.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */

  17. package org.apache.commons.exec;

  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.nio.file.Path;
  21. import java.util.Map;

  22. /**
  23.  * The main abstraction to start an external process.
  24.  *
  25.  * The interface allows to:
  26.  * <ul>
  27.  * <li>set a current working directory for the subprocess</li>
  28.  * <li>provide a set of environment variables passed to the subprocess</li>
  29.  * <li>capture the subprocess output of stdout and stderr using an ExecuteStreamHandler</li>
  30.  * <li>kill long-running processes using an ExecuteWatchdog</li>
  31.  * <li>define a set of expected exit values</li>
  32.  * <li>terminate any started processes when the main process is terminating using a ProcessDestroyer</li>
  33.  * </ul>
  34.  * <p>
  35.  * The following example shows the basic usage:
  36.  * </p>
  37.  *
  38.  * <pre>
  39.  * Executor exec = DefaultExecutor.builder().get();
  40.  * CommandLine cl = new CommandLine("ls -l");
  41.  * int exitvalue = exec.execute(cl);
  42.  * </pre>
  43.  */

  44. public interface Executor {

  45.     /** Invalid exit code. */
  46.     int INVALID_EXITVALUE = 0xdeadbeef;

  47.     /**
  48.      * Executes a command synchronously. The child process inherits all environment variables of the parent process.
  49.      *
  50.      * @param command the command to execute.
  51.      * @return process exit value.
  52.      * @throws ExecuteException execution of subprocess failed or the subprocess returned a exit value indicating a failure {@link Executor#setExitValue(int)}.
  53.      * @throws IOException      If an I/O error occurs.
  54.      */
  55.     int execute(CommandLine command) throws ExecuteException, IOException;

  56.     /**
  57.      * Executes a command asynchronously. The child process inherits all environment variables of the parent process. Result provided to callback handler.
  58.      *
  59.      * @param command the command to execute.
  60.      * @param handler capture process termination and exit code.
  61.      * @throws ExecuteException execution of subprocess failed.
  62.      * @throws IOException      If an I/O error occurs.
  63.      */
  64.     void execute(CommandLine command, ExecuteResultHandler handler) throws ExecuteException, IOException;

  65.     /**
  66.      * Executes a command synchronously.
  67.      *
  68.      * @param command     the command to execute.
  69.      * @param environment The environment for the new process. If null, the environment of the current process is used.
  70.      * @return process exit value.
  71.      * @throws ExecuteException execution of subprocess failed or the subprocess returned a exit value indicating a failure {@link Executor#setExitValue(int)}.
  72.      * @throws IOException      If an I/O error occurs.
  73.      */
  74.     int execute(CommandLine command, Map<String, String> environment) throws ExecuteException, IOException;

  75.     /**
  76.      * Executes a command asynchronously. The child process inherits all environment variables of the parent process. Result provided to callback handler.
  77.      *
  78.      * @param command     the command to execute.
  79.      * @param environment The environment for the new process. If null, the environment of the current process is used.
  80.      * @param handler     capture process termination and exit code.
  81.      * @throws ExecuteException execution of subprocess failed.
  82.      * @throws IOException      If an I/O error occurs.
  83.      */
  84.     void execute(CommandLine command, Map<String, String> environment, ExecuteResultHandler handler) throws ExecuteException, IOException;

  85.     /**
  86.      * Sets the handler for cleanup of started processes if the main process is going to terminate.
  87.      *
  88.      * @return the ProcessDestroyer.
  89.      */
  90.     ProcessDestroyer getProcessDestroyer();

  91.     /**
  92.      * Gets the StreamHandler used for providing input and retrieving the output.
  93.      *
  94.      * @return the StreamHandler.
  95.      */
  96.     ExecuteStreamHandler getStreamHandler();

  97.     /**
  98.      * Gets the watchdog used to kill of processes running, typically, too long time.
  99.      *
  100.      * @return the watchdog.
  101.      */
  102.     ExecuteWatchdog getWatchdog();

  103.     /**
  104.      * Gets the working directory of the created process.
  105.      *
  106.      * @return the working directory.
  107.      */
  108.     File getWorkingDirectory();

  109.     /**
  110.      * Gets the working directory of the created process.
  111.      *
  112.      * @return the working directory.
  113.      * @since 1.5.0
  114.      */
  115.     default Path getWorkingDirectoryPath() {
  116.         return getWorkingDirectory().toPath();
  117.     }

  118.     /**
  119.      * Tests whether {@code exitValue} signals a failure. If no exit values are set than the default conventions of the OS is used. e.g. most OS regard an exit
  120.      * code of '0' as successful execution and everything else as failure.
  121.      *
  122.      * @param exitValue the exit value (return code) to be checked.
  123.      * @return {@code true} if {@code exitValue} signals a failure.
  124.      */
  125.     boolean isFailure(final int exitValue);

  126.     /**
  127.      * Sets the {@code exitValue} of the process to be considered successful. If a different exit value is returned by the process then
  128.      * {@link org.apache.commons.exec.Executor#execute(CommandLine)} will throw an {@link org.apache.commons.exec.ExecuteException}.
  129.      *
  130.      * @param value the exit code representing successful execution.
  131.      */
  132.     void setExitValue(final int value);

  133.     /**
  134.      * Sets a list of {@code exitValue} of the process to be considered successful. The caller can pass one of the following values.
  135.      * <ul>
  136.      * <li>an array of exit values to be considered successful</li>
  137.      * <li>an empty array for auto-detect of successful exit codes relying on {@link org.apache.commons.exec.Executor#isFailure(int)}</li>
  138.      * <li>null to indicate to skip checking of exit codes</li>
  139.      * </ul>
  140.      *
  141.      * If an undefined exit value is returned by the process then {@link org.apache.commons.exec.Executor#execute(CommandLine)} will throw an
  142.      * {@link org.apache.commons.exec.ExecuteException}.
  143.      *
  144.      * @param values a list of the exit codes.
  145.      */
  146.     void setExitValues(final int[] values);

  147.     /**
  148.      * Sets the handler for cleanup of started processes if the main process is going to terminate.
  149.      *
  150.      * @param processDestroyer the ProcessDestroyer.
  151.      */
  152.     void setProcessDestroyer(ProcessDestroyer processDestroyer);

  153.     /**
  154.      * Sets a custom the StreamHandler used for providing input and retrieving the output. If you don't provide a proper stream handler the executed process
  155.      * might block when writing to stdout and/or stderr (see {@link Process Process}).
  156.      *
  157.      * @param streamHandler the stream handler.
  158.      */
  159.     void setStreamHandler(ExecuteStreamHandler streamHandler);

  160.     /**
  161.      * Sets the watchdog used to kill of processes running, typically, too long time.
  162.      *
  163.      * @param watchDog the watchdog.
  164.      */
  165.     void setWatchdog(ExecuteWatchdog watchDog);

  166.     /**
  167.      * Sets the working directory of the created process. The working directory must exist when you start the process.
  168.      *
  169.      * @param dir the working directory.
  170.      */
  171.     void setWorkingDirectory(File dir);

  172. }