1 /* 2 * Copyright (c) 2011, 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 26 package jdk.jpackage.internal; 27 28 import java.io.File; 29 import java.nio.file.Files; 30 import java.nio.file.Path; 31 import java.nio.file.InvalidPathException; 32 import java.text.MessageFormat; 33 import java.util.ArrayList; 34 import java.util.Arrays; 35 import java.util.Collection; 36 import java.util.LinkedHashMap; 37 import java.util.LinkedHashSet; 38 import java.util.LinkedList; 39 import java.util.List; 40 import java.util.Map; 41 import java.util.Set; 42 import java.util.TreeMap; 43 import java.util.TreeSet; 44 45 /** 46 * DeployParams 47 * 48 * This class is generated and used in Arguments.processArguments() as 49 * intermediate step in generating the BundleParams and ultimately the Bundles 50 */ 51 public class DeployParams { 52 53 final List<RelativeFileSet> resources = new ArrayList<>(); 54 55 String id; 56 String title; 57 String vendor; 58 String email; 59 String description; 60 String licenseType; 61 String copyright; 62 String version; 63 Boolean systemWide; 64 Boolean serviceHint; 65 Boolean signBundle; 66 Boolean installdirChooser; 67 68 String applicationClass; 69 70 List<Param> params; 71 List<String> arguments; //unnamed arguments 72 73 // Java 9 modules support 74 String addModules = null; 75 String limitModules = null; 76 String modulePath = null; 77 String module = null; 78 String debugPort = null; 79 80 File outdir = null; 81 82 String appId = null; 83 84 // list of jvm args 85 // (in theory string can contain spaces and need to be escaped 86 List<String> jvmargs = new LinkedList<>(); 87 88 // raw arguments to the bundler 89 Map<String, ? super Object> bundlerArguments = new LinkedHashMap<>(); 90 91 void setLicenseType(String licenseType) { 92 this.licenseType = licenseType; 93 } 94 95 void setCopyright(String copyright) { 96 this.copyright = copyright; 97 } 98 99 void setVersion(String version) { 100 this.version = version; 101 } 102 103 void setSystemWide(Boolean systemWide) { 104 this.systemWide = systemWide; 105 } 106 107 void setInstalldirChooser(Boolean installdirChooser) { 108 this.installdirChooser = installdirChooser; 109 } 110 111 void setSignBundle(Boolean signBundle) { 112 this.signBundle = signBundle; 113 } 114 115 void addJvmArg(String v) { 116 jvmargs.add(v); 117 } 118 119 void setArguments(List<String> args) { 120 this.arguments = args; 121 } 122 123 List<String> getArguments() { 124 return this.arguments; 125 } 126 127 void addArgument(String arg) { 128 this.arguments.add(arg); 129 } 130 131 void addAddModule(String value) { 132 if (addModules == null) { 133 addModules = value; 134 } 135 else { 136 addModules += "," + value; 137 } 138 } 139 140 void addLimitModule(String value) { 141 if (limitModules == null) { 142 limitModules = value; 143 } 144 else { 145 limitModules += "," + value; 146 } 147 } 148 149 String getModulePath() { 150 return this.modulePath; 151 } 152 153 void setModulePath(String value) { 154 this.modulePath = value; 155 } 156 157 void setModule(String value) { 158 this.module = value; 159 } 160 161 void setDebug(String value) { 162 this.debugPort = value; 163 } 164 165 void setDescription(String description) { 166 this.description = description; 167 } 168 169 public void setAppId(String id) { 170 appId = id; 171 } 172 173 void setParams(List<Param> params) { 174 this.params = params; 175 } 176 177 void setTitle(String title) { 178 this.title = title; 179 } 180 181 void setVendor(String vendor) { 182 this.vendor = vendor; 183 } 184 185 void setEmail(String email) { 186 this.email = email; 187 } 188 189 void setApplicationClass(String applicationClass) { 190 this.applicationClass = applicationClass; 191 } 192 193 File getOutput() { 194 return outdir; 195 } 196 197 public void setOutput(File output) { 198 outdir = output; 199 } 200 201 static class Template { 202 File in; 203 File out; 204 205 Template(File in, File out) { 206 this.in = in; 207 this.out = out; 208 } 209 } 210 211 // we need to expand as in some cases 212 // (most notably jpackage) 213 // we may get "." as filename and assumption is we include 214 // everything in the given folder 215 // (IOUtils.copyfiles() have recursive behavior) 216 List<File> expandFileset(File root) { 217 List<File> files = new LinkedList<>(); 218 if (!Files.isSymbolicLink(root.toPath())) { 219 if (root.isDirectory()) { 220 File[] children = root.listFiles(); 221 if (children != null) { 222 for (File f : children) { 223 files.addAll(expandFileset(f)); 224 } 225 } 226 } else { 227 files.add(root); 228 } 229 } 230 return files; 231 } 232 233 public void addResource(File baseDir, String path) { 234 File file = new File(baseDir, path); 235 // normalize top level dir 236 // to strip things like "." in the path 237 // or it can confuse symlink detection logic 238 file = file.getAbsoluteFile(); 239 240 if (baseDir == null) { 241 baseDir = file.getParentFile(); 242 } 243 resources.add(new RelativeFileSet( 244 baseDir, new LinkedHashSet<>(expandFileset(file)))); 245 } 246 247 public void addResource(File baseDir, File file) { 248 // normalize initial file 249 // to strip things like "." in the path 250 // or it can confuse symlink detection logic 251 file = file.getAbsoluteFile(); 252 253 if (baseDir == null) { 254 baseDir = file.getParentFile(); 255 } 256 resources.add(new RelativeFileSet( 257 baseDir, new LinkedHashSet<>(expandFileset(file)))); 258 } 259 260 void setClasspath() { 261 String classpath = ""; 262 for (RelativeFileSet resource : resources) { 263 for (String file : resource.getIncludedFiles()) { 264 if (file.endsWith(".jar")) { 265 classpath += file + File.pathSeparator; 266 } 267 } 268 } 269 addBundleArgument( 270 StandardBundlerParam.CLASSPATH.getID(), classpath); 271 } 272 273 private static File createFile(final File baseDir, final String path) { 274 final File testFile = new File(path); 275 return testFile.isAbsolute() ? 276 testFile : new File(baseDir == null ? 277 null : baseDir.getAbsolutePath(), path); 278 } 279 280 static void validateName(String s, boolean forApp) 281 throws PackagerException { 282 283 String exceptionKey = forApp ? 284 "ERR_InvalidAppName" : "ERR_InvalidSLName"; 285 286 if (s == null) { 287 if (forApp) { 288 return; 289 } else { 290 throw new PackagerException(exceptionKey, s); 291 } 292 } 293 if (s.length() == 0 || s.charAt(s.length() - 1) == '\\') { 294 throw new PackagerException(exceptionKey, s); 295 } 296 try { 297 // name must be valid path element for this file system 298 Path p = (new File(s)).toPath(); 299 // and it must be a single name element in a path 300 if (p.getNameCount() != 1) { 301 throw new PackagerException(exceptionKey, s); 302 } 303 } catch (InvalidPathException ipe) { 304 throw new PackagerException(ipe, exceptionKey, s); 305 } 306 307 for (int i = 0; i < s.length(); i++) { 308 char a = s.charAt(i); 309 // We check for ASCII codes first which we accept. If check fails, 310 // check if it is acceptable extended ASCII or unicode character. 311 if (a < ' ' || a > '~') { 312 // Accept anything else including special chars like copyright 313 // symbols. Note: space will be included by ASCII check above, 314 // but other whitespace like tabs or new line will be rejected. 315 if (Character.isISOControl(a) || 316 Character.isWhitespace(a)) { 317 throw new PackagerException(exceptionKey, s); 318 } 319 } else if (a == '"' || a == '%') { 320 throw new PackagerException(exceptionKey, s); 321 } 322 } 323 } 324 325 public void validate() throws PackagerException { 326 if (outdir == null) { 327 throw new PackagerException("ERR_MissingArgument", "--output"); 328 } 329 330 boolean hasModule = (bundlerArguments.get( 331 Arguments.CLIOptions.MODULE.getId()) != null); 332 boolean hasAppImage = (bundlerArguments.get( 333 Arguments.CLIOptions.PREDEFINED_APP_IMAGE.getId()) != null); 334 boolean hasClass = (bundlerArguments.get( 335 Arguments.CLIOptions.APPCLASS.getId()) != null); 336 boolean hasMain = (bundlerArguments.get( 337 Arguments.CLIOptions.MAIN_JAR.getId()) != null); 338 boolean hasRuntimeImage = (bundlerArguments.get( 339 Arguments.CLIOptions.PREDEFINED_RUNTIME_IMAGE.getId()) != null); 340 boolean hasInput = (bundlerArguments.get( 341 Arguments.CLIOptions.INPUT.getId()) != null); 342 boolean hasModulePath = (bundlerArguments.get( 343 Arguments.CLIOptions.MODULE_PATH.getId()) != null); 344 boolean runtimeInstaller = (BundlerType.INSTALLER == getBundleType()) && 345 !hasAppImage && !hasModule && !hasMain && hasRuntimeImage; 346 347 if (getBundleType() == BundlerType.IMAGE) { 348 // Module application requires --runtime-image or --module-path 349 if (hasModule) { 350 if (!hasModulePath && !hasRuntimeImage) { 351 throw new PackagerException("ERR_MissingArgument", 352 "--runtime-image or --module-path"); 353 } 354 } else { 355 if (!hasInput) { 356 throw new PackagerException( 357 "ERR_MissingArgument", "--input"); 358 } 359 } 360 } else if (getBundleType() == BundlerType.INSTALLER) { 361 if (!runtimeInstaller) { 362 if (hasModule) { 363 if (!hasModulePath && !hasRuntimeImage && !hasAppImage) { 364 throw new PackagerException("ERR_MissingArgument", 365 "--runtime-image, --module-path or --app-image"); 366 } 367 } else { 368 if (!hasInput && !hasAppImage) { 369 throw new PackagerException("ERR_MissingArgument", 370 "--input or --app-image"); 371 } 372 } 373 } 374 } 375 376 // if bundling non-modular image, or installer without app-image 377 // then we need some resources and a main class 378 if (!hasModule && !hasAppImage && !runtimeInstaller) { 379 if (resources.isEmpty()) { 380 throw new PackagerException("ERR_MissingAppResources"); 381 } 382 if (!hasClass) { 383 throw new PackagerException("ERR_MissingArgument", 384 "--main-class"); 385 } 386 if (!hasMain) { 387 throw new PackagerException("ERR_MissingArgument", 388 "--main-jar"); 389 } 390 } 391 392 String name = (String)bundlerArguments.get( 393 Arguments.CLIOptions.NAME.getId()); 394 validateName(name, true); 395 396 // Validate app image if set 397 String appImage = (String)bundlerArguments.get( 398 Arguments.CLIOptions.PREDEFINED_APP_IMAGE.getId()); 399 if (appImage != null) { 400 File appImageDir = new File(appImage); 401 if (!appImageDir.exists() || appImageDir.list().length == 0) { 402 throw new PackagerException("ERR_AppImageNotExist", appImage); 403 } 404 } 405 406 // Validate temp-root 407 String root = (String)bundlerArguments.get( 408 Arguments.CLIOptions.TEMP_ROOT.getId()); 409 if (root != null) { 410 String [] contents = (new File(root)).list(); 411 412 if (contents != null && contents.length > 0) { 413 throw new PackagerException("ERR_BuildRootInvalid", root); 414 } 415 } 416 417 // Validate license file if set 418 String license = (String)bundlerArguments.get( 419 Arguments.CLIOptions.LICENSE_FILE.getId()); 420 if (license != null) { 421 File licenseFile = new File(license); 422 if (!licenseFile.exists()) { 423 throw new PackagerException("ERR_LicenseFileNotExit"); 424 } 425 } 426 } 427 428 boolean validateForBundle() { 429 boolean result = false; 430 431 // Success 432 if (((applicationClass != null && !applicationClass.isEmpty()) || 433 (module != null && !module.isEmpty()))) { 434 result = true; 435 } 436 437 return result; 438 } 439 440 BundlerType bundleType = BundlerType.NONE; 441 String targetFormat = null; //means any 442 443 void setBundleType(BundlerType type) { 444 bundleType = type; 445 } 446 447 BundlerType getBundleType() { 448 return bundleType; 449 } 450 451 void setTargetFormat(String t) { 452 targetFormat = t; 453 } 454 455 String getTargetFormat() { 456 return targetFormat; 457 } 458 459 private String getArch() { 460 String arch = System.getProperty("os.arch").toLowerCase(); 461 462 if ("x86".equals(arch) || "i386".equals(arch) || "i486".equals(arch) 463 || "i586".equals(arch) || "i686".equals(arch)) { 464 arch = "x86"; 465 } else if ("x86_64".equals(arch) || "amd64".equals("arch")) { 466 arch = "x86_64"; 467 } 468 469 return arch; 470 } 471 472 static final Set<String> multi_args = new TreeSet<>(Arrays.asList( 473 StandardBundlerParam.JVM_OPTIONS.getID(), 474 StandardBundlerParam.ARGUMENTS.getID(), 475 StandardBundlerParam.MODULE_PATH.getID(), 476 StandardBundlerParam.ADD_MODULES.getID(), 477 StandardBundlerParam.LIMIT_MODULES.getID(), 478 StandardBundlerParam.FILE_ASSOCIATIONS.getID() 479 )); 480 481 @SuppressWarnings("unchecked") 482 public void addBundleArgument(String key, Object value) { 483 // special hack for multi-line arguments 484 if (multi_args.contains(key)) { 485 Object existingValue = bundlerArguments.get(key); 486 if (existingValue instanceof String && value instanceof String) { 487 bundlerArguments.put(key, existingValue + "\n\n" + value); 488 } else if (existingValue instanceof List && value instanceof List) { 489 ((List)existingValue).addAll((List)value); 490 } else if (existingValue instanceof Map && 491 value instanceof String && ((String)value).contains("=")) { 492 String[] mapValues = ((String)value).split("=", 2); 493 ((Map)existingValue).put(mapValues[0], mapValues[1]); 494 } else { 495 bundlerArguments.put(key, value); 496 } 497 } else { 498 bundlerArguments.put(key, value); 499 } 500 } 501 502 BundleParams getBundleParams() { 503 BundleParams bundleParams = new BundleParams(); 504 505 // construct app resources relative to output folder! 506 bundleParams.setAppResourcesList(resources); 507 508 bundleParams.setIdentifier(id); 509 510 bundleParams.setApplicationClass(applicationClass); 511 bundleParams.setAppVersion(version); 512 bundleParams.setType(bundleType); 513 bundleParams.setBundleFormat(targetFormat); 514 bundleParams.setVendor(vendor); 515 bundleParams.setEmail(email); 516 bundleParams.setInstalldirChooser(installdirChooser); 517 bundleParams.setCopyright(copyright); 518 bundleParams.setDescription(description); 519 bundleParams.setTitle(title); 520 521 bundleParams.setJvmargs(jvmargs); 522 bundleParams.setArguments(arguments); 523 524 if (addModules != null && !addModules.isEmpty()) { 525 bundleParams.setAddModules(addModules); 526 } 527 528 if (limitModules != null && !limitModules.isEmpty()) { 529 bundleParams.setLimitModules(limitModules); 530 } 531 532 if (modulePath != null && !modulePath.isEmpty()) { 533 bundleParams.setModulePath(modulePath); 534 } 535 536 if (module != null && !module.isEmpty()) { 537 bundleParams.setMainModule(module); 538 } 539 540 if (debugPort != null && !debugPort.isEmpty()) { 541 bundleParams.setDebug(debugPort); 542 } 543 544 Map<String, String> paramsMap = new TreeMap<>(); 545 if (params != null) { 546 for (Param p : params) { 547 paramsMap.put(p.name, p.value); 548 } 549 } 550 551 Map<String, String> unescapedHtmlParams = new TreeMap<>(); 552 Map<String, String> escapedHtmlParams = new TreeMap<>(); 553 554 // check for collisions 555 TreeSet<String> keys = new TreeSet<>(bundlerArguments.keySet()); 556 keys.retainAll(bundleParams.getBundleParamsAsMap().keySet()); 557 558 if (!keys.isEmpty()) { 559 throw new RuntimeException("Deploy Params and Bundler Arguments " 560 + "overlap in the following values:" + keys.toString()); 561 } 562 563 bundleParams.addAllBundleParams(bundlerArguments); 564 565 return bundleParams; 566 } 567 568 Map<String, ? super Object> getBundlerArguments() { 569 return this.bundlerArguments; 570 } 571 572 void putUnlessNull(String param, Object value) { 573 if (value != null) { 574 bundlerArguments.put(param, value); 575 } 576 } 577 578 void putUnlessNullOrEmpty(String param, Map<?, ?> value) { 579 if (value != null && !value.isEmpty()) { 580 bundlerArguments.put(param, value); 581 } 582 } 583 584 void putUnlessNullOrEmpty(String param, Collection<?> value) { 585 if (value != null && !value.isEmpty()) { 586 bundlerArguments.put(param, value); 587 } 588 } 589 590 @Override 591 public String toString() { 592 return "DeployParams {" + "output: " + outdir 593 + " resources: {" + resources + "}}"; 594 } 595 596 }