1 /* 2 * Copyright (c) 2013, 2015, 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 jdk.test.lib; 25 26 import java.util.ArrayList; 27 import java.util.Arrays; 28 import java.util.List; 29 30 /** 31 * A utility for constructing command lines for starting JDK tool processes. 32 * 33 * The JDKToolLauncher can in particular be combined with a 34 * java.lang.ProcessBuilder to easily run a JDK tool. For example, the following 35 * code run {@code jmap -heap} against a process with GC logging turned on for 36 * the {@code jmap} process: 37 * 38 * <pre> 39 * {@code 40 * JDKToolLauncher jmap = JDKToolLauncher.create("jmap") 41 * .addVMArg("-XX:+PrintGC"); 42 * .addVMArg("-XX:+PrintGCDetails") 43 * .addToolArg("-heap") 44 * .addToolArg(pid); 45 * ProcessBuilder pb = new ProcessBuilder(jmap.getCommand()); 46 * Process p = pb.start(); 47 * } 48 * </pre> 49 * @deprecated This class is deprecated. Use the one from 50 * {@code <root>/test/lib/share/classes/jdk/test/lib} 51 */ 52 @Deprecated 53 public class JDKToolLauncher { 54 private final String executable; 55 private final List<String> vmArgs = new ArrayList<String>(); 56 private final List<String> toolArgs = new ArrayList<String>(); 57 58 private JDKToolLauncher(String tool, boolean useCompilerJDK) { 59 if (useCompilerJDK) { 60 executable = JDKToolFinder.getJDKTool(tool); 61 } else { 62 executable = JDKToolFinder.getTestJDKTool(tool); 63 } 64 vmArgs.addAll(Arrays.asList(ProcessTools.getPlatformSpecificVMArgs())); 65 } 66 67 /** 68 * Creates a new JDKToolLauncher for the specified tool. Using tools path 69 * from the compiler JDK. 70 * 71 * @param tool 72 * The name of the tool 73 * @return A new JDKToolLauncher 74 */ 75 public static JDKToolLauncher create(String tool) { 76 return new JDKToolLauncher(tool, true); 77 } 78 79 /** 80 * Creates a new JDKToolLauncher for the specified tool in the Tested JDK. 81 * 82 * @param tool 83 * The name of the tool 84 * 85 * @return A new JDKToolLauncher 86 */ 87 public static JDKToolLauncher createUsingTestJDK(String tool) { 88 return new JDKToolLauncher(tool, false); 89 } 90 91 /** 92 * Adds an argument to the JVM running the tool. 93 * 94 * The JVM arguments are passed to the underlying JVM running the tool. 95 * Arguments will automatically be prepended with "-J". 96 * 97 * Any platform specific arguments required for running the tool are 98 * automatically added. 99 * 100 * 101 * @param arg 102 * The argument to VM running the tool 103 * @return The JDKToolLauncher instance 104 */ 105 public JDKToolLauncher addVMArg(String arg) { 106 vmArgs.add(arg); 107 return this; 108 } 109 110 /** 111 * Adds an argument to the tool. 112 * 113 * @param arg 114 * The argument to the tool 115 * @return The JDKToolLauncher instance 116 */ 117 public JDKToolLauncher addToolArg(String arg) { 118 toolArgs.add(arg); 119 return this; 120 } 121 122 /** 123 * Returns the command that can be used for running the tool. 124 * 125 * @return An array whose elements are the arguments of the command. 126 */ 127 public String[] getCommand() { 128 List<String> command = new ArrayList<String>(); 129 command.add(executable); 130 // Add -J in front of all vmArgs 131 for (String arg : vmArgs) { 132 command.add("-J" + arg); 133 } 134 command.addAll(toolArgs); 135 return command.toArray(new String[command.size()]); 136 } 137 }