1 /* 2 * Copyright (c) 2015, 2016, 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.jmod; 27 28 import java.io.BufferedInputStream; 29 import java.io.BufferedOutputStream; 30 import java.io.ByteArrayInputStream; 31 import java.io.ByteArrayOutputStream; 32 import java.io.File; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.io.OutputStream; 36 import java.io.PrintStream; 37 import java.io.UncheckedIOException; 38 import java.lang.module.Configuration; 39 import java.lang.module.ModuleReader; 40 import java.lang.module.ModuleReference; 41 import java.lang.module.ModuleFinder; 42 import java.lang.module.ModuleDescriptor; 43 import java.lang.module.ModuleDescriptor.Exports; 44 import java.lang.module.ModuleDescriptor.Provides; 45 import java.lang.module.ModuleDescriptor.Requires; 46 import java.lang.module.ModuleDescriptor.Version; 47 import java.lang.module.ResolutionException; 48 import java.lang.module.ResolvedModule; 49 import java.net.URI; 50 import java.nio.file.FileSystems; 51 import java.nio.file.FileVisitResult; 52 import java.nio.file.Files; 53 import java.nio.file.InvalidPathException; 54 import java.nio.file.Path; 55 import java.nio.file.PathMatcher; 56 import java.nio.file.Paths; 57 import java.nio.file.SimpleFileVisitor; 58 import java.nio.file.StandardCopyOption; 59 import java.nio.file.attribute.BasicFileAttributes; 60 import java.text.MessageFormat; 61 import java.util.ArrayDeque; 62 import java.util.ArrayList; 63 import java.util.Arrays; 64 import java.util.Collection; 65 import java.util.Collections; 66 import java.util.Comparator; 67 import java.util.Deque; 68 import java.util.HashMap; 69 import java.util.HashSet; 70 import java.util.List; 71 import java.util.Locale; 72 import java.util.Map; 73 import java.util.MissingResourceException; 74 import java.util.Optional; 75 import java.util.ResourceBundle; 76 import java.util.Set; 77 import java.util.function.Consumer; 78 import java.util.function.Function; 79 import java.util.function.Predicate; 80 import java.util.function.Supplier; 81 import java.util.jar.JarEntry; 82 import java.util.jar.JarFile; 83 import java.util.stream.Collectors; 84 import java.util.regex.Pattern; 85 import java.util.regex.PatternSyntaxException; 86 import java.util.zip.ZipEntry; 87 import java.util.zip.ZipException; 88 import java.util.zip.ZipFile; 89 import java.util.zip.ZipInputStream; 90 import java.util.zip.ZipOutputStream; 91 92 import jdk.internal.joptsimple.BuiltinHelpFormatter; 93 import jdk.internal.joptsimple.NonOptionArgumentSpec; 94 import jdk.internal.joptsimple.OptionDescriptor; 95 import jdk.internal.joptsimple.OptionException; 96 import jdk.internal.joptsimple.OptionParser; 97 import jdk.internal.joptsimple.OptionSet; 98 import jdk.internal.joptsimple.OptionSpec; 99 import jdk.internal.joptsimple.ValueConverter; 100 import jdk.internal.misc.JavaLangModuleAccess; 101 import jdk.internal.misc.SharedSecrets; 102 import jdk.internal.module.ConfigurableModuleFinder; 103 import jdk.internal.module.ConfigurableModuleFinder.Phase; 104 import jdk.internal.module.ModuleHashes; 105 import jdk.internal.module.ModuleInfoExtender; 106 import jdk.tools.jlink.internal.Utils; 107 108 import static java.util.stream.Collectors.joining; 109 110 /** 111 * Implementation for the jmod tool. 112 */ 113 public class JmodTask { 114 115 static class CommandException extends RuntimeException { 116 private static final long serialVersionUID = 0L; 117 boolean showUsage; 118 119 CommandException(String key, Object... args) { 120 super(getMessageOrKey(key, args)); 121 } 122 123 CommandException showUsage(boolean b) { 124 showUsage = b; 125 return this; 126 } 127 128 private static String getMessageOrKey(String key, Object... args) { 129 try { 130 return MessageFormat.format(ResourceBundleHelper.bundle.getString(key), args); 131 } catch (MissingResourceException e) { 132 return key; 133 } 134 } 135 } 136 137 private static final String PROGNAME = "jmod"; 138 private static final String MODULE_INFO = "module-info.class"; 139 140 private Options options; 141 private PrintStream out = System.out; 142 void setLog(PrintStream out) { 143 this.out = out; 144 } 145 146 /* Result codes. */ 147 static final int EXIT_OK = 0, // Completed with no errors. 148 EXIT_ERROR = 1, // Completed but reported errors. 149 EXIT_CMDERR = 2, // Bad command-line arguments 150 EXIT_SYSERR = 3, // System error or resource exhaustion. 151 EXIT_ABNORMAL = 4;// terminated abnormally 152 153 enum Mode { 154 CREATE, 155 LIST, 156 DESCRIBE, 157 HASH 158 }; 159 160 static class Options { 161 Mode mode; 162 Path jmodFile; 163 boolean help; 164 boolean version; 165 List<Path> classpath; 166 List<Path> cmds; 167 List<Path> configs; 168 List<Path> libs; 169 ModuleFinder moduleFinder; 170 Version moduleVersion; 171 String mainClass; 172 String osName; 173 String osArch; 174 String osVersion; 175 Pattern modulesToHash; 176 boolean dryrun; 177 List<PathMatcher> excludes; 178 } 179 180 public int run(String[] args) { 181 182 try { 183 handleOptions(args); 184 if (options == null) { 185 showUsageSummary(); 186 return EXIT_CMDERR; 187 } 188 if (options.help) { 189 showHelp(); 190 return EXIT_OK; 191 } 192 if (options.version) { 193 showVersion(); 194 return EXIT_OK; 195 } 196 197 boolean ok; 198 switch (options.mode) { 199 case CREATE: 200 ok = create(); 201 break; 202 case LIST: 203 ok = list(); 204 break; 205 case DESCRIBE: 206 ok = describe(); 207 break; 208 case HASH: 209 ok = hashModules(); 210 break; 211 default: 212 throw new AssertionError("Unknown mode: " + options.mode.name()); 213 } 214 215 return ok ? EXIT_OK : EXIT_ERROR; 216 } catch (CommandException e) { 217 reportError(e.getMessage()); 218 if (e.showUsage) 219 showUsageSummary(); 220 return EXIT_CMDERR; 221 } catch (Exception x) { 222 reportError(x.getMessage()); 223 x.printStackTrace(); 224 return EXIT_ABNORMAL; 225 } finally { 226 out.flush(); 227 } 228 } 229 230 private boolean list() throws IOException { 231 ZipFile zip = null; 232 try { 233 try { 234 zip = new ZipFile(options.jmodFile.toFile()); 235 } catch (IOException x) { 236 throw new IOException("error opening jmod file", x); 237 } 238 239 // Trivially print the archive entries for now, pending a more complete implementation 240 zip.stream().forEach(e -> out.println(e.getName())); 241 return true; 242 } finally { 243 if (zip != null) 244 zip.close(); 245 } 246 } 247 248 private boolean hashModules() { 249 return new Hasher(options.moduleFinder).run(); 250 } 251 252 private boolean describe() throws IOException { 253 ZipFile zip = null; 254 try { 255 try { 256 zip = new ZipFile(options.jmodFile.toFile()); 257 } catch (IOException x) { 258 throw new IOException("error opening jmod file", x); 259 } 260 261 try (InputStream in = Files.newInputStream(options.jmodFile)) { 262 boolean found = printModuleDescriptor(in); 263 if (!found) 264 throw new CommandException("err.module.descriptor.not.found"); 265 return found; 266 } 267 } finally { 268 if (zip != null) 269 zip.close(); 270 } 271 } 272 273 static <T> String toString(Set<T> set) { 274 if (set.isEmpty()) { return ""; } 275 return set.stream().map(e -> e.toString().toLowerCase(Locale.ROOT)) 276 .collect(joining(" ")); 277 } 278 279 private static final JavaLangModuleAccess JLMA = SharedSecrets.getJavaLangModuleAccess(); 280 281 private boolean printModuleDescriptor(InputStream in) 282 throws IOException 283 { 284 final String mi = Section.CLASSES.jmodDir() + "/" + MODULE_INFO; 285 try (BufferedInputStream bis = new BufferedInputStream(in); 286 ZipInputStream zis = new ZipInputStream(bis)) { 287 288 ZipEntry e; 289 while ((e = zis.getNextEntry()) != null) { 290 if (e.getName().equals(mi)) { 291 ModuleDescriptor md = ModuleDescriptor.read(zis); 292 StringBuilder sb = new StringBuilder(); 293 sb.append("\n").append(md.toNameAndVersion()); 294 295 md.requires().stream() 296 .sorted(Comparator.comparing(Requires::name)) 297 .forEach(r -> { 298 sb.append("\n requires "); 299 if (!r.modifiers().isEmpty()) 300 sb.append(toString(r.modifiers())).append(" "); 301 sb.append(r.name()); 302 }); 303 304 md.uses().stream().sorted() 305 .forEach(s -> sb.append("\n uses ").append(s)); 306 307 md.exports().stream() 308 .sorted(Comparator.comparing(Exports::source)) 309 .forEach(p -> sb.append("\n exports ").append(p)); 310 311 md.conceals().stream().sorted() 312 .forEach(p -> sb.append("\n conceals ").append(p)); 313 314 md.provides().values().stream() 315 .sorted(Comparator.comparing(Provides::service)) 316 .forEach(p -> sb.append("\n provides ").append(p.service()) 317 .append(" with ") 318 .append(toString(p.providers()))); 319 320 md.mainClass().ifPresent(v -> sb.append("\n main-class " + v)); 321 322 md.osName().ifPresent(v -> sb.append("\n operating-system-name " + v)); 323 324 md.osArch().ifPresent(v -> sb.append("\n operating-system-architecture " + v)); 325 326 md.osVersion().ifPresent(v -> sb.append("\n operating-system-version " + v)); 327 328 JLMA.hashes(md).ifPresent( 329 hashes -> hashes.names().stream().sorted().forEach( 330 mod -> sb.append("\n hashes ").append(mod).append(" ") 331 .append(hashes.algorithm()).append(" ") 332 .append(hashes.hashFor(mod)))); 333 334 out.println(sb.toString()); 335 return true; 336 } 337 } 338 } 339 return false; 340 } 341 342 private boolean create() throws IOException { 343 JmodFileWriter jmod = new JmodFileWriter(); 344 345 // create jmod with temporary name to avoid it being examined 346 // when scanning the module path 347 Path target = options.jmodFile; 348 Path tempTarget = target.resolveSibling(target.getFileName() + ".tmp"); 349 try { 350 try (OutputStream out = Files.newOutputStream(tempTarget); 351 BufferedOutputStream bos = new BufferedOutputStream(out)) { 352 jmod.write(bos); 353 } 354 Files.move(tempTarget, target); 355 } catch (Exception e) { 356 if (Files.exists(tempTarget)) { 357 try { 358 Files.delete(tempTarget); 359 } catch (IOException ioe) { 360 e.addSuppressed(ioe); 361 } 362 } 363 throw e; 364 } 365 return true; 366 } 367 368 private class JmodFileWriter { 369 final List<Path> cmds = options.cmds; 370 final List<Path> libs = options.libs; 371 final List<Path> configs = options.configs; 372 final List<Path> classpath = options.classpath; 373 final Version moduleVersion = options.moduleVersion; 374 final String mainClass = options.mainClass; 375 final String osName = options.osName; 376 final String osArch = options.osArch; 377 final String osVersion = options.osVersion; 378 final List<PathMatcher> excludes = options.excludes; 379 final Hasher hasher = hasher(); 380 381 JmodFileWriter() { } 382 383 /** 384 * Writes the jmod to the given output stream. 385 */ 386 void write(OutputStream out) throws IOException { 387 try (ZipOutputStream zos = new ZipOutputStream(out)) { 388 389 // module-info.class 390 writeModuleInfo(zos, findPackages(classpath)); 391 392 // classes 393 processClasses(zos, classpath); 394 395 processSection(zos, Section.NATIVE_CMDS, cmds); 396 processSection(zos, Section.NATIVE_LIBS, libs); 397 processSection(zos, Section.CONFIG, configs); 398 } 399 } 400 401 /** 402 * Returns a supplier of an input stream to the module-info.class 403 * on the class path of directories and JAR files. 404 */ 405 Supplier<InputStream> newModuleInfoSupplier() throws IOException { 406 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 407 for (Path e: classpath) { 408 if (Files.isDirectory(e)) { 409 Path mi = e.resolve(MODULE_INFO); 410 if (Files.isRegularFile(mi)) { 411 Files.copy(mi, baos); 412 break; 413 } 414 } else if (Files.isRegularFile(e) && e.toString().endsWith(".jar")) { 415 try (JarFile jf = new JarFile(e.toFile())) { 416 ZipEntry entry = jf.getEntry(MODULE_INFO); 417 if (entry != null) { 418 jf.getInputStream(entry).transferTo(baos); 419 break; 420 } 421 } catch (ZipException x) { 422 // Skip. Do nothing. No packages will be added. 423 } 424 } 425 } 426 if (baos.size() == 0) { 427 return null; 428 } else { 429 byte[] bytes = baos.toByteArray(); 430 return () -> new ByteArrayInputStream(bytes); 431 } 432 } 433 434 /** 435 * Writes the updated module-info.class to the ZIP output stream. 436 * 437 * The updated module-info.class will have a ConcealedPackages attribute 438 * with the set of module-private/non-exported packages. 439 * 440 * If --module-version, --main-class, or other options were provided 441 * then the corresponding class file attributes are added to the 442 * module-info here. 443 */ 444 void writeModuleInfo(ZipOutputStream zos, Set<String> packages) 445 throws IOException 446 { 447 Supplier<InputStream> miSupplier = newModuleInfoSupplier(); 448 if (miSupplier == null) { 449 throw new IOException(MODULE_INFO + " not found"); 450 } 451 452 ModuleDescriptor descriptor; 453 try (InputStream in = miSupplier.get()) { 454 descriptor = ModuleDescriptor.read(in); 455 } 456 457 // copy the module-info.class into the jmod with the additional 458 // attributes for the version, main class and other meta data 459 try (InputStream in = miSupplier.get()) { 460 ModuleInfoExtender extender = ModuleInfoExtender.newExtender(in); 461 462 // Add (or replace) the ConcealedPackages attribute 463 if (packages != null) { 464 Set<String> exported = descriptor.exports().stream() 465 .map(ModuleDescriptor.Exports::source) 466 .collect(Collectors.toSet()); 467 Set<String> concealed = packages.stream() 468 .filter(p -> !exported.contains(p)) 469 .collect(Collectors.toSet()); 470 extender.conceals(concealed); 471 } 472 473 // --main-class 474 if (mainClass != null) 475 extender.mainClass(mainClass); 476 477 // --os-name, --os-arch, --os-version 478 if (osName != null || osArch != null || osVersion != null) 479 extender.targetPlatform(osName, osArch, osVersion); 480 481 // --module-version 482 if (moduleVersion != null) 483 extender.version(moduleVersion); 484 485 if (hasher != null) { 486 ModuleHashes moduleHashes = hasher.computeHashes(descriptor.name()); 487 if (moduleHashes != null) { 488 extender.hashes(moduleHashes); 489 } else { 490 warning("warn.no.module.hashes", descriptor.name()); 491 } 492 } 493 494 // write the (possibly extended or modified) module-info.class 495 String e = Section.CLASSES.jmodDir() + "/" + MODULE_INFO; 496 ZipEntry ze = new ZipEntry(e); 497 zos.putNextEntry(ze); 498 extender.write(zos); 499 zos.closeEntry(); 500 } 501 } 502 503 /* 504 * Hasher resolves a module graph using the --hash-modules PATTERN 505 * as the roots. 506 * 507 * The jmod file is being created and does not exist in the 508 * given modulepath. 509 */ 510 private Hasher hasher() { 511 if (options.modulesToHash == null) 512 return null; 513 514 try { 515 Supplier<InputStream> miSupplier = newModuleInfoSupplier(); 516 if (miSupplier == null) { 517 throw new IOException(MODULE_INFO + " not found"); 518 } 519 520 ModuleDescriptor descriptor; 521 try (InputStream in = miSupplier.get()) { 522 descriptor = ModuleDescriptor.read(in); 523 } 524 525 URI uri = options.jmodFile.toUri(); 526 ModuleReference mref = new ModuleReference(descriptor, uri, new Supplier<>() { 527 @Override 528 public ModuleReader get() { 529 throw new UnsupportedOperationException(); 530 } 531 }); 532 533 // compose a module finder with the module path and also 534 // a module finder that can find the jmod file being created 535 ModuleFinder finder = ModuleFinder.compose(options.moduleFinder, 536 new ModuleFinder() { 537 @Override 538 public Optional<ModuleReference> find(String name) { 539 if (descriptor.name().equals(name)) 540 return Optional.of(mref); 541 else return Optional.empty(); 542 } 543 544 @Override 545 public Set<ModuleReference> findAll() { 546 return Collections.singleton(mref); 547 } 548 }); 549 550 return new Hasher(finder); 551 } catch (IOException e) { 552 throw new UncheckedIOException(e); 553 } 554 } 555 556 /** 557 * Returns the set of all packages on the given class path. 558 */ 559 Set<String> findPackages(List<Path> classpath) { 560 Set<String> packages = new HashSet<>(); 561 for (Path path : classpath) { 562 if (Files.isDirectory(path)) { 563 packages.addAll(findPackages(path)); 564 } else if (Files.isRegularFile(path) && path.toString().endsWith(".jar")) { 565 try (JarFile jf = new JarFile(path.toString())) { 566 packages.addAll(findPackages(jf)); 567 } catch (ZipException x) { 568 // Skip. Do nothing. No packages will be added. 569 } catch (IOException ioe) { 570 throw new UncheckedIOException(ioe); 571 } 572 } 573 } 574 return packages; 575 } 576 577 /** 578 * Returns the set of packages in the given directory tree. 579 */ 580 Set<String> findPackages(Path dir) { 581 try { 582 return Files.find(dir, Integer.MAX_VALUE, 583 ((path, attrs) -> attrs.isRegularFile() && 584 path.toString().endsWith(".class"))) 585 .map(path -> toPackageName(dir.relativize(path))) 586 .filter(pkg -> pkg.length() > 0) // module-info 587 .distinct() 588 .collect(Collectors.toSet()); 589 } catch (IOException ioe) { 590 throw new UncheckedIOException(ioe); 591 } 592 } 593 594 /** 595 * Returns the set of packages in the given JAR file. 596 */ 597 Set<String> findPackages(JarFile jf) { 598 return jf.stream() 599 .filter(e -> e.getName().endsWith(".class")) 600 .map(e -> toPackageName(e)) 601 .filter(pkg -> pkg.length() > 0) // module-info 602 .distinct() 603 .collect(Collectors.toSet()); 604 } 605 606 String toPackageName(Path path) { 607 String name = path.toString(); 608 assert name.endsWith(".class"); 609 int index = name.lastIndexOf(File.separatorChar); 610 if (index != -1) 611 return name.substring(0, index).replace(File.separatorChar, '.'); 612 613 if (!name.equals(MODULE_INFO)) { 614 IOException e = new IOException(name + " in the unnamed package"); 615 throw new UncheckedIOException(e); 616 } 617 return ""; 618 } 619 620 String toPackageName(ZipEntry entry) { 621 String name = entry.getName(); 622 assert name.endsWith(".class"); 623 int index = name.lastIndexOf("/"); 624 if (index != -1) 625 return name.substring(0, index).replace('/', '.'); 626 else 627 return ""; 628 } 629 630 void processClasses(ZipOutputStream zos, List<Path> classpaths) 631 throws IOException 632 { 633 if (classpaths == null) 634 return; 635 636 for (Path p : classpaths) { 637 if (Files.isDirectory(p)) { 638 processSection(zos, Section.CLASSES, p); 639 } else if (Files.isRegularFile(p) && p.toString().endsWith(".jar")) { 640 try (JarFile jf = new JarFile(p.toFile())) { 641 JarEntryConsumer jec = new JarEntryConsumer(zos, jf); 642 jf.stream().filter(jec).forEach(jec); 643 } 644 } 645 } 646 } 647 648 void processSection(ZipOutputStream zos, Section section, List<Path> paths) 649 throws IOException 650 { 651 if (paths == null) 652 return; 653 654 for (Path p : paths) 655 processSection(zos, section, p); 656 } 657 658 void processSection(ZipOutputStream zos, Section section, Path top) 659 throws IOException 660 { 661 final String prefix = section.jmodDir(); 662 663 Files.walkFileTree(top, new SimpleFileVisitor<Path>() { 664 @Override 665 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) 666 throws IOException 667 { 668 Path relPath = top.relativize(file); 669 if (!relPath.toString().equals(MODULE_INFO) 670 && !matches(relPath, excludes)) { 671 try (InputStream in = Files.newInputStream(file)) { 672 writeZipEntry(zos, in, prefix, relPath.toString()); 673 } 674 } 675 return FileVisitResult.CONTINUE; 676 } 677 }); 678 } 679 680 boolean matches(Path path, List<PathMatcher> matchers) { 681 if (matchers != null) { 682 for (PathMatcher pm : matchers) { 683 if (pm.matches(path)) 684 return true; 685 } 686 } 687 return false; 688 } 689 690 void writeZipEntry(ZipOutputStream zos, InputStream in, String prefix, String other) 691 throws IOException 692 { 693 String name = Paths.get(prefix, other).toString() 694 .replace(File.separatorChar, '/'); 695 ZipEntry ze = new ZipEntry(name); 696 zos.putNextEntry(ze); 697 in.transferTo(zos); 698 zos.closeEntry(); 699 } 700 701 class JarEntryConsumer implements Consumer<JarEntry>, Predicate<JarEntry> { 702 final ZipOutputStream zos; 703 final JarFile jarfile; 704 JarEntryConsumer(ZipOutputStream zos, JarFile jarfile) { 705 this.zos = zos; 706 this.jarfile = jarfile; 707 } 708 @Override 709 public void accept(JarEntry je) { 710 try (InputStream in = jarfile.getInputStream(je)) { 711 writeZipEntry(zos, in, Section.CLASSES.jmodDir(), je.getName()); 712 } catch (IOException e) { 713 throw new UncheckedIOException(e); 714 } 715 } 716 @Override 717 public boolean test(JarEntry je) { 718 String name = je.getName(); 719 // ## no support for excludes. Is it really needed? 720 return !name.endsWith(MODULE_INFO) && !je.isDirectory(); 721 } 722 } 723 } 724 725 /** 726 * Compute and record hashes 727 */ 728 private class Hasher { 729 final ModuleFinder moduleFinder; 730 final Map<String, Path> moduleNameToPath; 731 final Set<String> modules; 732 final Configuration configuration; 733 final boolean dryrun = options.dryrun; 734 Hasher(ModuleFinder finder) { 735 this.moduleFinder = finder; 736 // Determine the modules that matches the pattern {@code modulesToHash} 737 this.modules = moduleFinder.findAll().stream() 738 .map(mref -> mref.descriptor().name()) 739 .filter(mn -> options.modulesToHash.matcher(mn).find()) 740 .collect(Collectors.toSet()); 741 742 // a map from a module name to Path of the packaged module 743 this.moduleNameToPath = moduleFinder.findAll().stream() 744 .map(mref -> mref.descriptor().name()) 745 .collect(Collectors.toMap(Function.identity(), mn -> moduleToPath(mn))); 746 747 // get a resolved module graph 748 Configuration config = null; 749 try { 750 config = Configuration.empty() 751 .resolveRequires(ModuleFinder.ofSystem(), moduleFinder, modules); 752 } catch (ResolutionException e) { 753 warning("warn.module.resolution.fail", e.getMessage()); 754 } 755 this.configuration = config; 756 } 757 758 /** 759 * This method is for jmod hash command. 760 * 761 * Identify the base modules in the module graph, i.e. no outgoing edge 762 * to any of the modules to be hashed. 763 * 764 * For each base module M, compute the hashes of all modules that depend 765 * upon M directly or indirectly. Then update M's module-info.class 766 * to record the hashes. 767 */ 768 boolean run() { 769 if (configuration == null) 770 return false; 771 772 // transposed graph containing the the packaged modules and 773 // its transitive dependences matching --hash-modules 774 Map<String, Set<String>> graph = new HashMap<>(); 775 for (String root : modules) { 776 Deque<String> deque = new ArrayDeque<>(); 777 deque.add(root); 778 Set<String> visited = new HashSet<>(); 779 while (!deque.isEmpty()) { 780 String mn = deque.pop(); 781 if (!visited.contains(mn)) { 782 visited.add(mn); 783 784 if (modules.contains(mn)) 785 graph.computeIfAbsent(mn, _k -> new HashSet<>()); 786 787 ResolvedModule resolvedModule = configuration.findModule(mn).get(); 788 for (ResolvedModule dm : resolvedModule.reads()) { 789 String name = dm.name(); 790 if (!visited.contains(name)) { 791 deque.push(name); 792 } 793 794 // reverse edge 795 if (modules.contains(name) && modules.contains(mn)) { 796 graph.computeIfAbsent(name, _k -> new HashSet<>()).add(mn); 797 } 798 } 799 } 800 } 801 } 802 803 if (dryrun) 804 out.println("Dry run:"); 805 806 // each node in a transposed graph is a matching packaged module 807 // in which the hash of the modules that depend upon it is recorded 808 graph.entrySet().stream() 809 .filter(e -> !e.getValue().isEmpty()) 810 .forEach(e -> { 811 String mn = e.getKey(); 812 Map<String, Path> modulesForHash = e.getValue().stream() 813 .collect(Collectors.toMap(Function.identity(), 814 moduleNameToPath::get)); 815 ModuleHashes hashes = ModuleHashes.generate(modulesForHash, "SHA-256"); 816 if (dryrun) { 817 out.format("%s%n", mn); 818 hashes.names().stream() 819 .sorted() 820 .forEach(name -> out.format(" hashes %s %s %s%n", 821 name, hashes.algorithm(), hashes.hashFor(name))); 822 } else { 823 try { 824 updateModuleInfo(mn, hashes); 825 } catch (IOException ex) { 826 throw new UncheckedIOException(ex); 827 } 828 } 829 }); 830 return true; 831 } 832 833 /** 834 * Compute hashes of the specified module. 835 * 836 * It records the hashing modules that depend upon the specified 837 * module directly or indirectly. 838 */ 839 ModuleHashes computeHashes(String name) { 840 if (configuration == null) 841 return null; 842 843 // the transposed graph includes all modules in the resolved graph 844 Map<String, Set<String>> graph = transpose(); 845 846 // find the modules that transitively depend upon the specified name 847 Deque<String> deque = new ArrayDeque<>(); 848 deque.add(name); 849 Set<String> mods = visitNodes(graph, deque); 850 851 // filter modules matching the pattern specified --hash-modules 852 // as well as itself as the jmod file is being generated 853 Map<String, Path> modulesForHash = mods.stream() 854 .filter(mn -> !mn.equals(name) && modules.contains(mn)) 855 .collect(Collectors.toMap(Function.identity(), moduleNameToPath::get)); 856 857 if (modulesForHash.isEmpty()) 858 return null; 859 860 return ModuleHashes.generate(modulesForHash, "SHA-256"); 861 } 862 863 /** 864 * Returns all nodes traversed from the given roots. 865 */ 866 private Set<String> visitNodes(Map<String, Set<String>> graph, 867 Deque<String> roots) { 868 Set<String> visited = new HashSet<>(); 869 while (!roots.isEmpty()) { 870 String mn = roots.pop(); 871 if (!visited.contains(mn)) { 872 visited.add(mn); 873 // the given roots may not be part of the graph 874 if (graph.containsKey(mn)) { 875 for (String dm : graph.get(mn)) { 876 if (!visited.contains(dm)) { 877 roots.push(dm); 878 } 879 } 880 } 881 } 882 } 883 return visited; 884 } 885 886 /** 887 * Returns a transposed graph from the resolved module graph. 888 */ 889 private Map<String, Set<String>> transpose() { 890 Map<String, Set<String>> transposedGraph = new HashMap<>(); 891 Deque<String> deque = new ArrayDeque<>(modules); 892 893 Set<String> visited = new HashSet<>(); 894 while (!deque.isEmpty()) { 895 String mn = deque.pop(); 896 if (!visited.contains(mn)) { 897 visited.add(mn); 898 899 transposedGraph.computeIfAbsent(mn, _k -> new HashSet<>()); 900 901 ResolvedModule resolvedModule = configuration.findModule(mn).get(); 902 for (ResolvedModule dm : resolvedModule.reads()) { 903 String name = dm.name(); 904 if (!visited.contains(name)) { 905 deque.push(name); 906 } 907 908 // reverse edge 909 transposedGraph.computeIfAbsent(name, _k -> new HashSet<>()) 910 .add(mn); 911 } 912 } 913 } 914 return transposedGraph; 915 } 916 917 /** 918 * Reads the given input stream of module-info.class and write 919 * the extended module-info.class with the given ModuleHashes 920 * 921 * @param in InputStream of module-info.class 922 * @param out OutputStream to write the extended module-info.class 923 * @param hashes ModuleHashes 924 */ 925 private void recordHashes(InputStream in, OutputStream out, ModuleHashes hashes) 926 throws IOException 927 { 928 ModuleInfoExtender extender = ModuleInfoExtender.newExtender(in); 929 extender.hashes(hashes); 930 extender.write(out); 931 } 932 933 private void updateModuleInfo(String name, ModuleHashes moduleHashes) 934 throws IOException 935 { 936 Path target = moduleNameToPath.get(name); 937 Path tempTarget = target.resolveSibling(target.getFileName() + ".tmp"); 938 ZipFile zip = new ZipFile(target.toFile()); 939 try { 940 try (OutputStream out = Files.newOutputStream(tempTarget); 941 ZipOutputStream zos = new ZipOutputStream(out)) { 942 zip.stream().forEach(e -> { 943 try { 944 InputStream in = zip.getInputStream(e); 945 if (e.getName().equals(MODULE_INFO) || 946 e.getName().equals(Section.CLASSES.jmodDir() + "/" + MODULE_INFO)) { 947 ZipEntry ze = new ZipEntry(e.getName()); 948 ze.setTime(System.currentTimeMillis()); 949 zos.putNextEntry(ze); 950 recordHashes(in, zos, moduleHashes); 951 zos.closeEntry(); 952 } else { 953 zos.putNextEntry(e); 954 zos.write(in.readAllBytes()); 955 zos.closeEntry(); 956 } 957 } catch (IOException x) { 958 throw new UncheckedIOException(x); 959 } 960 }); 961 } 962 } catch (IOException|RuntimeException e) { 963 if (Files.exists(tempTarget)) { 964 try { 965 Files.delete(tempTarget); 966 } catch (IOException ioe) { 967 e.addSuppressed(ioe); 968 } 969 } 970 throw e; 971 } finally { 972 zip.close(); 973 } 974 out.println(getMessage("module.hashes.recorded", name)); 975 Files.move(tempTarget, target, StandardCopyOption.REPLACE_EXISTING); 976 } 977 978 private Path moduleToPath(String name) { 979 ModuleReference mref = moduleFinder.find(name).orElseThrow( 980 () -> new InternalError("Selected module " + name + " not on module path")); 981 982 URI uri = mref.location().get(); 983 Path path = Paths.get(uri); 984 String fn = path.getFileName().toString(); 985 if (!fn.endsWith(".jar") && !fn.endsWith(".jmod")) { 986 throw new InternalError(path + " is not a modular JAR or jmod file"); 987 } 988 return path; 989 } 990 } 991 992 enum Section { 993 NATIVE_LIBS("native"), 994 NATIVE_CMDS("bin"), 995 CLASSES("classes"), 996 CONFIG("conf"), 997 UNKNOWN("unknown"); 998 999 private final String jmodDir; 1000 1001 Section(String jmodDir) { 1002 this.jmodDir = jmodDir; 1003 } 1004 1005 String jmodDir() { return jmodDir; } 1006 } 1007 1008 static class ClassPathConverter implements ValueConverter<Path> { 1009 static final ValueConverter<Path> INSTANCE = new ClassPathConverter(); 1010 1011 private static final Path CWD = Paths.get(""); 1012 1013 @Override 1014 public Path convert(String value) { 1015 try { 1016 Path path = CWD.resolve(value); 1017 if (Files.notExists(path)) 1018 throw new CommandException("err.path.not.found", path); 1019 if (! (Files.isDirectory(path) || 1020 (Files.isRegularFile(path) && path.toString().endsWith(".jar")))) 1021 throw new CommandException("err.invalid.class.path.entry", path); 1022 return path; 1023 } catch (InvalidPathException x) { 1024 throw new CommandException("err.path.not.valid", value); 1025 } 1026 } 1027 1028 @Override public Class<Path> valueType() { return Path.class; } 1029 1030 @Override public String valuePattern() { return "path"; } 1031 } 1032 1033 static class DirPathConverter implements ValueConverter<Path> { 1034 static final ValueConverter<Path> INSTANCE = new DirPathConverter(); 1035 1036 private static final Path CWD = Paths.get(""); 1037 1038 @Override 1039 public Path convert(String value) { 1040 try { 1041 Path path = CWD.resolve(value); 1042 if (Files.notExists(path)) 1043 throw new CommandException("err.path.not.found", path); 1044 if (!Files.isDirectory(path)) 1045 throw new CommandException("err.path.not.a.dir", path); 1046 return path; 1047 } catch (InvalidPathException x) { 1048 throw new CommandException("err.path.not.valid", value); 1049 } 1050 } 1051 1052 @Override public Class<Path> valueType() { return Path.class; } 1053 1054 @Override public String valuePattern() { return "path"; } 1055 } 1056 1057 static class ModuleVersionConverter implements ValueConverter<Version> { 1058 @Override 1059 public Version convert(String value) { 1060 try { 1061 return Version.parse(value); 1062 } catch (IllegalArgumentException x) { 1063 throw new CommandException("err.invalid.version", x.getMessage()); 1064 } 1065 } 1066 1067 @Override public Class<Version> valueType() { return Version.class; } 1068 1069 @Override public String valuePattern() { return "module-version"; } 1070 } 1071 1072 static class PatternConverter implements ValueConverter<Pattern> { 1073 @Override 1074 public Pattern convert(String value) { 1075 try { 1076 if (value.startsWith("regex:")) { 1077 value = value.substring("regex:".length()).trim(); 1078 } 1079 1080 return Pattern.compile(value); 1081 } catch (PatternSyntaxException e) { 1082 throw new CommandException("err.bad.pattern", value); 1083 } 1084 } 1085 1086 @Override public Class<Pattern> valueType() { return Pattern.class; } 1087 1088 @Override public String valuePattern() { return "regex-pattern"; } 1089 } 1090 1091 static class PathMatcherConverter implements ValueConverter<PathMatcher> { 1092 @Override 1093 public PathMatcher convert(String pattern) { 1094 try { 1095 return Utils.getPathMatcher(FileSystems.getDefault(), pattern); 1096 } catch (PatternSyntaxException e) { 1097 throw new CommandException("err.bad.pattern", pattern); 1098 } 1099 } 1100 1101 @Override public Class<PathMatcher> valueType() { return PathMatcher.class; } 1102 1103 @Override public String valuePattern() { return "pattern-list"; } 1104 } 1105 1106 /* Support for @<file> in jmod help */ 1107 private static final String CMD_FILENAME = "@<filename>"; 1108 1109 /** 1110 * This formatter is adding the @filename option and does the required 1111 * formatting. 1112 */ 1113 private static final class JmodHelpFormatter extends BuiltinHelpFormatter { 1114 1115 private JmodHelpFormatter() { super(80, 2); } 1116 1117 @Override 1118 public String format(Map<String, ? extends OptionDescriptor> options) { 1119 Map<String, OptionDescriptor> all = new HashMap<>(); 1120 all.putAll(options); 1121 all.put(CMD_FILENAME, new OptionDescriptor() { 1122 @Override 1123 public Collection<String> options() { 1124 List<String> ret = new ArrayList<>(); 1125 ret.add(CMD_FILENAME); 1126 return ret; 1127 } 1128 @Override 1129 public String description() { return getMessage("main.opt.cmdfile"); } 1130 @Override 1131 public List<?> defaultValues() { return Collections.emptyList(); } 1132 @Override 1133 public boolean isRequired() { return false; } 1134 @Override 1135 public boolean acceptsArguments() { return false; } 1136 @Override 1137 public boolean requiresArgument() { return false; } 1138 @Override 1139 public String argumentDescription() { return null; } 1140 @Override 1141 public String argumentTypeIndicator() { return null; } 1142 @Override 1143 public boolean representsNonOptions() { return false; } 1144 }); 1145 String content = super.format(all); 1146 StringBuilder builder = new StringBuilder(); 1147 1148 builder.append(getMessage("main.opt.mode")).append("\n "); 1149 builder.append(getMessage("main.opt.mode.create")).append("\n "); 1150 builder.append(getMessage("main.opt.mode.list")).append("\n "); 1151 builder.append(getMessage("main.opt.mode.describe")).append("\n "); 1152 builder.append(getMessage("main.opt.mode.hash")).append("\n\n"); 1153 1154 String cmdfile = null; 1155 String[] lines = content.split("\n"); 1156 for (String line : lines) { 1157 if (line.startsWith("--@")) { 1158 cmdfile = line.replace("--" + CMD_FILENAME, CMD_FILENAME + " "); 1159 } else if (line.startsWith("Option") || line.startsWith("------")) { 1160 builder.append(" ").append(line).append("\n"); 1161 } else if (!line.matches("Non-option arguments")){ 1162 builder.append(" ").append(line).append("\n"); 1163 } 1164 } 1165 if (cmdfile != null) { 1166 builder.append(" ").append(cmdfile).append("\n"); 1167 } 1168 return builder.toString(); 1169 } 1170 } 1171 1172 private final OptionParser parser = new OptionParser(); 1173 1174 private void handleOptions(String[] args) { 1175 parser.formatHelpWith(new JmodHelpFormatter()); 1176 1177 OptionSpec<Path> classPath 1178 = parser.accepts("class-path", getMessage("main.opt.class-path")) 1179 .withRequiredArg() 1180 .withValuesSeparatedBy(File.pathSeparatorChar) 1181 .withValuesConvertedBy(ClassPathConverter.INSTANCE); 1182 1183 OptionSpec<Path> cmds 1184 = parser.accepts("cmds", getMessage("main.opt.cmds")) 1185 .withRequiredArg() 1186 .withValuesSeparatedBy(File.pathSeparatorChar) 1187 .withValuesConvertedBy(DirPathConverter.INSTANCE); 1188 1189 OptionSpec<Path> config 1190 = parser.accepts("config", getMessage("main.opt.config")) 1191 .withRequiredArg() 1192 .withValuesSeparatedBy(File.pathSeparatorChar) 1193 .withValuesConvertedBy(DirPathConverter.INSTANCE); 1194 1195 OptionSpec<Void> dryrun 1196 = parser.accepts("dry-run", getMessage("main.opt.dry-run")); 1197 1198 OptionSpec<PathMatcher> excludes 1199 = parser.accepts("exclude", getMessage("main.opt.exclude")) 1200 .withRequiredArg() 1201 .withValuesConvertedBy(new PathMatcherConverter()); 1202 1203 OptionSpec<Pattern> hashModules 1204 = parser.accepts("hash-modules", getMessage("main.opt.hash-modules")) 1205 .withRequiredArg() 1206 .withValuesConvertedBy(new PatternConverter()); 1207 1208 OptionSpec<Void> help 1209 = parser.accepts("help", getMessage("main.opt.help")) 1210 .forHelp(); 1211 1212 OptionSpec<Path> libs 1213 = parser.accepts("libs", getMessage("main.opt.libs")) 1214 .withRequiredArg() 1215 .withValuesSeparatedBy(File.pathSeparatorChar) 1216 .withValuesConvertedBy(DirPathConverter.INSTANCE); 1217 1218 OptionSpec<String> mainClass 1219 = parser.accepts("main-class", getMessage("main.opt.main-class")) 1220 .withRequiredArg() 1221 .describedAs(getMessage("main.opt.main-class.arg")); 1222 1223 OptionSpec<Path> modulePath // TODO: short version of --mp ?? 1224 = parser.acceptsAll(Arrays.asList("mp", "modulepath"), 1225 getMessage("main.opt.modulepath")) 1226 .withRequiredArg() 1227 .withValuesSeparatedBy(File.pathSeparatorChar) 1228 .withValuesConvertedBy(DirPathConverter.INSTANCE); 1229 1230 OptionSpec<Version> moduleVersion 1231 = parser.accepts("module-version", getMessage("main.opt.module-version")) 1232 .withRequiredArg() 1233 .withValuesConvertedBy(new ModuleVersionConverter()); 1234 1235 OptionSpec<String> osName 1236 = parser.accepts("os-name", getMessage("main.opt.os-name")) 1237 .withRequiredArg() 1238 .describedAs(getMessage("main.opt.os-name.arg")); 1239 1240 OptionSpec<String> osArch 1241 = parser.accepts("os-arch", getMessage("main.opt.os-arch")) 1242 .withRequiredArg() 1243 .describedAs(getMessage("main.opt.os-arch.arg")); 1244 1245 OptionSpec<String> osVersion 1246 = parser.accepts("os-version", getMessage("main.opt.os-version")) 1247 .withRequiredArg() 1248 .describedAs(getMessage("main.opt.os-version.arg")); 1249 1250 OptionSpec<Void> version 1251 = parser.accepts("version", getMessage("main.opt.version")); 1252 1253 NonOptionArgumentSpec<String> nonOptions 1254 = parser.nonOptions(); 1255 1256 try { 1257 OptionSet opts = parser.parse(args); 1258 1259 if (opts.has(help) || opts.has(version)) { 1260 options = new Options(); 1261 options.help = opts.has(help); 1262 options.version = opts.has(version); 1263 return; // informational message will be shown 1264 } 1265 1266 List<String> words = opts.valuesOf(nonOptions); 1267 if (words.isEmpty()) 1268 throw new CommandException("err.missing.mode").showUsage(true); 1269 String verb = words.get(0); 1270 options = new Options(); 1271 try { 1272 options.mode = Enum.valueOf(Mode.class, verb.toUpperCase()); 1273 } catch (IllegalArgumentException e) { 1274 throw new CommandException("err.invalid.mode", verb).showUsage(true); 1275 } 1276 1277 if (opts.has(classPath)) 1278 options.classpath = opts.valuesOf(classPath); 1279 if (opts.has(cmds)) 1280 options.cmds = opts.valuesOf(cmds); 1281 if (opts.has(config)) 1282 options.configs = opts.valuesOf(config); 1283 if (opts.has(dryrun)) 1284 options.dryrun = true; 1285 if (opts.has(excludes)) 1286 options.excludes = opts.valuesOf(excludes); 1287 if (opts.has(libs)) 1288 options.libs = opts.valuesOf(libs); 1289 if (opts.has(modulePath)) { 1290 Path[] dirs = opts.valuesOf(modulePath).toArray(new Path[0]); 1291 options.moduleFinder = ModuleFinder.of(dirs); 1292 if (options.moduleFinder instanceof ConfigurableModuleFinder) 1293 ((ConfigurableModuleFinder)options.moduleFinder).configurePhase(Phase.LINK_TIME); 1294 } 1295 if (opts.has(moduleVersion)) 1296 options.moduleVersion = opts.valueOf(moduleVersion); 1297 if (opts.has(mainClass)) 1298 options.mainClass = opts.valueOf(mainClass); 1299 if (opts.has(osName)) 1300 options.osName = opts.valueOf(osName); 1301 if (opts.has(osArch)) 1302 options.osArch = opts.valueOf(osArch); 1303 if (opts.has(osVersion)) 1304 options.osVersion = opts.valueOf(osVersion); 1305 if (opts.has(hashModules)) { 1306 options.modulesToHash = opts.valueOf(hashModules); 1307 // if storing hashes then the module path is required 1308 if (options.moduleFinder == null) 1309 throw new CommandException("err.modulepath.must.be.specified") 1310 .showUsage(true); 1311 } 1312 1313 if (options.mode.equals(Mode.HASH)) { 1314 if (options.moduleFinder == null || options.modulesToHash == null) 1315 throw new CommandException("err.modulepath.must.be.specified") 1316 .showUsage(true); 1317 } else { 1318 if (words.size() <= 1) 1319 throw new CommandException("err.jmod.must.be.specified").showUsage(true); 1320 Path path = Paths.get(words.get(1)); 1321 1322 if (options.mode.equals(Mode.CREATE) && Files.exists(path)) 1323 throw new CommandException("err.file.already.exists", path); 1324 else if ((options.mode.equals(Mode.LIST) || 1325 options.mode.equals(Mode.DESCRIBE)) 1326 && Files.notExists(path)) 1327 throw new CommandException("err.jmod.not.found", path); 1328 1329 if (options.dryrun) { 1330 throw new CommandException("err.invalid.dryrun.option"); 1331 } 1332 options.jmodFile = path; 1333 1334 if (words.size() > 2) 1335 throw new CommandException("err.unknown.option", 1336 words.subList(2, words.size())).showUsage(true); 1337 } 1338 1339 if (options.mode.equals(Mode.CREATE) && options.classpath == null) 1340 throw new CommandException("err.classpath.must.be.specified").showUsage(true); 1341 if (options.mainClass != null && !isValidJavaIdentifier(options.mainClass)) 1342 throw new CommandException("err.invalid.main-class", options.mainClass); 1343 } catch (OptionException e) { 1344 throw new CommandException(e.getMessage()); 1345 } 1346 } 1347 1348 /** 1349 * Returns true if, and only if, the given main class is a legal. 1350 */ 1351 static boolean isValidJavaIdentifier(String mainClass) { 1352 if (mainClass.length() == 0) 1353 return false; 1354 1355 if (!Character.isJavaIdentifierStart(mainClass.charAt(0))) 1356 return false; 1357 1358 int n = mainClass.length(); 1359 for (int i=1; i < n; i++) { 1360 char c = mainClass.charAt(i); 1361 if (!Character.isJavaIdentifierPart(c) && c != '.') 1362 return false; 1363 } 1364 if (mainClass.charAt(n-1) == '.') 1365 return false; 1366 1367 return true; 1368 } 1369 1370 private void reportError(String message) { 1371 out.println(getMessage("error.prefix") + " " + message); 1372 } 1373 1374 private void warning(String key, Object... args) { 1375 out.println(getMessage("warn.prefix") + " " + getMessage(key, args)); 1376 } 1377 1378 private void showUsageSummary() { 1379 out.println(getMessage("main.usage.summary", PROGNAME)); 1380 } 1381 1382 private void showHelp() { 1383 out.println(getMessage("main.usage", PROGNAME)); 1384 try { 1385 parser.printHelpOn(out); 1386 } catch (IOException x) { 1387 throw new AssertionError(x); 1388 } 1389 } 1390 1391 private void showVersion() { 1392 out.println(version()); 1393 } 1394 1395 private String version() { 1396 return System.getProperty("java.version"); 1397 } 1398 1399 private static String getMessage(String key, Object... args) { 1400 try { 1401 return MessageFormat.format(ResourceBundleHelper.bundle.getString(key), args); 1402 } catch (MissingResourceException e) { 1403 throw new InternalError("Missing message: " + key); 1404 } 1405 } 1406 1407 private static class ResourceBundleHelper { 1408 static final ResourceBundle bundle; 1409 1410 static { 1411 Locale locale = Locale.getDefault(); 1412 try { 1413 bundle = ResourceBundle.getBundle("jdk.tools.jmod.resources.jmod", locale); 1414 } catch (MissingResourceException e) { 1415 throw new InternalError("Cannot find jmod resource bundle for locale " + locale); 1416 } 1417 } 1418 } 1419 }