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