src/share/classes/org/openjdk/jigsaw/cli/Librarian.java

Print this page

        

*** 30,40 **** import java.net.*; import java.nio.file.Files; import java.nio.file.Path; import java.security.*; import java.util.*; - import java.util.regex.*; import static java.lang.System.out; import static java.lang.System.err; import org.openjdk.jigsaw.*; --- 30,39 ----
*** 45,63 **** --- 44,94 ---- public class Librarian { private static JigsawModuleSystem jms = JigsawModuleSystem.instance(); + private static final File homeLibrary = Library.systemLibraryPath(); + static class Create extends Command<SimpleLibrary> { protected void go(SimpleLibrary lib) throws Command.Exception { noDry(); finishArgs(); + File lp = libPath(opts); + File pp = null; + if (opts.has(parentPath)) { + pp = opts.valueOf(parentPath); + } else if (!opts.has("N")) { + pp = homeLibrary; } + File natlibs = null; + if (opts.has(nativeLibs)) { + natlibs = opts.valueOf(nativeLibs); } + File natcmds = null; + if (opts.has(nativeCmds)) { + natcmds = opts.valueOf(nativeCmds); + } + File configs = null; + if (opts.has(configFiles)) { + configs = opts.valueOf(configFiles); + } + Set<StorageOption> createOpts = new HashSet<>(); + if (opts.has("z")) + createOpts.add(StorageOption.DEFLATED); + + try { + lib = SimpleLibrary.create(lp, pp, natlibs, natcmds, + configs, createOpts); + } catch (IOException x) { + throw new Command.Exception(x); + } + } + } + static class DumpClass extends Command<SimpleLibrary> { protected void go(SimpleLibrary lib) throws Command.Exception { noDry();
*** 117,143 **** throws Command.Exception { noDry(); while (hasArg()) { File module = new File(takeArg()); ! File classes = null; try (FileInputStream fis = new FileInputStream(module); DataInputStream dis = new DataInputStream(fis); ModuleFile.Reader reader = new ModuleFile.Reader(dis)) { ModuleInfo mi = jms.parseModuleInfo(reader.readStart()); ! classes = new File(mi.id().name()); ! Path path = classes.toPath(); Files.deleteIfExists(path); Files.createDirectory(path); ! reader.readRest(classes, false); } catch (IOException x) { // Try to cleanup if an exception is thrown ! if (classes != null && classes.exists()) try { ! FilePaths.deleteTree(classes.toPath()); } catch (IOException y) { throw (Command.Exception) new Command.Exception(y).initCause(x); } --- 148,174 ---- throws Command.Exception { noDry(); while (hasArg()) { File module = new File(takeArg()); ! File destination = null; try (FileInputStream fis = new FileInputStream(module); DataInputStream dis = new DataInputStream(fis); ModuleFile.Reader reader = new ModuleFile.Reader(dis)) { ModuleInfo mi = jms.parseModuleInfo(reader.readStart()); ! destination = new File(mi.id().name()); ! Path path = destination.toPath(); Files.deleteIfExists(path); Files.createDirectory(path); ! reader.readRest(destination, false); } catch (IOException x) { // Try to cleanup if an exception is thrown ! if (destination != null && destination.exists()) try { ! FilePaths.deleteTree(destination.toPath()); } catch (IOException y) { throw (Command.Exception) new Command.Exception(y).initCause(x); }
*** 471,487 **** } private OptionParser parser; private static OptionSpec<Integer> repoIndex; // ## private void usage() { out.format("%n"); out.format("usage: jmod add-repo [-i <index>] URL%n"); out.format(" jmod extract <module-file> ...%n"); out.format(" jmod config [<module-id> ...]%n"); ! out.format(" jmod create [-L <library>] [-P <parent>]%n"); out.format(" jmod del-repo URL%n"); out.format(" jmod dump-class <module-id> <class-name> <output-file>%n"); out.format(" jmod dump-config <module-id>%n"); out.format(" jmod identify%n"); out.format(" jmod install [--noverify] [-n] <module-id-query> ...%n"); --- 502,524 ---- } private OptionParser parser; private static OptionSpec<Integer> repoIndex; // ## + private static OptionSpec<File> libPath; + private static OptionSpec<File> parentPath; + private static OptionSpec<File> nativeLibs; + private static OptionSpec<File> nativeCmds; + private static OptionSpec<File> configFiles; private void usage() { out.format("%n"); out.format("usage: jmod add-repo [-i <index>] URL%n"); out.format(" jmod extract <module-file> ...%n"); out.format(" jmod config [<module-id> ...]%n"); ! out.format(" jmod create [-L <library>] [-P <parent>]" + ! " [--natlib <natlib>] [--natcmd <natcmd>] [--config <config>]%n"); out.format(" jmod del-repo URL%n"); out.format(" jmod dump-class <module-id> <class-name> <output-file>%n"); out.format(" jmod dump-config <module-id>%n"); out.format(" jmod identify%n"); out.format(" jmod install [--noverify] [-n] <module-id-query> ...%n");
*** 508,530 **** private void exec(String[] args) throws OptionException, Command.Exception { parser = new OptionParser(); // ## Need subcommand-specific option parsing ! OptionSpec<File> libPath = (parser.acceptsAll(Arrays.asList("L", "library"), "Module-library location" + " (default $JAVA_MODULES)") .withRequiredArg() .describedAs("path") .ofType(File.class)); ! OptionSpec<File> parentPath = (parser.acceptsAll(Arrays.asList("P", "parent-path"), "Parent module-library location") .withRequiredArg() .describedAs("path") .ofType(File.class)); parser.acceptsAll(Arrays.asList("N", "no-parent"), "Use no parent library when creating"); parser.acceptsAll(Arrays.asList("v", "verbose"), "Enable verbose output"); parser.acceptsAll(Arrays.asList("h", "?", "help"), --- 545,582 ---- private void exec(String[] args) throws OptionException, Command.Exception { parser = new OptionParser(); // ## Need subcommand-specific option parsing ! libPath = (parser.acceptsAll(Arrays.asList("L", "library"), "Module-library location" + " (default $JAVA_MODULES)") .withRequiredArg() .describedAs("path") .ofType(File.class)); ! parentPath = (parser.acceptsAll(Arrays.asList("P", "parent-path"), "Parent module-library location") .withRequiredArg() .describedAs("path") .ofType(File.class)); + nativeLibs + = (parser.accepts("natlib", "Directory to store native libs") + .withRequiredArg() + .describedAs("dir") + .ofType(File.class)); + nativeCmds + = (parser.accepts("natcmd", "Directory to store native launchers") + .withRequiredArg() + .describedAs("dir") + .ofType(File.class)); + configFiles + = (parser.accepts("config", "Directory to store config files") + .withRequiredArg() + .describedAs("dir") + .ofType(File.class)); parser.acceptsAll(Arrays.asList("N", "no-parent"), "Use no parent library when creating"); parser.acceptsAll(Arrays.asList("v", "verbose"), "Enable verbose output"); parser.acceptsAll(Arrays.asList("h", "?", "help"),
*** 552,564 **** "Strip debug attributes during installation"); if (args.length == 0) usage(); - File homeLibrary = new File(System.getProperty("java.home"), - "lib/modules"); - OptionSet opts = parser.parse(args); if (opts.has("h")) usage(); List<String> words = opts.nonOptionArguments(); if (words.isEmpty()) --- 604,613 ----
*** 565,600 **** usage(); String verb = words.get(0); Class<? extends Command<SimpleLibrary>> cmd = commands.get(verb); if (cmd == null) throw new Command.Exception("%s: unknown command", verb); ! File lp = null; ! if (opts.has(libPath)) { ! lp = opts.valueOf(libPath); ! } else { ! String jm = System.getenv("JAVA_MODULES"); ! if (jm != null) ! lp = new File(jm); ! else ! lp = homeLibrary; ! } ! File pp = null; ! if (opts.has(parentPath)) { ! pp = opts.valueOf(parentPath); ! } else if (!opts.has("N")) { ! pp = homeLibrary; ! } SimpleLibrary lib = null; try { - if (verb.equals("create")) { - Set<StorageOption> createOpts = new HashSet<>(); - if (opts.has("z")) - createOpts.add(StorageOption.DEFLATED); - lib = SimpleLibrary.create(lp, pp, createOpts); - } else { lib = SimpleLibrary.open(lp); - } } catch (FileNotFoundException x) { String msg = null; File f = new File(x.getMessage()); try { f = f.getCanonicalFile(); --- 614,631 ---- usage(); String verb = words.get(0); Class<? extends Command<SimpleLibrary>> cmd = commands.get(verb); if (cmd == null) throw new Command.Exception("%s: unknown command", verb); ! ! // Every command, except 'create' and 'extract', needs ! // to have a valid reference to the library SimpleLibrary lib = null; + if (!(verb.equals("create") || verb.equals("extract"))) { + File lp = libPath(opts); try { lib = SimpleLibrary.open(lp); } catch (FileNotFoundException x) { String msg = null; File f = new File(x.getMessage()); try { f = f.getCanonicalFile();
*** 607,624 **** --- 638,668 ---- } throw new Command.Exception("%s: %s", lp, msg); } catch (IOException x) { throw new Command.Exception(x); } + } try { cmd.newInstance().run(lib, opts); } catch (InstantiationException x) { throw new AssertionError(x); } catch (IllegalAccessException x) { throw new AssertionError(x); } } + + private static File libPath(OptionSet opts) { + if (opts.has(libPath)) { + return opts.valueOf(libPath); + } else { + String jm = System.getenv("JAVA_MODULES"); + if (jm != null) + return new File(jm); + else + return homeLibrary; + } + } private Librarian() { } public static void main(String[] args) { try {