30 import java.net.*; 31 import java.nio.file.Files; 32 import java.nio.file.Path; 33 import java.security.*; 34 import java.util.*; 35 import java.util.regex.*; 36 37 import static java.lang.System.out; 38 import static java.lang.System.err; 39 40 import org.openjdk.jigsaw.*; 41 import org.openjdk.jigsaw.SimpleLibrary.StorageOption; 42 import org.openjdk.internal.joptsimple.*; 43 44 45 public class Librarian { 46 47 private static JigsawModuleSystem jms 48 = JigsawModuleSystem.instance(); 49 50 static class Create extends Command<SimpleLibrary> { 51 protected void go(SimpleLibrary lib) 52 throws Command.Exception 53 { 54 noDry(); 55 finishArgs(); 56 } 57 } 58 59 static class DumpClass extends Command<SimpleLibrary> { 60 protected void go(SimpleLibrary lib) 61 throws Command.Exception 62 { 63 noDry(); 64 String mids = takeArg(); 65 ModuleId mid = null; 66 try { 67 mid = jms.parseModuleId(mids); 68 } catch (IllegalArgumentException x) { 69 throw new Command.Exception(x.getMessage()); 70 } 71 String cn = takeArg(); 72 String ops = takeArg(); 73 finishArgs(); 74 byte[] bs = null; 75 try { 76 bs = lib.readClass(mid, cn); 77 if (bs == null) 456 commands.put("config", Config.class); 457 commands.put("create", Create.class); 458 commands.put("del-repo", DelRepo.class); 459 commands.put("dump-class", DumpClass.class); 460 commands.put("dump-config", DumpConfig.class); 461 commands.put("extract", Extract.class); 462 commands.put("id", Identify.class); 463 commands.put("identify", Identify.class); 464 commands.put("install", Install.class); 465 commands.put("list", Commands.ListLibrary.class); 466 commands.put("ls", Commands.ListLibrary.class); 467 commands.put("preinstall", PreInstall.class); 468 commands.put("refresh", Refresh.class); 469 commands.put("reindex", ReIndex.class); 470 commands.put("repos", Repos.class); 471 } 472 473 private OptionParser parser; 474 475 private static OptionSpec<Integer> repoIndex; // ## 476 477 private void usage() { 478 out.format("%n"); 479 out.format("usage: jmod add-repo [-i <index>] URL%n"); 480 out.format(" jmod extract <module-file> ...%n"); 481 out.format(" jmod config [<module-id> ...]%n"); 482 out.format(" jmod create [-L <library>] [-P <parent>]%n"); 483 out.format(" jmod del-repo URL%n"); 484 out.format(" jmod dump-class <module-id> <class-name> <output-file>%n"); 485 out.format(" jmod dump-config <module-id>%n"); 486 out.format(" jmod identify%n"); 487 out.format(" jmod install [--noverify] [-n] <module-id-query> ...%n"); 488 out.format(" jmod install [--noverify] <module-file> ...%n"); 489 out.format(" jmod install <classes-dir> <module-name> ...%n"); 490 out.format(" jmod list [-v] [-p] [-R] [<module-id-query>]%n"); 491 out.format(" jmod preinstall <classes-dir> <dst-dir> <module-name> ...%n"); 492 out.format(" jmod refresh [-f] [-n] [-v]%n"); 493 out.format(" jmod reindex [<module-id> ...]%n"); 494 out.format(" jmod repos [-v]%n"); 495 out.format("%n"); 496 try { 497 parser.printHelpOn(out); 498 } catch (IOException x) { 499 throw new AssertionError(x); 500 } 501 out.format("%n"); 502 System.exit(0); 503 } 504 505 public static void run(String [] args) throws OptionException, Command.Exception { 506 new Librarian().exec(args); 507 } 508 509 private void exec(String[] args) throws OptionException, Command.Exception { 510 parser = new OptionParser(); 511 512 // ## Need subcommand-specific option parsing 513 OptionSpec<File> libPath 514 = (parser.acceptsAll(Arrays.asList("L", "library"), 515 "Module-library location" 516 + " (default $JAVA_MODULES)") 517 .withRequiredArg() 518 .describedAs("path") 519 .ofType(File.class)); 520 OptionSpec<File> parentPath 521 = (parser.acceptsAll(Arrays.asList("P", "parent-path"), 522 "Parent module-library location") 523 .withRequiredArg() 524 .describedAs("path") 525 .ofType(File.class)); 526 parser.acceptsAll(Arrays.asList("N", "no-parent"), 527 "Use no parent library when creating"); 528 parser.acceptsAll(Arrays.asList("v", "verbose"), 529 "Enable verbose output"); 530 parser.acceptsAll(Arrays.asList("h", "?", "help"), 531 "Show this help message"); 532 parser.acceptsAll(Arrays.asList("p", "parent"), 533 "Apply operation to parent library, if any"); 534 parser.acceptsAll(Arrays.asList("z", "enable-compression"), 535 "Enable compression of module contents"); 536 repoIndex 537 = (parser.acceptsAll(Arrays.asList("i"), 538 "Repository-list index") 539 .withRequiredArg() 540 .describedAs("index") 541 .ofType(Integer.class)); 542 parser.acceptsAll(Arrays.asList("f", "force"), 543 "Force the requested operation"); 544 parser.acceptsAll(Arrays.asList("n", "dry-run"), 545 "Dry-run the requested operation"); 546 parser.acceptsAll(Arrays.asList("R", "repos"), 547 "List contents of associated repositories"); 548 parser.acceptsAll(Arrays.asList("noverify"), 549 "Do not verify module signatures. " 550 + "Treat as unsigned."); 551 parser.acceptsAll(Arrays.asList("G", "strip-debug"), 552 "Strip debug attributes during installation"); 553 554 if (args.length == 0) 555 usage(); 556 557 File homeLibrary = new File(System.getProperty("java.home"), 558 "lib/modules"); 559 560 OptionSet opts = parser.parse(args); 561 if (opts.has("h")) 562 usage(); 563 List<String> words = opts.nonOptionArguments(); 564 if (words.isEmpty()) 565 usage(); 566 String verb = words.get(0); 567 Class<? extends Command<SimpleLibrary>> cmd = commands.get(verb); 568 if (cmd == null) 569 throw new Command.Exception("%s: unknown command", verb); 570 File lp = null; 571 if (opts.has(libPath)) { 572 lp = opts.valueOf(libPath); 573 } else { 574 String jm = System.getenv("JAVA_MODULES"); 575 if (jm != null) 576 lp = new File(jm); 577 else 578 lp = homeLibrary; 579 } 580 File pp = null; 581 if (opts.has(parentPath)) { 582 pp = opts.valueOf(parentPath); 583 } else if (!opts.has("N")) { 584 pp = homeLibrary; 585 } 586 SimpleLibrary lib = null; 587 try { 588 if (verb.equals("create")) { 589 Set<StorageOption> createOpts = new HashSet<>(); 590 if (opts.has("z")) 591 createOpts.add(StorageOption.DEFLATED); 592 lib = SimpleLibrary.create(lp, pp, createOpts); 593 } else { 594 lib = SimpleLibrary.open(lp); 595 } 596 } catch (FileNotFoundException x) { 597 String msg = null; 598 File f = new File(x.getMessage()); 599 try { 600 f = f.getCanonicalFile(); 601 if (lp.getCanonicalFile().equals(f)) 602 msg = "No such library"; 603 else 604 msg = "Cannot open parent library " + f; 605 } catch (IOException y) { 606 throw new Command.Exception(y); 607 } 608 throw new Command.Exception("%s: %s", lp, msg); 609 } catch (IOException x) { 610 throw new Command.Exception(x); 611 } 612 try { 613 cmd.newInstance().run(lib, opts); 614 } catch (InstantiationException x) { 615 throw new AssertionError(x); 616 } catch (IllegalAccessException x) { 617 throw new AssertionError(x); 618 } 619 } 620 621 private Librarian() { } 622 623 public static void main(String[] args) { 624 try { 625 run(args); 626 } catch (OptionException x) { 627 err.println(x.getMessage()); 628 System.exit(1); 629 } catch (Command.Exception x) { 630 err.println(x.getMessage()); 631 x.printStackTrace(); 632 System.exit(1); 633 } 634 } 635 636 } | 30 import java.net.*; 31 import java.nio.file.Files; 32 import java.nio.file.Path; 33 import java.security.*; 34 import java.util.*; 35 import java.util.regex.*; 36 37 import static java.lang.System.out; 38 import static java.lang.System.err; 39 40 import org.openjdk.jigsaw.*; 41 import org.openjdk.jigsaw.SimpleLibrary.StorageOption; 42 import org.openjdk.internal.joptsimple.*; 43 44 45 public class Librarian { 46 47 private static JigsawModuleSystem jms 48 = JigsawModuleSystem.instance(); 49 50 private static final File homeLibrary 51 = new File(System.getProperty("java.home"), "lib/modules"); 52 53 static class Create extends Command<SimpleLibrary> { 54 protected void go(SimpleLibrary lib) 55 throws Command.Exception 56 { 57 //assert lib == null; 58 noDry(); 59 File lp = libPath(opts); 60 File pp = null; 61 if (opts.has(parentPath)) { 62 pp = opts.valueOf(parentPath); 63 } else if (!opts.has("N")) { 64 pp = homeLibrary; 65 } 66 File natlibs = null; 67 if (opts.has(nativeLibs)) { 68 natlibs = opts.valueOf(nativeLibs); 69 } 70 File natcmds = null; 71 if (opts.has(nativeCmds)) { 72 natcmds = opts.valueOf(nativeCmds); 73 } 74 finishArgs(); 75 76 Set<StorageOption> createOpts = new HashSet<>(); 77 if (opts.has("z")) 78 createOpts.add(StorageOption.DEFLATED); 79 80 try { 81 lib = SimpleLibrary.create(lp, pp, natlibs, natcmds, createOpts); 82 } catch (IOException x) { 83 throw new Command.Exception(x); 84 } 85 } 86 } 87 88 static class DumpClass extends Command<SimpleLibrary> { 89 protected void go(SimpleLibrary lib) 90 throws Command.Exception 91 { 92 noDry(); 93 String mids = takeArg(); 94 ModuleId mid = null; 95 try { 96 mid = jms.parseModuleId(mids); 97 } catch (IllegalArgumentException x) { 98 throw new Command.Exception(x.getMessage()); 99 } 100 String cn = takeArg(); 101 String ops = takeArg(); 102 finishArgs(); 103 byte[] bs = null; 104 try { 105 bs = lib.readClass(mid, cn); 106 if (bs == null) 485 commands.put("config", Config.class); 486 commands.put("create", Create.class); 487 commands.put("del-repo", DelRepo.class); 488 commands.put("dump-class", DumpClass.class); 489 commands.put("dump-config", DumpConfig.class); 490 commands.put("extract", Extract.class); 491 commands.put("id", Identify.class); 492 commands.put("identify", Identify.class); 493 commands.put("install", Install.class); 494 commands.put("list", Commands.ListLibrary.class); 495 commands.put("ls", Commands.ListLibrary.class); 496 commands.put("preinstall", PreInstall.class); 497 commands.put("refresh", Refresh.class); 498 commands.put("reindex", ReIndex.class); 499 commands.put("repos", Repos.class); 500 } 501 502 private OptionParser parser; 503 504 private static OptionSpec<Integer> repoIndex; // ## 505 private static OptionSpec<File> libPath; 506 private static OptionSpec<File> parentPath; 507 private static OptionSpec<File> nativeLibs; 508 private static OptionSpec<File> nativeCmds; 509 510 private void usage() { 511 out.format("%n"); 512 out.format("usage: jmod add-repo [-i <index>] URL%n"); 513 out.format(" jmod extract <module-file> ...%n"); 514 out.format(" jmod config [<module-id> ...]%n"); 515 out.format(" jmod create [-L <library>] [-P <parent>] [-natlibs <natlibs>] [-natcmds <natcmds>]%n"); 516 out.format(" jmod del-repo URL%n"); 517 out.format(" jmod dump-class <module-id> <class-name> <output-file>%n"); 518 out.format(" jmod dump-config <module-id>%n"); 519 out.format(" jmod identify%n"); 520 out.format(" jmod install [--noverify] [-n] <module-id-query> ...%n"); 521 out.format(" jmod install [--noverify] <module-file> ...%n"); 522 out.format(" jmod install <classes-dir> <module-name> ...%n"); 523 out.format(" jmod list [-v] [-p] [-R] [<module-id-query>]%n"); 524 out.format(" jmod preinstall <classes-dir> <dst-dir> <module-name> ...%n"); 525 out.format(" jmod refresh [-f] [-n] [-v]%n"); 526 out.format(" jmod reindex [<module-id> ...]%n"); 527 out.format(" jmod repos [-v]%n"); 528 out.format("%n"); 529 try { 530 parser.printHelpOn(out); 531 } catch (IOException x) { 532 throw new AssertionError(x); 533 } 534 out.format("%n"); 535 System.exit(0); 536 } 537 538 public static void run(String [] args) throws OptionException, Command.Exception { 539 new Librarian().exec(args); 540 } 541 542 private void exec(String[] args) throws OptionException, Command.Exception { 543 parser = new OptionParser(); 544 545 // ## Need subcommand-specific option parsing 546 libPath 547 = (parser.acceptsAll(Arrays.asList("L", "library"), 548 "Module-library location" 549 + " (default $JAVA_MODULES)") 550 .withRequiredArg() 551 .describedAs("path") 552 .ofType(File.class)); 553 parentPath 554 = (parser.acceptsAll(Arrays.asList("P", "parent-path"), 555 "Parent module-library location") 556 .withRequiredArg() 557 .describedAs("path") 558 .ofType(File.class)); 559 nativeLibs 560 = (parser.accepts("natlibs", "Directory with native libs") 561 .withRequiredArg() 562 .describedAs("dir") 563 .ofType(File.class)); 564 565 nativeCmds 566 = (parser.accepts("natcmds", "Directory with native launchers") 567 .withRequiredArg() 568 .describedAs("dir") 569 .ofType(File.class)); 570 parser.acceptsAll(Arrays.asList("N", "no-parent"), 571 "Use no parent library when creating"); 572 parser.acceptsAll(Arrays.asList("v", "verbose"), 573 "Enable verbose output"); 574 parser.acceptsAll(Arrays.asList("h", "?", "help"), 575 "Show this help message"); 576 parser.acceptsAll(Arrays.asList("p", "parent"), 577 "Apply operation to parent library, if any"); 578 parser.acceptsAll(Arrays.asList("z", "enable-compression"), 579 "Enable compression of module contents"); 580 repoIndex 581 = (parser.acceptsAll(Arrays.asList("i"), 582 "Repository-list index") 583 .withRequiredArg() 584 .describedAs("index") 585 .ofType(Integer.class)); 586 parser.acceptsAll(Arrays.asList("f", "force"), 587 "Force the requested operation"); 588 parser.acceptsAll(Arrays.asList("n", "dry-run"), 589 "Dry-run the requested operation"); 590 parser.acceptsAll(Arrays.asList("R", "repos"), 591 "List contents of associated repositories"); 592 parser.acceptsAll(Arrays.asList("noverify"), 593 "Do not verify module signatures. " 594 + "Treat as unsigned."); 595 parser.acceptsAll(Arrays.asList("G", "strip-debug"), 596 "Strip debug attributes during installation"); 597 598 if (args.length == 0) 599 usage(); 600 601 OptionSet opts = parser.parse(args); 602 if (opts.has("h")) 603 usage(); 604 List<String> words = opts.nonOptionArguments(); 605 if (words.isEmpty()) 606 usage(); 607 String verb = words.get(0); 608 Class<? extends Command<SimpleLibrary>> cmd = commands.get(verb); 609 if (cmd == null) 610 throw new Command.Exception("%s: unknown command", verb); 611 612 // every command, except create, needs to be passed a reference to the library 613 SimpleLibrary lib = null; 614 if (!verb.equals("create")) { 615 File lp = libPath(opts); 616 try { 617 lib = SimpleLibrary.open(lp); 618 } catch (FileNotFoundException x) { 619 String msg = null; 620 File f = new File(x.getMessage()); 621 try { 622 f = f.getCanonicalFile(); 623 if (lp.getCanonicalFile().equals(f)) 624 msg = "No such library"; 625 else 626 msg = "Cannot open parent library " + f; 627 } catch (IOException y) { 628 throw new Command.Exception(y); 629 } 630 throw new Command.Exception("%s: %s", lp, msg); 631 } catch (IOException x) { 632 throw new Command.Exception(x); 633 } 634 } 635 try { 636 cmd.newInstance().run(lib, opts); 637 } catch (InstantiationException x) { 638 throw new AssertionError(x); 639 } catch (IllegalAccessException x) { 640 throw new AssertionError(x); 641 } 642 } 643 644 private static File libPath(OptionSet opts) { 645 if (opts.has(libPath)) { 646 return opts.valueOf(libPath); 647 } else { 648 String jm = System.getenv("JAVA_MODULES"); 649 if (jm != null) 650 return new File(jm); 651 else 652 return homeLibrary; 653 } 654 } 655 656 private Librarian() { } 657 658 public static void main(String[] args) { 659 try { 660 run(args); 661 } catch (OptionException x) { 662 err.println(x.getMessage()); 663 System.exit(1); 664 } catch (Command.Exception x) { 665 err.println(x.getMessage()); 666 x.printStackTrace(); 667 System.exit(1); 668 } 669 } 670 671 } |