< prev index next >

src/jdk.incubator.jpackage/share/classes/jdk/incubator/jpackage/internal/JLinkBundlerHelper.java

Print this page

        

@@ -21,11 +21,11 @@
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
-package jdk.jpackage.internal;
+package jdk.incubator.jpackage.internal;
 
 import java.io.File;
 import java.io.IOException;
 import java.io.StringReader;
 import java.io.PrintWriter;

@@ -51,68 +51,27 @@
 import java.util.Arrays;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import java.util.regex.Matcher;
 import java.util.spi.ToolProvider;
+import java.util.jar.JarFile;
 import java.lang.module.Configuration;
 import java.lang.module.ResolvedModule;
 import java.lang.module.ModuleDescriptor;
 import java.lang.module.ModuleFinder;
 import java.lang.module.ModuleReference;
+import jdk.internal.module.ModulePath;
+
 
 final class JLinkBundlerHelper {
 
     private static final ResourceBundle I18N = ResourceBundle.getBundle(
-            "jdk.jpackage.internal.resources.MainResources");
-    private static final String JRE_MODULES_FILENAME =
-            "jdk/jpackage/internal/resources/jre.list";
-    private static final String SERVER_JRE_MODULES_FILENAME =
-            "jdk/jpackage/internal/resources/jre.module.list";
+            "jdk.incubator.jpackage.internal.resources.MainResources");
 
     static final ToolProvider JLINK_TOOL =
             ToolProvider.findFirst("jlink").orElseThrow();
 
-    private JLinkBundlerHelper() {}
-
-    @SuppressWarnings("unchecked")
-    static final BundlerParamInfo<Integer> DEBUG =
-            new StandardBundlerParam<>(
-                    "-J-Xdebug",
-                    Integer.class,
-                    p -> null,
-                    (s, p) -> {
-                        return Integer.valueOf(s);
-                    });
-
-    static String listOfPathToString(List<Path> value) {
-        String result = "";
-
-        for (Path path : value) {
-            if (result.length() > 0) {
-                result += File.pathSeparator;
-            }
-
-            result += path.toString();
-        }
-
-        return result;
-    }
-
-    static String setOfStringToString(Set<String> value) {
-        String result = "";
-
-        for (String element : value) {
-            if (result.length() > 0) {
-                result += ",";
-            }
-
-            result += element;
-        }
-
-        return result;
-    }
-
     static File getMainJar(Map<String, ? super Object> params) {
         File result = null;
         RelativeFileSet fileset =
                 StandardBundlerParam.MAIN_JAR.fetchFrom(params);
 

@@ -132,29 +91,33 @@
         }
 
         return result;
     }
 
-    static String getMainClass(Map<String, ? super Object> params) {
-        String result = "";
+    static String getMainClassFromModule(Map<String, ? super Object> params) {
         String mainModule = StandardBundlerParam.MODULE.fetchFrom(params);
         if (mainModule != null)  {
+
             int index = mainModule.indexOf("/");
             if (index > 0) {
-                result = mainModule.substring(index + 1);
-            }
-        } else {
-            RelativeFileSet fileset =
-                    StandardBundlerParam.MAIN_JAR.fetchFrom(params);
-            if (fileset != null) {
-                result = StandardBundlerParam.MAIN_CLASS.fetchFrom(params);
+                return mainModule.substring(index + 1);
             } else {
-                // possibly app-image
+                ModuleDescriptor descriptor =
+                        JLinkBundlerHelper.getMainModuleDescription(params);
+                if (descriptor != null) {
+                    Optional<String> mainClass = descriptor.mainClass();
+                    if (mainClass.isPresent()) {
+                        Log.verbose(MessageFormat.format(I18N.getString(
+                                    "message.module-class"),
+                                    mainClass.get(),
+                                    JLinkBundlerHelper.getMainModule(params)));
+                        return mainClass.get();
             }
         }
-
-        return result;
+            }
+        }
+        return null;
     }
 
     static String getMainModule(Map<String, ? super Object> params) {
         String result = null;
         String mainModule = StandardBundlerParam.MODULE.fetchFrom(params);

@@ -170,39 +133,33 @@
         }
 
         return result;
     }
 
-    private static Set<String> getValidModules(List<Path> modulePath,
-            Set<String> addModules, Set<String> limitModules) {
-        ModuleHelper moduleHelper = new ModuleHelper(
-                modulePath, addModules, limitModules);
-        return removeInvalidModules(modulePath, moduleHelper.modules());
-    }
-
     static void execute(Map<String, ? super Object> params,
             AbstractAppImageBuilder imageBuilder)
             throws IOException, Exception {
+
         List<Path> modulePath =
                 StandardBundlerParam.MODULE_PATH.fetchFrom(params);
         Set<String> addModules =
                 StandardBundlerParam.ADD_MODULES.fetchFrom(params);
         Set<String> limitModules =
                 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
-        Path outputDir = imageBuilder.getRoot();
-        String excludeFileList = imageBuilder.getExcludeFileList();
+        Path outputDir = imageBuilder.getRuntimeRoot();
         File mainJar = getMainJar(params);
         ModFile.ModType mainJarType = ModFile.ModType.Unknown;
 
         if (mainJar != null) {
             mainJarType = new ModFile(mainJar).getModType();
         } else if (StandardBundlerParam.MODULE.fetchFrom(params) == null) {
             // user specified only main class, all jars will be on the classpath
             mainJarType = ModFile.ModType.UnnamedJar;
         }
 
-        boolean bindServices = addModules.isEmpty();
+        boolean bindServices =
+                StandardBundlerParam.BIND_SERVICES.fetchFrom(params);
 
         // Modules
         String mainModule = getMainModule(params);
         if (mainModule == null) {
             if (mainJarType == ModFile.ModType.UnnamedJar) {

@@ -214,24 +171,21 @@
                     mainJarType == ModFile.ModType.ModularJar) {
                 addModules.add(ModuleHelper.ALL_DEFAULT);
             }
         }
 
-        Set<String> validModules =
-                  getValidModules(modulePath, addModules, limitModules);
+        Set<String> modules = new ModuleHelper(
+                modulePath, addModules, limitModules).modules();
 
         if (mainModule != null) {
-            validModules.add(mainModule);
+            modules.add(mainModule);
         }
 
-        Log.verbose(MessageFormat.format(
-                I18N.getString("message.modules"), validModules.toString()));
-
-        runJLink(outputDir, modulePath, validModules, limitModules,
-                excludeFileList, new HashMap<String,String>(), bindServices);
+        runJLink(outputDir, modulePath, modules, limitModules,
+                new HashMap<String,String>(), bindServices);
 
-        imageBuilder.prepareApplicationFiles();
+        imageBuilder.prepareApplicationFiles(params);
     }
 
 
     // Returns the path to the JDK modules in the user defined module path.
     static Path findPathOfModule( List<Path> modulePath, String moduleName) {

@@ -245,36 +199,45 @@
         }
 
         return null;
     }
 
+    static ModuleDescriptor getMainModuleDescription(Map<String, ? super Object> params) {
+        boolean hasModule = params.containsKey(StandardBundlerParam.MODULE.getID());
+        if (hasModule) {
+            List<Path> modulePath = StandardBundlerParam.MODULE_PATH.fetchFrom(params);
+            if (!modulePath.isEmpty()) {
+                ModuleFinder finder = ModuleFinder.of(modulePath.toArray(new Path[0]));
+                String mainModule = JLinkBundlerHelper.getMainModule(params);
+                Optional<ModuleReference> omref = finder.find(mainModule);
+                if (omref.isPresent()) {
+                    return omref.get().descriptor();
+                }
+            }
+        }
+
+        return null;
+    }
+
     /*
      * Returns the set of modules that would be visible by default for
      * a non-modular-aware application consisting of the given elements.
      */
     private static Set<String> getDefaultModules(
-            Path[] paths, String[] addModules) {
+            Collection<Path> paths, Collection<String> addModules) {
 
         // the modules in the run-time image that export an API
         Stream<String> systemRoots = ModuleFinder.ofSystem().findAll().stream()
                 .map(ModuleReference::descriptor)
-                .filter(descriptor -> exportsAPI(descriptor))
+                .filter(JLinkBundlerHelper::exportsAPI)
                 .map(ModuleDescriptor::name);
 
-        Set<String> roots;
-        if (addModules == null || addModules.length == 0) {
-            roots = systemRoots.collect(Collectors.toSet());
-        } else {
-            var extraRoots =  Stream.of(addModules);
-            roots = Stream.concat(systemRoots,
-                    extraRoots).collect(Collectors.toSet());
-        }
+        Set<String> roots = Stream.concat(systemRoots,
+                 addModules.stream()).collect(Collectors.toSet());
+
+        ModuleFinder finder = createModuleFinder(paths);
 
-        ModuleFinder finder = ModuleFinder.ofSystem();
-        if (paths != null && paths.length > 0) {
-            finder = ModuleFinder.compose(finder, ModuleFinder.of(paths));
-        }
         return Configuration.empty()
                 .resolveAndBind(finder, ModuleFinder.of(), roots)
                 .modules()
                 .stream()
                 .map(ResolvedModule::name)

@@ -285,52 +248,28 @@
      * Returns true if the given module exports an API to all module.
      */
     private static boolean exportsAPI(ModuleDescriptor descriptor) {
         return descriptor.exports()
                 .stream()
-                .filter(e -> !e.isQualified())
-                .findAny()
-                .isPresent();
-    }
-
-    private static Set<String> removeInvalidModules(
-            List<Path> modulePath, Set<String> modules) {
-        Set<String> result = new LinkedHashSet<String>();
-        ModuleManager mm = new ModuleManager(modulePath);
-        List<ModFile> lmodfiles =
-                mm.getModules(EnumSet.of(ModuleManager.SearchType.ModularJar,
-                        ModuleManager.SearchType.Jmod,
-                        ModuleManager.SearchType.ExplodedModule));
-
-        HashMap<String, ModFile> validModules = new HashMap<>();
-
-        for (ModFile modFile : lmodfiles) {
-            validModules.put(modFile.getModName(), modFile);
-        }
-
-        for (String name : modules) {
-            if (validModules.containsKey(name)) {
-                result.add(name);
-            } else {
-                Log.error(MessageFormat.format(
-                        I18N.getString("warning.module.does.not.exist"), name));
-            }
+                .anyMatch(e -> !e.isQualified());
         }
 
-        return result;
+    private static ModuleFinder createModuleFinder(Collection<Path> modulePath) {
+        return ModuleFinder.compose(
+                ModulePath.of(JarFile.runtimeVersion(), true,
+                        modulePath.toArray(Path[]::new)),
+                ModuleFinder.ofSystem());
     }
 
     private static class ModuleHelper {
         // The token for "all modules on the module path".
         private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
 
         // The token for "all valid runtime modules".
         static final String ALL_DEFAULT = "ALL-DEFAULT";
 
         private final Set<String> modules = new HashSet<>();
-        private enum Macros {None, AllModulePath, AllRuntime}
-
         ModuleHelper(List<Path> paths, Set<String> addModules,
                 Set<String> limitModules) {
             boolean addAllModulePath = false;
             boolean addDefaultMods = false;
 

@@ -354,42 +293,41 @@
 
             if (addAllModulePath) {
                 this.modules.addAll(getModuleNamesFromPath(paths));
             } else if (addDefaultMods) {
                 this.modules.addAll(getDefaultModules(
-                        paths.toArray(new Path[0]),
-                        addModules.toArray(new String[0])));
+                        paths, addModules));
             }
         }
 
         Set<String> modules() {
             return modules;
         }
 
-        private static Set<String> getModuleNamesFromPath(List<Path> Value) {
-            Set<String> result = new LinkedHashSet<String>();
-            ModuleManager mm = new ModuleManager(Value);
-            List<ModFile> modFiles = mm.getModules(
-                    EnumSet.of(ModuleManager.SearchType.ModularJar,
-                    ModuleManager.SearchType.Jmod,
-                    ModuleManager.SearchType.ExplodedModule));
+        private static Set<String> getModuleNamesFromPath(List<Path> paths) {
 
-            for (ModFile modFile : modFiles) {
-                result.add(modFile.getModName());
-            }
-            return result;
+            return createModuleFinder(paths)
+                    .findAll()
+                    .stream()
+                    .map(ModuleReference::descriptor)
+                    .map(ModuleDescriptor::name)
+                    .collect(Collectors.toSet());
         }
     }
 
     private static void runJLink(Path output, List<Path> modulePath,
-            Set<String> modules, Set<String> limitModules, String excludes,
+            Set<String> modules, Set<String> limitModules,
             HashMap<String, String> user, boolean bindServices)
-            throws IOException {
+            throws PackagerException {
 
         // This is just to ensure jlink is given a non-existant directory
         // The passed in output path should be non-existant or empty directory
+        try {
         IOUtils.deleteRecursive(output.toFile());
+        } catch (IOException ioe) {
+            throw new PackagerException(ioe);
+        }
 
         ArrayList<String> args = new ArrayList<String>();
         args.add("--output");
         args.add(output.toString());
         if (modulePath != null && !modulePath.isEmpty()) {

@@ -402,14 +340,10 @@
         }
         if (limitModules != null && !limitModules.isEmpty()) {
             args.add("--limit-modules");
             args.add(getStringList(limitModules));
         }
-        if (excludes != null) {
-            args.add("--exclude-files");
-            args.add(excludes);
-        }
         if (user != null && !user.isEmpty()) {
             for (Map.Entry<String, String> entry : user.entrySet()) {
                 args.add(entry.getKey());
                 args.add(entry.getValue());
             }

@@ -429,11 +363,11 @@
         Log.verbose("jlink arguments: " + args);
         int retVal = JLINK_TOOL.run(pw, pw, args.toArray(new String[0]));
         String jlinkOut = writer.toString();
 
         if (retVal != 0) {
-            throw new IOException("jlink failed with: " + jlinkOut);
+            throw new PackagerException("error.jlink.failed" , jlinkOut);
         } else if (jlinkOut.length() > 0) {
             Log.verbose("jlink output: " + jlinkOut);
         }
     }
 
< prev index next >