langtools/test/tools/lib/toolbox/ExecTask.java
changeset 36778 e04318f39f92
equal deleted inserted replaced
36777:28d33fb9097f 36778:e04318f39f92
       
     1 /*
       
     2  * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 package toolbox;
       
    25 
       
    26 import java.io.IOException;
       
    27 import java.nio.file.Path;
       
    28 import java.util.ArrayList;
       
    29 import java.util.Arrays;
       
    30 import java.util.List;
       
    31 
       
    32 /**
       
    33  * A task to configure and run a general command.
       
    34  */
       
    35 public class ExecTask extends AbstractTask<ExecTask> {
       
    36     private final String command;
       
    37     private List<String> args;
       
    38 
       
    39     /**
       
    40      * Create a task to execute a given command, to be run using {@code EXEC} mode.
       
    41      * @param toolBox the {@code ToolBox} to use
       
    42      * @param command the command to be executed
       
    43      */
       
    44     public ExecTask(ToolBox toolBox, String command) {
       
    45         super(toolBox, Task.Mode.EXEC);
       
    46         this.command = command;
       
    47     }
       
    48 
       
    49     /**
       
    50      * Create a task to execute a given command, to be run using {@code EXEC} mode.
       
    51      * @param toolBox the {@code ToolBox} to use
       
    52      * @param command the command to be executed
       
    53      */
       
    54     public ExecTask(ToolBox toolBox, Path command) {
       
    55         super(toolBox, Task.Mode.EXEC);
       
    56         this.command = command.toString();
       
    57     }
       
    58 
       
    59     /**
       
    60      * Sets the arguments for the command to be executed
       
    61      * @param args the arguments
       
    62      * @return this task object
       
    63      */
       
    64     public ExecTask args(String... args) {
       
    65         this.args = Arrays.asList(args);
       
    66         return this;
       
    67     }
       
    68 
       
    69     /**
       
    70      * {@inheritDoc}
       
    71      * @return the name "exec"
       
    72      */
       
    73     @Override
       
    74     public String name() {
       
    75         return "exec";
       
    76     }
       
    77 
       
    78     /**
       
    79      * Calls the command with the arguments as currently configured.
       
    80      * @return a Result object indicating the outcome of the task
       
    81      * and the content of any output written to stdout or stderr.
       
    82      * @throws TaskError if the outcome of the task is not as expected.
       
    83      */
       
    84     @Override
       
    85     public Task.Result run() {
       
    86         List<String> cmdArgs = new ArrayList<>();
       
    87         cmdArgs.add(command);
       
    88         if (args != null)
       
    89             cmdArgs.addAll(args);
       
    90         ProcessBuilder pb = getProcessBuilder();
       
    91         pb.command(cmdArgs);
       
    92         try {
       
    93             return runProcess(toolBox, this, pb.start());
       
    94         } catch (IOException | InterruptedException e) {
       
    95             throw new Error(e);
       
    96         }
       
    97     }
       
    98 }