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         boolean bindServices = addModules.isEmpty();
 204 
 205         // Modules
 206         String mainModule = getMainModule(params);
 207         if (mainModule == null) {
 208             if (mainJarType == ModFile.ModType.UnnamedJar) {
 209                 if (addModules.isEmpty()) {
 210                     // The default for an unnamed jar is ALL_DEFAULT
 211                     addModules.add(ModuleHelper.ALL_DEFAULT);
 212                 }
 213             } else if (mainJarType == ModFile.ModType.Unknown ||
 214                     mainJarType == ModFile.ModType.ModularJar) {
 215                 addModules.add(ModuleHelper.ALL_DEFAULT);
 216             }
 217         } 
 218 
 219         Set<String> validModules =
 220                   getValidModules(modulePath, addModules, limitModules);
 221 
 222         if (mainModule != null) {
 223             validModules.add(mainModule);
 224         }
 225 
 226         Log.verbose(MessageFormat.format(
 227                 I18N.getString("message.modules"), validModules.toString()));
 228 
 229         runJLink(outputDir, modulePath, validModules, limitModules,
 230                 excludeFileList, new HashMap<String,String>(), bindServices);
 231 
 232         imageBuilder.prepareApplicationFiles();
 233     }
 234 
 235 
 236     // Returns the path to the JDK modules in the user defined module path.
 237     static Path findPathOfModule( List<Path> modulePath, String moduleName) {
 238 
 239         for (Path path : modulePath) {
 240             Path moduleNamePath = path.resolve(moduleName);
 241 
 242             if (Files.exists(moduleNamePath)) {
 243                 return path;
 244             }
 245         }
 246 
 247         return null;
 248     }
 249 
 250     /*
 251      * Returns the set of modules that would be visible by default for
 252      * a non-modular-aware application consisting of the given elements.
 253      */
 254     private static Set<String> getDefaultModules(
 255             Path[] paths, String[] addModules) {
 256 
 257         // the modules in the run-time image that export an API
 258         Stream<String> systemRoots = ModuleFinder.ofSystem().findAll().stream()
 259                 .map(ModuleReference::descriptor)
 260                 .filter(descriptor -> exportsAPI(descriptor))
 261                 .map(ModuleDescriptor::name);
 262 
 263         Set<String> roots;
 264         if (addModules == null || addModules.length == 0) {
 265             roots = systemRoots.collect(Collectors.toSet());
 266         } else {
 267             var extraRoots =  Stream.of(addModules);
 268             roots = Stream.concat(systemRoots,
 269                     extraRoots).collect(Collectors.toSet());
 270         }
 271 
 272         ModuleFinder finder = ModuleFinder.ofSystem();
 273         if (paths != null && paths.length > 0) {
 274             finder = ModuleFinder.compose(finder, ModuleFinder.of(paths));
 275         }
 276         return Configuration.empty()
 277                 .resolveAndBind(finder, ModuleFinder.of(), roots)
 278                 .modules()
 279                 .stream()
 280                 .map(ResolvedModule::name)
 281                 .collect(Collectors.toSet());
 282     } 
 283 
 284     /*
 285      * Returns true if the given module exports an API to all module.
 286      */
 287     private static boolean exportsAPI(ModuleDescriptor descriptor) {
 288         return descriptor.exports()
 289                 .stream()
 290                 .filter(e -> !e.isQualified())
 291                 .findAny()
 292                 .isPresent();
 293     }
 294 
 295     private static Set<String> removeInvalidModules(
 296             List<Path> modulePath, Set<String> modules) {
 297         Set<String> result = new LinkedHashSet<String>();
 298         ModuleManager mm = new ModuleManager(modulePath);
 299         List<ModFile> lmodfiles =
 300                 mm.getModules(EnumSet.of(ModuleManager.SearchType.ModularJar,
 301                         ModuleManager.SearchType.Jmod,
 302                         ModuleManager.SearchType.ExplodedModule));
 303 
 304         HashMap<String, ModFile> validModules = new HashMap<>();
 305 
 306         for (ModFile modFile : lmodfiles) {
 307             validModules.put(modFile.getModName(), modFile);
 308         }
 309 
 310         for (String name : modules) {
 311             if (validModules.containsKey(name)) {
 312                 result.add(name);
 313             } else {
 314                 Log.error(MessageFormat.format(
 315                         I18N.getString("warning.module.does.not.exist"), name));
 316             }
 317         }
 318 
 319         return result;
 320     }
 321 
 322     private static class ModuleHelper {
 323         // The token for "all modules on the module path".
 324         private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
 325 
 326         // The token for "all valid runtime modules".
 327         static final String ALL_DEFAULT = "ALL-DEFAULT";
 328 
 329         private final Set<String> modules = new HashSet<>();
 330         private enum Macros {None, AllModulePath, AllRuntime}
 331 
 332         ModuleHelper(List<Path> paths, Set<String> addModules,
 333                 Set<String> limitModules) {
 334             boolean addAllModulePath = false;
 335             boolean addDefaultMods = false;
 336             
 337             for (Iterator<String> iterator = addModules.iterator();
 338                     iterator.hasNext();) {
 339                 String module = iterator.next();
 340 
 341                 switch (module) {
 342                     case ALL_MODULE_PATH:
 343                         iterator.remove();
 344                         addAllModulePath = true;
 345                         break;
 346                     case ALL_DEFAULT:
 347                         iterator.remove();
 348                         addDefaultMods = true;
 349                         break;
 350                     default:
 351                         this.modules.add(module);
 352                 }
 353             }
 354 
 355             if (addAllModulePath) {
 356                 this.modules.addAll(getModuleNamesFromPath(paths));
 357             } else if (addDefaultMods) {
 358                 this.modules.addAll(getDefaultModules(
 359                         paths.toArray(new Path[0]),
 360                         addModules.toArray(new String[0])));
 361             }
 362         }
 363 
 364         Set<String> modules() {
 365             return modules;
 366         }
 367 
 368         private static Set<String> getModuleNamesFromPath(List<Path> Value) {
 369             Set<String> result = new LinkedHashSet<String>();
 370             ModuleManager mm = new ModuleManager(Value);
 371             List<ModFile> modFiles = mm.getModules(
 372                     EnumSet.of(ModuleManager.SearchType.ModularJar,
 373                     ModuleManager.SearchType.Jmod,
 374                     ModuleManager.SearchType.ExplodedModule));
 375 
 376             for (ModFile modFile : modFiles) {
 377                 result.add(modFile.getModName());
 378             }
 379             return result;
 380         }
 381     }
 382 
 383     private static void runJLink(Path output, List<Path> modulePath,
 384             Set<String> modules, Set<String> limitModules, String excludes,
 385             HashMap<String, String> user, boolean bindServices)
 386             throws IOException {
 387 
 388         // This is just to ensure jlink is given a non-existant directory
 389         // The passed in output path should be non-existant or empty directory
 390         IOUtils.deleteRecursive(output.toFile());
 391 
 392         ArrayList<String> args = new ArrayList<String>();
 393         args.add("--output");
 394         args.add(output.toString());
 395         if (modulePath != null && !modulePath.isEmpty()) {
 396             args.add("--module-path");
 397             args.add(getPathList(modulePath));
 398         }
 399         if (modules != null && !modules.isEmpty()) {
 400             args.add("--add-modules");
 401             args.add(getStringList(modules));
 402         }
 403         if (limitModules != null && !limitModules.isEmpty()) {
 404             args.add("--limit-modules");
 405             args.add(getStringList(limitModules));
 406         }
 407         if (excludes != null) {
 408             args.add("--exclude-files");
 409             args.add(excludes);
 410         }
 411         if (user != null && !user.isEmpty()) {
 412             for (Map.Entry<String, String> entry : user.entrySet()) {
 413                 args.add(entry.getKey());
 414                 args.add(entry.getValue());
 415             }
 416         } else {
 417             args.add("--strip-native-commands");
 418             args.add("--strip-debug");
 419             args.add("--no-man-pages");
 420             args.add("--no-header-files");
 421             if (bindServices) {
 422                 args.add("--bind-services");
 423             }
 424         }
 425         
 426         StringWriter writer = new StringWriter();
 427         PrintWriter pw = new PrintWriter(writer);
 428 
 429         Log.verbose("jlink arguments: " + args);
 430         int retVal = JLINK_TOOL.run(pw, pw, args.toArray(new String[0]));
 431         String jlinkOut = writer.toString();
 432 
 433         if (retVal != 0) {
 434             throw new IOException("jlink failed with: " + jlinkOut);
 435         } else if (jlinkOut.length() > 0) {
 436             Log.verbose("jlink output: " + jlinkOut);
 437         }
 438     }
 439 
 440     private static String getPathList(List<Path> pathList) {
 441         String ret = null;
 442         for (Path p : pathList) {
 443             String s =  Matcher.quoteReplacement(p.toString());
 444             if (ret == null) {
 445                 ret = s;
 446             } else {
 447                 ret += File.pathSeparator +  s;
 448             }
 449         }
 450         return ret;
 451     }
 452 
 453     private static String getStringList(Set<String> strings) {
 454         String ret = null;
 455         for (String s : strings) {
 456             if (ret == null) {
 457                 ret = s;
 458             } else {
 459                 ret += "," + s;
 460             }
 461         }
 462         return (ret == null) ? null : Matcher.quoteReplacement(ret);
 463     }
 464 }