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