< prev index next >

src/jdk.jpackage/share/classes/jdk/jpackage/internal/Arguments.java

Print this page




  47 import java.util.stream.Stream;
  48 import java.util.regex.Matcher;
  49 import java.util.regex.Pattern;
  50 
  51 /**
  52  * Arguments
  53  *
  54  * This class encapsulates and processes the command line arguments,
  55  * in effect, implementing all the work of jpackage tool.
  56  *
  57  * The primary entry point, processArguments():
  58  * Processes and validates command line arguments, constructing DeployParams.
  59  * Validates the DeployParams, and generate the BundleParams.
  60  * Generates List of Bundlers from BundleParams valid for this platform.
  61  * Executes each Bundler in the list.
  62  */
  63 public class Arguments {
  64     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  65             "jdk.jpackage.internal.resources.MainResources");
  66 
  67     private static final String IMAGE_PACKAGE_TYPE = "app-image";
  68     private static final String FA_EXTENSIONS = "extension";
  69     private static final String FA_CONTENT_TYPE = "mime-type";
  70     private static final String FA_DESCRIPTION = "description";
  71     private static final String FA_ICON = "icon";
  72 
  73     // regexp for parsing args (for example, for additional launchers)
  74     private static Pattern pattern = Pattern.compile(
  75           "(?:(?:([\"'])(?:\\\\\\1|.)*?(?:\\1|$))|(?:\\\\[\"'\\s]|[^\\s]))++");
  76 
  77     private DeployParams deployParams = null;
  78     private String packageType = null;
  79 
  80     private int pos = 0;
  81     private List<String> argList = null;
  82 
  83     private List<CLIOptions> allOptions = null;
  84 
  85     private String input = null;
  86     private String output = null;
  87 


 360             this.category = category;
 361         }
 362 
 363         static void setContext(Arguments context) {
 364             argContext = context;
 365         }
 366 
 367         public static Arguments context() {
 368             if (argContext != null) {
 369                 return argContext;
 370             } else {
 371                 throw new RuntimeException("Argument context is not set.");
 372             }
 373         }
 374 
 375         public String getId() {
 376             return this.id;
 377         }
 378 
 379         String getIdWithPrefix() {
 380             String prefix = isMode() ? "" : "--";
 381             return prefix + this.id;
 382         }
 383 
 384         String getShortIdWithPrefix() {
 385             return this.shortId == null ? null : "-" + this.shortId;
 386         }
 387 
 388         void execute() {
 389             if (action != null) {
 390                 action.execute();
 391             } else {
 392                 defaultAction();
 393             }
 394         }
 395 
 396         boolean isMode() {
 397             return category == OptionCategories.MODE;
 398         }
 399 
 400         OptionCategories getCategory() {
 401             return category;
 402         }
 403 
 404         private void defaultAction() {
 405             context().deployParams.addBundleArgument(id, popArg());
 406         }
 407 
 408         private static void setOptionValue(String option, Object value) {
 409             context().deployParams.addBundleArgument(option, value);
 410         }
 411 
 412         private static String popArg() {
 413             nextArg();
 414             return (context().pos >= context().argList.size()) ?
 415                             "" : context().argList.get(context().pos);
 416         }
 417 
 418         private static String getArg() {
 419             return (context().pos >= context().argList.size()) ?
 420                         "" : context().argList.get(context().pos);
 421         }
 422 
 423         private static void nextArg() {
 424             context().pos++;
 425         }
 426 
 427         private static void prevArg() {
 428             context().pos--;
 429         }
 430 
 431         private static boolean hasNextArg() {
 432             return context().pos < context().argList.size();
 433         }
 434     }
 435 
 436     enum OptionCategories {
 437         MODE,
 438         MODULAR,
 439         PROPERTY,
 440         PLATFORM_MAC,
 441         PLATFORM_WIN,
 442         PLATFORM_LINUX;
 443     }
 444 
 445     public boolean processArguments() {
 446         try {
 447 
 448             // init context of arguments
 449             CLIOptions.setContext(this);
 450 
 451             // parse cmd line
 452             String arg;
 453             CLIOptions option;
 454             for (; CLIOptions.hasNextArg(); CLIOptions.nextArg()) {
 455                 arg = CLIOptions.getArg();
 456                 if ((option = toCLIOption(arg)) != null) {
 457                     // found a CLI option


 524         } catch (Exception e) {
 525             if (Log.isVerbose()) {
 526                 Log.verbose(e);
 527             } else {
 528                 String msg1 = e.getMessage();
 529                 Log.error(msg1);
 530                 if (e.getCause() != null && e.getCause() != e) {
 531                     String msg2 = e.getCause().getMessage();
 532                     if (!msg1.contains(msg2)) {
 533                         Log.error(msg2);
 534                     }
 535                 }
 536             }
 537             return false;
 538         }
 539     }
 540 
 541     private void validateArguments() throws PackagerException {
 542         String packageType = deployParams.getTargetFormat();
 543         String ptype = (packageType != null) ? packageType : "default";
 544         boolean imageOnly = IMAGE_PACKAGE_TYPE.equals(packageType);
 545         boolean hasAppImage = allOptions.contains(
 546                 CLIOptions.PREDEFINED_APP_IMAGE);
 547         boolean hasRuntime = allOptions.contains(
 548                 CLIOptions.PREDEFINED_RUNTIME_IMAGE);
 549         boolean installerOnly = !imageOnly && hasAppImage;
 550         runtimeInstaller = !imageOnly && hasRuntime && !hasAppImage &&
 551                 !hasMainModule && !hasMainJar;
 552 
 553         for (CLIOptions option : allOptions) {
 554             if (!ValidOptions.checkIfSupported(option)) {
 555                 // includes option valid only on different platform
 556                 throw new PackagerException("ERR_UnsupportedOption",
 557                         option.getIdWithPrefix());
 558             }
 559             if (imageOnly) {
 560                 if (!ValidOptions.checkIfImageSupported(option)) {
 561                     throw new PackagerException("ERR_InvalidTypeOption",
 562                         option.getIdWithPrefix(), packageType);
 563                 }
 564             } else if (installerOnly || runtimeInstaller) {


 675         File baseDir = new File(inputdir);
 676 
 677         if (!baseDir.isDirectory()) {
 678             throw new PackagerException("ERR_InputNotDirectory", inputdir);
 679         }
 680         if (!baseDir.canRead()) {
 681             throw new PackagerException("ERR_CannotReadInputDir", inputdir);
 682         }
 683 
 684         List<String> fileNames;
 685         fileNames = new ArrayList<>();
 686         try (Stream<Path> files = Files.list(baseDir.toPath())) {
 687             files.forEach(file -> fileNames.add(
 688                     file.getFileName().toString()));
 689         } catch (IOException e) {
 690             Log.error("Unable to add resources: " + e.getMessage());
 691         }
 692         fileNames.forEach(file -> deployParams.addResource(baseDir, file));
 693 
 694         deployParams.setClasspath();
 695     }
 696 
 697     static boolean isCLIOption(String arg) {
 698         return toCLIOption(arg) != null;
 699     }
 700 
 701     static CLIOptions toCLIOption(String arg) {
 702         CLIOptions option;
 703         if ((option = argIds.get(arg)) == null) {
 704             option = argShortIds.get(arg);
 705         }
 706         return option;
 707     }
 708 
 709     static Map<String, String> getPropertiesFromFile(String filename) {
 710         Map<String, String> map = new HashMap<>();
 711         // load properties file
 712         File file = new File(filename);
 713         Properties properties = new Properties();
 714         try (FileInputStream in = new FileInputStream(file)) {
 715             properties.load(in);
 716         } catch (IOException e) {
 717             Log.error("Exception: " + e.getMessage());
 718         }




  47 import java.util.stream.Stream;
  48 import java.util.regex.Matcher;
  49 import java.util.regex.Pattern;
  50 
  51 /**
  52  * Arguments
  53  *
  54  * This class encapsulates and processes the command line arguments,
  55  * in effect, implementing all the work of jpackage tool.
  56  *
  57  * The primary entry point, processArguments():
  58  * Processes and validates command line arguments, constructing DeployParams.
  59  * Validates the DeployParams, and generate the BundleParams.
  60  * Generates List of Bundlers from BundleParams valid for this platform.
  61  * Executes each Bundler in the list.
  62  */
  63 public class Arguments {
  64     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  65             "jdk.jpackage.internal.resources.MainResources");
  66 

  67     private static final String FA_EXTENSIONS = "extension";
  68     private static final String FA_CONTENT_TYPE = "mime-type";
  69     private static final String FA_DESCRIPTION = "description";
  70     private static final String FA_ICON = "icon";
  71 
  72     // regexp for parsing args (for example, for additional launchers)
  73     private static Pattern pattern = Pattern.compile(
  74           "(?:(?:([\"'])(?:\\\\\\1|.)*?(?:\\1|$))|(?:\\\\[\"'\\s]|[^\\s]))++");
  75 
  76     private DeployParams deployParams = null;
  77     private String packageType = null;
  78 
  79     private int pos = 0;
  80     private List<String> argList = null;
  81 
  82     private List<CLIOptions> allOptions = null;
  83 
  84     private String input = null;
  85     private String output = null;
  86 


 359             this.category = category;
 360         }
 361 
 362         static void setContext(Arguments context) {
 363             argContext = context;
 364         }
 365 
 366         public static Arguments context() {
 367             if (argContext != null) {
 368                 return argContext;
 369             } else {
 370                 throw new RuntimeException("Argument context is not set.");
 371             }
 372         }
 373 
 374         public String getId() {
 375             return this.id;
 376         }
 377 
 378         String getIdWithPrefix() {
 379             return "--" + this.id;

 380         }
 381 
 382         String getShortIdWithPrefix() {
 383             return this.shortId == null ? null : "-" + this.shortId;
 384         }
 385 
 386         void execute() {
 387             if (action != null) {
 388                 action.execute();
 389             } else {
 390                 defaultAction();
 391             }
 392         }
 393 




 394         OptionCategories getCategory() {
 395             return category;
 396         }
 397 
 398         private void defaultAction() {
 399             context().deployParams.addBundleArgument(id, popArg());
 400         }
 401 
 402         private static void setOptionValue(String option, Object value) {
 403             context().deployParams.addBundleArgument(option, value);
 404         }
 405 
 406         private static String popArg() {
 407             nextArg();
 408             return (context().pos >= context().argList.size()) ?
 409                             "" : context().argList.get(context().pos);
 410         }
 411 
 412         private static String getArg() {
 413             return (context().pos >= context().argList.size()) ?
 414                         "" : context().argList.get(context().pos);
 415         }
 416 
 417         private static void nextArg() {
 418             context().pos++;
 419         }
 420 




 421         private static boolean hasNextArg() {
 422             return context().pos < context().argList.size();
 423         }
 424     }
 425 
 426     enum OptionCategories {

 427         MODULAR,
 428         PROPERTY,
 429         PLATFORM_MAC,
 430         PLATFORM_WIN,
 431         PLATFORM_LINUX;
 432     }
 433 
 434     public boolean processArguments() {
 435         try {
 436 
 437             // init context of arguments
 438             CLIOptions.setContext(this);
 439 
 440             // parse cmd line
 441             String arg;
 442             CLIOptions option;
 443             for (; CLIOptions.hasNextArg(); CLIOptions.nextArg()) {
 444                 arg = CLIOptions.getArg();
 445                 if ((option = toCLIOption(arg)) != null) {
 446                     // found a CLI option


 513         } catch (Exception e) {
 514             if (Log.isVerbose()) {
 515                 Log.verbose(e);
 516             } else {
 517                 String msg1 = e.getMessage();
 518                 Log.error(msg1);
 519                 if (e.getCause() != null && e.getCause() != e) {
 520                     String msg2 = e.getCause().getMessage();
 521                     if (!msg1.contains(msg2)) {
 522                         Log.error(msg2);
 523                     }
 524                 }
 525             }
 526             return false;
 527         }
 528     }
 529 
 530     private void validateArguments() throws PackagerException {
 531         String packageType = deployParams.getTargetFormat();
 532         String ptype = (packageType != null) ? packageType : "default";
 533         boolean imageOnly = (packageType == null);
 534         boolean hasAppImage = allOptions.contains(
 535                 CLIOptions.PREDEFINED_APP_IMAGE);
 536         boolean hasRuntime = allOptions.contains(
 537                 CLIOptions.PREDEFINED_RUNTIME_IMAGE);
 538         boolean installerOnly = !imageOnly && hasAppImage;
 539         runtimeInstaller = !imageOnly && hasRuntime && !hasAppImage &&
 540                 !hasMainModule && !hasMainJar;
 541 
 542         for (CLIOptions option : allOptions) {
 543             if (!ValidOptions.checkIfSupported(option)) {
 544                 // includes option valid only on different platform
 545                 throw new PackagerException("ERR_UnsupportedOption",
 546                         option.getIdWithPrefix());
 547             }
 548             if (imageOnly) {
 549                 if (!ValidOptions.checkIfImageSupported(option)) {
 550                     throw new PackagerException("ERR_InvalidTypeOption",
 551                         option.getIdWithPrefix(), packageType);
 552                 }
 553             } else if (installerOnly || runtimeInstaller) {


 664         File baseDir = new File(inputdir);
 665 
 666         if (!baseDir.isDirectory()) {
 667             throw new PackagerException("ERR_InputNotDirectory", inputdir);
 668         }
 669         if (!baseDir.canRead()) {
 670             throw new PackagerException("ERR_CannotReadInputDir", inputdir);
 671         }
 672 
 673         List<String> fileNames;
 674         fileNames = new ArrayList<>();
 675         try (Stream<Path> files = Files.list(baseDir.toPath())) {
 676             files.forEach(file -> fileNames.add(
 677                     file.getFileName().toString()));
 678         } catch (IOException e) {
 679             Log.error("Unable to add resources: " + e.getMessage());
 680         }
 681         fileNames.forEach(file -> deployParams.addResource(baseDir, file));
 682 
 683         deployParams.setClasspath();




 684     }
 685 
 686     static CLIOptions toCLIOption(String arg) {
 687         CLIOptions option;
 688         if ((option = argIds.get(arg)) == null) {
 689             option = argShortIds.get(arg);
 690         }
 691         return option;
 692     }
 693 
 694     static Map<String, String> getPropertiesFromFile(String filename) {
 695         Map<String, String> map = new HashMap<>();
 696         // load properties file
 697         File file = new File(filename);
 698         Properties properties = new Properties();
 699         try (FileInputStream in = new FileInputStream(file)) {
 700             properties.load(in);
 701         } catch (IOException e) {
 702             Log.error("Exception: " + e.getMessage());
 703         }


< prev index next >