27
28 import java.lang.module.*;
29 import java.io.*;
30 import java.net.*;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.security.*;
35 import java.util.*;
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 = Library.systemLibraryPath();
51
52 static class Create extends Command<SimpleLibrary> {
53 protected void go(SimpleLibrary lib)
54 throws Command.Exception
55 {
56 noDry();
57 finishArgs();
58 File lp = libPath(opts);
59 File pp = null;
60 if (opts.has(parentPath))
61 pp = opts.valueOf(parentPath);
62 else if (!opts.has("N"))
63 pp = homeLibrary;
64
65 File natlibs = null;
66 if (opts.has(nativeLibs))
67 natlibs = opts.valueOf(nativeLibs);
183 }
184 }
185
186 static class Install extends Command<SimpleLibrary> {
187 protected void go(SimpleLibrary lib)
188 throws Command.Exception
189 {
190 String key = takeArg();
191 File kf = new File(key);
192 boolean verifySignature = !opts.has("noverify");
193 boolean strip = opts.has("G");
194
195 // Old form: install <classes-dir> <module-name> ...
196 //
197 if (kf.exists() && kf.isDirectory()) {
198 noDry();
199 List<Manifest> mfs = new ArrayList<>();
200 while (hasArg())
201 mfs.add(Manifest.create(takeArg(), kf));
202 finishArgs();
203 try {
204 lib.installFromManifests(mfs, strip);
205 } catch (ConfigurationException x) {
206 throw new Command.Exception(x);
207 } catch (IOException x) {
208 throw new Command.Exception(x);
209 }
210 return;
211 }
212
213 // Install one or more module file(s)
214 //
215 if (kf.exists() && kf.isFile()) {
216 noDry();
217 List<File> fs = new ArrayList<>();
218 fs.add(kf);
219 while (hasArg())
220 fs.add(new File(takeArg()));
221 finishArgs();
222 try {
277 // ## need to revisit this
278 static class PreInstall extends Command<SimpleLibrary> {
279 protected void go(SimpleLibrary lib)
280 throws Command.Exception
281 {
282 noDry();
283 File classes = new File(takeArg());
284 File dst = new File(takeArg());
285 List<Manifest> mfs = new ArrayList<Manifest>();
286 while (hasArg())
287 mfs.add(Manifest.create(takeArg(), classes));
288 finishArgs();
289 try {
290 lib.preInstall(mfs, dst);
291 } catch (IOException x) {
292 throw new Command.Exception(x);
293 }
294 }
295 }
296
297 static class DumpConfig extends Command<SimpleLibrary> {
298 protected void go(SimpleLibrary lib)
299 throws Command.Exception
300 {
301 noDry();
302 String midqs = takeArg();
303 ModuleIdQuery midq = null;
304 try {
305 midq = jms.parseModuleIdQuery(midqs);
306 } catch (IllegalArgumentException x) {
307 throw new Command.Exception(x.getMessage());
308 }
309 finishArgs();
310 try {
311 ModuleId mid = lib.findLatestModuleId(midq);
312 if (mid == null)
313 throw new Command.Exception(midq + ": No such module");
314 Configuration<Context> cf = lib.readConfiguration(mid);
315 if (cf == null)
316 throw new Command.Exception(mid + ": Not a root module");
471 RemoteRepositoryList rl = lib.repositoryList();
472 int n = 0;
473 for (RemoteRepository rr : rl.repositories()) {
474 out.format("%s - ", rr.location());
475 out.flush();
476 boolean stale
477 = dry ? rr.isCatalogStale() : rr.updateCatalog(force);
478 if (stale) {
479 n++;
480 out.format(dry ? "out of date%n" : "updated%n");
481 } else {
482 out.format("up to date%n");
483 }
484 }
485 } catch (IOException x) {
486 throw new Command.Exception(x);
487 }
488 }
489 }
490
491 private static Map<String,Class<? extends Command<SimpleLibrary>>> commands
492 = new HashMap<>();
493
494 static {
495 commands.put("add-repo", AddRepo.class);
496 commands.put("config", Config.class);
497 commands.put("create", Create.class);
498 commands.put("del-repo", DelRepo.class);
499 commands.put("dump-class", DumpClass.class);
500 commands.put("dump-config", DumpConfig.class);
501 commands.put("extract", Extract.class);
502 commands.put("id", Identify.class);
503 commands.put("identify", Identify.class);
504 commands.put("install", Install.class);
505 commands.put("list", Commands.ListLibrary.class);
506 commands.put("ls", Commands.ListLibrary.class);
507 commands.put("preinstall", PreInstall.class);
508 commands.put("refresh", Refresh.class);
509 commands.put("reindex", ReIndex.class);
510 commands.put("repos", Repos.class);
511 }
512
513 private OptionParser parser;
514
515 private static OptionSpec<Integer> repoIndex; // ##
516 private static OptionSpec<File> libPath;
517 private static OptionSpec<File> parentPath;
518 private static OptionSpec<File> nativeLibs;
519 private static OptionSpec<File> nativeCmds;
520 private static OptionSpec<File> configFiles;
521
522 private void usage() {
523 out.format("%n");
524 out.format("usage: jmod add-repo [-i <index>] URL%n");
525 out.format(" jmod extract <module-file> ...%n");
526 out.format(" jmod config [<module-id> ...]%n");
527 out.format(" jmod create [-L <library>] [-P <parent>]" +
528 " [--natlib <natlib>] [--natcmd <natcmd>] [--config <config>]%n");
529 out.format(" jmod del-repo URL%n");
530 out.format(" jmod dump-class <module-id> <class-name> <output-file>%n");
531 out.format(" jmod dump-config <module-id>%n");
532 out.format(" jmod identify%n");
533 out.format(" jmod install [--noverify] [-n] <module-id-query> ...%n");
534 out.format(" jmod install [--noverify] <module-file> ...%n");
535 out.format(" jmod install <classes-dir> <module-name> ...%n");
536 out.format(" jmod list [-v] [-p] [-R] [<module-id-query>]%n");
537 out.format(" jmod preinstall <classes-dir> <dst-dir> <module-name> ...%n");
538 out.format(" jmod refresh [-f] [-n] [-v]%n");
539 out.format(" jmod reindex [<module-id> ...]%n");
540 out.format(" jmod repos [-v]%n");
541 out.format("%n");
542 try {
543 parser.printHelpOn(out);
544 } catch (IOException x) {
545 throw new AssertionError(x);
546 }
547 out.format("%n");
548 System.exit(0);
549 }
550
551 public static void run(String [] args) throws OptionException, Command.Exception {
552 new Librarian().exec(args);
553 }
554
555 private void exec(String[] args) throws OptionException, Command.Exception {
556 parser = new OptionParser();
557
558 // ## Need subcommand-specific option parsing
559 libPath
|
27
28 import java.lang.module.*;
29 import java.io.*;
30 import java.net.*;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.security.*;
35 import java.util.*;
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 final JigsawModuleSystem jms
48 = JigsawModuleSystem.instance();
49
50 private static final File homeLibrary = Library.systemLibraryPath();
51
52 static class Create extends Command<SimpleLibrary> {
53 protected void go(SimpleLibrary lib)
54 throws Command.Exception
55 {
56 noDry();
57 finishArgs();
58 File lp = libPath(opts);
59 File pp = null;
60 if (opts.has(parentPath))
61 pp = opts.valueOf(parentPath);
62 else if (!opts.has("N"))
63 pp = homeLibrary;
64
65 File natlibs = null;
66 if (opts.has(nativeLibs))
67 natlibs = opts.valueOf(nativeLibs);
183 }
184 }
185
186 static class Install extends Command<SimpleLibrary> {
187 protected void go(SimpleLibrary lib)
188 throws Command.Exception
189 {
190 String key = takeArg();
191 File kf = new File(key);
192 boolean verifySignature = !opts.has("noverify");
193 boolean strip = opts.has("G");
194
195 // Old form: install <classes-dir> <module-name> ...
196 //
197 if (kf.exists() && kf.isDirectory()) {
198 noDry();
199 List<Manifest> mfs = new ArrayList<>();
200 while (hasArg())
201 mfs.add(Manifest.create(takeArg(), kf));
202 finishArgs();
203 if (mfs.isEmpty())
204 throw new Command.Exception("%s: no module-name specified",
205 command);
206 try {
207 lib.installFromManifests(mfs, strip);
208 } catch (ConfigurationException x) {
209 throw new Command.Exception(x);
210 } catch (IOException x) {
211 throw new Command.Exception(x);
212 }
213 return;
214 }
215
216 // Install one or more module file(s)
217 //
218 if (kf.exists() && kf.isFile()) {
219 noDry();
220 List<File> fs = new ArrayList<>();
221 fs.add(kf);
222 while (hasArg())
223 fs.add(new File(takeArg()));
224 finishArgs();
225 try {
280 // ## need to revisit this
281 static class PreInstall extends Command<SimpleLibrary> {
282 protected void go(SimpleLibrary lib)
283 throws Command.Exception
284 {
285 noDry();
286 File classes = new File(takeArg());
287 File dst = new File(takeArg());
288 List<Manifest> mfs = new ArrayList<Manifest>();
289 while (hasArg())
290 mfs.add(Manifest.create(takeArg(), classes));
291 finishArgs();
292 try {
293 lib.preInstall(mfs, dst);
294 } catch (IOException x) {
295 throw new Command.Exception(x);
296 }
297 }
298 }
299
300 static class Remove extends Command<SimpleLibrary> {
301 protected void go(SimpleLibrary lib)
302 throws Command.Exception
303 {
304 if (dry && force)
305 throw new Command.Exception("%s: specify only one of "
306 + "-n (--dry-run) or -f (--force)",
307 command);
308 List<ModuleId> mids = new ArrayList<ModuleId>();
309 try {
310 while (hasArg())
311 mids.add(jms.parseModuleId(takeArg()));
312 } catch (IllegalArgumentException x) {
313 throw new Command.Exception(x.getMessage());
314 }
315 boolean quiet = false; // ## Need -q
316 try {
317 List<IOException> excs;
318 if (force)
319 lib.removeForcibly(mids);
320 else
321 lib.remove(mids, dry);
322 } catch (ConfigurationException x) {
323 throw new Command.Exception(x);
324 } catch (IOException x) {
325 if (!quiet) {
326 for (Throwable t : x.getSuppressed())
327 out.format("Warning: %s%n", t.getMessage());
328 }
329 throw new Command.Exception(x);
330 }
331 }
332 }
333
334 static class DumpConfig extends Command<SimpleLibrary> {
335 protected void go(SimpleLibrary lib)
336 throws Command.Exception
337 {
338 noDry();
339 String midqs = takeArg();
340 ModuleIdQuery midq = null;
341 try {
342 midq = jms.parseModuleIdQuery(midqs);
343 } catch (IllegalArgumentException x) {
344 throw new Command.Exception(x.getMessage());
345 }
346 finishArgs();
347 try {
348 ModuleId mid = lib.findLatestModuleId(midq);
349 if (mid == null)
350 throw new Command.Exception(midq + ": No such module");
351 Configuration<Context> cf = lib.readConfiguration(mid);
352 if (cf == null)
353 throw new Command.Exception(mid + ": Not a root module");
508 RemoteRepositoryList rl = lib.repositoryList();
509 int n = 0;
510 for (RemoteRepository rr : rl.repositories()) {
511 out.format("%s - ", rr.location());
512 out.flush();
513 boolean stale
514 = dry ? rr.isCatalogStale() : rr.updateCatalog(force);
515 if (stale) {
516 n++;
517 out.format(dry ? "out of date%n" : "updated%n");
518 } else {
519 out.format("up to date%n");
520 }
521 }
522 } catch (IOException x) {
523 throw new Command.Exception(x);
524 }
525 }
526 }
527
528 private static final Map<String,Class<? extends Command<SimpleLibrary>>> commands
529 = new HashMap<>();
530
531 static {
532 commands.put("add-repo", AddRepo.class);
533 commands.put("config", Config.class);
534 commands.put("create", Create.class);
535 commands.put("del-repo", DelRepo.class);
536 commands.put("dump-class", DumpClass.class);
537 commands.put("dump-config", DumpConfig.class);
538 commands.put("extract", Extract.class);
539 commands.put("id", Identify.class);
540 commands.put("identify", Identify.class);
541 commands.put("install", Install.class);
542 commands.put("list", Commands.ListLibrary.class);
543 commands.put("ls", Commands.ListLibrary.class);
544 commands.put("preinstall", PreInstall.class);
545 commands.put("refresh", Refresh.class);
546 commands.put("reindex", ReIndex.class);
547 commands.put("remove", Remove.class);
548 commands.put("rm", Remove.class);
549 commands.put("repos", Repos.class);
550 }
551
552 private OptionParser parser;
553
554 private static OptionSpec<Integer> repoIndex; // ##
555 private static OptionSpec<File> libPath;
556 private static OptionSpec<File> parentPath;
557 private static OptionSpec<File> nativeLibs;
558 private static OptionSpec<File> nativeCmds;
559 private static OptionSpec<File> configFiles;
560
561 private void usage() {
562 out.format("%n");
563 out.format("usage: jmod add-repo [-i <index>] URL%n");
564 out.format(" jmod config [<module-id> ...]%n");
565 out.format(" jmod create [-L <library>] [-P <parent>]" +
566 " [--natlib <natlib>] [--natcmd <natcmd>] [--config <config>]%n");
567 out.format(" jmod del-repo URL%n");
568 out.format(" jmod dump-class <module-id> <class-name> <output-file>%n");
569 out.format(" jmod dump-config <module-id>%n");
570 out.format(" jmod extract <module-file> ...%n");
571 out.format(" jmod identify%n");
572 out.format(" jmod install [--noverify] [-n] <module-id-query> ...%n");
573 out.format(" jmod install [--noverify] <module-file> ...%n");
574 out.format(" jmod install <classes-dir> <module-name> ...%n");
575 out.format(" jmod list [-v] [-p] [-R] [<module-id-query>]%n");
576 out.format(" jmod preinstall <classes-dir> <dst-dir> <module-name> ...%n");
577 out.format(" jmod refresh [-f] [-n] [-v]%n");
578 out.format(" jmod reindex [<module-id> ...]%n");
579 out.format(" jmod remove [-f] [-n] [<module-id> ...]%n");
580 out.format(" jmod repos [-v]%n");
581 out.format("%n");
582 try {
583 parser.printHelpOn(out);
584 } catch (IOException x) {
585 throw new AssertionError(x);
586 }
587 out.format("%n");
588 System.exit(0);
589 }
590
591 public static void run(String [] args) throws OptionException, Command.Exception {
592 new Librarian().exec(args);
593 }
594
595 private void exec(String[] args) throws OptionException, Command.Exception {
596 parser = new OptionParser();
597
598 // ## Need subcommand-specific option parsing
599 libPath
|