|
1 /* |
|
2 * Copyright (c) 2015, 2018, 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 jdk.jpackage.internal.BundlerParamInfo; |
|
29 import jdk.jpackage.internal.Log; |
|
30 import jdk.jpackage.internal.RelativeFileSet; |
|
31 import jdk.jpackage.internal.IOUtils; |
|
32 import jdk.jpackage.internal.StandardBundlerParam; |
|
33 import jdk.jpackage.internal.resources.WinResources; |
|
34 import jdk.jpackage.internal.WindowsBundlerParam; |
|
35 import jdk.jpackage.internal.AbstractAppImageBuilder; |
|
36 import jdk.jpackage.internal.WindowsDefender; |
|
37 |
|
38 import java.io.File; |
|
39 import java.io.FileOutputStream; |
|
40 import java.io.FileInputStream; |
|
41 import java.io.IOException; |
|
42 import java.io.InputStream; |
|
43 import java.io.OutputStream; |
|
44 import java.io.OutputStreamWriter; |
|
45 import java.io.UncheckedIOException; |
|
46 import java.io.Writer; |
|
47 import java.io.BufferedWriter; |
|
48 import java.io.FileWriter; |
|
49 import java.nio.file.Files; |
|
50 import java.nio.file.Path; |
|
51 import java.nio.file.attribute.PosixFilePermission; |
|
52 import java.text.MessageFormat; |
|
53 import java.util.HashMap; |
|
54 import java.util.List; |
|
55 import java.util.Map; |
|
56 import java.util.Objects; |
|
57 import java.util.ResourceBundle; |
|
58 import java.util.Set; |
|
59 import java.util.concurrent.atomic.AtomicReference; |
|
60 import java.util.regex.Pattern; |
|
61 import java.util.stream.Stream; |
|
62 import jdk.jpackage.internal.Arguments; |
|
63 |
|
64 import static jdk.jpackage.internal.StandardBundlerParam.*; |
|
65 |
|
66 public class WindowsAppImageBuilder extends AbstractAppImageBuilder { |
|
67 |
|
68 private static final ResourceBundle I18N = |
|
69 ResourceBundle.getBundle( |
|
70 "jdk.jpackage.internal.resources.WindowsAppImageBuilder"); |
|
71 |
|
72 private static final String MODULES_FILENAME = |
|
73 "jdk/jpackage/internal/resources/windows.jre.list"; |
|
74 |
|
75 private final static String EXECUTABLE_NAME = "WinLauncher.exe"; |
|
76 private final static String LIBRARY_NAME = "jpackage.dll"; |
|
77 private final static String REDIST_MSVCR = "vcruntimeVS_VER.dll"; |
|
78 private final static String REDIST_MSVCP = "msvcpVS_VER.dll"; |
|
79 |
|
80 private final static String TEMPLATE_APP_ICON ="javalogo_white_48.ico"; |
|
81 |
|
82 private static final String EXECUTABLE_PROPERTIES_TEMPLATE = |
|
83 "WinLauncher.properties"; |
|
84 |
|
85 private final Path root; |
|
86 private final Path appDir; |
|
87 private final Path runtimeDir; |
|
88 private final Path mdir; |
|
89 |
|
90 private final Map<String, ? super Object> params; |
|
91 |
|
92 public static final BundlerParamInfo<File> CONFIG_ROOT = |
|
93 new WindowsBundlerParam<>( |
|
94 I18N.getString("param.config-root.name"), |
|
95 I18N.getString("param.config-root.description"), |
|
96 "configRoot", |
|
97 File.class, |
|
98 params -> { |
|
99 File imagesRoot = |
|
100 new File(BUILD_ROOT.fetchFrom(params), "windows"); |
|
101 imagesRoot.mkdirs(); |
|
102 return imagesRoot; |
|
103 }, |
|
104 (s, p) -> null); |
|
105 |
|
106 public static final BundlerParamInfo<Boolean> REBRAND_EXECUTABLE = |
|
107 new WindowsBundlerParam<>( |
|
108 I18N.getString("param.rebrand-executable.name"), |
|
109 I18N.getString("param.rebrand-executable.description"), |
|
110 "win.launcher.rebrand", |
|
111 Boolean.class, |
|
112 params -> Boolean.TRUE, |
|
113 (s, p) -> Boolean.valueOf(s)); |
|
114 |
|
115 public static final BundlerParamInfo<File> ICON_ICO = |
|
116 new StandardBundlerParam<>( |
|
117 I18N.getString("param.icon-ico.name"), |
|
118 I18N.getString("param.icon-ico.description"), |
|
119 "icon.ico", |
|
120 File.class, |
|
121 params -> { |
|
122 File f = ICON.fetchFrom(params); |
|
123 if (f != null && !f.getName().toLowerCase().endsWith(".ico")) { |
|
124 Log.error(MessageFormat.format( |
|
125 I18N.getString("message.icon-not-ico"), f)); |
|
126 return null; |
|
127 } |
|
128 return f; |
|
129 }, |
|
130 (s, p) -> new File(s)); |
|
131 |
|
132 public static final StandardBundlerParam<Boolean> CONSOLE_HINT = |
|
133 new WindowsBundlerParam<>( |
|
134 I18N.getString("param.console-hint.name"), |
|
135 I18N.getString("param.console-hint.description"), |
|
136 Arguments.CLIOptions.WIN_CONSOLE_HINT.getId(), |
|
137 Boolean.class, |
|
138 params -> false, |
|
139 // valueOf(null) is false, |
|
140 // and we actually do want null in some cases |
|
141 (s, p) -> (s == null |
|
142 || "null".equalsIgnoreCase(s)) ? true : Boolean.valueOf(s)); |
|
143 |
|
144 public WindowsAppImageBuilder(Map<String, Object> config, Path imageOutDir) |
|
145 throws IOException { |
|
146 super(config, |
|
147 imageOutDir.resolve(APP_NAME.fetchFrom(config) + "/runtime")); |
|
148 |
|
149 Objects.requireNonNull(imageOutDir); |
|
150 |
|
151 this.params = config; |
|
152 |
|
153 this.root = imageOutDir.resolve(APP_NAME.fetchFrom(params)); |
|
154 this.appDir = root.resolve("app"); |
|
155 this.runtimeDir = root.resolve("runtime"); |
|
156 this.mdir = runtimeDir.resolve("lib"); |
|
157 Files.createDirectories(appDir); |
|
158 Files.createDirectories(runtimeDir); |
|
159 } |
|
160 |
|
161 public WindowsAppImageBuilder(String jreName, Path imageOutDir) |
|
162 throws IOException { |
|
163 super(null, imageOutDir.resolve(jreName)); |
|
164 |
|
165 Objects.requireNonNull(imageOutDir); |
|
166 |
|
167 this.params = null; |
|
168 this.root = imageOutDir.resolve(jreName); |
|
169 this.appDir = null; |
|
170 this.runtimeDir = root; |
|
171 this.mdir = runtimeDir.resolve("lib"); |
|
172 Files.createDirectories(runtimeDir); |
|
173 } |
|
174 |
|
175 private Path destFile(String dir, String filename) { |
|
176 return runtimeDir.resolve(dir).resolve(filename); |
|
177 } |
|
178 |
|
179 private void writeEntry(InputStream in, Path dstFile) throws IOException { |
|
180 Files.createDirectories(dstFile.getParent()); |
|
181 Files.copy(in, dstFile); |
|
182 } |
|
183 |
|
184 private void writeSymEntry(Path dstFile, Path target) throws IOException { |
|
185 Files.createDirectories(dstFile.getParent()); |
|
186 Files.createLink(dstFile, target); |
|
187 } |
|
188 |
|
189 /** |
|
190 * chmod ugo+x file |
|
191 */ |
|
192 private void setExecutable(Path file) { |
|
193 try { |
|
194 Set<PosixFilePermission> perms = |
|
195 Files.getPosixFilePermissions(file); |
|
196 perms.add(PosixFilePermission.OWNER_EXECUTE); |
|
197 perms.add(PosixFilePermission.GROUP_EXECUTE); |
|
198 perms.add(PosixFilePermission.OTHERS_EXECUTE); |
|
199 Files.setPosixFilePermissions(file, perms); |
|
200 } catch (IOException ioe) { |
|
201 throw new UncheckedIOException(ioe); |
|
202 } |
|
203 } |
|
204 |
|
205 private static void createUtf8File(File file, String content) |
|
206 throws IOException { |
|
207 try (OutputStream fout = new FileOutputStream(file); |
|
208 Writer output = new OutputStreamWriter(fout, "UTF-8")) { |
|
209 output.write(content); |
|
210 } |
|
211 } |
|
212 |
|
213 public static String getLauncherName(Map<String, ? super Object> p) { |
|
214 return APP_FS_NAME.fetchFrom(p) + ".exe"; |
|
215 } |
|
216 |
|
217 // Returns launcher resource name for launcher we need to use. |
|
218 public static String getLauncherResourceName(Map<String, ? super Object> p) { |
|
219 if (CONSOLE_HINT.fetchFrom(p)) { |
|
220 return "papplauncherc.exe"; |
|
221 } |
|
222 |
|
223 return "papplauncher.exe"; |
|
224 } |
|
225 |
|
226 public static String getLauncherCfgName(Map<String, ? super Object> p) { |
|
227 return "app/" + APP_FS_NAME.fetchFrom(p) +".cfg"; |
|
228 } |
|
229 |
|
230 private File getConfig_AppIcon(Map<String, ? super Object> params) { |
|
231 return new File(getConfigRoot(params), |
|
232 APP_FS_NAME.fetchFrom(params) + ".ico"); |
|
233 } |
|
234 |
|
235 private File getConfig_ExecutableProperties( |
|
236 Map<String, ? super Object> params) { |
|
237 return new File(getConfigRoot(params), |
|
238 APP_FS_NAME.fetchFrom(params) + ".properties"); |
|
239 } |
|
240 |
|
241 File getConfigRoot(Map<String, ? super Object> params) { |
|
242 return CONFIG_ROOT.fetchFrom(params); |
|
243 } |
|
244 |
|
245 protected void cleanupConfigFiles(Map<String, ? super Object> params) { |
|
246 if (Log.isDebug() || Log.isVerbose()) { |
|
247 return; |
|
248 } |
|
249 |
|
250 getConfig_AppIcon(params).delete(); |
|
251 getConfig_ExecutableProperties(params).delete(); |
|
252 } |
|
253 |
|
254 @Override |
|
255 public InputStream getResourceAsStream(String name) { |
|
256 return WinResources.class.getResourceAsStream(name); |
|
257 } |
|
258 |
|
259 @Override |
|
260 public void prepareApplicationFiles() throws IOException { |
|
261 Map<String, ? super Object> originalParams = new HashMap<>(params); |
|
262 File rootFile = root.toFile(); |
|
263 if (!rootFile.isDirectory() && !rootFile.mkdirs()) { |
|
264 throw new RuntimeException(MessageFormat.format(I18N.getString( |
|
265 "error.cannot-create-output-dir"), rootFile.getAbsolutePath())); |
|
266 } |
|
267 if (!rootFile.canWrite()) { |
|
268 throw new RuntimeException(MessageFormat.format( |
|
269 I18N.getString("error.cannot-write-to-output-dir"), |
|
270 rootFile.getAbsolutePath())); |
|
271 } |
|
272 try { |
|
273 // create the .exe launchers |
|
274 createLauncherForEntryPoint(params); |
|
275 |
|
276 // copy the jars |
|
277 copyApplication(params); |
|
278 |
|
279 // copy in the needed libraries |
|
280 try (InputStream is_lib = getResourceAsStream("jpackage.dll")) { |
|
281 Files.copy(is_lib, root.resolve(LIBRARY_NAME)); |
|
282 } |
|
283 |
|
284 copyMSVCDLLs(); |
|
285 |
|
286 // create the secondary launchers, if any |
|
287 List<Map<String, ? super Object>> entryPoints = |
|
288 StandardBundlerParam.SECONDARY_LAUNCHERS.fetchFrom(params); |
|
289 for (Map<String, ? super Object> entryPoint : entryPoints) { |
|
290 Map<String, ? super Object> tmp = new HashMap<>(originalParams); |
|
291 tmp.putAll(entryPoint); |
|
292 createLauncherForEntryPoint(tmp); |
|
293 } |
|
294 } finally { |
|
295 cleanupConfigFiles(params); |
|
296 } |
|
297 } |
|
298 |
|
299 @Override |
|
300 public void prepareServerJreFiles() throws IOException {} |
|
301 |
|
302 private void copyMSVCDLLs() throws IOException { |
|
303 AtomicReference<IOException> ioe = new AtomicReference<>(); |
|
304 try (Stream<Path> files = Files.list(runtimeDir.resolve("bin"))) { |
|
305 files.filter(p -> Pattern.matches( |
|
306 "^(vcruntime|msvcp|msvcr|ucrtbase|api-ms-win-).*\\.dll$", |
|
307 p.toFile().getName().toLowerCase())) |
|
308 .forEach(p -> { |
|
309 try { |
|
310 Files.copy(p, root.resolve((p.toFile().getName()))); |
|
311 } catch (IOException e) { |
|
312 ioe.set(e); |
|
313 } |
|
314 }); |
|
315 } |
|
316 |
|
317 IOException e = ioe.get(); |
|
318 if (e != null) { |
|
319 throw e; |
|
320 } |
|
321 } |
|
322 |
|
323 // TODO: do we still need this? |
|
324 private boolean copyMSVCDLLs(String VS_VER) throws IOException { |
|
325 final InputStream REDIST_MSVCR_URL = |
|
326 WinResources.class.getResourceAsStream( |
|
327 REDIST_MSVCR.replaceAll("VS_VER", VS_VER)); |
|
328 final InputStream REDIST_MSVCP_URL = |
|
329 WinResources.class.getResourceAsStream( |
|
330 REDIST_MSVCP.replaceAll("VS_VER", VS_VER)); |
|
331 |
|
332 if (REDIST_MSVCR_URL != null && REDIST_MSVCP_URL != null) { |
|
333 Files.copy( |
|
334 REDIST_MSVCR_URL, |
|
335 root.resolve(REDIST_MSVCR.replaceAll("VS_VER", VS_VER))); |
|
336 Files.copy( |
|
337 REDIST_MSVCP_URL, |
|
338 root.resolve(REDIST_MSVCP.replaceAll("VS_VER", VS_VER))); |
|
339 return true; |
|
340 } |
|
341 |
|
342 return false; |
|
343 } |
|
344 |
|
345 private void validateValueAndPut( |
|
346 Map<String, String> data, String key, |
|
347 BundlerParamInfo<String> param, |
|
348 Map<String, ? super Object> params) { |
|
349 String value = param.fetchFrom(params); |
|
350 if (value.contains("\r") || value.contains("\n")) { |
|
351 Log.error("Configuration Parameter " + param.getID() |
|
352 + " contains multiple lines of text, ignore it"); |
|
353 data.put(key, ""); |
|
354 return; |
|
355 } |
|
356 data.put(key, value); |
|
357 } |
|
358 |
|
359 protected void prepareExecutableProperties( |
|
360 Map<String, ? super Object> params) throws IOException { |
|
361 Map<String, String> data = new HashMap<>(); |
|
362 |
|
363 // mapping Java parameters in strings for version resource |
|
364 data.put("COMMENTS", ""); |
|
365 validateValueAndPut(data, "COMPANY_NAME", VENDOR, params); |
|
366 validateValueAndPut(data, "FILE_DESCRIPTION", DESCRIPTION, params); |
|
367 validateValueAndPut(data, "FILE_VERSION", VERSION, params); |
|
368 data.put("INTERNAL_NAME", getLauncherName(params)); |
|
369 validateValueAndPut(data, "LEGAL_COPYRIGHT", COPYRIGHT, params); |
|
370 data.put("LEGAL_TRADEMARK", ""); |
|
371 data.put("ORIGINAL_FILENAME", getLauncherName(params)); |
|
372 data.put("PRIVATE_BUILD", ""); |
|
373 validateValueAndPut(data, "PRODUCT_NAME", APP_NAME, params); |
|
374 validateValueAndPut(data, "PRODUCT_VERSION", VERSION, params); |
|
375 data.put("SPECIAL_BUILD", ""); |
|
376 |
|
377 Writer w = new BufferedWriter( |
|
378 new FileWriter(getConfig_ExecutableProperties(params))); |
|
379 String content = preprocessTextResource(BUNDLER_PREFIX |
|
380 + getConfig_ExecutableProperties(params).getName(), |
|
381 I18N.getString("resource.executable-properties-template"), |
|
382 EXECUTABLE_PROPERTIES_TEMPLATE, data, |
|
383 VERBOSE.fetchFrom(params), |
|
384 DROP_IN_RESOURCES_ROOT.fetchFrom(params)); |
|
385 w.write(content); |
|
386 w.close(); |
|
387 } |
|
388 |
|
389 private void createLauncherForEntryPoint( |
|
390 Map<String, ? super Object> p) throws IOException { |
|
391 |
|
392 File launcherIcon = ICON_ICO.fetchFrom(p); |
|
393 File icon = launcherIcon != null ? |
|
394 launcherIcon : ICON_ICO.fetchFrom(params); |
|
395 File iconTarget = getConfig_AppIcon(p); |
|
396 |
|
397 InputStream in = locateResource( |
|
398 "package/windows/" + APP_NAME.fetchFrom(params) + ".ico", |
|
399 "icon", |
|
400 TEMPLATE_APP_ICON, |
|
401 icon, |
|
402 VERBOSE.fetchFrom(params), |
|
403 DROP_IN_RESOURCES_ROOT.fetchFrom(params)); |
|
404 Files.copy(in, iconTarget.toPath()); |
|
405 |
|
406 writeCfgFile(p, root.resolve( |
|
407 getLauncherCfgName(p)).toFile(), "$APPDIR\\runtime"); |
|
408 |
|
409 prepareExecutableProperties(p); |
|
410 |
|
411 // Copy executable root folder |
|
412 Path executableFile = root.resolve(getLauncherName(p)); |
|
413 try (InputStream is_launcher = |
|
414 getResourceAsStream(getLauncherResourceName(p))) { |
|
415 writeEntry(is_launcher, executableFile); |
|
416 } |
|
417 |
|
418 File launcher = executableFile.toFile(); |
|
419 launcher.setWritable(true, true); |
|
420 |
|
421 // Update branding of EXE file |
|
422 if (REBRAND_EXECUTABLE.fetchFrom(p)) { |
|
423 File tool = new File( |
|
424 System.getProperty("java.home") + "\\bin\\jpackage.exe"); |
|
425 |
|
426 // Run tool on launcher file to change the icon and the metadata. |
|
427 try { |
|
428 if (WindowsDefender.isThereAPotentialWindowsDefenderIssue()) { |
|
429 Log.error(MessageFormat.format(I18N.getString( |
|
430 "message.potential.windows.defender.issue"), |
|
431 WindowsDefender.getUserTempDirectory())); |
|
432 } |
|
433 |
|
434 launcher.setWritable(true); |
|
435 |
|
436 if (iconTarget.exists()) { |
|
437 ProcessBuilder pb = new ProcessBuilder( |
|
438 tool.getAbsolutePath(), |
|
439 "--icon-swap", |
|
440 iconTarget.getAbsolutePath(), |
|
441 launcher.getAbsolutePath()); |
|
442 IOUtils.exec(pb, false); |
|
443 } |
|
444 |
|
445 File executableProperties = getConfig_ExecutableProperties(p); |
|
446 |
|
447 if (executableProperties.exists()) { |
|
448 ProcessBuilder pb = new ProcessBuilder( |
|
449 tool.getAbsolutePath(), |
|
450 "--version-swap", |
|
451 executableProperties.getAbsolutePath(), |
|
452 launcher.getAbsolutePath()); |
|
453 IOUtils.exec(pb, false); |
|
454 } |
|
455 } |
|
456 finally { |
|
457 executableFile.toFile().setReadOnly(); |
|
458 } |
|
459 } |
|
460 |
|
461 Files.copy(iconTarget.toPath(), |
|
462 root.resolve(APP_NAME.fetchFrom(p) + ".ico")); |
|
463 } |
|
464 |
|
465 private void copyApplication(Map<String, ? super Object> params) |
|
466 throws IOException { |
|
467 List<RelativeFileSet> appResourcesList = |
|
468 APP_RESOURCES_LIST.fetchFrom(params); |
|
469 if (appResourcesList == null) { |
|
470 throw new RuntimeException("Null app resources?"); |
|
471 } |
|
472 for (RelativeFileSet appResources : appResourcesList) { |
|
473 if (appResources == null) { |
|
474 throw new RuntimeException("Null app resources?"); |
|
475 } |
|
476 File srcdir = appResources.getBaseDirectory(); |
|
477 for (String fname : appResources.getIncludedFiles()) { |
|
478 copyEntry(appDir, srcdir, fname); |
|
479 } |
|
480 } |
|
481 } |
|
482 |
|
483 @Override |
|
484 public String getPlatformSpecificModulesFile() { |
|
485 return MODULES_FILENAME; |
|
486 } |
|
487 |
|
488 } |