57038
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 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 |
|
57039
|
26 |
package jdk.jpackage.internal;
|
57038
|
27 |
|
57039
|
28 |
import jdk.jpackage.internal.BundlerParamInfo;
|
|
29 |
import jdk.jpackage.internal.StandardBundlerParam;
|
|
30 |
import jdk.jpackage.internal.Arguments;
|
|
31 |
import jdk.jpackage.internal.Log;
|
|
32 |
import jdk.jpackage.internal.ConfigException;
|
|
33 |
import jdk.jpackage.internal.IOUtils;
|
|
34 |
import jdk.jpackage.internal.Platform;
|
|
35 |
import jdk.jpackage.internal.UnsupportedPlatformException;
|
|
36 |
import jdk.jpackage.internal.MacAppImageBuilder;
|
|
37 |
import jdk.jpackage.internal.resources.MacResources;
|
57038
|
38 |
|
|
39 |
import java.io.File;
|
|
40 |
import java.io.IOException;
|
|
41 |
import java.text.MessageFormat;
|
|
42 |
import java.util.ArrayList;
|
|
43 |
import java.util.Arrays;
|
|
44 |
import java.util.Collection;
|
|
45 |
import java.util.LinkedHashSet;
|
|
46 |
import java.util.List;
|
|
47 |
import java.util.Map;
|
|
48 |
import java.util.Optional;
|
|
49 |
import java.util.ResourceBundle;
|
|
50 |
|
57039
|
51 |
import static jdk.jpackage.internal.StandardBundlerParam.*;
|
|
52 |
import static jdk.jpackage.internal.MacAppBundler.*;
|
57038
|
53 |
|
|
54 |
public class MacAppStoreBundler extends MacBaseInstallerBundler {
|
|
55 |
|
57059
|
56 |
private static final ResourceBundle I18N = ResourceBundle.getBundle(
|
|
57 |
"jdk.jpackage.internal.resources.MacResources");
|
57038
|
58 |
|
|
59 |
private static final String TEMPLATE_BUNDLE_ICON_HIDPI =
|
|
60 |
"GenericAppHiDPI.icns";
|
|
61 |
private final static String DEFAULT_ENTITLEMENTS =
|
|
62 |
"MacAppStore.entitlements";
|
|
63 |
private final static String DEFAULT_INHERIT_ENTITLEMENTS =
|
|
64 |
"MacAppStore_Inherit.entitlements";
|
|
65 |
|
|
66 |
public static final BundlerParamInfo<String> MAC_APP_STORE_APP_SIGNING_KEY =
|
|
67 |
new StandardBundlerParam<>(
|
|
68 |
I18N.getString("param.signing-key-app.name"),
|
|
69 |
I18N.getString("param.signing-key-app.description"),
|
|
70 |
"mac.signing-key-app",
|
|
71 |
String.class,
|
|
72 |
params -> {
|
|
73 |
String result = MacBaseInstallerBundler.findKey(
|
|
74 |
"3rd Party Mac Developer Application: " +
|
|
75 |
SIGNING_KEY_USER.fetchFrom(params),
|
|
76 |
SIGNING_KEYCHAIN.fetchFrom(params),
|
|
77 |
VERBOSE.fetchFrom(params));
|
|
78 |
if (result != null) {
|
|
79 |
MacCertificate certificate = new MacCertificate(result,
|
|
80 |
VERBOSE.fetchFrom(params));
|
|
81 |
|
|
82 |
if (!certificate.isValid()) {
|
|
83 |
Log.error(MessageFormat.format(
|
|
84 |
I18N.getString("error.certificate.expired"),
|
|
85 |
result));
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
return result;
|
|
90 |
},
|
|
91 |
(s, p) -> s);
|
|
92 |
|
|
93 |
public static final BundlerParamInfo<String> MAC_APP_STORE_PKG_SIGNING_KEY =
|
|
94 |
new StandardBundlerParam<>(
|
|
95 |
I18N.getString("param.signing-key-pkg.name"),
|
|
96 |
I18N.getString("param.signing-key-pkg.description"),
|
|
97 |
"mac.signing-key-pkg",
|
|
98 |
String.class,
|
|
99 |
params -> {
|
|
100 |
String result = MacBaseInstallerBundler.findKey(
|
|
101 |
"3rd Party Mac Developer Installer: " +
|
|
102 |
SIGNING_KEY_USER.fetchFrom(params),
|
|
103 |
SIGNING_KEYCHAIN.fetchFrom(params),
|
|
104 |
VERBOSE.fetchFrom(params));
|
|
105 |
|
|
106 |
if (result != null) {
|
|
107 |
MacCertificate certificate = new MacCertificate(
|
|
108 |
result, VERBOSE.fetchFrom(params));
|
|
109 |
|
|
110 |
if (!certificate.isValid()) {
|
|
111 |
Log.error(MessageFormat.format(
|
|
112 |
I18N.getString("error.certificate.expired"),
|
|
113 |
result));
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
return result;
|
|
118 |
},
|
|
119 |
(s, p) -> s);
|
|
120 |
|
|
121 |
public static final StandardBundlerParam<File> MAC_APP_STORE_ENTITLEMENTS =
|
|
122 |
new StandardBundlerParam<>(
|
|
123 |
I18N.getString("param.mac-app-store-entitlements.name"),
|
|
124 |
I18N.getString("param.mac-app-store-entitlements.description"),
|
|
125 |
Arguments.CLIOptions.MAC_APP_STORE_ENTITLEMENTS.getId(),
|
|
126 |
File.class,
|
|
127 |
params -> null,
|
|
128 |
(s, p) -> new File(s));
|
|
129 |
|
|
130 |
public static final BundlerParamInfo<String> INSTALLER_SUFFIX =
|
|
131 |
new StandardBundlerParam<> (
|
|
132 |
I18N.getString("param.installer-suffix.name"),
|
|
133 |
I18N.getString("param.installer-suffix.description"),
|
|
134 |
"mac.app-store.installerName.suffix",
|
|
135 |
String.class,
|
|
136 |
params -> "-MacAppStore",
|
|
137 |
(s, p) -> s);
|
|
138 |
|
|
139 |
public MacAppStoreBundler() {
|
|
140 |
super();
|
|
141 |
baseResourceLoader = MacResources.class;
|
|
142 |
}
|
|
143 |
|
|
144 |
//@Override
|
|
145 |
public File bundle(Map<String, ? super Object> p, File outdir) {
|
|
146 |
Log.verbose(MessageFormat.format(I18N.getString(
|
|
147 |
"message.building-bundle"), APP_NAME.fetchFrom(p)));
|
|
148 |
if (!outdir.isDirectory() && !outdir.mkdirs()) {
|
|
149 |
throw new RuntimeException(MessageFormat.format(I18N.getString(
|
|
150 |
"error.cannot-create-output-dir"),
|
|
151 |
outdir.getAbsolutePath()));
|
|
152 |
}
|
|
153 |
if (!outdir.canWrite()) {
|
|
154 |
throw new RuntimeException(MessageFormat.format(I18N.getString(
|
|
155 |
"error.cannot-write-to-output-dir"),
|
|
156 |
outdir.getAbsolutePath()));
|
|
157 |
}
|
|
158 |
|
|
159 |
// first, load in some overrides
|
|
160 |
// icns needs @2 versions, so load in the @2 default
|
|
161 |
p.put(DEFAULT_ICNS_ICON.getID(), TEMPLATE_BUNDLE_ICON_HIDPI);
|
|
162 |
|
|
163 |
// now we create the app
|
|
164 |
File appImageDir = APP_IMAGE_BUILD_ROOT.fetchFrom(p);
|
|
165 |
try {
|
|
166 |
appImageDir.mkdirs();
|
|
167 |
|
|
168 |
try {
|
|
169 |
MacAppImageBuilder.addNewKeychain(p);
|
|
170 |
} catch (InterruptedException e) {
|
|
171 |
Log.error(e.getMessage());
|
|
172 |
}
|
|
173 |
// first, make sure we don't use the local signing key
|
|
174 |
p.put(DEVELOPER_ID_APP_SIGNING_KEY.getID(), null);
|
|
175 |
File appLocation = prepareAppBundle(p, false);
|
|
176 |
|
|
177 |
prepareEntitlements(p);
|
|
178 |
|
|
179 |
String signingIdentity = MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(p);
|
|
180 |
String identifierPrefix = BUNDLE_ID_SIGNING_PREFIX.fetchFrom(p);
|
|
181 |
String entitlementsFile = getConfig_Entitlements(p).toString();
|
|
182 |
String inheritEntitlements =
|
|
183 |
getConfig_Inherit_Entitlements(p).toString();
|
|
184 |
|
|
185 |
MacAppImageBuilder.signAppBundle(p, appLocation.toPath(),
|
|
186 |
signingIdentity, identifierPrefix,
|
|
187 |
entitlementsFile, inheritEntitlements);
|
|
188 |
MacAppImageBuilder.restoreKeychainList(p);
|
|
189 |
|
|
190 |
ProcessBuilder pb;
|
|
191 |
|
|
192 |
// create the final pkg file
|
|
193 |
File finalPKG = new File(outdir, INSTALLER_NAME.fetchFrom(p)
|
|
194 |
+ INSTALLER_SUFFIX.fetchFrom(p)
|
|
195 |
+ ".pkg");
|
|
196 |
outdir.mkdirs();
|
|
197 |
|
|
198 |
String installIdentify =
|
|
199 |
MAC_APP_STORE_PKG_SIGNING_KEY.fetchFrom(p);
|
|
200 |
|
|
201 |
List<String> buildOptions = new ArrayList<>();
|
|
202 |
buildOptions.add("productbuild");
|
|
203 |
buildOptions.add("--component");
|
|
204 |
buildOptions.add(appLocation.toString());
|
|
205 |
buildOptions.add("/Applications");
|
|
206 |
buildOptions.add("--sign");
|
|
207 |
buildOptions.add(installIdentify);
|
|
208 |
buildOptions.add("--product");
|
|
209 |
buildOptions.add(appLocation + "/Contents/Info.plist");
|
|
210 |
String keychainName = SIGNING_KEYCHAIN.fetchFrom(p);
|
|
211 |
if (keychainName != null && !keychainName.isEmpty()) {
|
|
212 |
buildOptions.add("--keychain");
|
|
213 |
buildOptions.add(keychainName);
|
|
214 |
}
|
|
215 |
buildOptions.add(finalPKG.getAbsolutePath());
|
|
216 |
|
|
217 |
pb = new ProcessBuilder(buildOptions);
|
|
218 |
|
|
219 |
IOUtils.exec(pb, false);
|
|
220 |
return finalPKG;
|
|
221 |
} catch (Exception ex) {
|
|
222 |
Log.error("App Store Ready Bundle failed : " + ex.getMessage());
|
|
223 |
Log.verbose(ex);
|
|
224 |
return null;
|
|
225 |
} finally {
|
|
226 |
try {
|
|
227 |
if (appImageDir != null &&
|
|
228 |
PREDEFINED_APP_IMAGE.fetchFrom(p) == null &&
|
|
229 |
(PREDEFINED_RUNTIME_IMAGE.fetchFrom(p) == null ||
|
|
230 |
!Arguments.CREATE_JRE_INSTALLER.fetchFrom(p)) &&
|
|
231 |
!Log.isDebug() &&
|
|
232 |
!Log.isVerbose()) {
|
|
233 |
IOUtils.deleteRecursive(appImageDir);
|
|
234 |
} else if (appImageDir != null) {
|
|
235 |
Log.verbose(MessageFormat.format(I18N.getString(
|
|
236 |
"mesasge.intermediate-bundle-location"),
|
|
237 |
appImageDir.getAbsolutePath()));
|
|
238 |
}
|
|
239 |
|
|
240 |
//cleanup
|
|
241 |
cleanupConfigFiles(p);
|
|
242 |
} catch (IOException ex) {
|
|
243 |
//noinspection ReturnInsideFinallyBlock
|
|
244 |
Log.debug(ex.getMessage());
|
|
245 |
return null;
|
|
246 |
}
|
|
247 |
}
|
|
248 |
}
|
|
249 |
|
|
250 |
protected void cleanupConfigFiles(Map<String, ? super Object> params) {
|
|
251 |
if (Log.isDebug() || Log.isVerbose()) {
|
|
252 |
return;
|
|
253 |
}
|
|
254 |
|
|
255 |
if (getConfig_Entitlements(params) != null) {
|
|
256 |
getConfig_Entitlements(params).delete();
|
|
257 |
}
|
|
258 |
if (getConfig_Inherit_Entitlements(params) != null) {
|
|
259 |
getConfig_Inherit_Entitlements(params).delete();
|
|
260 |
}
|
|
261 |
if (PREDEFINED_APP_IMAGE.fetchFrom(params) == null) {
|
|
262 |
APP_BUNDLER.fetchFrom(params).cleanupConfigFiles(params);
|
|
263 |
}
|
|
264 |
}
|
|
265 |
|
|
266 |
private File getConfig_Entitlements(Map<String, ? super Object> params) {
|
|
267 |
return new File(CONFIG_ROOT.fetchFrom(params),
|
|
268 |
APP_NAME.fetchFrom(params) + ".entitlements");
|
|
269 |
}
|
|
270 |
|
|
271 |
private File getConfig_Inherit_Entitlements(
|
|
272 |
Map<String, ? super Object> params) {
|
|
273 |
return new File(CONFIG_ROOT.fetchFrom(params),
|
|
274 |
APP_NAME.fetchFrom(params) + "_Inherit.entitlements");
|
|
275 |
}
|
|
276 |
|
|
277 |
private void prepareEntitlements(Map<String, ? super Object> params)
|
|
278 |
throws IOException {
|
|
279 |
File entitlements = MAC_APP_STORE_ENTITLEMENTS.fetchFrom(params);
|
|
280 |
if (entitlements == null || !entitlements.exists()) {
|
|
281 |
fetchResource(getEntitlementsFileName(params),
|
|
282 |
I18N.getString("resource.mac-app-store-entitlements"),
|
|
283 |
DEFAULT_ENTITLEMENTS,
|
|
284 |
getConfig_Entitlements(params),
|
|
285 |
VERBOSE.fetchFrom(params),
|
|
286 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params));
|
|
287 |
} else {
|
|
288 |
fetchResource(getEntitlementsFileName(params),
|
|
289 |
I18N.getString("resource.mac-app-store-entitlements"),
|
|
290 |
entitlements,
|
|
291 |
getConfig_Entitlements(params),
|
|
292 |
VERBOSE.fetchFrom(params),
|
|
293 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params));
|
|
294 |
}
|
|
295 |
fetchResource(getInheritEntitlementsFileName(params),
|
|
296 |
I18N.getString("resource.mac-app-store-inherit-entitlements"),
|
|
297 |
DEFAULT_INHERIT_ENTITLEMENTS,
|
|
298 |
getConfig_Inherit_Entitlements(params),
|
|
299 |
VERBOSE.fetchFrom(params),
|
|
300 |
DROP_IN_RESOURCES_ROOT.fetchFrom(params));
|
|
301 |
}
|
|
302 |
|
|
303 |
private String getEntitlementsFileName(Map<String, ? super Object> params) {
|
|
304 |
return BUNDLER_PREFIX + APP_NAME.fetchFrom(params) + ".entitlements";
|
|
305 |
}
|
|
306 |
|
|
307 |
private String getInheritEntitlementsFileName(
|
|
308 |
Map<String, ? super Object> params) {
|
|
309 |
return BUNDLER_PREFIX + APP_NAME.fetchFrom(params)
|
|
310 |
+ "_Inherit.entitlements";
|
|
311 |
}
|
|
312 |
|
|
313 |
|
|
314 |
///////////////////////////////////////////////////////////////////////
|
|
315 |
// Implement Bundler
|
|
316 |
///////////////////////////////////////////////////////////////////////
|
|
317 |
|
|
318 |
@Override
|
|
319 |
public String getName() {
|
57059
|
320 |
return I18N.getString("store.bundler.name");
|
57038
|
321 |
}
|
|
322 |
|
|
323 |
@Override
|
|
324 |
public String getDescription() {
|
57059
|
325 |
return I18N.getString("store.bundler.description");
|
57038
|
326 |
}
|
|
327 |
|
|
328 |
@Override
|
|
329 |
public String getID() {
|
|
330 |
return "mac.appStore";
|
|
331 |
}
|
|
332 |
|
|
333 |
@Override
|
|
334 |
public Collection<BundlerParamInfo<?>> getBundleParameters() {
|
|
335 |
Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>();
|
|
336 |
results.addAll(getAppBundleParameters());
|
|
337 |
results.addAll(getMacAppStoreBundleParameters());
|
|
338 |
return results;
|
|
339 |
}
|
|
340 |
|
|
341 |
public Collection<BundlerParamInfo<?>> getMacAppStoreBundleParameters() {
|
|
342 |
Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>();
|
|
343 |
|
|
344 |
results.addAll(getAppBundleParameters());
|
|
345 |
results.remove(DEVELOPER_ID_APP_SIGNING_KEY);
|
|
346 |
results.addAll(Arrays.asList(
|
|
347 |
INSTALLER_SUFFIX,
|
|
348 |
MAC_APP_STORE_APP_SIGNING_KEY,
|
|
349 |
MAC_APP_STORE_ENTITLEMENTS,
|
|
350 |
MAC_APP_STORE_PKG_SIGNING_KEY,
|
|
351 |
SIGNING_KEYCHAIN
|
|
352 |
));
|
|
353 |
|
|
354 |
return results;
|
|
355 |
}
|
|
356 |
|
|
357 |
@Override
|
|
358 |
public boolean validate(Map<String, ? super Object> params)
|
|
359 |
throws UnsupportedPlatformException, ConfigException {
|
|
360 |
try {
|
|
361 |
if (Platform.getPlatform() != Platform.MAC) {
|
|
362 |
throw new UnsupportedPlatformException();
|
|
363 |
}
|
|
364 |
|
|
365 |
if (params == null) {
|
|
366 |
throw new ConfigException(
|
|
367 |
I18N.getString("error.parameters-null"),
|
|
368 |
I18N.getString("error.parameters-null.advice"));
|
|
369 |
}
|
|
370 |
|
|
371 |
// hdiutil is always available so there's no need to test for
|
|
372 |
// availability.
|
|
373 |
// run basic validation to ensure requirements are met
|
|
374 |
|
|
375 |
// TODO Mac App Store apps cannot use the system runtime
|
|
376 |
|
|
377 |
// we are not interested in return code, only possible exception
|
|
378 |
validateAppImageAndBundeler(params);
|
|
379 |
|
|
380 |
// reject explicitly set to not sign
|
|
381 |
if (!Optional.ofNullable(MacAppImageBuilder.
|
|
382 |
SIGN_BUNDLE.fetchFrom(params)).orElse(Boolean.TRUE)) {
|
|
383 |
throw new ConfigException(
|
|
384 |
I18N.getString("error.must-sign-app-store"),
|
|
385 |
I18N.getString("error.must-sign-app-store.advice"));
|
|
386 |
}
|
|
387 |
|
|
388 |
// make sure we have settings for signatures
|
|
389 |
if (MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(params) == null) {
|
|
390 |
throw new ConfigException(
|
|
391 |
I18N.getString("error.no-app-signing-key"),
|
|
392 |
I18N.getString("error.no-app-signing-key.advice"));
|
|
393 |
}
|
|
394 |
if (MAC_APP_STORE_PKG_SIGNING_KEY.fetchFrom(params) == null) {
|
|
395 |
throw new ConfigException(
|
|
396 |
I18N.getString("error.no-pkg-signing-key"),
|
|
397 |
I18N.getString("error.no-pkg-signing-key.advice"));
|
|
398 |
}
|
|
399 |
|
|
400 |
// things we could check...
|
|
401 |
// check the icons, make sure it has hidpi icons
|
|
402 |
// check the category,
|
|
403 |
// make sure it fits in the list apple has provided
|
|
404 |
// validate bundle identifier is reverse dns
|
|
405 |
// check for \a+\.\a+\..
|
|
406 |
|
|
407 |
return true;
|
|
408 |
} catch (RuntimeException re) {
|
|
409 |
if (re.getCause() instanceof ConfigException) {
|
|
410 |
throw (ConfigException) re.getCause();
|
|
411 |
} else {
|
|
412 |
throw new ConfigException(re);
|
|
413 |
}
|
|
414 |
}
|
|
415 |
}
|
|
416 |
|
|
417 |
@Override
|
|
418 |
public File execute(Map<String, ? super Object> params,
|
|
419 |
File outputParentDir) {
|
|
420 |
return bundle(params, outputParentDir);
|
|
421 |
}
|
|
422 |
|
|
423 |
@Override
|
|
424 |
public boolean supported() {
|
|
425 |
return !Arguments.isJreInstaller() &&
|
|
426 |
Platform.getPlatform() == Platform.MAC;
|
|
427 |
}
|
|
428 |
}
|