1/*
2 * Copyright (c) 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
24
25import java.io.PrintStream;
26import java.io.PrintWriter;
27import java.io.StringWriter;
28import java.util.Arrays;
29import java.util.List;
30import java.util.spi.ToolProvider;
31import java.util.stream.Collectors;
32
33/**
34 * JdepsRunner class to invoke jdeps with the given command line argument
35 */
36public class JdepsRunner {
37    private static final ToolProvider JDEPS_TOOL = ToolProvider.findFirst("jdeps")
38        .orElseThrow(() -> new RuntimeException("jdeps tool not found"));
39
40    public static JdepsRunner run(String... args) {
41        JdepsRunner jdeps = new JdepsRunner(args);
42        int rc = jdeps.run(true);
43        if (rc != 0)
44           throw new Error("jdeps failed: rc=" + rc);
45        return jdeps;
46    }
47
48    final StringWriter stdout = new StringWriter();
49    final StringWriter stderr = new StringWriter();
50    final String[] args;
51    public JdepsRunner(String... args) {
52        System.out.println("jdeps " + Arrays.stream(args)
53                                            .collect(Collectors.joining(" ")));
54        this.args = args;
55    }
56
57    public JdepsRunner(List<String> args) {
58        this(args.toArray(new String[0]));
59    }
60
61    public int run() {
62        return run(false);
63    }
64
65    public int run(boolean showOutput) {
66        try (PrintWriter pwout = new PrintWriter(stdout);
67             PrintWriter pwerr = new PrintWriter(stderr)) {
68            int rc = JDEPS_TOOL.run(pwout, pwerr, args);
69            if (showOutput) {
70                printStdout(System.out);
71                printStderr(System.out);
72            }
73            return rc;
74        }
75    }
76
77    public boolean outputContains(String s) {
78        return stdout.toString().contains(s) || stderr.toString().contains(s);
79    }
80
81    public void printStdout(PrintStream stream) {
82        stream.println(stdout.toString());
83    }
84
85    public void printStderr(PrintStream stream) {
86        stream.println(stderr.toString());
87    }
88
89    public String[] output() {
90        String lineSep = System.getProperty("line.separator");
91        return stdout.toString().split(lineSep);
92    }
93}
94