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.util.ArrayList;
  28 import java.util.Arrays;
  29 import java.util.List;
  30 
  31 /**
  32  * A task to configure and run the Java launcher.
  33  */
  34 public class JavaTask extends AbstractTask<JavaTask> {
  35     boolean includeStandardOptions = true;
  36     private String classpath;
  37     private List<String> vmOptions;
  38     private String className;
  39     private List<String> classArgs;
  40 
  41     /**
  42      * Create a task to run the Java launcher, using {@code EXEC} mode.
  43      * @param toolBox the {@code ToolBox} to use
  44      */
  45     public JavaTask(ToolBox toolBox) {
  46         super(toolBox, Task.Mode.EXEC);
  47     }
  48 
  49     /**
  50      * Sets the classpath.
  51      * @param classpath the classpath
  52      * @return this task object
  53      */
  54     public JavaTask classpath(String classpath) {
  55         this.classpath = classpath;
  56         return this;
  57     }
  58 
  59     /**
  60      * Sets the VM options.
  61      * @param vmOptions the options
  62      * @return this task object
  63      */
  64     public JavaTask vmOptions(String... vmOptions) {
  65         this.vmOptions = Arrays.asList(vmOptions);
  66         return this;
  67     }
  68 
  69     /**
  70      * Sets the VM options.
  71      * @param vmOptions the options
  72      * @return this task object
  73      */
  74     public JavaTask vmOptions(List<String> vmOptions) {
  75         this.vmOptions = vmOptions;
  76         return this;
  77     }
  78 
  79     /**
  80      * Sets the name of the class to be executed.
  81      * @param className the name of the class
  82      * @return this task object
  83      */
  84     public JavaTask className(String className) {
  85         this.className = className;
  86         return this;
  87     }
  88 
  89     /**
  90      * Sets the arguments for the class to be executed.
  91      * @param classArgs the arguments
  92      * @return this task object
  93      */
  94     public JavaTask classArgs(String... classArgs) {
  95         this.classArgs = Arrays.asList(classArgs);
  96         return this;
  97     }
  98 
  99     /**
 100      * Sets the arguments for the class to be executed.
 101      * @param classArgs the arguments
 102      * @return this task object
 103      */
 104     public JavaTask classArgs(List<String> classArgs) {
 105         this.classArgs = classArgs;
 106         return this;
 107     }
 108 
 109     /**
 110      * Sets whether or not the standard VM and java options for the test should be passed
 111      * to the new VM instance. If this method is not called, the default behavior is that
 112      * the options will be passed to the new VM instance.
 113      *
 114      * @param includeStandardOptions whether or not the standard VM and java options for
 115      *                               the test should be passed to the new VM instance.
 116      * @return this task object
 117      */
 118     public JavaTask includeStandardOptions(boolean includeStandardOptions) {
 119         this.includeStandardOptions = includeStandardOptions;
 120         return this;
 121     }
 122 
 123     /**
 124      * {@inheritDoc}
 125      * @return the name "java"
 126      */
 127     @Override
 128     public String name() {
 129         return "java";
 130     }
 131 
 132     /**
 133      * Calls the Java launcher with the arguments as currently configured.
 134      * @return a Result object indicating the outcome of the task
 135      * and the content of any output written to stdout or stderr.
 136      * @throws TaskError if the outcome of the task is not as expected.
 137      */
 138     @Override
 139     public Task.Result run() {
 140         List<String> args = new ArrayList<>();
 141         args.add(toolBox.getJDKTool("java").toString());
 142         if (includeStandardOptions) {
 143             args.addAll(toolBox.split(System.getProperty("test.vm.opts"), " +"));
 144             args.addAll(toolBox.split(System.getProperty("test.java.opts"), " +"));
 145         }
 146         if (classpath != null) {
 147             args.add("-classpath");
 148             args.add(classpath);
 149         }
 150         if (vmOptions != null)
 151             args.addAll(vmOptions);
 152         if (className != null)
 153             args.add(className);
 154         if (classArgs != null)
 155             args.addAll(classArgs);
 156         ProcessBuilder pb = getProcessBuilder();
 157         pb.command(args);
 158         try {
 159             return runProcess(toolBox, this, pb.start());
 160         } catch (IOException | InterruptedException e) {
 161             throw new Error(e);
 162         }
 163     }
 164 }