author | herrick |
Thu, 20 Jun 2019 14:13:50 -0400 | |
branch | JDK-8200758-branch |
changeset 57414 | 6eda749d3117 |
parent 57396 | 3944e4c2f779 |
child 57438 | 4a31db8d42bd |
permissions | -rw-r--r-- |
57038 | 1 |
/* |
57106
ea870b9ce89a
8216492: Update copyright of all new jpackage fils to 2019
kcr
parents:
57099
diff
changeset
|
2 |
* Copyright (c) 2012, 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.IOException; |
|
30 |
import java.util.*; |
|
31 |
import java.util.jar.Attributes; |
|
32 |
import java.util.jar.JarFile; |
|
33 |
import java.util.jar.Manifest; |
|
34 |
||
57039 | 35 |
import static jdk.jpackage.internal.StandardBundlerParam.*; |
57038 | 36 |
|
37 |
public class BundleParams { |
|
38 |
||
39 |
final protected Map<String, ? super Object> params; |
|
40 |
||
41 |
// RelativeFileSet |
|
42 |
public static final String PARAM_APP_RESOURCES = "appResources"; |
|
43 |
||
57414
6eda749d3117
8225428: CLI change to remove "mode", rename to "package", and build only one target
herrick
parents:
57396
diff
changeset
|
44 |
// String - Icon file name |
57038 | 45 |
public static final String PARAM_ICON = "icon"; |
46 |
||
47 |
// String - Name of bundle file and native launcher |
|
48 |
public static final String PARAM_NAME = "name"; |
|
49 |
||
50 |
// String - application vendor, used by most of the bundlers |
|
51 |
public static final String PARAM_VENDOR = "vendor"; |
|
52 |
||
53 |
// String - email name and email, only used for debian */ |
|
54 |
public static final String PARAM_EMAIL = "email"; |
|
55 |
||
57255
f686bda3b831
8215574: Investigate and document usage of --category, --copyright, --vendor and --description
herrick
parents:
57213
diff
changeset
|
56 |
// String - vendor <email>, only used for debian */ |
f686bda3b831
8215574: Investigate and document usage of --category, --copyright, --vendor and --description
herrick
parents:
57213
diff
changeset
|
57 |
public static final String PARAM_MAINTAINER = "maintainer"; |
f686bda3b831
8215574: Investigate and document usage of --category, --copyright, --vendor and --description
herrick
parents:
57213
diff
changeset
|
58 |
|
57038 | 59 |
/* String - Copyright. Used on Mac */ |
60 |
public static final String PARAM_COPYRIGHT = "copyright"; |
|
61 |
||
62 |
// String - GUID on windows for MSI, CFBundleIdentifier on Mac |
|
63 |
// If not compatible with requirements then bundler either do not bundle |
|
64 |
// or autogenerate |
|
65 |
public static final String PARAM_IDENTIFIER = "identifier"; |
|
66 |
||
67 |
/* boolean - shortcut preferences */ |
|
68 |
public static final String PARAM_SHORTCUT = "shortcutHint"; |
|
69 |
// boolean - menu shortcut preference |
|
70 |
public static final String PARAM_MENU = "menuHint"; |
|
71 |
||
72 |
// String - Application version. Format may differ for different bundlers |
|
73 |
public static final String PARAM_VERSION = "appVersion"; |
|
74 |
||
75 |
// String - Optional application description. Used by MSI and on Linux |
|
76 |
public static final String PARAM_DESCRIPTION = "description"; |
|
77 |
||
78 |
// String - License type. Needed on Linux (rpm) |
|
79 |
public static final String PARAM_LICENSE_TYPE = "licenseType"; |
|
80 |
||
57080 | 81 |
// String - File with license. Format is OS/bundler specific |
57038 | 82 |
public static final String PARAM_LICENSE_FILE = "licenseFile"; |
83 |
||
84 |
// String Main application class. |
|
85 |
// Not used directly but used to derive default values |
|
86 |
public static final String PARAM_APPLICATION_CLASS = "applicationClass"; |
|
87 |
||
88 |
// boolean - Adds a dialog to let the user choose a directory |
|
89 |
// where the product will be installed. |
|
90 |
public static final String PARAM_INSTALLDIR_CHOOSER = "installdirChooser"; |
|
91 |
||
92 |
/** |
|
93 |
* create a new bundle with all default values |
|
94 |
*/ |
|
95 |
public BundleParams() { |
|
96 |
params = new HashMap<>(); |
|
97 |
} |
|
98 |
||
99 |
/** |
|
100 |
* Create a bundle params with a copy of the params |
|
101 |
* @param params map of initial parameters to be copied in. |
|
102 |
*/ |
|
103 |
public BundleParams(Map<String, ?> params) { |
|
104 |
this.params = new HashMap<>(params); |
|
105 |
} |
|
106 |
||
57396
3944e4c2f779
8223586: remove jpackage dead code and other cleanup
herrick
parents:
57392
diff
changeset
|
107 |
public void addAllBundleParams(Map<String, ? super Object> params) { |
3944e4c2f779
8223586: remove jpackage dead code and other cleanup
herrick
parents:
57392
diff
changeset
|
108 |
this.params.putAll(params); |
57038 | 109 |
} |
110 |
||
57392 | 111 |
public <T> T fetchParam(BundlerParamInfo<T> paramInfo) { |
57038 | 112 |
return paramInfo.fetchFrom(params); |
113 |
} |
|
114 |
||
115 |
@SuppressWarnings("unchecked") |
|
57392 | 116 |
public <T> T fetchParamWithDefault( |
117 |
Class<T> klass, T defaultValue, String... keys) { |
|
57038 | 118 |
for (String key : keys) { |
119 |
Object o = params.get(key); |
|
120 |
if (klass.isInstance(o)) { |
|
57392 | 121 |
return (T) o; |
57038 | 122 |
} else if (params.containsKey(key) && o == null) { |
123 |
return null; |
|
124 |
} else if (o != null) { |
|
125 |
Log.debug("Bundle param " + key + " is not type " + klass); |
|
126 |
} |
|
127 |
} |
|
128 |
return defaultValue; |
|
129 |
} |
|
130 |
||
57392 | 131 |
public <T> T fetchParam(Class<T> klass, String... keys) { |
57038 | 132 |
return fetchParamWithDefault(klass, null, keys); |
133 |
} |
|
134 |
||
135 |
// NOTE: we do not care about application parameters here |
|
136 |
// as they will be embeded into jar file manifest and |
|
137 |
// java launcher will take care of them! |
|
138 |
||
139 |
public Map<String, ? super Object> getBundleParamsAsMap() { |
|
140 |
return new HashMap<>(params); |
|
141 |
} |
|
142 |
||
143 |
public void setJvmargs(List<String> jvmargs) { |
|
57316 | 144 |
putUnlessNullOrEmpty(JAVA_OPTIONS.getID(), jvmargs); |
57038 | 145 |
} |
146 |
||
147 |
public void setAddModules(String value) { |
|
148 |
putUnlessNull(StandardBundlerParam.ADD_MODULES.getID(), value); |
|
149 |
} |
|
150 |
||
151 |
public void setLimitModules(String value) { |
|
152 |
putUnlessNull(StandardBundlerParam.LIMIT_MODULES.getID(), value); |
|
153 |
} |
|
154 |
||
155 |
public void setModulePath(String value) { |
|
156 |
putUnlessNull(StandardBundlerParam.MODULE_PATH.getID(), value); |
|
157 |
} |
|
158 |
||
159 |
public void setMainModule(String value) { |
|
160 |
putUnlessNull(StandardBundlerParam.MODULE.getID(), value); |
|
161 |
} |
|
162 |
||
163 |
public String getApplicationID() { |
|
164 |
return fetchParam(IDENTIFIER); |
|
165 |
} |
|
166 |
||
167 |
public String getApplicationClass() { |
|
168 |
return fetchParam(MAIN_CLASS); |
|
169 |
} |
|
170 |
||
171 |
public void setApplicationClass(String applicationClass) { |
|
172 |
putUnlessNull(PARAM_APPLICATION_CLASS, applicationClass); |
|
173 |
} |
|
174 |
||
175 |
public String getAppVersion() { |
|
176 |
return fetchParam(VERSION); |
|
177 |
} |
|
178 |
||
179 |
public void setAppVersion(String version) { |
|
180 |
putUnlessNull(PARAM_VERSION, version); |
|
181 |
} |
|
182 |
||
183 |
public String getDescription() { |
|
184 |
return fetchParam(DESCRIPTION); |
|
185 |
} |
|
186 |
||
187 |
public void setDescription(String s) { |
|
188 |
putUnlessNull(PARAM_DESCRIPTION, s); |
|
189 |
} |
|
190 |
||
191 |
public void setInstalldirChooser(Boolean b) { |
|
192 |
putUnlessNull(PARAM_INSTALLDIR_CHOOSER, b); |
|
193 |
} |
|
194 |
||
195 |
public String getName() { |
|
196 |
return fetchParam(APP_NAME); |
|
197 |
} |
|
198 |
||
199 |
public void setName(String name) { |
|
200 |
putUnlessNull(PARAM_NAME, name); |
|
201 |
} |
|
202 |
||
203 |
public boolean getVerbose() { |
|
204 |
return fetchParam(VERBOSE); |
|
205 |
} |
|
206 |
||
207 |
public List<String> getJvmargs() { |
|
57316 | 208 |
return JAVA_OPTIONS.fetchFrom(params); |
57038 | 209 |
} |
210 |
||
57039 | 211 |
public jdk.jpackage.internal.RelativeFileSet getAppResource() { |
57038 | 212 |
return fetchParam(APP_RESOURCES); |
213 |
} |
|
214 |
||
57039 | 215 |
public void setAppResource(jdk.jpackage.internal.RelativeFileSet fs) { |
57038 | 216 |
putUnlessNull(PARAM_APP_RESOURCES, fs); |
217 |
} |
|
218 |
||
219 |
public void setAppResourcesList( |
|
57039 | 220 |
List<jdk.jpackage.internal.RelativeFileSet> rfs) { |
57038 | 221 |
putUnlessNull(APP_RESOURCES_LIST.getID(), rfs); |
222 |
} |
|
223 |
||
224 |
public String getMainClassName() { |
|
225 |
String applicationClass = getApplicationClass(); |
|
226 |
||
227 |
if (applicationClass == null) { |
|
228 |
return null; |
|
229 |
} |
|
230 |
||
231 |
int idx = applicationClass.lastIndexOf("."); |
|
232 |
if (idx >= 0) { |
|
233 |
return applicationClass.substring(idx+1); |
|
234 |
} |
|
235 |
return applicationClass; |
|
236 |
} |
|
237 |
||
238 |
public String getCopyright() { |
|
239 |
return fetchParam(COPYRIGHT); |
|
240 |
} |
|
241 |
||
242 |
public void setCopyright(String c) { |
|
243 |
putUnlessNull(PARAM_COPYRIGHT, c); |
|
244 |
} |
|
245 |
||
246 |
private String mainJar = null; |
|
247 |
||
248 |
// assuming that application was packaged according to the rules |
|
249 |
// we must have application jar, i.e. jar where we embed launcher |
|
250 |
// and have main application class listed as main class! |
|
57392 | 251 |
// If there are more than one, or none - it will be treated as an error |
252 |
||
57038 | 253 |
public String getMainApplicationJar() { |
57039 | 254 |
jdk.jpackage.internal.RelativeFileSet appResources = getAppResource(); |
57038 | 255 |
if (mainJar != null) { |
256 |
if (getApplicationClass() == null) try { |
|
257 |
if (appResources != null) { |
|
258 |
File srcdir = appResources.getBaseDirectory(); |
|
259 |
JarFile jf = new JarFile(new File(srcdir, mainJar)); |
|
260 |
Manifest m = jf.getManifest(); |
|
261 |
Attributes attrs = (m != null) ? |
|
262 |
m.getMainAttributes() : null; |
|
263 |
if (attrs != null) { |
|
264 |
setApplicationClass( |
|
265 |
attrs.getValue(Attributes.Name.MAIN_CLASS)); |
|
266 |
} |
|
267 |
} |
|
268 |
} catch (IOException ignore) { |
|
269 |
} |
|
270 |
return mainJar; |
|
271 |
} |
|
272 |
||
273 |
String applicationClass = getApplicationClass(); |
|
274 |
||
275 |
if (appResources == null || applicationClass == null) { |
|
276 |
return null; |
|
277 |
} |
|
278 |
File srcdir = appResources.getBaseDirectory(); |
|
279 |
for (String fname : appResources.getIncludedFiles()) { |
|
280 |
JarFile jf; |
|
281 |
try { |
|
282 |
jf = new JarFile(new File(srcdir, fname)); |
|
283 |
Manifest m = jf.getManifest(); |
|
284 |
Attributes attrs = (m != null) ? m.getMainAttributes() : null; |
|
285 |
if (attrs != null) { |
|
286 |
boolean javaMain = applicationClass.equals( |
|
287 |
attrs.getValue(Attributes.Name.MAIN_CLASS)); |
|
288 |
||
289 |
if (javaMain) { |
|
290 |
mainJar = fname; |
|
291 |
return mainJar; |
|
292 |
} |
|
293 |
} |
|
294 |
} catch (IOException ignore) { |
|
295 |
} |
|
296 |
} |
|
297 |
return null; |
|
298 |
} |
|
299 |
||
300 |
public String getVendor() { |
|
301 |
return fetchParam(VENDOR); |
|
302 |
} |
|
303 |
||
304 |
public void setVendor(String vendor) { |
|
305 |
putUnlessNull(PARAM_VENDOR, vendor); |
|
306 |
} |
|
307 |
||
308 |
public String getEmail() { |
|
309 |
return fetchParam(String.class, PARAM_EMAIL); |
|
310 |
} |
|
311 |
||
312 |
public void setEmail(String email) { |
|
313 |
putUnlessNull(PARAM_EMAIL, email); |
|
314 |
} |
|
315 |
||
316 |
public void putUnlessNull(String param, Object value) { |
|
317 |
if (value != null) { |
|
318 |
params.put(param, value); |
|
319 |
} |
|
320 |
} |
|
321 |
||
322 |
public void putUnlessNullOrEmpty(String param, Collection<?> value) { |
|
323 |
if (value != null && !value.isEmpty()) { |
|
324 |
params.put(param, value); |
|
325 |
} |
|
326 |
} |
|
327 |
||
328 |
public void putUnlessNullOrEmpty(String param, Map<?,?> value) { |
|
329 |
if (value != null && !value.isEmpty()) { |
|
330 |
params.put(param, value); |
|
331 |
} |
|
332 |
} |
|
333 |
||
334 |
} |