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