1 /* 2 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jpackage.internal; 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 35 import static jdk.jpackage.internal.StandardBundlerParam.*; 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 44 // BundlerType 45 public static final String PARAM_TYPE = "type"; 46 47 // String 48 public static final String PARAM_BUNDLE_FORMAT = "bundleFormat"; 49 // String 50 public static final String PARAM_ICON = "icon"; 51 52 // String - Name of bundle file and native launcher 53 public static final String PARAM_NAME = "name"; 54 55 // String - application vendor, used by most of the bundlers 56 public static final String PARAM_VENDOR = "vendor"; 57 58 // String - email name and email, only used for debian */ 59 public static final String PARAM_EMAIL = "email"; 60 61 // String - vendor <email>, only used for debian */ 62 public static final String PARAM_MAINTAINER = "maintainer"; 63 64 /* String - Copyright. Used on Mac */ 65 public static final String PARAM_COPYRIGHT = "copyright"; 66 67 // String - GUID on windows for MSI, CFBundleIdentifier on Mac 68 // If not compatible with requirements then bundler either do not bundle 69 // or autogenerate 70 public static final String PARAM_IDENTIFIER = "identifier"; 71 72 /* boolean - shortcut preferences */ 73 public static final String PARAM_SHORTCUT = "shortcutHint"; 74 // boolean - menu shortcut preference 75 public static final String PARAM_MENU = "menuHint"; 76 77 // String - Application version. Format may differ for different bundlers 78 public static final String PARAM_VERSION = "appVersion"; 79 80 // String - Optional application description. Used by MSI and on Linux 81 public static final String PARAM_DESCRIPTION = "description"; 82 83 // String - License type. Needed on Linux (rpm) 84 public static final String PARAM_LICENSE_TYPE = "licenseType"; 85 86 // String - File with license. Format is OS/bundler specific 87 public static final String PARAM_LICENSE_FILE = "licenseFile"; 88 89 // String Main application class. 90 // Not used directly but used to derive default values 91 public static final String PARAM_APPLICATION_CLASS = "applicationClass"; 92 93 // boolean - Adds a dialog to let the user choose a directory 94 // where the product will be installed. 95 public static final String PARAM_INSTALLDIR_CHOOSER = "installdirChooser"; 96 97 /** 98 * create a new bundle with all default values 99 */ 100 public BundleParams() { 101 params = new HashMap<>(); 102 } 103 104 /** 105 * Create a bundle params with a copy of the params 106 * @param params map of initial parameters to be copied in. 107 */ 108 public BundleParams(Map<String, ?> params) { 109 this.params = new HashMap<>(params); 110 } 111 112 public void addAllBundleParams(Map<String, ? super Object> p) { 113 params.putAll(p); 114 } 115 116 public <C> C fetchParam(BundlerParamInfo<C> paramInfo) { 117 return paramInfo.fetchFrom(params); 118 } 119 120 @SuppressWarnings("unchecked") 121 public <C> C fetchParamWithDefault( 122 Class<C> klass, C defaultValue, String... keys) { 123 for (String key : keys) { 124 Object o = params.get(key); 125 if (klass.isInstance(o)) { 126 return (C) o; 127 } else if (params.containsKey(key) && o == null) { 128 return null; 129 } else if (o != null) { 130 Log.debug("Bundle param " + key + " is not type " + klass); 131 } 132 } 133 return defaultValue; 134 } 135 136 public <C> C fetchParam(Class<C> klass, String... keys) { 137 return fetchParamWithDefault(klass, null, keys); 138 } 139 140 // NOTE: we do not care about application parameters here 141 // as they will be embeded into jar file manifest and 142 // java launcher will take care of them! 143 144 public Map<String, ? super Object> getBundleParamsAsMap() { 145 return new HashMap<>(params); 146 } 147 148 public void setJvmargs(List<String> jvmargs) { 149 putUnlessNullOrEmpty(JVM_OPTIONS.getID(), jvmargs); 150 } 151 152 public void setArguments(List<String> arguments) { 153 putUnlessNullOrEmpty(ARGUMENTS.getID(), arguments); 154 } 155 156 public void setAddModules(String value) { 157 putUnlessNull(StandardBundlerParam.ADD_MODULES.getID(), value); 158 } 159 160 public void setLimitModules(String value) { 161 putUnlessNull(StandardBundlerParam.LIMIT_MODULES.getID(), value); 162 } 163 164 public void setModulePath(String value) { 165 putUnlessNull(StandardBundlerParam.MODULE_PATH.getID(), value); 166 } 167 168 public void setMainModule(String value) { 169 putUnlessNull(StandardBundlerParam.MODULE.getID(), value); 170 } 171 172 public void setDebug(String value) { 173 putUnlessNull(JLinkBundlerHelper.DEBUG.getID(), value); 174 } 175 176 public String getApplicationID() { 177 return fetchParam(IDENTIFIER); 178 } 179 180 public String getPreferencesID() { 181 return fetchParam(PREFERENCES_ID); 182 } 183 184 public String getApplicationClass() { 185 return fetchParam(MAIN_CLASS); 186 } 187 188 public void setApplicationClass(String applicationClass) { 189 putUnlessNull(PARAM_APPLICATION_CLASS, applicationClass); 190 } 191 192 public String getAppVersion() { 193 return fetchParam(VERSION); 194 } 195 196 public void setAppVersion(String version) { 197 putUnlessNull(PARAM_VERSION, version); 198 } 199 200 public String getDescription() { 201 return fetchParam(DESCRIPTION); 202 } 203 204 public void setDescription(String s) { 205 putUnlessNull(PARAM_DESCRIPTION, s); 206 } 207 208 public void setInstalldirChooser(Boolean b) { 209 putUnlessNull(PARAM_INSTALLDIR_CHOOSER, b); 210 } 211 212 public String getName() { 213 return fetchParam(APP_NAME); 214 } 215 216 public void setName(String name) { 217 putUnlessNull(PARAM_NAME, name); 218 } 219 220 @SuppressWarnings("deprecation") 221 public BundlerType getType() { 222 return fetchParam(BundlerType.class, PARAM_TYPE); 223 } 224 225 @SuppressWarnings("deprecation") 226 public void setType(BundlerType type) { 227 putUnlessNull(PARAM_TYPE, type); 228 } 229 230 public String getBundleFormat() { 231 return fetchParam(String.class, PARAM_BUNDLE_FORMAT); 232 } 233 234 public void setBundleFormat(String t) { 235 putUnlessNull(PARAM_BUNDLE_FORMAT, t); 236 } 237 238 public boolean getVerbose() { 239 return fetchParam(VERBOSE); 240 } 241 242 public List<String> getJvmargs() { 243 return JVM_OPTIONS.fetchFrom(params); 244 } 245 246 public List<String> getArguments() { 247 return ARGUMENTS.fetchFrom(params); 248 } 249 250 public jdk.jpackage.internal.RelativeFileSet getAppResource() { 251 return fetchParam(APP_RESOURCES); 252 } 253 254 public void setAppResource(jdk.jpackage.internal.RelativeFileSet fs) { 255 putUnlessNull(PARAM_APP_RESOURCES, fs); 256 } 257 258 public void setAppResourcesList( 259 List<jdk.jpackage.internal.RelativeFileSet> rfs) { 260 putUnlessNull(APP_RESOURCES_LIST.getID(), rfs); 261 } 262 263 public String getMainClassName() { 264 String applicationClass = getApplicationClass(); 265 266 if (applicationClass == null) { 267 return null; 268 } 269 270 int idx = applicationClass.lastIndexOf("."); 271 if (idx >= 0) { 272 return applicationClass.substring(idx+1); 273 } 274 return applicationClass; 275 } 276 277 public String getCopyright() { 278 return fetchParam(COPYRIGHT); 279 } 280 281 public void setCopyright(String c) { 282 putUnlessNull(PARAM_COPYRIGHT, c); 283 } 284 285 public String getIdentifier() { 286 return fetchParam(IDENTIFIER); 287 } 288 289 public void setIdentifier(String s) { 290 putUnlessNull(PARAM_IDENTIFIER, s); 291 } 292 293 private String mainJar = null; 294 295 // assuming that application was packaged according to the rules 296 // we must have application jar, i.e. jar where we embed launcher 297 // and have main application class listed as main class! 298 // If there are more than one, or none - it will be treated as 299 // deployment error 300 // 301 // Note we look for both JavaFX executable jars and regular executable jars 302 // As long as main "application" entry point is the same it is main class 303 // (i.e. for FX jar we will use JavaFX manifest entry ...) 304 public String getMainApplicationJar() { 305 jdk.jpackage.internal.RelativeFileSet appResources = getAppResource(); 306 if (mainJar != null) { 307 if (getApplicationClass() == null) try { 308 if (appResources != null) { 309 File srcdir = appResources.getBaseDirectory(); 310 JarFile jf = new JarFile(new File(srcdir, mainJar)); 311 Manifest m = jf.getManifest(); 312 Attributes attrs = (m != null) ? 313 m.getMainAttributes() : null; 314 if (attrs != null) { 315 setApplicationClass( 316 attrs.getValue(Attributes.Name.MAIN_CLASS)); 317 } 318 } 319 } catch (IOException ignore) { 320 } 321 return mainJar; 322 } 323 324 String applicationClass = getApplicationClass(); 325 326 if (appResources == null || applicationClass == null) { 327 return null; 328 } 329 File srcdir = appResources.getBaseDirectory(); 330 for (String fname : appResources.getIncludedFiles()) { 331 JarFile jf; 332 try { 333 jf = new JarFile(new File(srcdir, fname)); 334 Manifest m = jf.getManifest(); 335 Attributes attrs = (m != null) ? m.getMainAttributes() : null; 336 if (attrs != null) { 337 boolean javaMain = applicationClass.equals( 338 attrs.getValue(Attributes.Name.MAIN_CLASS)); 339 340 if (javaMain) { 341 mainJar = fname; 342 return mainJar; 343 } 344 } 345 } catch (IOException ignore) { 346 } 347 } 348 return null; 349 } 350 351 public String getVendor() { 352 return fetchParam(VENDOR); 353 } 354 355 public void setVendor(String vendor) { 356 putUnlessNull(PARAM_VENDOR, vendor); 357 } 358 359 public String getEmail() { 360 return fetchParam(String.class, PARAM_EMAIL); 361 } 362 363 public void setEmail(String email) { 364 putUnlessNull(PARAM_EMAIL, email); 365 } 366 367 public void putUnlessNull(String param, Object value) { 368 if (value != null) { 369 params.put(param, value); 370 } 371 } 372 373 public void putUnlessNullOrEmpty(String param, Collection<?> value) { 374 if (value != null && !value.isEmpty()) { 375 params.put(param, value); 376 } 377 } 378 379 public void putUnlessNullOrEmpty(String param, Map<?,?> value) { 380 if (value != null && !value.isEmpty()) { 381 params.put(param, value); 382 } 383 } 384 385 }