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 name of the class to be executed. 71 * @param className the name of the class 72 * @return this task object 73 */ 74 public JavaTask className(String className) { 75 this.className = className; 76 return this; 77 } 78 79 /** 80 * Sets the arguments for the class to be executed. 81 * @param classArgs the arguments 82 * @return this task object 83 */ 84 public JavaTask classArgs(String... classArgs) { 85 this.classArgs = Arrays.asList(classArgs); 86 return this; 87 } 88 89 /** 90 * Sets whether or not the standard VM and java options for the test should be passed 91 * to the new VM instance. If this method is not called, the default behavior is that 92 * the options will be passed to the new VM instance. 93 * 94 * @param includeStandardOptions whether or not the standard VM and java options for 95 * the test should be passed to the new VM instance. 96 * @return this task object 97 */ 98 public JavaTask includeStandardOptions(boolean includeStandardOptions) { 99 this.includeStandardOptions = includeStandardOptions; 100 return this; 101 } 102 103 /** 104 * {@inheritDoc} 105 * @return the name "java" 106 */ 107 @Override 108 public String name() { 109 return "java"; 110 } 111 112 /** 113 * Calls the Java launcher with the arguments as currently configured. 114 * @return a Result object indicating the outcome of the task 115 * and the content of any output written to stdout or stderr. 116 * @throws TaskError if the outcome of the task is not as expected. 117 */ 118 @Override 119 public Task.Result run() { 120 List<String> args = new ArrayList<>(); 121 args.add(toolBox.getJDKTool("java").toString()); 122 if (includeStandardOptions) { 123 args.addAll(toolBox.split(System.getProperty("test.vm.opts"), " +")); 124 args.addAll(toolBox.split(System.getProperty("test.java.opts"), " +")); 125 } 126 if (classpath != null) { 127 args.add("-classpath"); 128 args.add(classpath); 129 } 130 if (vmOptions != null) 131 args.addAll(vmOptions); 132 if (className != null) 133 args.add(className); 134 if (classArgs != null) 135 args.addAll(classArgs); 136 ProcessBuilder pb = getProcessBuilder(); 137 pb.command(args); 138 try { 139 return runProcess(toolBox, this, pb.start()); 140 } catch (IOException | InterruptedException e) { 141 throw new Error(e); 142 } 143 } 144 }