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 license file if set 266 String license = (String)bundlerArguments.get( 267 Arguments.CLIOptions.LICENSE_FILE.getId()); 268 if (license != null) { 269 File licenseFile = new File(license); 270 if (!licenseFile.exists()) { 271 throw new PackagerException("ERR_LicenseFileNotExit"); 272 } 273 } 274 } 275 276 void setTargetFormat(String t) { 277 targetFormat = t; 278 } 279 280 String getTargetFormat() { 281 return targetFormat; 282 } 283 284 boolean isTargetAppImage() { 285 return ("app-image".equals(targetFormat)); 286 } 287 288 private static final Set<String> multi_args = new TreeSet<>(Arrays.asList( 289 StandardBundlerParam.JAVA_OPTIONS.getID(), 290 StandardBundlerParam.ARGUMENTS.getID(), 291 StandardBundlerParam.MODULE_PATH.getID(), 292 StandardBundlerParam.ADD_MODULES.getID(), 293 StandardBundlerParam.LIMIT_MODULES.getID(), 294 StandardBundlerParam.FILE_ASSOCIATIONS.getID() 295 )); 296 297 @SuppressWarnings("unchecked") 298 public void addBundleArgument(String key, Object value) { 299 // special hack for multi-line arguments 300 if (multi_args.contains(key)) { 301 Object existingValue = bundlerArguments.get(key); 302 if (existingValue instanceof String && value instanceof String) { 303 String delim = "\n\n"; 304 if (key.equals(StandardBundlerParam.MODULE_PATH.getID())) { 305 delim = File.pathSeparator; 306 } else if (key.equals( 307 StandardBundlerParam.ADD_MODULES.getID())) { 308 delim = ","; 309 } 310 bundlerArguments.put(key, existingValue + delim + value); 311 } else if (existingValue instanceof List && value instanceof List) { 312 ((List)existingValue).addAll((List)value); 313 } else if (existingValue instanceof Map && 314 value instanceof String && ((String)value).contains("=")) { 315 String[] mapValues = ((String)value).split("=", 2); 316 ((Map)existingValue).put(mapValues[0], mapValues[1]); 317 } else { 318 bundlerArguments.put(key, value); 319 } 320 } else { 321 bundlerArguments.put(key, value); 322 } 323 } 324 325 BundleParams getBundleParams() { 326 BundleParams bundleParams = new BundleParams(); 327 328 // construct app resources relative to destination folder! 329 bundleParams.setAppResourcesList(resources); 330 331 Map<String, String> unescapedHtmlParams = new TreeMap<>(); 332 Map<String, String> escapedHtmlParams = new TreeMap<>(); 333 334 // check for collisions 335 TreeSet<String> keys = new TreeSet<>(bundlerArguments.keySet()); 336 keys.retainAll(bundleParams.getBundleParamsAsMap().keySet()); 337 338 if (!keys.isEmpty()) { 339 throw new RuntimeException("Deploy Params and Bundler Arguments " 340 + "overlap in the following values:" + keys.toString()); 341 } 342 343 bundleParams.addAllBundleParams(bundlerArguments); 344 345 return bundleParams; 346 } 347 348 @Override 349 public String toString() { 350 return "DeployParams {" + "output: " + outdir 351 + " resources: {" + resources + "}}"; 352 } 353 354 }