Path: blob/master/test/langtools/tools/lib/toolbox/ExecTask.java
41152 views
/*1* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package toolbox;2425import java.io.IOException;26import java.nio.file.Path;27import java.util.ArrayList;28import java.util.Arrays;29import java.util.List;3031/**32* A task to configure and run a general command.33*/34public class ExecTask extends AbstractTask<ExecTask> {35private final String command;36private List<String> args;3738/**39* Create a task to execute a given command, to be run using {@code EXEC} mode.40* @param toolBox the {@code ToolBox} to use41* @param command the command to be executed42*/43public ExecTask(ToolBox toolBox, String command) {44super(toolBox, Task.Mode.EXEC);45this.command = command;46}4748/**49* Create a task to execute a given command, to be run using {@code EXEC} mode.50* @param toolBox the {@code ToolBox} to use51* @param command the command to be executed52*/53public ExecTask(ToolBox toolBox, Path command) {54super(toolBox, Task.Mode.EXEC);55this.command = command.toString();56}5758/**59* Sets the arguments for the command to be executed60* @param args the arguments61* @return this task object62*/63public ExecTask args(String... args) {64this.args = Arrays.asList(args);65return this;66}6768/**69* {@inheritDoc}70* @return the name "exec"71*/72@Override73public String name() {74return "exec";75}7677/**78* Calls the command with the arguments as currently configured.79* @return a Result object indicating the outcome of the task80* and the content of any output written to stdout or stderr.81* @throws TaskError if the outcome of the task is not as expected.82*/83@Override84public Task.Result run() {85List<String> cmdArgs = new ArrayList<>();86cmdArgs.add(command);87if (args != null)88cmdArgs.addAll(args);89ProcessBuilder pb = getProcessBuilder();90pb.command(cmdArgs);91try {92return runProcess(toolBox, this, pb.start());93} catch (IOException | InterruptedException e) {94throw new Error(e);95}96}97}9899100