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