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