1 /* 2 * Copyright (c) 2015, 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.io.FileOutputStream; 30 import java.io.IOException; 31 import java.io.InputStream; 32 import java.io.OutputStream; 33 import java.io.OutputStreamWriter; 34 import java.io.UncheckedIOException; 35 import java.io.Writer; 36 import java.io.BufferedWriter; 37 import java.io.FileWriter; 38 import java.nio.charset.StandardCharsets; 39 import java.nio.file.Files; 40 import java.nio.file.Path; 41 import java.nio.file.StandardCopyOption; 42 import java.nio.file.attribute.PosixFilePermission; 43 import java.text.MessageFormat; 44 import java.util.HashMap; 45 import java.util.List; 46 import java.util.Map; 47 import java.util.Objects; 48 import java.util.ResourceBundle; 49 import java.util.Set; 50 import java.util.concurrent.atomic.AtomicReference; 51 import java.util.regex.Pattern; 52 import java.util.stream.Stream; 53 54 import static jdk.jpackage.internal.StandardBundlerParam.*; 55 56 public class WindowsAppImageBuilder extends AbstractAppImageBuilder { 57 58 static { 59 System.loadLibrary("jpackage"); 60 } 61 62 private static final ResourceBundle I18N = ResourceBundle.getBundle( 63 "jdk.jpackage.internal.resources.WinResources"); 64 65 private final static String LIBRARY_NAME = "applauncher.dll"; 66 private final static String REDIST_MSVCR = "vcruntimeVS_VER.dll"; 67 private final static String REDIST_MSVCP = "msvcpVS_VER.dll"; 68 69 private final static String TEMPLATE_APP_ICON ="javalogo_white_48.ico"; 70 71 private static final String EXECUTABLE_PROPERTIES_TEMPLATE = 72 "WinLauncher.template"; 73 74 private final Path root; 75 private final Path appDir; 76 private final Path appModsDir; 77 private final Path runtimeDir; 78 private final Path mdir; 79 private final Path binDir; 80 81 private final Map<String, ? super Object> params; 82 83 public static final BundlerParamInfo<Boolean> REBRAND_EXECUTABLE = 84 new WindowsBundlerParam<>( 85 "win.launcher.rebrand", 86 Boolean.class, 87 params -> Boolean.TRUE, 88 (s, p) -> Boolean.valueOf(s)); 89 90 public static final BundlerParamInfo<File> ICON_ICO = 91 new StandardBundlerParam<>( 92 "icon.ico", 93 File.class, 94 params -> { 95 File f = ICON.fetchFrom(params); 96 if (f != null && !f.getName().toLowerCase().endsWith(".ico")) { 97 Log.error(MessageFormat.format( 98 I18N.getString("message.icon-not-ico"), f)); 99 return null; 100 } 101 return f; 102 }, 103 (s, p) -> new File(s)); 104 105 public static final StandardBundlerParam<Boolean> CONSOLE_HINT = 106 new WindowsBundlerParam<>( 107 Arguments.CLIOptions.WIN_CONSOLE_HINT.getId(), 108 Boolean.class, 109 params -> false, 110 // valueOf(null) is false, 111 // and we actually do want null in some cases 112 (s, p) -> (s == null 113 || "null".equalsIgnoreCase(s)) ? true : Boolean.valueOf(s)); 114 115 public WindowsAppImageBuilder(Map<String, Object> config, Path imageOutDir) 116 throws IOException { 117 super(config, 118 imageOutDir.resolve(APP_NAME.fetchFrom(config) + "/runtime")); 119 120 Objects.requireNonNull(imageOutDir); 121 122 this.params = config; 123 124 this.root = imageOutDir.resolve(APP_NAME.fetchFrom(params)); 125 this.appDir = root.resolve("app"); 126 this.appModsDir = appDir.resolve("mods"); 127 this.runtimeDir = root.resolve("runtime"); 128 this.mdir = runtimeDir.resolve("lib"); 129 this.binDir = root.resolve("bin"); 130 Files.createDirectories(appDir); 131 Files.createDirectories(runtimeDir); 132 } 133 134 public WindowsAppImageBuilder(String jreName, Path imageOutDir) 135 throws IOException { 136 super(null, imageOutDir.resolve(jreName)); 137 138 Objects.requireNonNull(imageOutDir); 139 140 this.params = null; 141 this.root = imageOutDir.resolve(jreName); 142 this.appDir = null; 143 this.appModsDir = null; 144 this.runtimeDir = root; 145 this.mdir = runtimeDir.resolve("lib"); 146 this.binDir = null; 147 Files.createDirectories(runtimeDir); 148 } 149 150 private void writeEntry(InputStream in, Path dstFile) throws IOException { 151 Files.createDirectories(dstFile.getParent()); 152 Files.copy(in, dstFile); 153 } 154 155 private static String getLauncherName(Map<String, ? super Object> params) { 156 return APP_NAME.fetchFrom(params) + ".exe"; 157 } 158 159 // Returns launcher resource name for launcher we need to use. 160 public static String getLauncherResourceName( 161 Map<String, ? super Object> params) { 162 if (CONSOLE_HINT.fetchFrom(params)) { 163 return "jpackageapplauncher.exe"; 164 } else { 165 return "jpackageapplauncherw.exe"; 166 } 167 } 168 169 public static String getLauncherCfgName( 170 Map<String, ? super Object> params) { 171 return "app/" + APP_NAME.fetchFrom(params) +".cfg"; 172 } 173 174 private File getConfig_AppIcon(Map<String, ? super Object> params) { 175 return new File(getConfigRoot(params), 176 APP_NAME.fetchFrom(params) + ".ico"); 177 } 178 179 private File getConfig_ExecutableProperties( 180 Map<String, ? super Object> params) { 181 return new File(getConfigRoot(params), 182 APP_NAME.fetchFrom(params) + ".properties"); 183 } 184 185 File getConfigRoot(Map<String, ? super Object> params) { 186 return CONFIG_ROOT.fetchFrom(params); 187 } 188 189 @Override 190 public Path getAppDir() { 191 return appDir; 192 } 193 194 @Override 195 public Path getAppModsDir() { 196 return appModsDir; 197 } 198 199 @Override 200 public void prepareApplicationFiles() throws IOException { 201 Map<String, ? super Object> originalParams = new HashMap<>(params); 202 203 try { 204 IOUtils.writableOutputDir(root); 205 IOUtils.writableOutputDir(binDir); 206 } catch (PackagerException pe) { 207 throw new RuntimeException(pe); 208 } 209 210 // create the .exe launchers 211 createLauncherForEntryPoint(params); 212 213 // copy the jars 214 copyApplication(params); 215 216 // copy in the needed libraries 217 try (InputStream is_lib = getResourceAsStream(LIBRARY_NAME)) { 218 Files.copy(is_lib, binDir.resolve(LIBRARY_NAME)); 219 } 220 221 copyMSVCDLLs(); 222 223 // create the additional launcher(s), if any 224 List<Map<String, ? super Object>> entryPoints = 225 StandardBundlerParam.ADD_LAUNCHERS.fetchFrom(params); 226 for (Map<String, ? super Object> entryPoint : entryPoints) { 227 createLauncherForEntryPoint( 228 AddLauncherArguments.merge(originalParams, entryPoint)); 229 } 230 } 231 232 @Override 233 public void prepareJreFiles() throws IOException {} 234 235 private void copyMSVCDLLs() throws IOException { 236 AtomicReference<IOException> ioe = new AtomicReference<>(); 237 try (Stream<Path> files = Files.list(runtimeDir.resolve("bin"))) { 238 files.filter(p -> Pattern.matches( 239 "^(vcruntime|msvcp|msvcr|ucrtbase|api-ms-win-).*\\.dll$", 240 p.toFile().getName().toLowerCase())) 241 .forEach(p -> { 242 try { 243 Files.copy(p, binDir.resolve((p.toFile().getName()))); 244 } catch (IOException e) { 245 ioe.set(e); 246 } 247 }); 248 } 249 250 IOException e = ioe.get(); 251 if (e != null) { 252 throw e; 253 } 254 } 255 256 private void validateValueAndPut( 257 Map<String, String> data, String key, 258 BundlerParamInfo<String> param, 259 Map<String, ? super Object> params) { 260 String value = param.fetchFrom(params); 261 if (value.contains("\r") || value.contains("\n")) { 262 Log.error("Configuration Parameter " + param.getID() 263 + " contains multiple lines of text, ignore it"); 264 data.put(key, ""); 265 return; 266 } 267 data.put(key, value); 268 } 269 270 protected void prepareExecutableProperties( 271 Map<String, ? super Object> params) throws IOException { 272 Map<String, String> data = new HashMap<>(); 273 274 // mapping Java parameters in strings for version resource 275 validateValueAndPut(data, "COMPANY_NAME", VENDOR, params); 276 validateValueAndPut(data, "FILE_DESCRIPTION", DESCRIPTION, params); 277 validateValueAndPut(data, "FILE_VERSION", VERSION, params); 278 data.put("INTERNAL_NAME", getLauncherName(params)); 279 validateValueAndPut(data, "LEGAL_COPYRIGHT", COPYRIGHT, params); 280 data.put("ORIGINAL_FILENAME", getLauncherName(params)); 281 validateValueAndPut(data, "PRODUCT_NAME", APP_NAME, params); 282 validateValueAndPut(data, "PRODUCT_VERSION", VERSION, params); 283 284 try (Writer w = Files.newBufferedWriter( 285 getConfig_ExecutableProperties(params).toPath(), 286 StandardCharsets.UTF_8)) { 287 String content = preprocessTextResource( 288 getConfig_ExecutableProperties(params).getName(), 289 I18N.getString("resource.executable-properties-template"), 290 EXECUTABLE_PROPERTIES_TEMPLATE, data, 291 VERBOSE.fetchFrom(params), 292 RESOURCE_DIR.fetchFrom(params)); 293 w.write(content); 294 } 295 } 296 297 private void createLauncherForEntryPoint( 298 Map<String, ? super Object> params) throws IOException { 299 300 File launcherIcon = ICON_ICO.fetchFrom(params); 301 File icon = launcherIcon != null ? 302 launcherIcon : ICON_ICO.fetchFrom(params); 303 File iconTarget = getConfig_AppIcon(params); 304 305 InputStream in = locateResource( 306 APP_NAME.fetchFrom(params) + ".ico", 307 "icon", 308 TEMPLATE_APP_ICON, 309 icon, 310 VERBOSE.fetchFrom(params), 311 RESOURCE_DIR.fetchFrom(params)); 312 313 Files.copy(in, iconTarget.toPath(), 314 StandardCopyOption.REPLACE_EXISTING); 315 316 writeCfgFile(params, root.resolve( 317 getLauncherCfgName(params)).toFile()); 318 319 prepareExecutableProperties(params); 320 321 // Copy executable to bin folder 322 Path executableFile = binDir.resolve(getLauncherName(params)); 323 324 try (InputStream is_launcher = 325 getResourceAsStream(getLauncherResourceName(params))) { 326 writeEntry(is_launcher, executableFile); 327 } 328 329 File launcher = executableFile.toFile(); 330 launcher.setWritable(true, true); 331 332 // Update branding of EXE file 333 if (REBRAND_EXECUTABLE.fetchFrom(params)) { 334 try { 335 String tempDirectory = WindowsDefender.getUserTempDirectory(); 336 if (Arguments.CLIOptions.context().userProvidedBuildRoot) { 337 tempDirectory = 338 TEMP_ROOT.fetchFrom(params).getAbsolutePath(); 339 } 340 if (WindowsDefender.isThereAPotentialWindowsDefenderIssue( 341 tempDirectory)) { 342 Log.error(MessageFormat.format(I18N.getString( 343 "message.potential.windows.defender.issue"), 344 tempDirectory)); 345 } 346 347 launcher.setWritable(true); 348 349 if (iconTarget.exists()) { 350 iconSwap(iconTarget.getAbsolutePath(), 351 launcher.getAbsolutePath()); 352 } 353 354 File executableProperties = 355 getConfig_ExecutableProperties(params); 356 357 if (executableProperties.exists()) { 358 if (versionSwap(executableProperties.getAbsolutePath(), 359 launcher.getAbsolutePath()) != 0) { 360 throw new RuntimeException(MessageFormat.format( 361 I18N.getString("error.version-swap"), 362 executableProperties.getAbsolutePath())); 363 } 364 } 365 } finally { 366 executableFile.toFile().setExecutable(true); 367 executableFile.toFile().setReadOnly(); 368 } 369 } 370 371 Files.copy(iconTarget.toPath(), 372 binDir.resolve(APP_NAME.fetchFrom(params) + ".ico")); 373 } 374 375 private void copyApplication(Map<String, ? super Object> params) 376 throws IOException { 377 List<RelativeFileSet> appResourcesList = 378 APP_RESOURCES_LIST.fetchFrom(params); 379 if (appResourcesList == null) { 380 throw new RuntimeException("Null app resources?"); 381 } 382 for (RelativeFileSet appResources : appResourcesList) { 383 if (appResources == null) { 384 throw new RuntimeException("Null app resources?"); 385 } 386 File srcdir = appResources.getBaseDirectory(); 387 for (String fname : appResources.getIncludedFiles()) { 388 copyEntry(appDir, srcdir, fname); 389 } 390 } 391 } 392 393 private static native int iconSwap(String iconTarget, String launcher); 394 395 private static native int versionSwap(String executableProperties, String launcher); 396 397 }