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() { 116 String classpath = ""; 117 for (RelativeFileSet resource : resources) { 118 for (String file : resource.getIncludedFiles()) { 119 if (file.endsWith(".jar")) { 120 classpath += file + File.pathSeparator; 121 } 122 } 123 } 124 addBundleArgument( 125 StandardBundlerParam.CLASSPATH.getID(), classpath); 126 } 127 128 static void validateName(String s, boolean forApp) 129 throws PackagerException { 130 131 String exceptionKey = forApp ? 132 "ERR_InvalidAppName" : "ERR_InvalidSLName"; 133 134 if (s == null) { 135 if (forApp) { 136 return; 137 } else { 138 throw new PackagerException(exceptionKey, s); 139 } 140 } 141 if (s.length() == 0 || s.charAt(s.length() - 1) == '\\') { 142 throw new PackagerException(exceptionKey, s); 143 } 144 try { 145 // name must be valid path element for this file system 146 Path p = (new File(s)).toPath(); 147 // and it must be a single name element in a path 148 if (p.getNameCount() != 1) { 149 throw new PackagerException(exceptionKey, s); 150 } 151 } catch (InvalidPathException ipe) { 152 throw new PackagerException(ipe, exceptionKey, s); 153 } 154 155 for (int i = 0; i < s.length(); i++) { 156 char a = s.charAt(i); 157 // We check for ASCII codes first which we accept. If check fails, 158 // check if it is acceptable extended ASCII or unicode character. 159 if (a < ' ' || a > '~') { 160 // Accept anything else including special chars like copyright 161 // symbols. Note: space will be included by ASCII check above, 162 // but other whitespace like tabs or new line will be rejected. 163 if (Character.isISOControl(a) || 164 Character.isWhitespace(a)) { 165 throw new PackagerException(exceptionKey, s); 166 } 167 } else if (a == '"' || a == '%') { 168 throw new PackagerException(exceptionKey, s); 169 } 170 } 171 } 172 173 public void validate() throws PackagerException { 174 if (outdir == null) { 175 throw new PackagerException("ERR_MissingArgument", "--output"); 176 } 177 178 boolean hasModule = (bundlerArguments.get( 179 Arguments.CLIOptions.MODULE.getId()) != null); 180 boolean hasAppImage = (bundlerArguments.get( 181 Arguments.CLIOptions.PREDEFINED_APP_IMAGE.getId()) != null); 182 boolean hasClass = (bundlerArguments.get( 183 Arguments.CLIOptions.APPCLASS.getId()) != null); 184 boolean hasMain = (bundlerArguments.get( 185 Arguments.CLIOptions.MAIN_JAR.getId()) != null); 186 boolean hasRuntimeImage = (bundlerArguments.get( 187 Arguments.CLIOptions.PREDEFINED_RUNTIME_IMAGE.getId()) != null); 188 boolean hasInput = (bundlerArguments.get( 189 Arguments.CLIOptions.INPUT.getId()) != null); 190 boolean hasModulePath = (bundlerArguments.get( 191 Arguments.CLIOptions.MODULE_PATH.getId()) != null); 192 boolean runtimeInstaller = targetFormat != null && 193 !hasAppImage && !hasModule && !hasMain && hasRuntimeImage; 194 195 if (targetFormat == null) { 196 // Module application requires --runtime-image or --module-path 197 if (hasModule) { 198 if (!hasModulePath && !hasRuntimeImage) { 199 throw new PackagerException("ERR_MissingArgument", 200 "--runtime-image or --module-path"); 201 } 202 } else { 203 if (!hasInput) { 204 throw new PackagerException( 205 "ERR_MissingArgument", "--input"); 206 } 207 } 208 } else { 209 if (!runtimeInstaller) { 210 if (hasModule) { 211 if (!hasModulePath && !hasRuntimeImage && !hasAppImage) { 212 throw new PackagerException("ERR_MissingArgument", 213 "--runtime-image, --module-path or --app-image"); 214 } 215 } else { 216 if (!hasInput && !hasAppImage) { 217 throw new PackagerException("ERR_MissingArgument", 218 "--input or --app-image"); 219 } 220 } 221 } 222 } 223 224 // if bundling non-modular image, or installer without app-image 225 // then we need some resources and a main class 226 if (!hasModule && !hasAppImage && !runtimeInstaller) { 227 if (resources.isEmpty()) { 228 throw new PackagerException("ERR_MissingAppResources"); 229 } 230 if (!hasMain) { 231 throw new PackagerException("ERR_MissingArgument", 232 "--main-jar"); 233 } 234 } 235 236 String name = (String)bundlerArguments.get( 237 Arguments.CLIOptions.NAME.getId()); 238 validateName(name, true); 239 240 // Validate app image if set 241 String appImage = (String)bundlerArguments.get( 242 Arguments.CLIOptions.PREDEFINED_APP_IMAGE.getId()); 243 if (appImage != null) { 244 File appImageDir = new File(appImage); 245 if (!appImageDir.exists() || appImageDir.list().length == 0) { 246 throw new PackagerException("ERR_AppImageNotExist", appImage); 247 } 248 } 249 250 // Validate temp-root 251 String root = (String)bundlerArguments.get( 252 Arguments.CLIOptions.TEMP_ROOT.getId()); 253 if (root != null) { 254 String [] contents = (new File(root)).list(); 255 256 if (contents != null && contents.length > 0) { 257 throw new PackagerException("ERR_BuildRootInvalid", root); 258 } 259 } 260 261 // Validate license file if set 262 String license = (String)bundlerArguments.get( 263 Arguments.CLIOptions.LICENSE_FILE.getId()); 264 if (license != null) { 265 File licenseFile = new File(license); 266 if (!licenseFile.exists()) { 267 throw new PackagerException("ERR_LicenseFileNotExit"); 268 } 269 } 270 } 271 272 void setTargetFormat(String t) { 273 targetFormat = t; 274 } 275 276 String getTargetFormat() { 277 return targetFormat; 278 } 279 280 private static final Set<String> multi_args = new TreeSet<>(Arrays.asList( 281 StandardBundlerParam.JAVA_OPTIONS.getID(), 282 StandardBundlerParam.ARGUMENTS.getID(), 283 StandardBundlerParam.MODULE_PATH.getID(), 284 StandardBundlerParam.ADD_MODULES.getID(), 285 StandardBundlerParam.LIMIT_MODULES.getID(), 286 StandardBundlerParam.FILE_ASSOCIATIONS.getID() 287 )); 288 289 @SuppressWarnings("unchecked") 290 public void addBundleArgument(String key, Object value) { 291 // special hack for multi-line arguments 292 if (multi_args.contains(key)) { 293 Object existingValue = bundlerArguments.get(key); 294 if (existingValue instanceof String && value instanceof String) { 295 String delim = "\n\n"; 296 if (key.equals(StandardBundlerParam.MODULE_PATH.getID())) { 297 delim = File.pathSeparator; 298 } else if (key.equals( 299 StandardBundlerParam.ADD_MODULES.getID())) { 300 delim = ","; 301 } 302 bundlerArguments.put(key, existingValue + delim + value); 303 } else if (existingValue instanceof List && value instanceof List) { 304 ((List)existingValue).addAll((List)value); 305 } else if (existingValue instanceof Map && 306 value instanceof String && ((String)value).contains("=")) { 307 String[] mapValues = ((String)value).split("=", 2); 308 ((Map)existingValue).put(mapValues[0], mapValues[1]); 309 } else { 310 bundlerArguments.put(key, value); 311 } 312 } else { 313 bundlerArguments.put(key, value); 314 } 315 } 316 317 BundleParams getBundleParams() { 318 BundleParams bundleParams = new BundleParams(); 319 320 // construct app resources relative to output folder! 321 bundleParams.setAppResourcesList(resources); 322 323 Map<String, String> unescapedHtmlParams = new TreeMap<>(); 324 Map<String, String> escapedHtmlParams = new TreeMap<>(); 325 326 // check for collisions 327 TreeSet<String> keys = new TreeSet<>(bundlerArguments.keySet()); 328 keys.retainAll(bundleParams.getBundleParamsAsMap().keySet()); 329 330 if (!keys.isEmpty()) { 331 throw new RuntimeException("Deploy Params and Bundler Arguments " 332 + "overlap in the following values:" + keys.toString()); 333 } 334 335 bundleParams.addAllBundleParams(bundlerArguments); 336 337 return bundleParams; 338 } 339 340 @Override 341 public String toString() { 342 return "DeployParams {" + "output: " + outdir 343 + " resources: {" + resources + "}}"; 344 } 345 346 }