1 /*
   2  * Copyright (c) 2015, 2019, 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 jdk.jpackage.internal;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.StringReader;
  31 import java.io.PrintWriter;
  32 import java.io.StringWriter;
  33 import java.nio.file.Files;
  34 import java.nio.file.Path;
  35 import java.text.MessageFormat;
  36 import java.util.ArrayList;
  37 import java.util.Collection;
  38 import java.util.Collections;
  39 import java.util.EnumSet;
  40 import java.util.HashMap;
  41 import java.util.HashSet;
  42 import java.util.Iterator;
  43 import java.util.LinkedHashMap;
  44 import java.util.LinkedHashSet;
  45 import java.util.List;
  46 import java.util.Map;
  47 import java.util.Properties;
  48 import java.util.ResourceBundle;
  49 import java.util.Set;
  50 import java.util.Optional;
  51 import java.util.Arrays;
  52 import java.util.stream.Collectors;
  53 import java.util.stream.Stream;
  54 import java.util.regex.Matcher;
  55 import java.util.spi.ToolProvider;
  56 import java.lang.module.Configuration;
  57 import java.lang.module.ResolvedModule;
  58 import java.lang.module.ModuleDescriptor;
  59 import java.lang.module.ModuleFinder;
  60 import java.lang.module.ModuleReference;
  61 
  62 final class JLinkBundlerHelper {
  63 
  64     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  65             "jdk.jpackage.internal.resources.MainResources");
  66     private static final String JRE_MODULES_FILENAME =
  67             "jdk/jpackage/internal/resources/jre.list";
  68     private static final String SERVER_JRE_MODULES_FILENAME =
  69             "jdk/jpackage/internal/resources/jre.module.list";
  70 
  71     static final ToolProvider JLINK_TOOL =
  72             ToolProvider.findFirst("jlink").orElseThrow();
  73 
  74     private JLinkBundlerHelper() {}
  75 
  76     @SuppressWarnings("unchecked")
  77     static final BundlerParamInfo<Integer> DEBUG =
  78             new StandardBundlerParam<>(
  79                     "-J-Xdebug",
  80                     Integer.class,
  81                     p -> null,
  82                     (s, p) -> {
  83                         return Integer.valueOf(s);
  84                     });
  85 
  86     static String listOfPathToString(List<Path> value) {
  87         String result = "";
  88 
  89         for (Path path : value) {
  90             if (result.length() > 0) {
  91                 result += File.pathSeparator;
  92             }
  93 
  94             result += path.toString();
  95         }
  96 
  97         return result;
  98     }
  99 
 100     static String setOfStringToString(Set<String> value) {
 101         String result = "";
 102 
 103         for (String element : value) {
 104             if (result.length() > 0) {
 105                 result += ",";
 106             }
 107 
 108             result += element;
 109         }
 110 
 111         return result;
 112     }
 113 
 114     static File getMainJar(Map<String, ? super Object> params) {
 115         File result = null;
 116         RelativeFileSet fileset =
 117                 StandardBundlerParam.MAIN_JAR.fetchFrom(params);
 118 
 119         if (fileset != null) {
 120             String filename = fileset.getIncludedFiles().iterator().next();
 121             result = fileset.getBaseDirectory().toPath().
 122                     resolve(filename).toFile();
 123 
 124             if (result == null || !result.exists()) {
 125                 String srcdir =
 126                     StandardBundlerParam.SOURCE_DIR.fetchFrom(params);
 127 
 128                 if (srcdir != null) {
 129                     result = new File(srcdir + File.separator + filename);
 130                 }
 131             }
 132         }
 133 
 134         return result;
 135     }
 136 
 137     static String getMainClass(Map<String, ? super Object> params) {
 138         String result = "";
 139         String mainModule = StandardBundlerParam.MODULE.fetchFrom(params);
 140         if (mainModule != null)  {
 141             int index = mainModule.indexOf("/");
 142             if (index > 0) {
 143                 result = mainModule.substring(index + 1);
 144             }
 145         } else {
 146             RelativeFileSet fileset =
 147                     StandardBundlerParam.MAIN_JAR.fetchFrom(params);
 148             if (fileset != null) {
 149                 result = StandardBundlerParam.MAIN_CLASS.fetchFrom(params);
 150             } else {
 151                 // possibly app-image
 152             }
 153         }
 154 
 155         return result;
 156     }
 157 
 158     static String getMainModule(Map<String, ? super Object> params) {
 159         String result = null;
 160         String mainModule = StandardBundlerParam.MODULE.fetchFrom(params);
 161 
 162         if (mainModule != null) {
 163             int index = mainModule.indexOf("/");
 164 
 165             if (index > 0) {
 166                 result = mainModule.substring(0, index);
 167             } else {
 168                 result = mainModule;
 169             }
 170         }
 171 
 172         return result;
 173     }
 174 
 175     private static Set<String> getValidModules(List<Path> modulePath,
 176             Set<String> addModules, Set<String> limitModules) {
 177         ModuleHelper moduleHelper = new ModuleHelper(
 178                 modulePath, addModules, limitModules);
 179         return removeInvalidModules(modulePath, moduleHelper.modules());
 180     }
 181 
 182     static void execute(Map<String, ? super Object> params,
 183             AbstractAppImageBuilder imageBuilder)
 184             throws IOException, Exception {
 185         List<Path> modulePath =
 186                 StandardBundlerParam.MODULE_PATH.fetchFrom(params);
 187         Set<String> addModules =
 188                 StandardBundlerParam.ADD_MODULES.fetchFrom(params);
 189         Set<String> limitModules =
 190                 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
 191         Path outputDir = imageBuilder.getRoot();
 192         String excludeFileList = imageBuilder.getExcludeFileList();
 193         File mainJar = getMainJar(params);
 194         ModFile.ModType mainJarType = ModFile.ModType.Unknown;
 195 
 196         if (mainJar != null) {
 197             mainJarType = new ModFile(mainJar).getModType();
 198         } else if (StandardBundlerParam.MODULE.fetchFrom(params) == null) {
 199             // user specified only main class, all jars will be on the classpath
 200             mainJarType = ModFile.ModType.UnnamedJar;
 201         }
 202 
 203         // Modules
 204         String mainModule = getMainModule(params);
 205         if (mainModule == null) {
 206             if (mainJarType == ModFile.ModType.UnnamedJar) {
 207                 if (addModules.isEmpty()) {
 208                     // The default for an unnamed jar is ALL_DEFAULT
 209                     addModules.add(ModuleHelper.ALL_DEFAULT);
 210                 }
 211             } else if (mainJarType == ModFile.ModType.Unknown ||
 212                     mainJarType == ModFile.ModType.ModularJar) {
 213                 addModules.add(ModuleHelper.ALL_DEFAULT);
 214             }
 215         } 
 216 
 217         Set<String> validModules =
 218                   getValidModules(modulePath, addModules, limitModules);
 219 
 220         if (mainModule != null) {
 221             validModules.add(mainModule);
 222         }
 223 
 224         Log.verbose(MessageFormat.format(
 225                 I18N.getString("message.modules"), validModules.toString()));
 226 
 227         runJLink(outputDir, modulePath, validModules, limitModules,
 228                 excludeFileList, new HashMap<String,String>());
 229 
 230         imageBuilder.prepareApplicationFiles();
 231     }
 232 
 233 
 234     // Returns the path to the JDK modules in the user defined module path.
 235     static Path findPathOfModule( List<Path> modulePath, String moduleName) {
 236 
 237         for (Path path : modulePath) {
 238             Path moduleNamePath = path.resolve(moduleName);
 239 
 240             if (Files.exists(moduleNamePath)) {
 241                 return path;
 242             }
 243         }
 244 
 245         return null;
 246     }
 247 
 248     /*
 249      * Returns the set of modules that would be visible by default for
 250      * a non-modular-aware application consisting of the given elements.
 251      */
 252     private static Set<String> getDefaultModules(
 253             Path[] paths, String[] addModules) {
 254 
 255         // the modules in the run-time image that export an API
 256         Stream<String> systemRoots = ModuleFinder.ofSystem().findAll().stream()
 257                 .map(ModuleReference::descriptor)
 258                 .filter(descriptor -> exportsAPI(descriptor))
 259                 .map(ModuleDescriptor::name);
 260 
 261         Set<String> roots;
 262         if (addModules == null || addModules.length == 0) {
 263             roots = systemRoots.collect(Collectors.toSet());
 264         } else {
 265             var extraRoots =  Stream.of(addModules);
 266             roots = Stream.concat(systemRoots,
 267                     extraRoots).collect(Collectors.toSet());
 268         }
 269 
 270         ModuleFinder finder = ModuleFinder.ofSystem();
 271         if (paths != null && paths.length > 0) {
 272             finder = ModuleFinder.compose(finder, ModuleFinder.of(paths));
 273         }
 274         return Configuration.empty()
 275                 .resolveAndBind(finder, ModuleFinder.of(), roots)
 276                 .modules()
 277                 .stream()
 278                 .map(ResolvedModule::name)
 279                 .collect(Collectors.toSet());
 280     } 
 281 
 282     /*
 283      * Returns true if the given module exports an API to all module.
 284      */
 285     private static boolean exportsAPI(ModuleDescriptor descriptor) {
 286         return descriptor.exports()
 287                 .stream()
 288                 .filter(e -> !e.isQualified())
 289                 .findAny()
 290                 .isPresent();
 291     }
 292 
 293     private static Set<String> removeInvalidModules(
 294             List<Path> modulePath, Set<String> modules) {
 295         Set<String> result = new LinkedHashSet<String>();
 296         ModuleManager mm = new ModuleManager(modulePath);
 297         List<ModFile> lmodfiles =
 298                 mm.getModules(EnumSet.of(ModuleManager.SearchType.ModularJar,
 299                         ModuleManager.SearchType.Jmod,
 300                         ModuleManager.SearchType.ExplodedModule));
 301 
 302         HashMap<String, ModFile> validModules = new HashMap<>();
 303 
 304         for (ModFile modFile : lmodfiles) {
 305             validModules.put(modFile.getModName(), modFile);
 306         }
 307 
 308         for (String name : modules) {
 309             if (validModules.containsKey(name)) {
 310                 result.add(name);
 311             } else {
 312                 Log.error(MessageFormat.format(
 313                         I18N.getString("warning.module.does.not.exist"), name));
 314             }
 315         }
 316 
 317         return result;
 318     }
 319 
 320     private static class ModuleHelper {
 321         // The token for "all modules on the module path".
 322         private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
 323 
 324         // The token for "all valid runtime modules".
 325         static final String ALL_DEFAULT = "ALL-DEFAULT";
 326 
 327         private final Set<String> modules = new HashSet<>();
 328         private enum Macros {None, AllModulePath, AllRuntime}
 329 
 330         ModuleHelper(List<Path> paths, Set<String> addModules,
 331                 Set<String> limitModules) {
 332             boolean addAllModulePath = false;
 333             boolean addDefaultMods = false;
 334             
 335             for (Iterator<String> iterator = addModules.iterator();
 336                     iterator.hasNext();) {
 337                 String module = iterator.next();
 338 
 339                 switch (module) {
 340                     case ALL_MODULE_PATH:
 341                         iterator.remove();
 342                         addAllModulePath = true;
 343                         break;
 344                     case ALL_DEFAULT:
 345                         iterator.remove();
 346                         addDefaultMods = true;
 347                         break;
 348                     default:
 349                         this.modules.add(module);
 350                 }
 351             }
 352 
 353             if (addAllModulePath) {
 354                 this.modules.addAll(getModuleNamesFromPath(paths));
 355             } else if (addDefaultMods) {
 356                 this.modules.addAll(getDefaultModules(
 357                         paths.toArray(new Path[0]),
 358                         addModules.toArray(new String[0])));
 359             }
 360         }
 361 
 362         Set<String> modules() {
 363             return modules;
 364         }
 365 
 366         private static Set<String> getModuleNamesFromPath(List<Path> Value) {
 367             Set<String> result = new LinkedHashSet<String>();
 368             ModuleManager mm = new ModuleManager(Value);
 369             List<ModFile> modFiles = mm.getModules(
 370                     EnumSet.of(ModuleManager.SearchType.ModularJar,
 371                     ModuleManager.SearchType.Jmod,
 372                     ModuleManager.SearchType.ExplodedModule));
 373 
 374             for (ModFile modFile : modFiles) {
 375                 result.add(modFile.getModName());
 376             }
 377             return result;
 378         }
 379     }
 380 
 381     private static void runJLink(Path output, List<Path> modulePath,
 382             Set<String> modules, Set<String> limitModules, String excludes,
 383             HashMap<String, String> user) throws IOException {
 384 
 385         // This is just to ensure jlink is given a non-existant directory
 386         // The passed in output path should be non-existant or empty directory
 387         IOUtils.deleteRecursive(output.toFile());
 388 
 389         ArrayList<String> args = new ArrayList<String>();
 390         args.add("--output");
 391         args.add(output.toString());
 392         if (modulePath != null && !modulePath.isEmpty()) {
 393             args.add("--module-path");
 394             args.add(getPathList(modulePath));
 395         }
 396         if (modules != null && !modules.isEmpty()) {
 397             args.add("--add-modules");
 398             args.add(getStringList(modules));
 399         }
 400         if (limitModules != null && !limitModules.isEmpty()) {
 401             args.add("--limit-modules");
 402             args.add(getStringList(limitModules));
 403         }
 404         if (excludes != null) {
 405             args.add("--exclude-files");
 406             args.add(excludes);
 407         }
 408         if (user != null && !user.isEmpty()) {
 409             for (Map.Entry<String, String> entry : user.entrySet()) {
 410                 args.add(entry.getKey());
 411                 args.add(entry.getValue());
 412             }
 413         } else {
 414             args.add("--bind-services");
 415             args.add("--strip-native-commands");
 416             args.add("--strip-debug");
 417             args.add("--no-man-pages");
 418             args.add("--no-header-files");
 419         }
 420         
 421         StringWriter writer = new StringWriter();
 422         PrintWriter pw = new PrintWriter(writer);
 423 
 424         Log.verbose("jlink arguments: " + args);
 425         int retVal = JLINK_TOOL.run(pw, pw, args.toArray(new String[0]));
 426         String jlinkOut = writer.toString();
 427 
 428         if (retVal != 0) {
 429             throw new IOException("jlink failed with: " + jlinkOut);
 430         } else if (jlinkOut.length() > 0) {
 431             Log.verbose("jlink output: " + jlinkOut);
 432         }
 433     }
 434 
 435     private static String getPathList(List<Path> pathList) {
 436         String ret = null;
 437         for (Path p : pathList) {
 438             String s =  Matcher.quoteReplacement(p.toString());
 439             if (ret == null) {
 440                 ret = s;
 441             } else {
 442                 ret += File.pathSeparator +  s;
 443             }
 444         }
 445         return ret;
 446     }
 447 
 448     private static String getStringList(Set<String> strings) {
 449         String ret = null;
 450         for (String s : strings) {
 451             if (ret == null) {
 452                 ret = s;
 453             } else {
 454                 ret += "," + s;
 455             }
 456         }
 457         return (ret == null) ? null : Matcher.quoteReplacement(ret);
 458     }
 459 }