001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017
018package org.apache.commons.exec.launcher;
019
020import java.io.File;
021import java.io.IOException;
022import java.nio.file.Path;
023import java.util.Map;
024
025import org.apache.commons.exec.CommandLine;
026
027/**
028 * Abstracts platform-dependent implementations.
029 */
030public interface CommandLauncher {
031
032    /**
033     * Executes the given command in a new process.
034     *
035     * @param commandLine The command to execute.
036     * @param env         The environment for the new process. If null, the environment of the current process is used.
037     * @return the newly created process.
038     * @throws IOException if attempting to run a command in a specific directory.
039     */
040    Process exec(final CommandLine commandLine, final Map<String, String> env) throws IOException;
041
042    /**
043     * Executes the given command in a new process, in the given working directory.
044     *
045     * @param commandLine      The command to execute.
046     * @param env              The environment for the new process. If null, the environment of the current process is used.
047     * @param workingDirectory The directory to start the command in. If null, the current directory is used.
048     * @return the newly created process.
049     * @throws IOException if trying to change directory.
050     */
051    Process exec(final CommandLine commandLine, final Map<String, String> env, final File workingDirectory) throws IOException;
052
053    /**
054     * Executes the given command in a new process, in the given working directory.
055     *
056     * @param commandLine      The command to execute.
057     * @param env              The environment for the new process. If null, the environment of the current process is used.
058     * @param workingDirectory The directory to start the command in. If null, the current directory is used.
059     * @return the newly created process.
060     * @throws IOException if trying to change directory.
061     * @since 1.5.0
062     */
063    default Process exec(final CommandLine commandLine, final Map<String, String> env, final Path workingDirectory) throws IOException {
064        return exec(commandLine, env, workingDirectory != null ? workingDirectory.toFile() : null);
065    }
066
067    /**
068     * Tests whether {@code exitValue} signals a failure on the current system (OS specific).
069     * <p>
070     * <strong>Note</strong> that this method relies on the conventions of the OS, it will return false results if the application you are running doesn't
071     * follow these conventions. One notable exception is the Java VM provided by HP for OpenVMS - it will return 0 if successful (like on any other platform),
072     * but this signals a failure on OpenVMS. So if you execute a new Java VM on OpenVMS, you cannot trust this method.
073     * </p>
074     *
075     * @param exitValue the exit value (return code) to be checked.
076     * @return {@code true} if {@code exitValue} signals a failure.
077     */
078    boolean isFailure(final int exitValue);
079}