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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.oracle.tools.packager;
  27 
  28 import com.sun.tools.jdeps.Main;
  29 import java.io.ByteArrayOutputStream;
  30 import java.io.File;
  31 import java.io.IOException;
  32 import java.io.PrintWriter;
  33 import java.nio.file.Path;
  34 import java.util.ArrayList;
  35 import java.util.Arrays;
  36 import java.util.Collections;
  37 import java.util.LinkedHashSet;
  38 import java.util.List;
  39 import java.util.Map;
  40 import java.util.Set;
  41 import java.util.stream.Collectors;
  42 
  43 
  44 public final class JDepHelper {
  45     private JDepHelper() {}
  46 
  47     private static int invokeJdep(String[] args, PrintWriter out) {
  48         return com.sun.tools.jdeps.Main.run(args, out);
  49     }
  50 
  51     public static List<String> getResourceFileJarList(Map<String, ? super Object> params) {
  52         List<String> files = new ArrayList();
  53 
  54         for (RelativeFileSet rfs : StandardBundlerParam.APP_RESOURCES_LIST.fetchFrom(params)) {
  55             for (String s : rfs.files) {
  56                 if (s.endsWith(".jar")) {
  57                     files.add(rfs.getBaseDirectory() + File.separator + s);
  58                 }
  59             }
  60         }
  61 
  62         return files;
  63     }
  64 
  65     public static Set<String> calculateModules(List<String> Files, List<Path> modulePath) {
  66         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  67              PrintWriter writer = new PrintWriter(baos)) {
  68 
  69             List<String> arguments = new ArrayList<>();
  70             arguments.add("-s");
  71             //TODO JDK-8148654 - add support for -modulepath.
  72             //if (modulePath != null || !modulePath.empty()) {
  73             //    arguments.add("-modulepath");
  74             //    ...
  75             //}
  76             arguments.addAll(Files);
  77 
  78             invokeJdep(arguments.toArray(new String[arguments.size()]), writer);
  79 
  80             // output format is multiple lines of "this.jar -> that.module.name"
  81             // we only care about what is to the right of the arrow
  82             return Arrays.stream(baos.toString().split("\\s*\\S+\\s+->\\s+"))
  83                     .map(String::trim)
  84                     .filter(s -> !s.isEmpty() && !arguments.contains(s) && !"not found".equals(s))
  85                     .collect(Collectors.toSet());
  86         } catch (IOException ioe) {
  87             Log.verbose(ioe);
  88             return new LinkedHashSet();
  89         }
  90     }
  91 }