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