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