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 - Copyright. Used on Mac */ 62 public static final String PARAM_COPYRIGHT = "copyright"; 63 64 // String - GUID on windows for MSI, CFBundleIdentifier on Mac 65 // If not compatible with requirements then bundler either do not bundle 66 // or autogenerate 67 public static final String PARAM_IDENTIFIER = "identifier"; 68 69 /* boolean - shortcut preferences */ 70 public static final String PARAM_SHORTCUT = "shortcutHint"; 71 // boolean - menu shortcut preference 72 public static final String PARAM_MENU = "menuHint"; 73 74 // String - Application version. Format may differ for different bundlers 75 public static final String PARAM_VERSION = "appVersion"; 76 77 // String - Optional short application 78 public static final String PARAM_TITLE = "title"; 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 getTitle() { 185 return fetchParam(TITLE); 186 } 187 188 public void setTitle(String title) { 189 putUnlessNull(PARAM_TITLE, title); 190 } 191 192 public String getApplicationClass() { 193 return fetchParam(MAIN_CLASS); 194 } 195 196 public void setApplicationClass(String applicationClass) { 197 putUnlessNull(PARAM_APPLICATION_CLASS, applicationClass); 198 } 199 200 public String getAppVersion() { 201 return fetchParam(VERSION); 202 } 203 204 public void setAppVersion(String version) { 205 putUnlessNull(PARAM_VERSION, version); 206 } 207 208 public String getDescription() { 209 return fetchParam(DESCRIPTION); 210 } 211 212 public void setDescription(String s) { 213 putUnlessNull(PARAM_DESCRIPTION, s); 214 } 215 216 public void setInstalldirChooser(Boolean b) { 217 putUnlessNull(PARAM_INSTALLDIR_CHOOSER, b); 218 } 219 220 public String getName() { 221 return fetchParam(APP_NAME); 222 } 223 224 public void setName(String name) { 225 putUnlessNull(PARAM_NAME, name); 226 } 227 228 @SuppressWarnings("deprecation") 229 public BundlerType getType() { 230 return fetchParam(BundlerType.class, PARAM_TYPE); 231 } 232 233 @SuppressWarnings("deprecation") 234 public void setType(BundlerType type) { 235 putUnlessNull(PARAM_TYPE, type); 236 } 237 238 public String getBundleFormat() { 239 return fetchParam(String.class, PARAM_BUNDLE_FORMAT); 240 } 241 242 public void setBundleFormat(String t) { 243 putUnlessNull(PARAM_BUNDLE_FORMAT, t); 244 } 245 246 public boolean getVerbose() { 247 return fetchParam(VERBOSE); 248 } 249 250 public List<String> getJvmargs() { 251 return JVM_OPTIONS.fetchFrom(params); 252 } 253 254 public List<String> getArguments() { 255 return ARGUMENTS.fetchFrom(params); 256 } 257 258 public jdk.jpackage.internal.RelativeFileSet getAppResource() { 259 return fetchParam(APP_RESOURCES); 260 } 261 262 public void setAppResource(jdk.jpackage.internal.RelativeFileSet fs) { 263 putUnlessNull(PARAM_APP_RESOURCES, fs); 264 } 265 266 public void setAppResourcesList( 267 List<jdk.jpackage.internal.RelativeFileSet> rfs) { 268 putUnlessNull(APP_RESOURCES_LIST.getID(), rfs); 269 } 270 271 public String getMainClassName() { 272 String applicationClass = getApplicationClass(); 273 274 if (applicationClass == null) { 275 return null; 276 } 277 278 int idx = applicationClass.lastIndexOf("."); 279 if (idx >= 0) { 280 return applicationClass.substring(idx+1); 281 } 282 return applicationClass; 283 } 284 285 public String getCopyright() { 286 return fetchParam(COPYRIGHT); 287 } 288 289 public void setCopyright(String c) { 290 putUnlessNull(PARAM_COPYRIGHT, c); 291 } 292 293 public String getIdentifier() { 294 return fetchParam(IDENTIFIER); 295 } 296 297 public void setIdentifier(String s) { 298 putUnlessNull(PARAM_IDENTIFIER, s); 299 } 300 301 private String mainJar = null; 302 303 // assuming that application was packaged according to the rules 304 // we must have application jar, i.e. jar where we embed launcher 305 // and have main application class listed as main class! 306 // If there are more than one, or none - it will be treated as 307 // deployment error 308 // 309 // Note we look for both JavaFX executable jars and regular executable jars 310 // As long as main "application" entry point is the same it is main class 311 // (i.e. for FX jar we will use JavaFX manifest entry ...) 312 public String getMainApplicationJar() { 313 jdk.jpackage.internal.RelativeFileSet appResources = getAppResource(); 314 if (mainJar != null) { 315 if (getApplicationClass() == null) try { 316 if (appResources != null) { 317 File srcdir = appResources.getBaseDirectory(); 318 JarFile jf = new JarFile(new File(srcdir, mainJar)); 319 Manifest m = jf.getManifest(); 320 Attributes attrs = (m != null) ? 321 m.getMainAttributes() : null; 322 if (attrs != null) { 323 setApplicationClass( 324 attrs.getValue(Attributes.Name.MAIN_CLASS)); 325 } 326 } 327 } catch (IOException ignore) { 328 } 329 return mainJar; 330 } 331 332 String applicationClass = getApplicationClass(); 333 334 if (appResources == null || applicationClass == null) { 335 return null; 336 } 337 File srcdir = appResources.getBaseDirectory(); 338 for (String fname : appResources.getIncludedFiles()) { 339 JarFile jf; 340 try { 341 jf = new JarFile(new File(srcdir, fname)); 342 Manifest m = jf.getManifest(); 343 Attributes attrs = (m != null) ? m.getMainAttributes() : null; 344 if (attrs != null) { 345 boolean javaMain = applicationClass.equals( 346 attrs.getValue(Attributes.Name.MAIN_CLASS)); 347 348 if (javaMain) { 349 mainJar = fname; 350 return mainJar; 351 } 352 } 353 } catch (IOException ignore) { 354 } 355 } 356 return null; 357 } 358 359 public String getVendor() { 360 return fetchParam(VENDOR); 361 } 362 363 public void setVendor(String vendor) { 364 putUnlessNull(PARAM_VENDOR, vendor); 365 } 366 367 public String getEmail() { 368 return fetchParam(String.class, PARAM_EMAIL); 369 } 370 371 public void setEmail(String email) { 372 putUnlessNull(PARAM_EMAIL, email); 373 } 374 375 public void putUnlessNull(String param, Object value) { 376 if (value != null) { 377 params.put(param, value); 378 } 379 } 380 381 public void putUnlessNullOrEmpty(String param, Collection<?> value) { 382 if (value != null && !value.isEmpty()) { 383 params.put(param, value); 384 } 385 } 386 387 public void putUnlessNullOrEmpty(String param, Map<?,?> value) { 388 if (value != null && !value.isEmpty()) { 389 params.put(param, value); 390 } 391 } 392 393 }