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