author | herrick |
Wed, 16 Oct 2019 09:57:23 -0400 | |
branch | JDK-8200758-branch |
changeset 58647 | 2c43b89b1679 |
parent 58538 | 12c965587689 |
child 58670 | 6fb9e12d5595 |
permissions | -rw-r--r-- |
57038 | 1 |
/* |
57106
ea870b9ce89a
8216492: Update copyright of all new jpackage fils to 2019
kcr
parents:
57096
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
57038 | 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 |
||
57039 | 26 |
package jdk.jpackage.internal; |
57038 | 27 |
|
28 |
import java.io.File; |
|
29 |
import java.io.FileInputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
32 |
import java.io.Writer; |
|
33 |
import java.math.BigInteger; |
|
34 |
import java.nio.file.Files; |
|
35 |
import java.nio.file.Path; |
|
57096
d06bec27f8c9
8215903: modify behavior of retaining temporary output dir
herrick
parents:
57091
diff
changeset
|
36 |
import java.nio.file.StandardCopyOption; |
57038 | 37 |
import java.nio.file.attribute.PosixFilePermission; |
38 |
import java.text.MessageFormat; |
|
39 |
import java.util.ArrayList; |
|
40 |
import java.util.Arrays; |
|
41 |
import java.util.EnumSet; |
|
42 |
import java.util.HashMap; |
|
43 |
import java.util.List; |
|
44 |
import java.util.Map; |
|
45 |
import java.util.Objects; |
|
46 |
import java.util.Optional; |
|
47 |
import java.util.ResourceBundle; |
|
48 |
import java.util.Set; |
|
49 |
import java.util.concurrent.atomic.AtomicReference; |
|
50 |
import java.util.function.Consumer; |
|
58415 | 51 |
import javax.xml.parsers.DocumentBuilder; |
52 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
53 |
import javax.xml.xpath.XPath; |
|
54 |
import javax.xml.xpath.XPathConstants; |
|
55 |
import javax.xml.xpath.XPathFactory; |
|
57038 | 56 |
|
57039 | 57 |
import static jdk.jpackage.internal.StandardBundlerParam.*; |
58 |
import static jdk.jpackage.internal.MacBaseInstallerBundler.*; |
|
59 |
import static jdk.jpackage.internal.MacAppBundler.*; |
|
58647
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
60 |
import static jdk.jpackage.internal.OverridableResource.createResource; |
57038 | 61 |
|
62 |
public class MacAppImageBuilder extends AbstractAppImageBuilder { |
|
63 |
||
57059 | 64 |
private static final ResourceBundle I18N = ResourceBundle.getBundle( |
65 |
"jdk.jpackage.internal.resources.MacResources"); |
|
57038 | 66 |
|
57064
a7fdadf67a92
8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
57059
diff
changeset
|
67 |
private static final String LIBRARY_NAME = "libapplauncher.dylib"; |
57908
9a005146bc1c
8230191: Replace javacup logo with duke everywhere in jpackage
herrick
parents:
57789
diff
changeset
|
68 |
private static final String TEMPLATE_BUNDLE_ICON = "java.icns"; |
57038 | 69 |
private static final String OS_TYPE_CODE = "APPL"; |
70 |
private static final String TEMPLATE_INFO_PLIST_LITE = |
|
71 |
"Info-lite.plist.template"; |
|
72 |
private static final String TEMPLATE_RUNTIME_INFO_PLIST = |
|
73 |
"Runtime-Info.plist.template"; |
|
74 |
||
75 |
private final Path root; |
|
76 |
private final Path contentsDir; |
|
58455
0d95b41d0895
8231706: change "Java" dir to "app" dir in jpackage app image
herrick
parents:
58415
diff
changeset
|
77 |
private final Path appDir; |
57077
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
78 |
private final Path javaModsDir; |
57038 | 79 |
private final Path resourcesDir; |
80 |
private final Path macOSDir; |
|
81 |
private final Path runtimeDir; |
|
82 |
private final Path runtimeRoot; |
|
83 |
private final Path mdir; |
|
84 |
||
85 |
private static List<String> keyChains; |
|
86 |
||
87 |
public static final BundlerParamInfo<Boolean> |
|
88 |
MAC_CONFIGURE_LAUNCHER_IN_PLIST = new StandardBundlerParam<>( |
|
89 |
"mac.configure-launcher-in-plist", |
|
90 |
Boolean.class, |
|
91 |
params -> Boolean.FALSE, |
|
92 |
(s, p) -> Boolean.valueOf(s)); |
|
93 |
||
57256 | 94 |
public static final EnumeratedBundlerParam<String> MAC_CATEGORY = |
95 |
new EnumeratedBundlerParam<>( |
|
96 |
Arguments.CLIOptions.MAC_APP_STORE_CATEGORY.getId(), |
|
57038 | 97 |
String.class, |
57256 | 98 |
params -> "Unknown", |
99 |
(s, p) -> s, |
|
100 |
MacAppBundler.getMacCategories(), |
|
101 |
false //strict - for MacStoreBundler this should be strict |
|
57038 | 102 |
); |
103 |
||
104 |
public static final BundlerParamInfo<String> MAC_CF_BUNDLE_NAME = |
|
105 |
new StandardBundlerParam<>( |
|
57401
8a257decfe24
8223318: jpackage --mac-bundle-name option doesn't work
ssadetsky
parents:
57397
diff
changeset
|
106 |
Arguments.CLIOptions.MAC_BUNDLE_NAME.getId(), |
57038 | 107 |
String.class, |
108 |
params -> null, |
|
109 |
(s, p) -> s); |
|
110 |
||
111 |
public static final BundlerParamInfo<String> MAC_CF_BUNDLE_IDENTIFIER = |
|
112 |
new StandardBundlerParam<>( |
|
113 |
Arguments.CLIOptions.MAC_BUNDLE_IDENTIFIER.getId(), |
|
114 |
String.class, |
|
58415 | 115 |
params -> { |
116 |
// Get identifier from app image if user provided |
|
117 |
// app image and did not provide the identifier via CLI. |
|
118 |
String identifier = extractBundleIdentifier(params); |
|
119 |
if (identifier != null) { |
|
120 |
return identifier; |
|
121 |
} |
|
122 |
||
123 |
return IDENTIFIER.fetchFrom(params); |
|
124 |
}, |
|
57038 | 125 |
(s, p) -> s); |
126 |
||
127 |
public static final BundlerParamInfo<String> MAC_CF_BUNDLE_VERSION = |
|
128 |
new StandardBundlerParam<>( |
|
129 |
"mac.CFBundleVersion", |
|
130 |
String.class, |
|
131 |
p -> { |
|
132 |
String s = VERSION.fetchFrom(p); |
|
133 |
if (validCFBundleVersion(s)) { |
|
134 |
return s; |
|
135 |
} else { |
|
136 |
return "100"; |
|
137 |
} |
|
138 |
}, |
|
139 |
(s, p) -> s); |
|
140 |
||
141 |
public static final BundlerParamInfo<File> ICON_ICNS = |
|
142 |
new StandardBundlerParam<>( |
|
143 |
"icon.icns", |
|
144 |
File.class, |
|
145 |
params -> { |
|
146 |
File f = ICON.fetchFrom(params); |
|
147 |
if (f != null && !f.getName().toLowerCase().endsWith(".icns")) { |
|
148 |
Log.error(MessageFormat.format( |
|
149 |
I18N.getString("message.icon-not-icns"), f)); |
|
150 |
return null; |
|
151 |
} |
|
152 |
return f; |
|
153 |
}, |
|
154 |
(s, p) -> new File(s)); |
|
155 |
||
156 |
public static final StandardBundlerParam<Boolean> SIGN_BUNDLE = |
|
157 |
new StandardBundlerParam<>( |
|
158 |
Arguments.CLIOptions.MAC_SIGN.getId(), |
|
159 |
Boolean.class, |
|
160 |
params -> false, |
|
161 |
// valueOf(null) is false, we actually do want null in some cases |
|
162 |
(s, p) -> (s == null || "null".equalsIgnoreCase(s)) ? |
|
163 |
null : Boolean.valueOf(s) |
|
164 |
); |
|
165 |
||
57789 | 166 |
public MacAppImageBuilder(Map<String, Object> params, Path imageOutDir) |
57038 | 167 |
throws IOException { |
57789 | 168 |
super(params, imageOutDir.resolve(APP_NAME.fetchFrom(params) |
57333 | 169 |
+ ".app/Contents/runtime/Contents/Home")); |
57038 | 170 |
|
171 |
Objects.requireNonNull(imageOutDir); |
|
172 |
||
173 |
this.root = imageOutDir.resolve(APP_NAME.fetchFrom(params) + ".app"); |
|
174 |
this.contentsDir = root.resolve("Contents"); |
|
58455
0d95b41d0895
8231706: change "Java" dir to "app" dir in jpackage app image
herrick
parents:
58415
diff
changeset
|
175 |
this.appDir = contentsDir.resolve("app"); |
0d95b41d0895
8231706: change "Java" dir to "app" dir in jpackage app image
herrick
parents:
58415
diff
changeset
|
176 |
this.javaModsDir = appDir.resolve("mods"); |
57038 | 177 |
this.resourcesDir = contentsDir.resolve("Resources"); |
178 |
this.macOSDir = contentsDir.resolve("MacOS"); |
|
57333 | 179 |
this.runtimeDir = contentsDir.resolve("runtime"); |
57038 | 180 |
this.runtimeRoot = runtimeDir.resolve("Contents/Home"); |
181 |
this.mdir = runtimeRoot.resolve("lib"); |
|
58455
0d95b41d0895
8231706: change "Java" dir to "app" dir in jpackage app image
herrick
parents:
58415
diff
changeset
|
182 |
Files.createDirectories(appDir); |
57038 | 183 |
Files.createDirectories(resourcesDir); |
184 |
Files.createDirectories(macOSDir); |
|
185 |
Files.createDirectories(runtimeDir); |
|
186 |
} |
|
187 |
||
188 |
private void writeEntry(InputStream in, Path dstFile) throws IOException { |
|
189 |
Files.createDirectories(dstFile.getParent()); |
|
190 |
Files.copy(in, dstFile); |
|
191 |
} |
|
192 |
||
193 |
public static boolean validCFBundleVersion(String v) { |
|
194 |
// CFBundleVersion (String - iOS, OS X) specifies the build version |
|
195 |
// number of the bundle, which identifies an iteration (released or |
|
196 |
// unreleased) of the bundle. The build version number should be a |
|
197 |
// string comprised of three non-negative, period-separated integers |
|
198 |
// with the first integer being greater than zero. The string should |
|
199 |
// only contain numeric (0-9) and period (.) characters. Leading zeros |
|
200 |
// are truncated from each integer and will be ignored (that is, |
|
201 |
// 1.02.3 is equivalent to 1.2.3). This key is not localizable. |
|
202 |
||
203 |
if (v == null) { |
|
204 |
return false; |
|
205 |
} |
|
206 |
||
207 |
String p[] = v.split("\\."); |
|
208 |
if (p.length > 3 || p.length < 1) { |
|
209 |
Log.verbose(I18N.getString( |
|
210 |
"message.version-string-too-many-components")); |
|
211 |
return false; |
|
212 |
} |
|
213 |
||
214 |
try { |
|
215 |
BigInteger n = new BigInteger(p[0]); |
|
216 |
if (BigInteger.ONE.compareTo(n) > 0) { |
|
217 |
Log.verbose(I18N.getString( |
|
218 |
"message.version-string-first-number-not-zero")); |
|
219 |
return false; |
|
220 |
} |
|
221 |
if (p.length > 1) { |
|
222 |
n = new BigInteger(p[1]); |
|
223 |
if (BigInteger.ZERO.compareTo(n) > 0) { |
|
224 |
Log.verbose(I18N.getString( |
|
225 |
"message.version-string-no-negative-numbers")); |
|
226 |
return false; |
|
227 |
} |
|
228 |
} |
|
229 |
if (p.length > 2) { |
|
230 |
n = new BigInteger(p[2]); |
|
231 |
if (BigInteger.ZERO.compareTo(n) > 0) { |
|
232 |
Log.verbose(I18N.getString( |
|
233 |
"message.version-string-no-negative-numbers")); |
|
234 |
return false; |
|
235 |
} |
|
236 |
} |
|
237 |
} catch (NumberFormatException ne) { |
|
238 |
Log.verbose(I18N.getString("message.version-string-numbers-only")); |
|
239 |
Log.verbose(ne); |
|
240 |
return false; |
|
241 |
} |
|
242 |
||
243 |
return true; |
|
244 |
} |
|
245 |
||
246 |
@Override |
|
57077
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
247 |
public Path getAppDir() { |
58455
0d95b41d0895
8231706: change "Java" dir to "app" dir in jpackage app image
herrick
parents:
58415
diff
changeset
|
248 |
return appDir; |
57077
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
249 |
} |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
250 |
|
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
251 |
@Override |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
252 |
public Path getAppModsDir() { |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
253 |
return javaModsDir; |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
254 |
} |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
255 |
|
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
256 |
@Override |
57789 | 257 |
public void prepareApplicationFiles(Map<String, ? super Object> params) |
258 |
throws IOException { |
|
57038 | 259 |
Map<String, ? super Object> originalParams = new HashMap<>(params); |
260 |
// Generate PkgInfo |
|
261 |
File pkgInfoFile = new File(contentsDir.toFile(), "PkgInfo"); |
|
262 |
pkgInfoFile.createNewFile(); |
|
263 |
writePkgInfo(pkgInfoFile); |
|
264 |
||
265 |
Path executable = macOSDir.resolve(getLauncherName(params)); |
|
266 |
||
267 |
// create the main app launcher |
|
57064
a7fdadf67a92
8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
57059
diff
changeset
|
268 |
try (InputStream is_launcher = |
a7fdadf67a92
8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
57059
diff
changeset
|
269 |
getResourceAsStream("jpackageapplauncher"); |
a7fdadf67a92
8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
57059
diff
changeset
|
270 |
InputStream is_lib = getResourceAsStream(LIBRARY_NAME)) { |
57038 | 271 |
// Copy executable and library to MacOS folder |
272 |
writeEntry(is_launcher, executable); |
|
273 |
writeEntry(is_lib, macOSDir.resolve(LIBRARY_NAME)); |
|
274 |
} |
|
275 |
executable.toFile().setExecutable(true, false); |
|
276 |
// generate main app launcher config file |
|
277 |
File cfg = new File(root.toFile(), getLauncherCfgName(params)); |
|
57450
82c78b40b39d
8227058: Regressions related to no longer setting user.dir
herrick
parents:
57446
diff
changeset
|
278 |
writeCfgFile(params, cfg); |
57038 | 279 |
|
57256 | 280 |
// create additional app launcher(s) and config file(s) |
57038 | 281 |
List<Map<String, ? super Object>> entryPoints = |
57256 | 282 |
StandardBundlerParam.ADD_LAUNCHERS.fetchFrom(params); |
57038 | 283 |
for (Map<String, ? super Object> entryPoint : entryPoints) { |
57348
d3a667ff5579
JDK-8223189: Fix trailing whitespace and whitespace only file modification.
herrick
parents:
57333
diff
changeset
|
284 |
Map<String, ? super Object> tmp = |
57259
a84c657c713d
8171959: add-launcher is not working when normal jar is used for first launcher and module is used for second launcher
herrick
parents:
57256
diff
changeset
|
285 |
AddLauncherArguments.merge(originalParams, entryPoint); |
57038 | 286 |
|
57256 | 287 |
// add executable for add launcher |
288 |
Path addExecutable = macOSDir.resolve(getLauncherName(tmp)); |
|
57064
a7fdadf67a92
8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
57059
diff
changeset
|
289 |
try (InputStream is = getResourceAsStream("jpackageapplauncher");) { |
57256 | 290 |
writeEntry(is, addExecutable); |
57038 | 291 |
} |
57256 | 292 |
addExecutable.toFile().setExecutable(true, false); |
57038 | 293 |
|
57256 | 294 |
// add config file for add launcher |
57038 | 295 |
cfg = new File(root.toFile(), getLauncherCfgName(tmp)); |
57450
82c78b40b39d
8227058: Regressions related to no longer setting user.dir
herrick
parents:
57446
diff
changeset
|
296 |
writeCfgFile(tmp, cfg); |
57038 | 297 |
} |
298 |
||
299 |
// Copy class path entries to Java folder |
|
58455
0d95b41d0895
8231706: change "Java" dir to "app" dir in jpackage app image
herrick
parents:
58415
diff
changeset
|
300 |
copyClassPathEntries(appDir, params); |
57038 | 301 |
|
302 |
/*********** Take care of "config" files *******/ |
|
57096
d06bec27f8c9
8215903: modify behavior of retaining temporary output dir
herrick
parents:
57091
diff
changeset
|
303 |
|
58647
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
304 |
createResource(TEMPLATE_BUNDLE_ICON, params) |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
305 |
.setCategory("icon") |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
306 |
.setExternal(ICON_ICNS.fetchFrom(params)) |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
307 |
.saveToFile(resourcesDir.resolve(APP_NAME.fetchFrom(params) |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
308 |
+ ".icns")); |
57038 | 309 |
|
310 |
// copy file association icons |
|
311 |
for (Map<String, ? |
|
312 |
super Object> fa : FILE_ASSOCIATIONS.fetchFrom(params)) { |
|
313 |
File f = FA_ICON.fetchFrom(fa); |
|
314 |
if (f != null && f.exists()) { |
|
315 |
try (InputStream in2 = new FileInputStream(f)) { |
|
316 |
Files.copy(in2, resourcesDir.resolve(f.getName())); |
|
317 |
} |
|
318 |
||
319 |
} |
|
320 |
} |
|
321 |
||
57789 | 322 |
copyRuntimeFiles(params); |
323 |
sign(params); |
|
57038 | 324 |
} |
325 |
||
326 |
@Override |
|
57789 | 327 |
public void prepareJreFiles(Map<String, ? super Object> params) |
328 |
throws IOException { |
|
329 |
copyRuntimeFiles(params); |
|
330 |
sign(params); |
|
57038 | 331 |
} |
332 |
||
57687
c56bbf4aaf98
JDK-8224788: jpackage fails on OS X when using --runtime-image
herrick
parents:
57450
diff
changeset
|
333 |
@Override |
c56bbf4aaf98
JDK-8224788: jpackage fails on OS X when using --runtime-image
herrick
parents:
57450
diff
changeset
|
334 |
File getRuntimeImageDir(File runtimeImageTop) { |
c56bbf4aaf98
JDK-8224788: jpackage fails on OS X when using --runtime-image
herrick
parents:
57450
diff
changeset
|
335 |
File home = new File(runtimeImageTop, "Contents/Home"); |
c56bbf4aaf98
JDK-8224788: jpackage fails on OS X when using --runtime-image
herrick
parents:
57450
diff
changeset
|
336 |
return (home.exists() ? home : runtimeImageTop); |
c56bbf4aaf98
JDK-8224788: jpackage fails on OS X when using --runtime-image
herrick
parents:
57450
diff
changeset
|
337 |
} |
c56bbf4aaf98
JDK-8224788: jpackage fails on OS X when using --runtime-image
herrick
parents:
57450
diff
changeset
|
338 |
|
57789 | 339 |
private void copyRuntimeFiles(Map<String, ? super Object> params) |
340 |
throws IOException { |
|
57038 | 341 |
// Generate Info.plist |
57789 | 342 |
writeInfoPlist(contentsDir.resolve("Info.plist").toFile(), params); |
57038 | 343 |
|
344 |
// generate java runtime info.plist |
|
345 |
writeRuntimeInfoPlist( |
|
57789 | 346 |
runtimeDir.resolve("Contents/Info.plist").toFile(), params); |
57038 | 347 |
|
348 |
// copy library |
|
349 |
Path runtimeMacOSDir = Files.createDirectories( |
|
350 |
runtimeDir.resolve("Contents/MacOS")); |
|
351 |
||
352 |
// JDK 9, 10, and 11 have extra '/jli/' subdir |
|
353 |
Path jli = runtimeRoot.resolve("lib/libjli.dylib"); |
|
354 |
if (!Files.exists(jli)) { |
|
355 |
jli = runtimeRoot.resolve("lib/jli/libjli.dylib"); |
|
356 |
} |
|
357 |
||
358 |
Files.copy(jli, runtimeMacOSDir.resolve("libjli.dylib")); |
|
359 |
} |
|
360 |
||
57789 | 361 |
private void sign(Map<String, ? super Object> params) throws IOException { |
57038 | 362 |
if (Optional.ofNullable( |
363 |
SIGN_BUNDLE.fetchFrom(params)).orElse(Boolean.TRUE)) { |
|
364 |
try { |
|
365 |
addNewKeychain(params); |
|
366 |
} catch (InterruptedException e) { |
|
367 |
Log.error(e.getMessage()); |
|
368 |
} |
|
369 |
String signingIdentity = |
|
370 |
DEVELOPER_ID_APP_SIGNING_KEY.fetchFrom(params); |
|
371 |
if (signingIdentity != null) { |
|
372 |
signAppBundle(params, root, signingIdentity, |
|
373 |
BUNDLE_ID_SIGNING_PREFIX.fetchFrom(params), null, null); |
|
374 |
} |
|
375 |
restoreKeychainList(params); |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
private String getLauncherName(Map<String, ? super Object> params) { |
|
380 |
if (APP_NAME.fetchFrom(params) != null) { |
|
381 |
return APP_NAME.fetchFrom(params); |
|
382 |
} else { |
|
383 |
return MAIN_CLASS.fetchFrom(params); |
|
384 |
} |
|
385 |
} |
|
386 |
||
57396
3944e4c2f779
8223586: remove jpackage dead code and other cleanup
herrick
parents:
57392
diff
changeset
|
387 |
public static String getLauncherCfgName( |
3944e4c2f779
8223586: remove jpackage dead code and other cleanup
herrick
parents:
57392
diff
changeset
|
388 |
Map<String, ? super Object> params) { |
58455
0d95b41d0895
8231706: change "Java" dir to "app" dir in jpackage app image
herrick
parents:
58415
diff
changeset
|
389 |
return "Contents/app/" + APP_NAME.fetchFrom(params) + ".cfg"; |
57038 | 390 |
} |
391 |
||
57789 | 392 |
private void copyClassPathEntries(Path javaDirectory, |
393 |
Map<String, ? super Object> params) throws IOException { |
|
57038 | 394 |
List<RelativeFileSet> resourcesList = |
395 |
APP_RESOURCES_LIST.fetchFrom(params); |
|
396 |
if (resourcesList == null) { |
|
397 |
throw new RuntimeException( |
|
398 |
I18N.getString("message.null-classpath")); |
|
399 |
} |
|
400 |
||
401 |
for (RelativeFileSet classPath : resourcesList) { |
|
402 |
File srcdir = classPath.getBaseDirectory(); |
|
403 |
for (String fname : classPath.getIncludedFiles()) { |
|
404 |
copyEntry(javaDirectory, srcdir, fname); |
|
405 |
} |
|
406 |
} |
|
407 |
} |
|
408 |
||
409 |
private String getBundleName(Map<String, ? super Object> params) { |
|
410 |
if (MAC_CF_BUNDLE_NAME.fetchFrom(params) != null) { |
|
411 |
String bn = MAC_CF_BUNDLE_NAME.fetchFrom(params); |
|
412 |
if (bn.length() > 16) { |
|
413 |
Log.error(MessageFormat.format(I18N.getString( |
|
414 |
"message.bundle-name-too-long-warning"), |
|
415 |
MAC_CF_BUNDLE_NAME.getID(), bn)); |
|
416 |
} |
|
417 |
return MAC_CF_BUNDLE_NAME.fetchFrom(params); |
|
418 |
} else if (APP_NAME.fetchFrom(params) != null) { |
|
419 |
return APP_NAME.fetchFrom(params); |
|
420 |
} else { |
|
421 |
String nm = MAIN_CLASS.fetchFrom(params); |
|
422 |
if (nm.length() > 16) { |
|
423 |
nm = nm.substring(0, 16); |
|
424 |
} |
|
425 |
return nm; |
|
426 |
} |
|
427 |
} |
|
428 |
||
57789 | 429 |
private void writeRuntimeInfoPlist(File file, |
430 |
Map<String, ? super Object> params) throws IOException { |
|
57038 | 431 |
Map<String, String> data = new HashMap<>(); |
57256 | 432 |
String identifier = StandardBundlerParam.isRuntimeInstaller(params) ? |
57038 | 433 |
MAC_CF_BUNDLE_IDENTIFIER.fetchFrom(params) : |
434 |
"com.oracle.java." + MAC_CF_BUNDLE_IDENTIFIER.fetchFrom(params); |
|
435 |
data.put("CF_BUNDLE_IDENTIFIER", identifier); |
|
57256 | 436 |
String name = StandardBundlerParam.isRuntimeInstaller(params) ? |
57038 | 437 |
getBundleName(params): "Java Runtime Image"; |
438 |
data.put("CF_BUNDLE_NAME", name); |
|
439 |
data.put("CF_BUNDLE_VERSION", VERSION.fetchFrom(params)); |
|
440 |
data.put("CF_BUNDLE_SHORT_VERSION_STRING", VERSION.fetchFrom(params)); |
|
441 |
||
58647
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
442 |
createResource(TEMPLATE_RUNTIME_INFO_PLIST, params) |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
443 |
.setPublicName("Runtime-Info.plist") |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
444 |
.setCategory(I18N.getString("resource.runtime-info-plist")) |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
445 |
.setSubstitutionData(data) |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
446 |
.saveToFile(file); |
57038 | 447 |
} |
448 |
||
57789 | 449 |
private void writeInfoPlist(File file, Map<String, ? super Object> params) |
450 |
throws IOException { |
|
57038 | 451 |
Log.verbose(MessageFormat.format(I18N.getString( |
452 |
"message.preparing-info-plist"), file.getAbsolutePath())); |
|
453 |
||
454 |
//prepare config for exe |
|
455 |
//Note: do not need CFBundleDisplayName if we don't support localization |
|
456 |
Map<String, String> data = new HashMap<>(); |
|
457 |
data.put("DEPLOY_ICON_FILE", APP_NAME.fetchFrom(params) + ".icns"); |
|
458 |
data.put("DEPLOY_BUNDLE_IDENTIFIER", |
|
459 |
MAC_CF_BUNDLE_IDENTIFIER.fetchFrom(params)); |
|
460 |
data.put("DEPLOY_BUNDLE_NAME", |
|
461 |
getBundleName(params)); |
|
462 |
data.put("DEPLOY_BUNDLE_COPYRIGHT", |
|
463 |
COPYRIGHT.fetchFrom(params) != null ? |
|
464 |
COPYRIGHT.fetchFrom(params) : "Unknown"); |
|
465 |
data.put("DEPLOY_LAUNCHER_NAME", getLauncherName(params)); |
|
466 |
data.put("DEPLOY_BUNDLE_SHORT_VERSION", |
|
467 |
VERSION.fetchFrom(params) != null ? |
|
468 |
VERSION.fetchFrom(params) : "1.0.0"); |
|
469 |
data.put("DEPLOY_BUNDLE_CFBUNDLE_VERSION", |
|
470 |
MAC_CF_BUNDLE_VERSION.fetchFrom(params) != null ? |
|
471 |
MAC_CF_BUNDLE_VERSION.fetchFrom(params) : "100"); |
|
472 |
data.put("DEPLOY_BUNDLE_CATEGORY", MAC_CATEGORY.fetchFrom(params)); |
|
473 |
||
474 |
boolean hasMainJar = MAIN_JAR.fetchFrom(params) != null; |
|
475 |
boolean hasMainModule = |
|
476 |
StandardBundlerParam.MODULE.fetchFrom(params) != null; |
|
477 |
||
478 |
if (hasMainJar) { |
|
479 |
data.put("DEPLOY_MAIN_JAR_NAME", MAIN_JAR.fetchFrom(params). |
|
480 |
getIncludedFiles().iterator().next()); |
|
481 |
} |
|
482 |
else if (hasMainModule) { |
|
483 |
data.put("DEPLOY_MODULE_NAME", |
|
484 |
StandardBundlerParam.MODULE.fetchFrom(params)); |
|
485 |
} |
|
486 |
||
487 |
StringBuilder sb = new StringBuilder(); |
|
57316 | 488 |
List<String> jvmOptions = JAVA_OPTIONS.fetchFrom(params); |
57038 | 489 |
|
490 |
String newline = ""; //So we don't add extra line after last append |
|
491 |
for (String o : jvmOptions) { |
|
492 |
sb.append(newline).append( |
|
493 |
" <string>").append(o).append("</string>"); |
|
494 |
newline = "\n"; |
|
495 |
} |
|
496 |
||
57316 | 497 |
data.put("DEPLOY_JAVA_OPTIONS", sb.toString()); |
57038 | 498 |
|
499 |
sb = new StringBuilder(); |
|
500 |
List<String> args = ARGUMENTS.fetchFrom(params); |
|
501 |
newline = ""; |
|
502 |
// So we don't add unneccessary extra line after last append |
|
503 |
||
504 |
for (String o : args) { |
|
505 |
sb.append(newline).append(" <string>").append(o).append( |
|
506 |
"</string>"); |
|
507 |
newline = "\n"; |
|
508 |
} |
|
509 |
data.put("DEPLOY_ARGUMENTS", sb.toString()); |
|
510 |
||
511 |
newline = ""; |
|
512 |
||
513 |
data.put("DEPLOY_LAUNCHER_CLASS", MAIN_CLASS.fetchFrom(params)); |
|
514 |
||
57450
82c78b40b39d
8227058: Regressions related to no longer setting user.dir
herrick
parents:
57446
diff
changeset
|
515 |
data.put("DEPLOY_APP_CLASSPATH", |
82c78b40b39d
8227058: Regressions related to no longer setting user.dir
herrick
parents:
57446
diff
changeset
|
516 |
getCfgClassPath(CLASSPATH.fetchFrom(params))); |
57038 | 517 |
|
518 |
StringBuilder bundleDocumentTypes = new StringBuilder(); |
|
519 |
StringBuilder exportedTypes = new StringBuilder(); |
|
520 |
for (Map<String, ? super Object> |
|
521 |
fileAssociation : FILE_ASSOCIATIONS.fetchFrom(params)) { |
|
522 |
||
523 |
List<String> extensions = FA_EXTENSIONS.fetchFrom(fileAssociation); |
|
524 |
||
525 |
if (extensions == null) { |
|
526 |
Log.verbose(I18N.getString( |
|
527 |
"message.creating-association-with-null-extension")); |
|
528 |
} |
|
529 |
||
530 |
List<String> mimeTypes = FA_CONTENT_TYPE.fetchFrom(fileAssociation); |
|
531 |
String itemContentType = MAC_CF_BUNDLE_IDENTIFIER.fetchFrom(params) |
|
532 |
+ "." + ((extensions == null || extensions.isEmpty()) |
|
533 |
? "mime" : extensions.get(0)); |
|
534 |
String description = FA_DESCRIPTION.fetchFrom(fileAssociation); |
|
57397
89549ecec1c7
8223212: Code cleanup found during jpackage review
herrick
parents:
57396
diff
changeset
|
535 |
File icon = FA_ICON.fetchFrom(fileAssociation); |
57038 | 536 |
|
537 |
bundleDocumentTypes.append(" <dict>\n") |
|
538 |
.append(" <key>LSItemContentTypes</key>\n") |
|
539 |
.append(" <array>\n") |
|
540 |
.append(" <string>") |
|
541 |
.append(itemContentType) |
|
542 |
.append("</string>\n") |
|
543 |
.append(" </array>\n") |
|
544 |
.append("\n") |
|
545 |
.append(" <key>CFBundleTypeName</key>\n") |
|
546 |
.append(" <string>") |
|
547 |
.append(description) |
|
548 |
.append("</string>\n") |
|
549 |
.append("\n") |
|
550 |
.append(" <key>LSHandlerRank</key>\n") |
|
551 |
.append(" <string>Owner</string>\n") |
|
552 |
// TODO make a bundler arg |
|
553 |
.append("\n") |
|
554 |
.append(" <key>CFBundleTypeRole</key>\n") |
|
555 |
.append(" <string>Editor</string>\n") |
|
556 |
// TODO make a bundler arg |
|
557 |
.append("\n") |
|
558 |
.append(" <key>LSIsAppleDefaultForType</key>\n") |
|
559 |
.append(" <true/>\n") |
|
560 |
// TODO make a bundler arg |
|
561 |
.append("\n"); |
|
562 |
||
563 |
if (icon != null && icon.exists()) { |
|
564 |
bundleDocumentTypes |
|
565 |
.append(" <key>CFBundleTypeIconFile</key>\n") |
|
566 |
.append(" <string>") |
|
567 |
.append(icon.getName()) |
|
568 |
.append("</string>\n"); |
|
569 |
} |
|
570 |
bundleDocumentTypes.append(" </dict>\n"); |
|
571 |
||
572 |
exportedTypes.append(" <dict>\n") |
|
573 |
.append(" <key>UTTypeIdentifier</key>\n") |
|
574 |
.append(" <string>") |
|
575 |
.append(itemContentType) |
|
576 |
.append("</string>\n") |
|
577 |
.append("\n") |
|
578 |
.append(" <key>UTTypeDescription</key>\n") |
|
579 |
.append(" <string>") |
|
580 |
.append(description) |
|
581 |
.append("</string>\n") |
|
582 |
.append(" <key>UTTypeConformsTo</key>\n") |
|
583 |
.append(" <array>\n") |
|
584 |
.append(" <string>public.data</string>\n") |
|
585 |
//TODO expose this? |
|
586 |
.append(" </array>\n") |
|
587 |
.append("\n"); |
|
588 |
||
589 |
if (icon != null && icon.exists()) { |
|
590 |
exportedTypes.append(" <key>UTTypeIconFile</key>\n") |
|
591 |
.append(" <string>") |
|
592 |
.append(icon.getName()) |
|
593 |
.append("</string>\n") |
|
594 |
.append("\n"); |
|
595 |
} |
|
596 |
||
597 |
exportedTypes.append("\n") |
|
598 |
.append(" <key>UTTypeTagSpecification</key>\n") |
|
599 |
.append(" <dict>\n") |
|
600 |
// TODO expose via param? .append( |
|
601 |
// " <key>com.apple.ostype</key>\n"); |
|
602 |
// TODO expose via param? .append( |
|
603 |
// " <string>ABCD</string>\n") |
|
604 |
.append("\n"); |
|
605 |
||
606 |
if (extensions != null && !extensions.isEmpty()) { |
|
607 |
exportedTypes.append( |
|
608 |
" <key>public.filename-extension</key>\n") |
|
609 |
.append(" <array>\n"); |
|
610 |
||
611 |
for (String ext : extensions) { |
|
612 |
exportedTypes.append(" <string>") |
|
613 |
.append(ext) |
|
614 |
.append("</string>\n"); |
|
615 |
} |
|
616 |
exportedTypes.append(" </array>\n"); |
|
617 |
} |
|
618 |
if (mimeTypes != null && !mimeTypes.isEmpty()) { |
|
619 |
exportedTypes.append(" <key>public.mime-type</key>\n") |
|
620 |
.append(" <array>\n"); |
|
621 |
||
622 |
for (String mime : mimeTypes) { |
|
623 |
exportedTypes.append(" <string>") |
|
624 |
.append(mime) |
|
625 |
.append("</string>\n"); |
|
626 |
} |
|
627 |
exportedTypes.append(" </array>\n"); |
|
628 |
} |
|
629 |
exportedTypes.append(" </dict>\n") |
|
630 |
.append(" </dict>\n"); |
|
631 |
} |
|
632 |
String associationData; |
|
633 |
if (bundleDocumentTypes.length() > 0) { |
|
634 |
associationData = |
|
635 |
"\n <key>CFBundleDocumentTypes</key>\n <array>\n" |
|
636 |
+ bundleDocumentTypes.toString() |
|
637 |
+ " </array>\n\n" |
|
638 |
+ " <key>UTExportedTypeDeclarations</key>\n <array>\n" |
|
639 |
+ exportedTypes.toString() |
|
640 |
+ " </array>\n"; |
|
641 |
} else { |
|
642 |
associationData = ""; |
|
643 |
} |
|
644 |
data.put("DEPLOY_FILE_ASSOCIATIONS", associationData); |
|
645 |
||
58647
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
646 |
createResource(TEMPLATE_INFO_PLIST_LITE, params) |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
647 |
.setCategory(I18N.getString("resource.app-info-plist")) |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
648 |
.setSubstitutionData(data) |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
649 |
.setPublicName("Info.plist") |
2c43b89b1679
8231862: Decouple DesktopIntegration and LinuxPackageBundler classes
herrick
parents:
58538
diff
changeset
|
650 |
.saveToFile(file); |
57038 | 651 |
} |
652 |
||
653 |
private void writePkgInfo(File file) throws IOException { |
|
654 |
//hardcoded as it does not seem we need to change it ever |
|
655 |
String signature = "????"; |
|
656 |
||
57390 | 657 |
try (Writer out = Files.newBufferedWriter(file.toPath())) { |
57038 | 658 |
out.write(OS_TYPE_CODE + signature); |
659 |
out.flush(); |
|
660 |
} |
|
661 |
} |
|
662 |
||
663 |
public static void addNewKeychain(Map<String, ? super Object> params) |
|
664 |
throws IOException, InterruptedException { |
|
665 |
if (Platform.getMajorVersion() < 10 || |
|
666 |
(Platform.getMajorVersion() == 10 && |
|
667 |
Platform.getMinorVersion() < 12)) { |
|
668 |
// we need this for OS X 10.12+ |
|
669 |
return; |
|
670 |
} |
|
671 |
||
672 |
String keyChain = SIGNING_KEYCHAIN.fetchFrom(params); |
|
673 |
if (keyChain == null || keyChain.isEmpty()) { |
|
674 |
return; |
|
675 |
} |
|
676 |
||
677 |
// get current keychain list |
|
678 |
String keyChainPath = new File (keyChain).getAbsolutePath().toString(); |
|
679 |
List<String> keychainList = new ArrayList<>(); |
|
680 |
int ret = IOUtils.getProcessOutput( |
|
681 |
keychainList, "security", "list-keychains"); |
|
682 |
if (ret != 0) { |
|
683 |
Log.error(I18N.getString("message.keychain.error")); |
|
684 |
return; |
|
685 |
} |
|
686 |
||
687 |
boolean contains = keychainList.stream().anyMatch( |
|
688 |
str -> str.trim().equals("\""+keyChainPath.trim()+"\"")); |
|
689 |
if (contains) { |
|
690 |
// keychain is already added in the search list |
|
691 |
return; |
|
692 |
} |
|
693 |
||
694 |
keyChains = new ArrayList<>(); |
|
695 |
// remove " |
|
696 |
keychainList.forEach((String s) -> { |
|
697 |
String path = s.trim(); |
|
698 |
if (path.startsWith("\"") && path.endsWith("\"")) { |
|
699 |
path = path.substring(1, path.length()-1); |
|
700 |
} |
|
701 |
keyChains.add(path); |
|
702 |
}); |
|
703 |
||
704 |
List<String> args = new ArrayList<>(); |
|
705 |
args.add("security"); |
|
706 |
args.add("list-keychains"); |
|
707 |
args.add("-s"); |
|
708 |
||
709 |
args.addAll(keyChains); |
|
710 |
args.add(keyChain); |
|
711 |
||
712 |
ProcessBuilder pb = new ProcessBuilder(args); |
|
57391 | 713 |
IOUtils.exec(pb); |
57038 | 714 |
} |
715 |
||
716 |
public static void restoreKeychainList(Map<String, ? super Object> params) |
|
717 |
throws IOException{ |
|
718 |
if (Platform.getMajorVersion() < 10 || |
|
719 |
(Platform.getMajorVersion() == 10 && |
|
720 |
Platform.getMinorVersion() < 12)) { |
|
721 |
// we need this for OS X 10.12+ |
|
722 |
return; |
|
723 |
} |
|
724 |
||
725 |
if (keyChains == null || keyChains.isEmpty()) { |
|
726 |
return; |
|
727 |
} |
|
728 |
||
729 |
List<String> args = new ArrayList<>(); |
|
730 |
args.add("security"); |
|
731 |
args.add("list-keychains"); |
|
732 |
args.add("-s"); |
|
733 |
||
734 |
args.addAll(keyChains); |
|
735 |
||
736 |
ProcessBuilder pb = new ProcessBuilder(args); |
|
57391 | 737 |
IOUtils.exec(pb); |
57038 | 738 |
} |
739 |
||
740 |
public static void signAppBundle( |
|
741 |
Map<String, ? super Object> params, Path appLocation, |
|
742 |
String signingIdentity, String identifierPrefix, |
|
743 |
String entitlementsFile, String inheritedEntitlements) |
|
744 |
throws IOException { |
|
745 |
AtomicReference<IOException> toThrow = new AtomicReference<>(); |
|
746 |
String appExecutable = "/Contents/MacOS/" + APP_NAME.fetchFrom(params); |
|
747 |
String keyChain = SIGNING_KEYCHAIN.fetchFrom(params); |
|
748 |
||
749 |
// sign all dylibs and jars |
|
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
750 |
Files.walk(appLocation).peek(path -> { // fix permissions |
57038 | 751 |
try { |
752 |
Set<PosixFilePermission> pfp = |
|
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
753 |
Files.getPosixFilePermissions(path); |
57038 | 754 |
if (!pfp.contains(PosixFilePermission.OWNER_WRITE)) { |
755 |
pfp = EnumSet.copyOf(pfp); |
|
756 |
pfp.add(PosixFilePermission.OWNER_WRITE); |
|
757 |
Files.setPosixFilePermissions(path, pfp); |
|
758 |
} |
|
759 |
} catch (IOException e) { |
|
57776 | 760 |
Log.verbose(e); |
57038 | 761 |
} |
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
762 |
}).filter(p -> Files.isRegularFile(p) |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
763 |
&& !(p.toString().contains("/Contents/MacOS/libjli.dylib") |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
764 |
|| p.toString().endsWith(appExecutable) |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
765 |
|| p.toString().contains("/Contents/runtime") |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
766 |
|| p.toString().contains("/Contents/Frameworks"))).forEach(p -> { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
767 |
//noinspection ThrowableResultOfMethodCallIgnored |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
768 |
if (toThrow.get() != null) return; |
57038 | 769 |
|
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
770 |
// If p is a symlink then skip the signing process. |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
771 |
if (Files.isSymbolicLink(p)) { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
772 |
if (VERBOSE.fetchFrom(params)) { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
773 |
Log.verbose(MessageFormat.format(I18N.getString( |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
774 |
"message.ignoring.symlink"), p.toString())); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
775 |
} |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
776 |
} else { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
777 |
if (p.toString().endsWith(LIBRARY_NAME)) { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
778 |
if (isFileSigned(p)) { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
779 |
return; |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
780 |
} |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
781 |
} |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
782 |
|
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
783 |
List<String> args = new ArrayList<>(); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
784 |
args.addAll(Arrays.asList("codesign", |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
785 |
"-s", signingIdentity, // sign with this key |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
786 |
"--prefix", identifierPrefix, |
57038 | 787 |
// use the identifier as a prefix |
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
788 |
"-vvvv")); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
789 |
if (entitlementsFile != null && |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
790 |
(p.toString().endsWith(".jar") |
57038 | 791 |
|| p.toString().endsWith(".dylib"))) { |
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
792 |
args.add("--entitlements"); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
793 |
args.add(entitlementsFile); // entitlements |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
794 |
} else if (inheritedEntitlements != null && |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
795 |
Files.isExecutable(p)) { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
796 |
args.add("--entitlements"); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
797 |
args.add(inheritedEntitlements); |
57038 | 798 |
// inherited entitlements for executable processes |
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
799 |
} |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
800 |
if (keyChain != null && !keyChain.isEmpty()) { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
801 |
args.add("--keychain"); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
802 |
args.add(keyChain); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
803 |
} |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
804 |
args.add(p.toString()); |
57038 | 805 |
|
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
806 |
try { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
807 |
Set<PosixFilePermission> oldPermissions = |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
808 |
Files.getPosixFilePermissions(p); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
809 |
File f = p.toFile(); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
810 |
f.setWritable(true, true); |
57038 | 811 |
|
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
812 |
ProcessBuilder pb = new ProcessBuilder(args); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
813 |
IOUtils.exec(pb); |
57038 | 814 |
|
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
815 |
Files.setPosixFilePermissions(p, oldPermissions); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
816 |
} catch (IOException ioe) { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
817 |
toThrow.set(ioe); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
818 |
} |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
819 |
} |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
820 |
}); |
57038 | 821 |
|
822 |
IOException ioe = toThrow.get(); |
|
823 |
if (ioe != null) { |
|
824 |
throw ioe; |
|
825 |
} |
|
826 |
||
57333 | 827 |
// sign all runtime and frameworks |
57038 | 828 |
Consumer<? super Path> signIdentifiedByPList = path -> { |
829 |
//noinspection ThrowableResultOfMethodCallIgnored |
|
830 |
if (toThrow.get() != null) return; |
|
831 |
||
832 |
try { |
|
833 |
List<String> args = new ArrayList<>(); |
|
834 |
args.addAll(Arrays.asList("codesign", |
|
835 |
"-s", signingIdentity, // sign with this key |
|
836 |
"--prefix", identifierPrefix, |
|
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
837 |
// use the identifier as a prefix |
57038 | 838 |
"-vvvv")); |
839 |
if (keyChain != null && !keyChain.isEmpty()) { |
|
840 |
args.add("--keychain"); |
|
841 |
args.add(keyChain); |
|
842 |
} |
|
843 |
args.add(path.toString()); |
|
844 |
ProcessBuilder pb = new ProcessBuilder(args); |
|
57391 | 845 |
IOUtils.exec(pb); |
57038 | 846 |
|
847 |
args = new ArrayList<>(); |
|
848 |
args.addAll(Arrays.asList("codesign", |
|
849 |
"-s", signingIdentity, // sign with this key |
|
850 |
"--prefix", identifierPrefix, |
|
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
851 |
// use the identifier as a prefix |
57038 | 852 |
"-vvvv")); |
853 |
if (keyChain != null && !keyChain.isEmpty()) { |
|
854 |
args.add("--keychain"); |
|
855 |
args.add(keyChain); |
|
856 |
} |
|
857 |
args.add(path.toString() |
|
858 |
+ "/Contents/_CodeSignature/CodeResources"); |
|
859 |
pb = new ProcessBuilder(args); |
|
57391 | 860 |
IOUtils.exec(pb); |
57038 | 861 |
} catch (IOException e) { |
862 |
toThrow.set(e); |
|
863 |
} |
|
864 |
}; |
|
865 |
||
57333 | 866 |
Path javaPath = appLocation.resolve("Contents/runtime"); |
867 |
if (Files.isDirectory(javaPath)) { |
|
57402 | 868 |
signIdentifiedByPList.accept(javaPath); |
57038 | 869 |
|
870 |
ioe = toThrow.get(); |
|
871 |
if (ioe != null) { |
|
872 |
throw ioe; |
|
873 |
} |
|
874 |
} |
|
875 |
Path frameworkPath = appLocation.resolve("Contents/Frameworks"); |
|
876 |
if (Files.isDirectory(frameworkPath)) { |
|
877 |
Files.list(frameworkPath) |
|
878 |
.forEach(signIdentifiedByPList); |
|
879 |
||
880 |
ioe = toThrow.get(); |
|
881 |
if (ioe != null) { |
|
882 |
throw ioe; |
|
883 |
} |
|
884 |
} |
|
885 |
||
886 |
// sign the app itself |
|
887 |
List<String> args = new ArrayList<>(); |
|
888 |
args.addAll(Arrays.asList("codesign", |
|
889 |
"-s", signingIdentity, // sign with this key |
|
890 |
"-vvvv")); // super verbose output |
|
891 |
if (entitlementsFile != null) { |
|
892 |
args.add("--entitlements"); |
|
893 |
args.add(entitlementsFile); // entitlements |
|
894 |
} |
|
895 |
if (keyChain != null && !keyChain.isEmpty()) { |
|
896 |
args.add("--keychain"); |
|
897 |
args.add(keyChain); |
|
898 |
} |
|
899 |
args.add(appLocation.toString()); |
|
900 |
||
901 |
ProcessBuilder pb = |
|
902 |
new ProcessBuilder(args.toArray(new String[args.size()])); |
|
57391 | 903 |
IOUtils.exec(pb); |
57038 | 904 |
} |
905 |
||
58172
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
906 |
private static boolean isFileSigned(Path file) { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
907 |
ProcessBuilder pb = |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
908 |
new ProcessBuilder("codesign", "--verify", file.toString()); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
909 |
|
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
910 |
try { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
911 |
IOUtils.exec(pb); |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
912 |
} catch (IOException ex) { |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
913 |
return false; |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
914 |
} |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
915 |
|
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
916 |
return true; |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
917 |
} |
bf06a1d3aef6
8230629: jpackage signing on macOS does not work as expected
herrick
parents:
57908
diff
changeset
|
918 |
|
58415 | 919 |
private static String extractBundleIdentifier(Map<String, Object> params) { |
920 |
if (PREDEFINED_APP_IMAGE.fetchFrom(params) == null) { |
|
921 |
return null; |
|
922 |
} |
|
923 |
||
924 |
try { |
|
925 |
File infoPList = new File(PREDEFINED_APP_IMAGE.fetchFrom(params) + |
|
926 |
File.separator + "Contents" + |
|
927 |
File.separator + "Info.plist"); |
|
928 |
||
929 |
DocumentBuilderFactory dbf |
|
930 |
= DocumentBuilderFactory.newDefaultInstance(); |
|
931 |
dbf.setFeature("http://apache.org/xml/features/" + |
|
932 |
"nonvalidating/load-external-dtd", false); |
|
933 |
DocumentBuilder b = dbf.newDocumentBuilder(); |
|
934 |
org.w3c.dom.Document doc = b.parse(new FileInputStream( |
|
935 |
infoPList.getAbsolutePath())); |
|
936 |
||
937 |
XPath xPath = XPathFactory.newInstance().newXPath(); |
|
938 |
// Query for the value of <string> element preceding <key> |
|
939 |
// element with value equal to CFBundleIdentifier |
|
940 |
String v = (String) xPath.evaluate( |
|
941 |
"//string[preceding-sibling::key = \"CFBundleIdentifier\"][1]", |
|
942 |
doc, XPathConstants.STRING); |
|
943 |
||
944 |
if (v != null && !v.isEmpty()) { |
|
945 |
return v; |
|
946 |
} |
|
947 |
} catch (Exception ex) { |
|
948 |
Log.verbose(ex); |
|
949 |
} |
|
950 |
||
951 |
return null; |
|
952 |
} |
|
953 |
||
57038 | 954 |
} |