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 targetFormat = null; // means app-image 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 if (outdir == null) { 183 throw new PackagerException("ERR_MissingArgument", "--output"); 184 } 185 186 boolean hasModule = (bundlerArguments.get( 187 Arguments.CLIOptions.MODULE.getId()) != null); 188 boolean hasAppImage = (bundlerArguments.get( 189 Arguments.CLIOptions.PREDEFINED_APP_IMAGE.getId()) != null); 190 boolean hasClass = (bundlerArguments.get( 191 Arguments.CLIOptions.APPCLASS.getId()) != null); 192 boolean hasMain = (bundlerArguments.get( 193 Arguments.CLIOptions.MAIN_JAR.getId()) != null); 194 boolean hasRuntimeImage = (bundlerArguments.get( 195 Arguments.CLIOptions.PREDEFINED_RUNTIME_IMAGE.getId()) != null); 196 boolean hasInput = (bundlerArguments.get( 197 Arguments.CLIOptions.INPUT.getId()) != null); 198 boolean hasModulePath = (bundlerArguments.get( 199 Arguments.CLIOptions.MODULE_PATH.getId()) != null); 200 boolean runtimeInstaller = targetFormat != null && 201 !hasAppImage && !hasModule && !hasMain && hasRuntimeImage; 202 203 if (targetFormat == null) { 204 // Module application requires --runtime-image or --module-path 205 if (hasModule) { 206 if (!hasModulePath && !hasRuntimeImage) { 207 throw new PackagerException("ERR_MissingArgument", 208 "--runtime-image or --module-path"); 209 } 210 } else { 211 if (!hasInput) { 212 throw new PackagerException( 213 "ERR_MissingArgument", "--input"); 214 } 215 } 216 } else { 217 if (!runtimeInstaller) { 218 if (hasModule) { 219 if (!hasModulePath && !hasRuntimeImage && !hasAppImage) { 220 throw new PackagerException("ERR_MissingArgument", 221 "--runtime-image, --module-path or --app-image"); 222 } 223 } else { 224 if (!hasInput && !hasAppImage) { 225 throw new PackagerException("ERR_MissingArgument", 226 "--input or --app-image"); 227 } 228 } 229 } 230 } 231 232 // if bundling non-modular image, or installer without app-image 233 // then we need some resources and a main class 234 if (!hasModule && !hasAppImage && !runtimeInstaller) { 235 if (resources.isEmpty()) { 236 throw new PackagerException("ERR_MissingAppResources"); 237 } 238 if (!hasMain) { 239 throw new PackagerException("ERR_MissingArgument", 240 "--main-jar"); 241 } 242 } 243 244 String name = (String)bundlerArguments.get( 245 Arguments.CLIOptions.NAME.getId()); 246 validateName(name, true); 247 248 // Validate app image if set 249 String appImage = (String)bundlerArguments.get( 250 Arguments.CLIOptions.PREDEFINED_APP_IMAGE.getId()); 251 if (appImage != null) { 252 File appImageDir = new File(appImage); 253 if (!appImageDir.exists() || appImageDir.list().length == 0) { 254 throw new PackagerException("ERR_AppImageNotExist", appImage); 255 } 256 } 257 258 // Validate temp-root 259 String root = (String)bundlerArguments.get( 260 Arguments.CLIOptions.TEMP_ROOT.getId()); 261 if (root != null) { 262 String [] contents = (new File(root)).list(); 263 264 if (contents != null && contents.length > 0) { 265 throw new PackagerException("ERR_BuildRootInvalid", root); 266 } 267 } 268 269 // Validate license file if set 270 String license = (String)bundlerArguments.get( 271 Arguments.CLIOptions.LICENSE_FILE.getId()); 272 if (license != null) { 273 File licenseFile = new File(license); 274 if (!licenseFile.exists()) { 275 throw new PackagerException("ERR_LicenseFileNotExit"); 276 } 277 } 278 } 279 280 void setTargetFormat(String t) { 281 targetFormat = t; 282 } 283 284 String getTargetFormat() { 285 return 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 output 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 }