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