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.incubator.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 targetFormat = null; // means default type for this platform 56 57 File outdir = null; 58 59 // raw arguments to the bundler 60 Map<String, ? super Object> bundlerArguments = new LinkedHashMap<>(); 61 62 public void setOutput(File output) { 63 outdir = output; 64 } 65 66 static class Template { 67 File in; 68 File out; 69 70 Template(File in, File out) { 71 this.in = in; 72 this.out = out; 73 } 74 } 75 76 // we need to expand as in some cases 77 // (most notably jpackage) 78 // we may get "." as filename and assumption is we include 79 // everything in the given folder 80 // (IOUtils.copyfiles() have recursive behavior) 81 List<File> expandFileset(File root) { 82 List<File> files = new LinkedList<>(); 83 if (!Files.isSymbolicLink(root.toPath())) { 84 if (root.isDirectory()) { 85 File[] children = root.listFiles(); 86 if (children != null) { 87 for (File f : children) { 88 files.addAll(expandFileset(f)); 89 } 90 } 91 } else { 92 files.add(root); 93 } 94 } 95 return files; 96 } 97 98 public void addResource(File baseDir, String path) { 99 addResource(baseDir, new File(baseDir, path)); 100 } 101 102 public void addResource(File baseDir, File file) { 103 // normalize initial file 104 // to strip things like "." in the path 105 // or it can confuse symlink detection logic 106 file = file.getAbsoluteFile(); 107 108 if (baseDir == null) { 109 baseDir = file.getParentFile(); 110 } 111 resources.add(new RelativeFileSet( 112 baseDir, new LinkedHashSet<>(expandFileset(file)))); 113 } 114 115 void setClasspath(String mainJarPath) { 116 String classpath; 117 // we want main jar first on the classpath 118 if (mainJarPath != null) { 119 classpath = mainJarPath + File.pathSeparator; 120 } else { 121 classpath = ""; 122 } 123 for (RelativeFileSet resource : resources) { 124 for (String file : resource.getIncludedFiles()) { 125 if (file.endsWith(".jar")) { 126 if (!file.equals(mainJarPath)) { 127 classpath += file + File.pathSeparator; 128 } 129 } 130 } 131 } 132 addBundleArgument( 133 StandardBundlerParam.CLASSPATH.getID(), classpath); 134 } 135 136 static void validateName(String s, boolean forApp) 137 throws PackagerException { 138 139 String exceptionKey = forApp ? 140 "ERR_InvalidAppName" : "ERR_InvalidSLName"; 141 142 if (s == null) { 143 if (forApp) { 144 return; 145 } else { 146 throw new PackagerException(exceptionKey, s); 147 } 148 } 149 if (s.length() == 0 || s.charAt(s.length() - 1) == '\\') { 150 throw new PackagerException(exceptionKey, s); 151 } 152 try { 153 // name must be valid path element for this file system 154 Path p = (new File(s)).toPath(); 155 // and it must be a single name element in a path 156 if (p.getNameCount() != 1) { 157 throw new PackagerException(exceptionKey, s); 158 } 159 } catch (InvalidPathException ipe) { 160 throw new PackagerException(ipe, exceptionKey, s); 161 } 162 163 for (int i = 0; i < s.length(); i++) { 164 char a = s.charAt(i); 165 // We check for ASCII codes first which we accept. If check fails, 166 // check if it is acceptable extended ASCII or unicode character. 167 if (a < ' ' || a > '~') { 168 // Accept anything else including special chars like copyright 169 // symbols. Note: space will be included by ASCII check above, 170 // but other whitespace like tabs or new line will be rejected. 171 if (Character.isISOControl(a) || 172 Character.isWhitespace(a)) { 173 throw new PackagerException(exceptionKey, s); 174 } 175 } else if (a == '"' || a == '%') { 176 throw new PackagerException(exceptionKey, s); 177 } 178 } 179 } 180 181 public void validate() throws PackagerException { 182 boolean hasModule = (bundlerArguments.get( 183 Arguments.CLIOptions.MODULE.getId()) != null); 184 boolean hasAppImage = (bundlerArguments.get( 185 Arguments.CLIOptions.PREDEFINED_APP_IMAGE.getId()) != null); 186 boolean hasClass = (bundlerArguments.get( 187 Arguments.CLIOptions.APPCLASS.getId()) != null); 188 boolean hasMain = (bundlerArguments.get( 189 Arguments.CLIOptions.MAIN_JAR.getId()) != null); 190 boolean hasRuntimeImage = (bundlerArguments.get( 191 Arguments.CLIOptions.PREDEFINED_RUNTIME_IMAGE.getId()) != null); 192 boolean hasInput = (bundlerArguments.get( 193 Arguments.CLIOptions.INPUT.getId()) != null); 194 boolean hasModulePath = (bundlerArguments.get( 195 Arguments.CLIOptions.MODULE_PATH.getId()) != null); 196 boolean runtimeInstaller = !isTargetAppImage() && 197 !hasAppImage && !hasModule && !hasMain && hasRuntimeImage; 198 199 if (isTargetAppImage()) { 200 // Module application requires --runtime-image or --module-path 201 if (hasModule) { 202 if (!hasModulePath && !hasRuntimeImage) { 203 throw new PackagerException("ERR_MissingArgument", 204 "--runtime-image or --module-path"); 205 } 206 } else { 207 if (!hasInput) { 208 throw new PackagerException( 209 "ERR_MissingArgument", "--input"); 210 } 211 } 212 } else { 213 if (!runtimeInstaller) { 214 if (hasModule) { 215 if (!hasModulePath && !hasRuntimeImage && !hasAppImage) { 216 throw new PackagerException("ERR_MissingArgument", 217 "--runtime-image, --module-path or --app-image"); 218 } 219 } else { 220 if (!hasInput && !hasAppImage) { 221 throw new PackagerException("ERR_MissingArgument", 222 "--input or --app-image"); 223 } 224 } 225 } 226 } 227 228 // if bundling non-modular image, or installer without app-image 229 // then we need some resources and a main class 230 if (!hasModule && !hasAppImage && !runtimeInstaller) { 231 if (resources.isEmpty()) { 232 throw new PackagerException("ERR_MissingAppResources"); 233 } 234 if (!hasMain) { 235 throw new PackagerException("ERR_MissingArgument", 236 "--main-jar"); 237 } 238 } 239 240 String name = (String)bundlerArguments.get( 241 Arguments.CLIOptions.NAME.getId()); 242 validateName(name, true); 243 244 // Validate app image if set 245 String appImage = (String)bundlerArguments.get( 246 Arguments.CLIOptions.PREDEFINED_APP_IMAGE.getId()); 247 if (appImage != null) { 248 File appImageDir = new File(appImage); 249 if (!appImageDir.exists() || appImageDir.list().length == 0) { 250 throw new PackagerException("ERR_AppImageNotExist", appImage); 251 } 252 } 253 254 // Validate temp dir 255 String root = (String)bundlerArguments.get( 256 Arguments.CLIOptions.TEMP_ROOT.getId()); 257 if (root != null) { 258 String [] contents = (new File(root)).list(); 259 260 if (contents != null && contents.length > 0) { 261 throw new PackagerException("ERR_BuildRootInvalid", root); 262 } 263 } 264 265 // Validate resource dir 266 String resources = (String)bundlerArguments.get( 267 Arguments.CLIOptions.RESOURCE_DIR.getId()); 268 if (resources != null) { 269 if (!(new File(resources)).exists()) { 270 throw new PackagerException( 271 "message.resource-dir-does-not-exist", 272 Arguments.CLIOptions.RESOURCE_DIR.getId(), resources); 273 } 274 } 275 276 // Validate predefined runtime dir 277 String runtime = (String)bundlerArguments.get( 278 Arguments.CLIOptions.PREDEFINED_RUNTIME_IMAGE.getId()); 279 if (runtime != null) { 280 if (!(new File(runtime)).exists()) { 281 throw new PackagerException( 282 "message.runtime-image-dir-does-not-exist", 283 Arguments.CLIOptions.PREDEFINED_RUNTIME_IMAGE.getId(), 284 runtime); 285 } 286 } 287 288 289 290 // Validate license file if set 291 String license = (String)bundlerArguments.get( 292 Arguments.CLIOptions.LICENSE_FILE.getId()); 293 if (license != null) { 294 File licenseFile = new File(license); 295 if (!licenseFile.exists()) { 296 throw new PackagerException("ERR_LicenseFileNotExit"); 297 } 298 } 299 } 300 301 void setTargetFormat(String t) { 302 targetFormat = t; 303 } 304 305 String getTargetFormat() { 306 return targetFormat; 307 } 308 309 boolean isTargetAppImage() { 310 return ("app-image".equals(targetFormat)); 311 } 312 313 private static final Set<String> multi_args = new TreeSet<>(Arrays.asList( 314 StandardBundlerParam.JAVA_OPTIONS.getID(), 315 StandardBundlerParam.ARGUMENTS.getID(), 316 StandardBundlerParam.MODULE_PATH.getID(), 317 StandardBundlerParam.ADD_MODULES.getID(), 318 StandardBundlerParam.LIMIT_MODULES.getID(), 319 StandardBundlerParam.FILE_ASSOCIATIONS.getID() 320 )); 321 322 @SuppressWarnings("unchecked") 323 public void addBundleArgument(String key, Object value) { 324 // special hack for multi-line arguments 325 if (multi_args.contains(key)) { 326 Object existingValue = bundlerArguments.get(key); 327 if (existingValue instanceof String && value instanceof String) { 328 String delim = "\n\n"; 329 if (key.equals(StandardBundlerParam.MODULE_PATH.getID())) { 330 delim = File.pathSeparator; 331 } else if (key.equals( 332 StandardBundlerParam.ADD_MODULES.getID())) { 333 delim = ","; 334 } 335 bundlerArguments.put(key, existingValue + delim + value); 336 } else if (existingValue instanceof List && value instanceof List) { 337 ((List)existingValue).addAll((List)value); 338 } else if (existingValue instanceof Map && 339 value instanceof String && ((String)value).contains("=")) { 340 String[] mapValues = ((String)value).split("=", 2); 341 ((Map)existingValue).put(mapValues[0], mapValues[1]); 342 } else { 343 bundlerArguments.put(key, value); 344 } 345 } else { 346 bundlerArguments.put(key, value); 347 } 348 } 349 350 BundleParams getBundleParams() { 351 BundleParams bundleParams = new BundleParams(); 352 353 // construct app resources relative to destination folder! 354 bundleParams.setAppResourcesList(resources); 355 356 Map<String, String> unescapedHtmlParams = new TreeMap<>(); 357 Map<String, String> escapedHtmlParams = new TreeMap<>(); 358 359 // check for collisions 360 TreeSet<String> keys = new TreeSet<>(bundlerArguments.keySet()); 361 keys.retainAll(bundleParams.getBundleParamsAsMap().keySet()); 362 363 if (!keys.isEmpty()) { 364 throw new RuntimeException("Deploy Params and Bundler Arguments " 365 + "overlap in the following values:" + keys.toString()); 366 } 367 368 bundleParams.addAllBundleParams(bundlerArguments); 369 370 return bundleParams; 371 } 372 373 @Override 374 public String toString() { 375 return "DeployParams {" + "output: " + outdir 376 + " resources: {" + resources + "}}"; 377 } 378 379 }