1 /*
   2  * Copyright (c) 2018, 2019, 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 package jdk.incubator.jpackage.internal;
  26 
  27 import java.io.File;
  28 import java.io.FileInputStream;
  29 import java.io.IOException;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.text.MessageFormat;
  34 import java.util.ArrayList;
  35 import java.util.Arrays;
  36 import java.util.Collection;
  37 import java.util.EnumSet;
  38 import java.util.HashMap;
  39 import java.util.HashSet;
  40 import java.util.List;
  41 import java.util.Map;
  42 import java.util.Set;
  43 import java.util.Properties;
  44 import java.util.ResourceBundle;
  45 import java.util.jar.Attributes;
  46 import java.util.jar.JarFile;
  47 import java.util.jar.Manifest;
  48 import java.util.stream.Stream;
  49 import java.util.regex.Matcher;
  50 import java.util.regex.Pattern;
  51 
  52 /**
  53  * Arguments
  54  *
  55  * This class encapsulates and processes the command line arguments,
  56  * in effect, implementing all the work of jpackage tool.
  57  *
  58  * The primary entry point, processArguments():
  59  * Processes and validates command line arguments, constructing DeployParams.
  60  * Validates the DeployParams, and generate the BundleParams.
  61  * Generates List of Bundlers from BundleParams valid for this platform.
  62  * Executes each Bundler in the list.
  63  */
  64 public class Arguments {
  65     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  66             "jdk.incubator.jpackage.internal.resources.MainResources");
  67 
  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 
  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 
  87     private boolean hasMainJar = false;
  88     private boolean hasMainClass = false;
  89     private boolean hasMainModule = false;
  90     public boolean userProvidedBuildRoot = false;
  91 
  92     private String buildRoot = null;
  93     private String mainJarPath = null;
  94 
  95     private static boolean runtimeInstaller = false;
  96 
  97     private List<AddLauncherArguments> addLaunchers = null;
  98 
  99     private static Map<String, CLIOptions> argIds = new HashMap<>();
 100     private static Map<String, CLIOptions> argShortIds = new HashMap<>();
 101 
 102     static {
 103         // init maps for parsing arguments
 104         (EnumSet.allOf(CLIOptions.class)).forEach(option -> {
 105             argIds.put(option.getIdWithPrefix(), option);
 106             if (option.getShortIdWithPrefix() != null) {
 107                 argShortIds.put(option.getShortIdWithPrefix(), option);
 108             }
 109         });
 110     }
 111 
 112     public Arguments(String[] args) {
 113         argList = new ArrayList<String>(args.length);
 114         for (String arg : args) {
 115             argList.add(arg);
 116         }
 117         Log.verbose ("\njpackage argument list: \n" + argList + "\n");
 118         pos = 0;
 119 
 120         deployParams = new DeployParams();
 121 
 122         allOptions = new ArrayList<>();
 123 
 124         addLaunchers = new ArrayList<>();
 125 
 126         output = Paths.get("").toAbsolutePath().toString();
 127         deployParams.setOutput(new File(output));
 128     }
 129 
 130     // CLIOptions is public for DeployParamsTest
 131     public enum CLIOptions {
 132         PACKAGE_TYPE("type", "t", OptionCategories.PROPERTY, () -> {
 133             context().deployParams.setTargetFormat(popArg());
 134         }),
 135 
 136         INPUT ("input", "i", OptionCategories.PROPERTY, () -> {
 137             context().input = popArg();
 138             setOptionValue("input", context().input);
 139         }),
 140 
 141         OUTPUT ("dest", "d", OptionCategories.PROPERTY, () -> {
 142             context().output = popArg();
 143             context().deployParams.setOutput(new File(context().output));
 144         }),
 145 
 146         DESCRIPTION ("description", OptionCategories.PROPERTY),
 147 
 148         VENDOR ("vendor", OptionCategories.PROPERTY),
 149 
 150         APPCLASS ("main-class", OptionCategories.PROPERTY, () -> {
 151             context().hasMainClass = true;
 152             setOptionValue("main-class", popArg());
 153         }),
 154 
 155         NAME ("name", "n", OptionCategories.PROPERTY),
 156 
 157         VERBOSE ("verbose", OptionCategories.PROPERTY, () -> {
 158             setOptionValue("verbose", true);
 159             Log.setVerbose();
 160         }),
 161 
 162         RESOURCE_DIR("resource-dir",
 163                 OptionCategories.PROPERTY, () -> {
 164             String resourceDir = popArg();
 165             setOptionValue("resource-dir", resourceDir);
 166         }),
 167 
 168         ARGUMENTS ("arguments", OptionCategories.PROPERTY, () -> {
 169             List<String> arguments = getArgumentList(popArg());
 170             setOptionValue("arguments", arguments);
 171         }),
 172 
 173         ICON ("icon", OptionCategories.PROPERTY),
 174 
 175         COPYRIGHT ("copyright", OptionCategories.PROPERTY),
 176 
 177         LICENSE_FILE ("license-file", OptionCategories.PROPERTY),
 178 
 179         VERSION ("app-version", OptionCategories.PROPERTY),
 180 
 181         RELEASE ("linux-app-release", OptionCategories.PROPERTY),
 182 
 183         JAVA_OPTIONS ("java-options", OptionCategories.PROPERTY, () -> {
 184             List<String> args = getArgumentList(popArg());
 185             args.forEach(a -> setOptionValue("java-options", a));
 186         }),
 187 
 188         FILE_ASSOCIATIONS ("file-associations",
 189                 OptionCategories.PROPERTY, () -> {
 190             Map<String, ? super Object> args = new HashMap<>();
 191 
 192             // load .properties file
 193             Map<String, String> initialMap = getPropertiesFromFile(popArg());
 194 
 195             String ext = initialMap.get(FA_EXTENSIONS);
 196             if (ext != null) {
 197                 args.put(StandardBundlerParam.FA_EXTENSIONS.getID(), ext);
 198             }
 199 
 200             String type = initialMap.get(FA_CONTENT_TYPE);
 201             if (type != null) {
 202                 args.put(StandardBundlerParam.FA_CONTENT_TYPE.getID(), type);
 203             }
 204 
 205             String desc = initialMap.get(FA_DESCRIPTION);
 206             if (desc != null) {
 207                 args.put(StandardBundlerParam.FA_DESCRIPTION.getID(), desc);
 208             }
 209 
 210             String icon = initialMap.get(FA_ICON);
 211             if (icon != null) {
 212                 args.put(StandardBundlerParam.FA_ICON.getID(), icon);
 213             }
 214 
 215             ArrayList<Map<String, ? super Object>> associationList =
 216                 new ArrayList<Map<String, ? super Object>>();
 217 
 218             associationList.add(args);
 219 
 220             // check that we really add _another_ value to the list
 221             setOptionValue("file-associations", associationList);
 222 
 223         }),
 224 
 225         ADD_LAUNCHER ("add-launcher",
 226                     OptionCategories.PROPERTY, () -> {
 227             String spec = popArg();
 228             String name = null;
 229             String filename = spec;
 230             if (spec.contains("=")) {
 231                 String[] values = spec.split("=", 2);
 232                 name = values[0];
 233                 filename = values[1];
 234             }
 235             context().addLaunchers.add(
 236                 new AddLauncherArguments(name, filename));
 237         }),
 238 
 239         TEMP_ROOT ("temp", OptionCategories.PROPERTY, () -> {
 240             context().buildRoot = popArg();
 241             context().userProvidedBuildRoot = true;
 242             setOptionValue("temp", context().buildRoot);
 243         }),
 244 
 245         INSTALL_DIR ("install-dir", OptionCategories.PROPERTY),
 246 
 247         PREDEFINED_APP_IMAGE ("app-image", OptionCategories.PROPERTY),
 248 
 249         PREDEFINED_RUNTIME_IMAGE ("runtime-image", OptionCategories.PROPERTY),
 250 
 251         MAIN_JAR ("main-jar",  OptionCategories.PROPERTY, () -> {
 252             context().mainJarPath = popArg();
 253             context().hasMainJar = true;
 254             setOptionValue("main-jar", context().mainJarPath);
 255         }),
 256 
 257         MODULE ("module", "m", OptionCategories.MODULAR, () -> {
 258             context().hasMainModule = true;
 259             setOptionValue("module", popArg());
 260         }),
 261 
 262         ADD_MODULES ("add-modules", OptionCategories.MODULAR),
 263 
 264         MODULE_PATH ("module-path", "p", OptionCategories.MODULAR),
 265 
 266         BIND_SERVICES ("bind-services", OptionCategories.PROPERTY, () -> {
 267             setOptionValue("bind-services", true);
 268         }),
 269 
 270         MAC_SIGN ("mac-sign", "s", OptionCategories.PLATFORM_MAC, () -> {
 271             setOptionValue("mac-sign", true);
 272         }),
 273 
 274         MAC_BUNDLE_NAME ("mac-package-name", OptionCategories.PLATFORM_MAC),
 275 
 276         MAC_BUNDLE_IDENTIFIER("mac-package-identifier",
 277                     OptionCategories.PLATFORM_MAC),
 278 
 279         MAC_BUNDLE_SIGNING_PREFIX ("mac-package-signing-prefix",
 280                     OptionCategories.PLATFORM_MAC),
 281 
 282         MAC_SIGNING_KEY_NAME ("mac-signing-key-user-name",
 283                     OptionCategories.PLATFORM_MAC),
 284 
 285         MAC_SIGNING_KEYCHAIN ("mac-signing-keychain",
 286                     OptionCategories.PLATFORM_MAC),
 287 
 288         WIN_MENU_HINT ("win-menu", OptionCategories.PLATFORM_WIN, () -> {
 289             setOptionValue("win-menu", true);
 290         }),
 291 
 292         WIN_MENU_GROUP ("win-menu-group", OptionCategories.PLATFORM_WIN),
 293 
 294         WIN_SHORTCUT_HINT ("win-shortcut",
 295                 OptionCategories.PLATFORM_WIN, () -> {
 296             setOptionValue("win-shortcut", true);
 297         }),
 298 
 299         WIN_PER_USER_INSTALLATION ("win-per-user-install",
 300                 OptionCategories.PLATFORM_WIN, () -> {
 301             setOptionValue("win-per-user-install", false);
 302         }),
 303 
 304         WIN_DIR_CHOOSER ("win-dir-chooser",
 305                 OptionCategories.PLATFORM_WIN, () -> {
 306             setOptionValue("win-dir-chooser", true);
 307         }),
 308 
 309         WIN_UPGRADE_UUID ("win-upgrade-uuid",
 310                 OptionCategories.PLATFORM_WIN),
 311 
 312         WIN_CONSOLE_HINT ("win-console", OptionCategories.PLATFORM_WIN, () -> {
 313             setOptionValue("win-console", true);
 314         }),
 315 
 316         LINUX_BUNDLE_NAME ("linux-package-name",
 317                 OptionCategories.PLATFORM_LINUX),
 318 
 319         LINUX_DEB_MAINTAINER ("linux-deb-maintainer",
 320                 OptionCategories.PLATFORM_LINUX),
 321 
 322         LINUX_CATEGORY ("linux-app-category",
 323                 OptionCategories.PLATFORM_LINUX),
 324 
 325         LINUX_RPM_LICENSE_TYPE ("linux-rpm-license-type",
 326                 OptionCategories.PLATFORM_LINUX),
 327 
 328         LINUX_PACKAGE_DEPENDENCIES ("linux-package-deps",
 329                 OptionCategories.PLATFORM_LINUX),
 330 
 331         LINUX_SHORTCUT_HINT ("linux-shortcut",
 332                 OptionCategories.PLATFORM_LINUX, () -> {
 333             setOptionValue("linux-shortcut", true);
 334         }),
 335 
 336         LINUX_MENU_GROUP ("linux-menu-group", OptionCategories.PLATFORM_LINUX);
 337 
 338         private final String id;
 339         private final String shortId;
 340         private final OptionCategories category;
 341         private final ArgAction action;
 342         private static Arguments argContext;
 343 
 344         private CLIOptions(String id, OptionCategories category) {
 345             this(id, null, category, null);
 346         }
 347 
 348         private CLIOptions(String id, String shortId,
 349                            OptionCategories category) {
 350             this(id, shortId, category, null);
 351         }
 352 
 353         private CLIOptions(String id,
 354                 OptionCategories category, ArgAction action) {
 355             this(id, null, category, action);
 356         }
 357 
 358         private CLIOptions(String id, String shortId,
 359                            OptionCategories category, ArgAction action) {
 360             this.id = id;
 361             this.shortId = shortId;
 362             this.action = action;
 363             this.category = category;
 364         }
 365 
 366         static void setContext(Arguments context) {
 367             argContext = context;
 368         }
 369 
 370         public static Arguments context() {
 371             if (argContext != null) {
 372                 return argContext;
 373             } else {
 374                 throw new RuntimeException("Argument context is not set.");
 375             }
 376         }
 377 
 378         public String getId() {
 379             return this.id;
 380         }
 381 
 382         String getIdWithPrefix() {
 383             return "--" + this.id;
 384         }
 385 
 386         String getShortIdWithPrefix() {
 387             return this.shortId == null ? null : "-" + this.shortId;
 388         }
 389 
 390         void execute() {
 391             if (action != null) {
 392                 action.execute();
 393             } else {
 394                 defaultAction();
 395             }
 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
 447                     allOptions.add(option);
 448                     option.execute();
 449                 } else {
 450                     throw new PackagerException("ERR_InvalidOption", arg);
 451                 }
 452             }
 453 
 454             if (hasMainJar && !hasMainClass) {
 455                 // try to get main-class from manifest
 456                 String mainClass = getMainClassFromManifest();
 457                 if (mainClass != null) {
 458                     CLIOptions.setOptionValue(
 459                             CLIOptions.APPCLASS.getId(), mainClass);
 460                 }
 461             }
 462 
 463             // display error for arguments that are not supported
 464             // for current configuration.
 465 
 466             validateArguments();
 467 
 468             addResources(deployParams, input, mainJarPath);
 469 
 470             List<Map<String, ? super Object>> launchersAsMap =
 471                     new ArrayList<>();
 472 
 473             for (AddLauncherArguments sl : addLaunchers) {
 474                 launchersAsMap.add(sl.getLauncherMap());
 475             }
 476 
 477             deployParams.addBundleArgument(
 478                     StandardBundlerParam.ADD_LAUNCHERS.getID(),
 479                     launchersAsMap);
 480 
 481             // at this point deployParams should be already configured
 482 
 483             deployParams.validate();
 484 
 485             BundleParams bp = deployParams.getBundleParams();
 486 
 487             // validate name(s)
 488             ArrayList<String> usedNames = new ArrayList<String>();
 489             usedNames.add(bp.getName()); // add main app name
 490 
 491             for (AddLauncherArguments sl : addLaunchers) {
 492                 Map<String, ? super Object> slMap = sl.getLauncherMap();
 493                 String slName =
 494                         (String) slMap.get(Arguments.CLIOptions.NAME.getId());
 495                 if (slName == null) {
 496                     throw new PackagerException("ERR_NoAddLauncherName");
 497                 }
 498                 // same rules apply to additional launcher names as app name
 499                 DeployParams.validateName(slName, false);
 500                 for (String usedName : usedNames) {
 501                     if (slName.equals(usedName)) {
 502                         throw new PackagerException("ERR_NoUniqueName");
 503                     }
 504                 }
 505                 usedNames.add(slName);
 506             }
 507             if (runtimeInstaller && bp.getName() == null) {
 508                 throw new PackagerException("ERR_NoJreInstallerName");
 509             }
 510 
 511             generateBundle(bp.getBundleParamsAsMap());
 512             return true;
 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 (msg2 != null && !msg1.contains(msg2)) {
 522                         Log.error(msg2);
 523                     }
 524                 }
 525             }
 526             return false;
 527         }
 528     }
 529 
 530     private void validateArguments() throws PackagerException {
 531         String type = deployParams.getTargetFormat();
 532         String ptype = (type != null) ? type : "default";
 533         boolean imageOnly = deployParams.isTargetAppImage();
 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(), type);
 552                 }
 553             } else if (installerOnly || runtimeInstaller) {
 554                 if (!ValidOptions.checkIfInstallerSupported(option)) {
 555                     if (runtimeInstaller) {
 556                         throw new PackagerException("ERR_NoInstallerEntryPoint",
 557                             option.getIdWithPrefix());
 558                     } else {
 559                         throw new PackagerException("ERR_InvalidTypeOption",
 560                             option.getIdWithPrefix(), ptype);
 561                    }
 562                 }
 563             }
 564         }
 565         if (hasRuntime) {
 566             if (hasAppImage) {
 567                 // note --runtime-image is only for image or runtime installer.
 568                 throw new PackagerException("ERR_MutuallyExclusiveOptions",
 569                         CLIOptions.PREDEFINED_RUNTIME_IMAGE.getIdWithPrefix(),
 570                         CLIOptions.PREDEFINED_APP_IMAGE.getIdWithPrefix());
 571             }
 572             if (allOptions.contains(CLIOptions.ADD_MODULES)) {
 573                 throw new PackagerException("ERR_MutuallyExclusiveOptions",
 574                         CLIOptions.PREDEFINED_RUNTIME_IMAGE.getIdWithPrefix(),
 575                         CLIOptions.ADD_MODULES.getIdWithPrefix());
 576             }
 577             if (allOptions.contains(CLIOptions.BIND_SERVICES)) {
 578                 throw new PackagerException("ERR_MutuallyExclusiveOptions",
 579                         CLIOptions.PREDEFINED_RUNTIME_IMAGE.getIdWithPrefix(),
 580                         CLIOptions.BIND_SERVICES.getIdWithPrefix());
 581             }
 582 
 583         }
 584         if (hasMainJar && hasMainModule) {
 585             throw new PackagerException("ERR_BothMainJarAndModule");
 586         }
 587         if (imageOnly && !hasMainJar && !hasMainModule) {
 588             throw new PackagerException("ERR_NoEntryPoint");
 589         }
 590     }
 591 
 592     private jdk.incubator.jpackage.internal.Bundler getPlatformBundler() {
 593         boolean appImage = deployParams.isTargetAppImage();
 594         String type = deployParams.getTargetFormat();
 595         String bundleType = (appImage ?  "IMAGE" : "INSTALLER");
 596 
 597         for (jdk.incubator.jpackage.internal.Bundler bundler :
 598                 Bundlers.createBundlersInstance().getBundlers(bundleType)) {
 599             if (type == null) {
 600                  if (bundler.isDefault()
 601                          && bundler.supported(runtimeInstaller)) {
 602                      return bundler;
 603                  }
 604             } else {
 605                  if ((appImage || type.equalsIgnoreCase(bundler.getID()))
 606                          && bundler.supported(runtimeInstaller)) {
 607                      return bundler;
 608                  }
 609             }
 610         }
 611         return null;
 612     }
 613 
 614     private void generateBundle(Map<String,? super Object> params)
 615             throws PackagerException {
 616 
 617         boolean bundleCreated = false;
 618 
 619         // the temp dir needs to be fetched from the params early,
 620         // to prevent each copy of the params (such as may be used for
 621         // additional launchers) from generating a separate temp dir when
 622         // the default is used (the default is a new temp directory)
 623         // The bundler.cleanup() below would not otherwise be able to
 624         // clean these extra (and unneeded) temp directories.
 625         StandardBundlerParam.TEMP_ROOT.fetchFrom(params);
 626 
 627         // determine what bundler to run
 628         jdk.incubator.jpackage.internal.Bundler bundler = getPlatformBundler();
 629 
 630         if (bundler == null) {
 631             throw new PackagerException("ERR_InvalidInstallerType",
 632                       deployParams.getTargetFormat());
 633         }
 634 
 635         Map<String, ? super Object> localParams = new HashMap<>(params);
 636         try {
 637             bundler.validate(localParams);
 638             File result = bundler.execute(localParams, deployParams.outdir);
 639             if (result == null) {
 640                 throw new PackagerException("MSG_BundlerFailed",
 641                         bundler.getID(), bundler.getName());
 642             }
 643             Log.verbose(MessageFormat.format(
 644                     I18N.getString("message.bundle-created"),
 645                     bundler.getName()));
 646         } catch (ConfigException e) {
 647             Log.verbose(e);
 648             if (e.getAdvice() != null)  {
 649                 throw new PackagerException(e, "MSG_BundlerConfigException",
 650                         bundler.getName(), e.getMessage(), e.getAdvice());
 651             } else {
 652                 throw new PackagerException(e,
 653                        "MSG_BundlerConfigExceptionNoAdvice",
 654                         bundler.getName(), e.getMessage());
 655             }
 656         } catch (RuntimeException re) {
 657             Log.verbose(re);
 658             throw new PackagerException(re, "MSG_BundlerRuntimeException",
 659                     bundler.getName(), re.toString());
 660         } finally {
 661             if (userProvidedBuildRoot) {
 662                 Log.verbose(MessageFormat.format(
 663                         I18N.getString("message.debug-working-directory"),
 664                         (new File(buildRoot)).getAbsolutePath()));
 665             } else {
 666                 // always clean up the temporary directory created
 667                 // when --temp option not used.
 668                 bundler.cleanup(localParams);
 669             }
 670         }
 671     }
 672 
 673     private void addResources(DeployParams deployParams,
 674             String inputdir, String mainJar) throws PackagerException {
 675 
 676         if (inputdir == null || inputdir.isEmpty()) {
 677             return;
 678         }
 679 
 680         File baseDir = new File(inputdir);
 681 
 682         if (!baseDir.isDirectory()) {
 683             throw new PackagerException("ERR_InputNotDirectory", inputdir);
 684         }
 685         if (!baseDir.canRead()) {
 686             throw new PackagerException("ERR_CannotReadInputDir", inputdir);
 687         }
 688 
 689         List<String> fileNames;
 690         fileNames = new ArrayList<>();
 691         try (Stream<Path> files = Files.list(baseDir.toPath())) {
 692             files.forEach(file -> fileNames.add(
 693                     file.getFileName().toString()));
 694         } catch (IOException e) {
 695             Log.error("Unable to add resources: " + e.getMessage());
 696         }
 697         fileNames.forEach(file -> deployParams.addResource(baseDir, file));
 698 
 699         deployParams.setClasspath(mainJar);
 700     }
 701 
 702     static CLIOptions toCLIOption(String arg) {
 703         CLIOptions option;
 704         if ((option = argIds.get(arg)) == null) {
 705             option = argShortIds.get(arg);
 706         }
 707         return option;
 708     }
 709 
 710     static Map<String, String> getPropertiesFromFile(String filename) {
 711         Map<String, String> map = new HashMap<>();
 712         // load properties file
 713         File file = new File(filename);
 714         Properties properties = new Properties();
 715         try (FileInputStream in = new FileInputStream(file)) {
 716             properties.load(in);
 717         } catch (IOException e) {
 718             Log.error("Exception: " + e.getMessage());
 719         }
 720 
 721         for (final String name: properties.stringPropertyNames()) {
 722             map.put(name, properties.getProperty(name));
 723         }
 724 
 725         return map;
 726     }
 727 
 728     static List<String> getArgumentList(String inputString) {
 729         List<String> list = new ArrayList<>();
 730         if (inputString == null || inputString.isEmpty()) {
 731              return list;
 732         }
 733 
 734         // The "pattern" regexp attempts to abide to the rule that
 735         // strings are delimited by whitespace unless surrounded by
 736         // quotes, then it is anything (including spaces) in the quotes.
 737         Matcher m = pattern.matcher(inputString);
 738         while (m.find()) {
 739             String s = inputString.substring(m.start(), m.end()).trim();
 740             // Ensure we do not have an empty string. trim() will take care of
 741             // whitespace only strings. The regex preserves quotes and escaped
 742             // chars so we need to clean them before adding to the List
 743             if (!s.isEmpty()) {
 744                 list.add(unquoteIfNeeded(s));
 745             }
 746         }
 747         return list;
 748     }
 749 
 750     private static String unquoteIfNeeded(String in) {
 751         if (in == null) {
 752             return null;
 753         }
 754 
 755         if (in.isEmpty()) {
 756             return "";
 757         }
 758 
 759         // Use code points to preserve non-ASCII chars
 760         StringBuilder sb = new StringBuilder();
 761         int codeLen = in.codePointCount(0, in.length());
 762         int quoteChar = -1;
 763         for (int i = 0; i < codeLen; i++) {
 764             int code = in.codePointAt(i);
 765             if (code == '"' || code == '\'') {
 766                 // If quote is escaped make sure to copy it
 767                 if (i > 0 && in.codePointAt(i - 1) == '\\') {
 768                     sb.deleteCharAt(sb.length() - 1);
 769                     sb.appendCodePoint(code);
 770                     continue;
 771                 }
 772                 if (quoteChar != -1) {
 773                     if (code == quoteChar) {
 774                         // close quote, skip char
 775                         quoteChar = -1;
 776                     } else {
 777                         sb.appendCodePoint(code);
 778                     }
 779                 } else {
 780                     // opening quote, skip char
 781                     quoteChar = code;
 782                 }
 783             } else {
 784                 sb.appendCodePoint(code);
 785             }
 786         }
 787         return sb.toString();
 788     }
 789 
 790     private String getMainClassFromManifest() {
 791         if (mainJarPath == null ||
 792             input == null ) {
 793             return null;
 794         }
 795 
 796         JarFile jf;
 797         try {
 798             File file = new File(input, mainJarPath);
 799             if (!file.exists()) {
 800                 return null;
 801             }
 802             jf = new JarFile(file);
 803             Manifest m = jf.getManifest();
 804             Attributes attrs = (m != null) ? m.getMainAttributes() : null;
 805             if (attrs != null) {
 806                 return attrs.getValue(Attributes.Name.MAIN_CLASS);
 807             }
 808         } catch (IOException ignore) {}
 809         return null;
 810     }
 811 
 812 }