1 /*
   2  * Copyright (c) 2013, 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.testlibrary;
  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("-Xlog:gc*=debug")
  42  *                                       .addToolArg("-heap")
  43  *                                       .addToolArg(pid);
  44  * ProcessBuilder pb = new ProcessBuilder(jmap.getCommand());
  45  * Process p = pb.start();
  46  * }
  47  * </pre>
  48  */
  49 public class JDKToolLauncher {
  50     private final String executable;
  51     private final List<String> vmArgs = new ArrayList<String>();
  52     private final List<String> toolArgs = new ArrayList<String>();
  53 
  54     private JDKToolLauncher(String tool, boolean useCompilerJDK) {
  55         if (useCompilerJDK) {
  56             executable = JDKToolFinder.getJDKTool(tool);
  57         } else {
  58             executable = JDKToolFinder.getTestJDKTool(tool);
  59         }
  60         vmArgs.addAll(Arrays.asList(ProcessTools.getPlatformSpecificVMArgs()));
  61     }
  62 
  63     /**
  64      * Creates a new JDKToolLauncher for the specified tool. Using tools path
  65      * from the compiler JDK.
  66      *
  67      * @param tool
  68      *            The name of the tool
  69      * @return A new JDKToolLauncher
  70      */
  71     public static JDKToolLauncher create(String tool) {
  72         return new JDKToolLauncher(tool, true);
  73     }
  74 
  75     /**
  76      * Creates a new JDKToolLauncher for the specified tool in the Tested JDK.
  77      *
  78      * @param tool
  79      *            The name of the tool
  80      *
  81      * @return A new JDKToolLauncher
  82      */
  83     public static JDKToolLauncher createUsingTestJDK(String tool) {
  84         return new JDKToolLauncher(tool, false);
  85     }
  86 
  87     /**
  88      * Adds an argument to the JVM running the tool.
  89      *
  90      * The JVM arguments are passed to the underlying JVM running the tool.
  91      * Arguments will automatically be prepended with "-J".
  92      *
  93      * Any platform specific arguments required for running the tool are
  94      * automatically added.
  95      *
  96      *
  97      * @param arg
  98      *            The argument to VM running the tool
  99      * @return The JDKToolLauncher instance
 100      */
 101     public JDKToolLauncher addVMArg(String arg) {
 102         vmArgs.add(arg);
 103         return this;
 104     }
 105 
 106     /**
 107      * Adds an argument to the tool.
 108      *
 109      * @param arg
 110      *            The argument to the tool
 111      * @return The JDKToolLauncher instance
 112      */
 113     public JDKToolLauncher addToolArg(String arg) {
 114         toolArgs.add(arg);
 115         return this;
 116     }
 117 
 118     /**
 119      * Returns the command that can be used for running the tool.
 120      *
 121      * @return An array whose elements are the arguments of the command.
 122      */
 123     public String[] getCommand() {
 124         List<String> command = new ArrayList<String>();
 125         command.add(executable);
 126         // Add -J in front of all vmArgs
 127         for (String arg : vmArgs) {
 128             command.add("-J" + arg);
 129         }
 130         command.addAll(toolArgs);
 131         return command.toArray(new String[command.size()]);
 132     }
 133 }