1 /*
   2  * Copyright (c) 2015, 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.tools.jlink.builder;
  27 
  28 import java.io.BufferedOutputStream;
  29 import java.io.BufferedWriter;
  30 import java.io.ByteArrayInputStream;
  31 import java.io.DataOutputStream;
  32 import java.io.File;
  33 import java.io.FileInputStream;
  34 import java.io.FileOutputStream;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.io.OutputStream;
  38 import java.io.OutputStreamWriter;
  39 import java.io.UncheckedIOException;
  40 import java.io.Writer;
  41 import java.lang.module.ModuleDescriptor;
  42 import java.nio.charset.StandardCharsets;
  43 import java.nio.file.FileAlreadyExistsException;
  44 import java.nio.file.Files;
  45 import java.nio.file.Path;
  46 import java.nio.file.Paths;
  47 import java.nio.file.StandardOpenOption;
  48 import java.nio.file.attribute.PosixFilePermission;
  49 import java.util.Collections;
  50 import java.util.HashMap;
  51 import java.util.HashSet;
  52 import java.util.List;
  53 import java.util.Map;
  54 import java.util.Objects;
  55 import java.util.Optional;
  56 import java.util.Properties;
  57 import java.util.Set;
  58 import static java.util.stream.Collectors.*;
  59 
  60 import jdk.tools.jlink.internal.BasicImageWriter;
  61 import jdk.tools.jlink.internal.ExecutableImage;
  62 import jdk.tools.jlink.plugin.ResourcePool;
  63 import jdk.tools.jlink.plugin.ResourcePoolEntry;
  64 import jdk.tools.jlink.plugin.ResourcePoolEntry.Type;
  65 import jdk.tools.jlink.plugin.ResourcePoolModule;
  66 import jdk.tools.jlink.plugin.PluginException;
  67 
  68 /**
  69  *
  70  * Default Image Builder. This builder creates the default runtime image layout.
  71  */
  72 public final class DefaultImageBuilder implements ImageBuilder {
  73     // Top-level directory names in a modular runtime image
  74     public static final String BIN_DIRNAME      = "bin";
  75     public static final String CONF_DIRNAME     = "conf";
  76     public static final String INCLUDE_DIRNAME  = "include";
  77     public static final String LIB_DIRNAME      = "lib";
  78     public static final String LEGAL_DIRNAME    = "legal";
  79     public static final String MAN_DIRNAME      = "man";
  80 
  81     /**
  82      * The default java executable Image.
  83      */
  84     static final class DefaultExecutableImage implements ExecutableImage {
  85 
  86         private final Path home;
  87         private final List<String> args;
  88         private final Set<String> modules;
  89 
  90         DefaultExecutableImage(Path home, Set<String> modules) {
  91             Objects.requireNonNull(home);
  92             if (!Files.exists(home)) {
  93                 throw new IllegalArgumentException("Invalid image home");
  94             }
  95             this.home = home;
  96             this.modules = Collections.unmodifiableSet(modules);
  97             this.args = createArgs(home);
  98         }
  99 
 100         private static List<String> createArgs(Path home) {
 101             Objects.requireNonNull(home);
 102             Path binDir = home.resolve("bin");
 103             String java = Files.exists(binDir.resolve("java"))? "java" : "java.exe";
 104             return List.of(binDir.resolve(java).toString());
 105         }
 106 
 107         @Override
 108         public Path getHome() {
 109             return home;
 110         }
 111 
 112         @Override
 113         public Set<String> getModules() {
 114             return modules;
 115         }
 116 
 117         @Override
 118         public List<String> getExecutionArgs() {
 119             return args;
 120         }
 121 
 122         @Override
 123         public void storeLaunchArgs(List<String> args) {
 124             try {
 125                 patchScripts(this, args);
 126             } catch (IOException ex) {
 127                 throw new UncheckedIOException(ex);
 128             }
 129         }
 130     }
 131 
 132     private final Path root;
 133     private final Map<String, String> launchers;
 134     private final Path mdir;
 135     private final Set<String> modules = new HashSet<>();
 136     private String targetOsName;
 137 
 138     /**
 139      * Default image builder constructor.
 140      *
 141      * @param root The image root directory.
 142      * @throws IOException
 143      */
 144     public DefaultImageBuilder(Path root, Map<String, String> launchers) throws IOException {
 145         this.root = Objects.requireNonNull(root);
 146         this.launchers = Objects.requireNonNull(launchers);
 147         this.mdir = root.resolve("lib");
 148         Files.createDirectories(mdir);
 149     }
 150 
 151     @Override
 152     public void storeFiles(ResourcePool files) {
 153         try {
 154             // populate targetOsName field up-front because it's used elsewhere.
 155             Optional<ResourcePoolModule> javaBase = files.moduleView().findModule("java.base");
 156             javaBase.ifPresent(mod -> {
 157                 // fill release information available from transformed "java.base" module!
 158                 ModuleDescriptor desc = mod.descriptor();
 159                 desc.osName().ifPresent(s -> {
 160                     this.targetOsName = s;
 161                 });
 162             });
 163 
 164             if (this.targetOsName == null) {
 165                 throw new PluginException("ModuleTarget attribute is missing for java.base module");
 166             }
 167 
 168             checkResourcePool(files);
 169 
 170             Path bin = root.resolve(BIN_DIRNAME);
 171 
 172             // write non-classes resource files to the image
 173             files.entries()
 174                 .filter(f -> f.type() != ResourcePoolEntry.Type.CLASS_OR_RESOURCE)
 175                 .forEach(f -> {
 176                     try {
 177                         accept(f);
 178                     } catch (FileAlreadyExistsException e) {
 179                         // Should not happen! Duplicates checking already done!
 180                         throw new AssertionError("Duplicate entry!", e);
 181                     } catch (IOException ioExp) {
 182                         throw new UncheckedIOException(ioExp);
 183                     }
 184                 });
 185 
 186             files.moduleView().modules().forEach(m -> {
 187                 // Only add modules that contain packages
 188                 if (!m.packages().isEmpty()) {
 189                     modules.add(m.name());
 190                 }
 191             });
 192 
 193             if (root.getFileSystem().supportedFileAttributeViews()
 194                     .contains("posix")) {
 195                 // launchers in the bin directory need execute permission.
 196                 // On Windows, "bin" also subdirectories containing jvm.dll.
 197                 if (Files.isDirectory(bin)) {
 198                     Files.find(bin, 2, (path, attrs) -> {
 199                         return attrs.isRegularFile() && !path.toString().endsWith(".diz");
 200                     }).forEach(this::setExecutable);
 201                 }
 202 
 203                 // jspawnhelper is in lib or lib/<arch>
 204                 Path lib = root.resolve(LIB_DIRNAME);
 205                 if (Files.isDirectory(lib)) {
 206                     Files.find(lib, 2, (path, attrs) -> {
 207                         return path.getFileName().toString().equals("jspawnhelper")
 208                                 || path.getFileName().toString().equals("jexec");
 209                     }).forEach(this::setExecutable);
 210                 }
 211 
 212                 // read-only legal notices/license files
 213                 Path legal = root.resolve(LEGAL_DIRNAME);
 214                 if (Files.isDirectory(legal)) {
 215                     Files.find(legal, 2, (path, attrs) -> {
 216                         return attrs.isRegularFile();
 217                     }).forEach(this::setReadOnly);
 218                 }
 219             }
 220 
 221             // If native files are stripped completely, <root>/bin dir won't exist!
 222             // So, don't bother generating launcher scripts.
 223             if (Files.isDirectory(bin)) {
 224                  prepareApplicationFiles(files);
 225             }
 226         } catch (IOException ex) {
 227             throw new PluginException(ex);
 228         }
 229     }
 230 
 231     private void checkResourcePool(ResourcePool pool) {
 232         // For now, only duplicate resources check. Add more checks here (if any)
 233         checkDuplicateResources(pool);
 234     }
 235 
 236     private void checkDuplicateResources(ResourcePool pool) {
 237         // check any duplicated resources
 238         Map<Path, Set<String>> duplicates = new HashMap<>();
 239         pool.entries()
 240              .filter(f -> f.type() != ResourcePoolEntry.Type.CLASS_OR_RESOURCE)
 241              .collect(groupingBy(this::entryToImagePath,
 242                       mapping(ResourcePoolEntry::moduleName, toSet())))
 243              .entrySet()
 244              .stream()
 245              .filter(e -> e.getValue().size() > 1)
 246              .forEach(e -> duplicates.put(e.getKey(), e.getValue()));
 247         if (!duplicates.isEmpty()) {
 248             throw new PluginException("Duplicate resources: " + duplicates);
 249         }
 250     }
 251 
 252     /**
 253      * Generates launcher scripts.
 254      *
 255      * @param imageContent The image content.
 256      * @throws IOException
 257      */
 258     protected void prepareApplicationFiles(ResourcePool imageContent) throws IOException {
 259         // generate launch scripts for the modules with a main class
 260         for (Map.Entry<String, String> entry : launchers.entrySet()) {
 261             String launcherEntry = entry.getValue();
 262             int slashIdx = launcherEntry.indexOf("/");
 263             String module, mainClassName;
 264             if (slashIdx == -1) {
 265                 module = launcherEntry;
 266                 mainClassName = null;
 267             } else {
 268                 module = launcherEntry.substring(0, slashIdx);
 269                 assert !module.isEmpty();
 270                 mainClassName = launcherEntry.substring(slashIdx + 1);
 271                 assert !mainClassName.isEmpty();
 272             }
 273 
 274             String path = "/" + module + "/module-info.class";
 275             Optional<ResourcePoolEntry> res = imageContent.findEntry(path);
 276             if (!res.isPresent()) {
 277                 throw new IOException("module-info.class not found for " + module + " module");
 278             }
 279             ByteArrayInputStream stream = new ByteArrayInputStream(res.get().contentBytes());
 280             Optional<String> mainClass = ModuleDescriptor.read(stream).mainClass();
 281             if (mainClassName == null && mainClass.isPresent()) {
 282                 mainClassName = mainClass.get();
 283             }
 284 
 285             if (mainClassName != null) {
 286                 // make sure main class exists!
 287                 if (!imageContent.findEntry("/" + module + "/" +
 288                         mainClassName.replace('.', '/') + ".class").isPresent()) {
 289                     throw new IllegalArgumentException(module + " does not have main class: " + mainClassName);
 290                 }
 291 
 292                 String launcherFile = entry.getKey();
 293                 Path cmd = root.resolve("bin").resolve(launcherFile);
 294                 // generate shell script for Unix platforms
 295                 StringBuilder sb = new StringBuilder();
 296                 sb.append("#!/bin/sh")
 297                         .append("\n");
 298                 sb.append("JLINK_VM_OPTIONS=")
 299                         .append("\n");
 300                 sb.append("DIR=`dirname $0`")
 301                         .append("\n");
 302                 sb.append("$DIR/java $JLINK_VM_OPTIONS -m ")
 303                         .append(module).append('/')
 304                         .append(mainClassName)
 305                         .append(" $@\n");
 306 
 307                 try (BufferedWriter writer = Files.newBufferedWriter(cmd,
 308                         StandardCharsets.ISO_8859_1,
 309                         StandardOpenOption.CREATE_NEW)) {
 310                     writer.write(sb.toString());
 311                 }
 312                 if (root.resolve("bin").getFileSystem()
 313                         .supportedFileAttributeViews().contains("posix")) {
 314                     setExecutable(cmd);
 315                 }
 316                 // generate .bat file for Windows
 317                 if (isWindows()) {
 318                     Path bat = root.resolve(BIN_DIRNAME).resolve(launcherFile + ".bat");
 319                     sb = new StringBuilder();
 320                     sb.append("@echo off")
 321                             .append("\r\n");
 322                     sb.append("set JLINK_VM_OPTIONS=")
 323                             .append("\r\n");
 324                     sb.append("set DIR=%~dp0")
 325                             .append("\r\n");
 326                     sb.append("\"%DIR%\\java\" %JLINK_VM_OPTIONS% -m ")
 327                             .append(module).append('/')
 328                             .append(mainClassName)
 329                             .append(" %*\r\n");
 330 
 331                     try (BufferedWriter writer = Files.newBufferedWriter(bat,
 332                             StandardCharsets.ISO_8859_1,
 333                             StandardOpenOption.CREATE_NEW)) {
 334                         writer.write(sb.toString());
 335                     }
 336                 }
 337             } else {
 338                 throw new IllegalArgumentException(module + " doesn't contain main class & main not specified in command line");
 339             }
 340         }
 341     }
 342 
 343     @Override
 344     public DataOutputStream getJImageOutputStream() {
 345         try {
 346             Path jimageFile = mdir.resolve(BasicImageWriter.MODULES_IMAGE_NAME);
 347             OutputStream fos = Files.newOutputStream(jimageFile);
 348             BufferedOutputStream bos = new BufferedOutputStream(fos);
 349             return new DataOutputStream(bos);
 350         } catch (IOException ex) {
 351             throw new UncheckedIOException(ex);
 352         }
 353     }
 354 
 355     /**
 356      * Returns the file name of this entry
 357      */
 358     private String entryToFileName(ResourcePoolEntry entry) {
 359         if (entry.type() == ResourcePoolEntry.Type.CLASS_OR_RESOURCE)
 360             throw new IllegalArgumentException("invalid type: " + entry);
 361 
 362         String module = "/" + entry.moduleName() + "/";
 363         String filename = entry.path().substring(module.length());
 364 
 365         // Remove radical lib|config|...
 366         return filename.substring(filename.indexOf('/') + 1);
 367     }
 368 
 369     /**
 370      * Returns the path of the given entry to be written in the image
 371      */
 372     private Path entryToImagePath(ResourcePoolEntry entry) {
 373         switch (entry.type()) {
 374             case NATIVE_LIB:
 375                 String filename = entryToFileName(entry);
 376                 return Paths.get(nativeDir(filename), filename);
 377             case NATIVE_CMD:
 378                 return Paths.get(BIN_DIRNAME, entryToFileName(entry));
 379             case CONFIG:
 380                 return Paths.get(CONF_DIRNAME, entryToFileName(entry));
 381             case HEADER_FILE:
 382                 return Paths.get(INCLUDE_DIRNAME, entryToFileName(entry));
 383             case MAN_PAGE:
 384                 return Paths.get(MAN_DIRNAME, entryToFileName(entry));
 385             case LEGAL_NOTICE:
 386                 return Paths.get(LEGAL_DIRNAME, entryToFileName(entry));
 387             case TOP:
 388                 return Paths.get(entryToFileName(entry));
 389             default:
 390                 throw new IllegalArgumentException("invalid type: " + entry);
 391         }
 392     }
 393 
 394     private void accept(ResourcePoolEntry file) throws IOException {
 395         if (file.linkedTarget() != null && file.type() != Type.LEGAL_NOTICE) {
 396             throw new UnsupportedOperationException("symbolic link not implemented: " + file);
 397         }
 398 
 399         try (InputStream in = file.content()) {
 400             switch (file.type()) {
 401                 case NATIVE_LIB:
 402                     Path dest = root.resolve(entryToImagePath(file));
 403                     writeEntry(in, dest);
 404                     break;
 405                 case NATIVE_CMD:
 406                     Path p = root.resolve(entryToImagePath(file));
 407                     writeEntry(in, p);
 408                     p.toFile().setExecutable(true);
 409                     break;
 410                 case CONFIG:
 411                 case HEADER_FILE:
 412                 case MAN_PAGE:
 413                     writeEntry(in, root.resolve(entryToImagePath(file)));
 414                     break;
 415                 case LEGAL_NOTICE:
 416                     Path source = entryToImagePath(file);
 417                     if (file.linkedTarget() == null) {
 418                         writeEntry(in, root.resolve(source));
 419                     } else {
 420                         Path target = entryToImagePath(file.linkedTarget());
 421                         Path relPath = source.getParent().relativize(target);
 422                         writeSymLinkEntry(root.resolve(source), relPath);
 423                     }
 424                     break;
 425                 case TOP:
 426                     // Copy TOP files of the "java.base" module (only)
 427                     if ("java.base".equals(file.moduleName())) {
 428                         writeEntry(in, root.resolve(entryToImagePath(file)));
 429                     } else {
 430                         throw new InternalError("unexpected TOP entry: " + file.path());
 431                     }
 432                     break;
 433                 default:
 434                     throw new InternalError("unexpected entry: " + file.path());
 435             }
 436         }
 437     }
 438 
 439     private void writeEntry(InputStream in, Path dstFile) throws IOException {
 440         Objects.requireNonNull(in);
 441         Objects.requireNonNull(dstFile);
 442         Files.createDirectories(Objects.requireNonNull(dstFile.getParent()));
 443         Files.copy(in, dstFile);
 444     }
 445 
 446     private void writeSymEntry(Path dstFile, Path target) throws IOException {
 447         Objects.requireNonNull(dstFile);
 448         Objects.requireNonNull(target);
 449         Files.createDirectories(Objects.requireNonNull(dstFile.getParent()));
 450         Files.createLink(dstFile, target);
 451     }
 452 
 453     /*
 454      * Create a symbolic link to the given target if the target platform
 455      * supports symbolic link; otherwise, it will create a tiny file
 456      * to contain the path to the target.
 457      */
 458     private void writeSymLinkEntry(Path dstFile, Path target) throws IOException {
 459         Objects.requireNonNull(dstFile);
 460         Objects.requireNonNull(target);
 461         Files.createDirectories(Objects.requireNonNull(dstFile.getParent()));
 462         if (!isWindows() && root.getFileSystem()
 463                                 .supportedFileAttributeViews()
 464                                 .contains("posix")) {
 465             Files.createSymbolicLink(dstFile, target);
 466         } else {
 467             try (BufferedWriter writer = Files.newBufferedWriter(dstFile)) {
 468                 writer.write(String.format("Please see %s%n", target.toString()));
 469             }
 470         }
 471     }
 472 
 473     private String nativeDir(String filename) {
 474         if (isWindows()) {
 475             if (filename.endsWith(".dll") || filename.endsWith(".diz")
 476                     || filename.endsWith(".pdb") || filename.endsWith(".map")) {
 477                 return BIN_DIRNAME;
 478             } else {
 479                 return LIB_DIRNAME;
 480             }
 481         } else {
 482             return LIB_DIRNAME;
 483         }
 484     }
 485 
 486     private boolean isWindows() {
 487         return targetOsName.startsWith("Windows");
 488     }
 489 
 490     /**
 491      * chmod ugo+x file
 492      */
 493     private void setExecutable(Path file) {
 494         try {
 495             Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file);
 496             perms.add(PosixFilePermission.OWNER_EXECUTE);
 497             perms.add(PosixFilePermission.GROUP_EXECUTE);
 498             perms.add(PosixFilePermission.OTHERS_EXECUTE);
 499             Files.setPosixFilePermissions(file, perms);
 500         } catch (IOException ioe) {
 501             throw new UncheckedIOException(ioe);
 502         }
 503     }
 504 
 505     /**
 506      * chmod ugo-w file
 507      */
 508     private void setReadOnly(Path file) {
 509         try {
 510             Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file);
 511             perms.remove(PosixFilePermission.OWNER_WRITE);
 512             perms.remove(PosixFilePermission.GROUP_WRITE);
 513             perms.remove(PosixFilePermission.OTHERS_WRITE);
 514             Files.setPosixFilePermissions(file, perms);
 515         } catch (IOException ioe) {
 516             throw new UncheckedIOException(ioe);
 517         }
 518     }
 519 
 520     private static void createUtf8File(File file, String content) throws IOException {
 521         try (OutputStream fout = new FileOutputStream(file);
 522                 Writer output = new OutputStreamWriter(fout, "UTF-8")) {
 523             output.write(content);
 524         }
 525     }
 526 
 527     @Override
 528     public ExecutableImage getExecutableImage() {
 529         return new DefaultExecutableImage(root, modules);
 530     }
 531 
 532     // This is experimental, we should get rid-off the scripts in a near future
 533     private static void patchScripts(ExecutableImage img, List<String> args) throws IOException {
 534         Objects.requireNonNull(args);
 535         if (!args.isEmpty()) {
 536             Files.find(img.getHome().resolve(BIN_DIRNAME), 2, (path, attrs) -> {
 537                 return img.getModules().contains(path.getFileName().toString());
 538             }).forEach((p) -> {
 539                 try {
 540                     String pattern = "JLINK_VM_OPTIONS=";
 541                     byte[] content = Files.readAllBytes(p);
 542                     String str = new String(content, StandardCharsets.UTF_8);
 543                     int index = str.indexOf(pattern);
 544                     StringBuilder builder = new StringBuilder();
 545                     if (index != -1) {
 546                         builder.append(str.substring(0, index)).
 547                                 append(pattern);
 548                         for (String s : args) {
 549                             builder.append(s).append(" ");
 550                         }
 551                         String remain = str.substring(index + pattern.length());
 552                         builder.append(remain);
 553                         str = builder.toString();
 554                         try (BufferedWriter writer = Files.newBufferedWriter(p,
 555                                 StandardCharsets.ISO_8859_1,
 556                                 StandardOpenOption.WRITE)) {
 557                             writer.write(str);
 558                         }
 559                     }
 560                 } catch (IOException ex) {
 561                     throw new RuntimeException(ex);
 562                 }
 563             });
 564         }
 565     }
 566 
 567     public static ExecutableImage getExecutableImage(Path root) {
 568         Path binDir = root.resolve(BIN_DIRNAME);
 569         if (Files.exists(binDir.resolve("java")) ||
 570             Files.exists(binDir.resolve("java.exe"))) {
 571             return new DefaultExecutableImage(root, retrieveModules(root));
 572         }
 573         return null;
 574     }
 575 
 576     private static Set<String> retrieveModules(Path root) {
 577         Path releaseFile = root.resolve("release");
 578         Set<String> modules = new HashSet<>();
 579         if (Files.exists(releaseFile)) {
 580             Properties release = new Properties();
 581             try (FileInputStream fi = new FileInputStream(releaseFile.toFile())) {
 582                 release.load(fi);
 583             } catch (IOException ex) {
 584                 System.err.println("Can't read release file " + ex);
 585             }
 586             String mods = release.getProperty("MODULES");
 587             if (mods != null) {
 588                 String[] arr = mods.substring(1, mods.length() - 1).split(" ");
 589                 for (String m : arr) {
 590                     modules.add(m.trim());
 591                 }
 592 
 593             }
 594         }
 595         return modules;
 596     }
 597 }