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 <T> T fetchParam(BundlerParamInfo<T> paramInfo) { 117 return paramInfo.fetchFrom(params); 118 } 119 120 @SuppressWarnings("unchecked") 121 public <T> T fetchParamWithDefault( 122 Class<T> klass, T defaultValue, String... keys) { 123 for (String key : keys) { 124 Object o = params.get(key); 125 if (klass.isInstance(o)) { 126 return (T) 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 <T> T fetchParam(Class<T> 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(JAVA_OPTIONS.getID(), jvmargs); 150 } 151 152 public void setAddModules(String value) { 153 putUnlessNull(StandardBundlerParam.ADD_MODULES.getID(), value); 154 } 155 156 public void setLimitModules(String value) { 157 putUnlessNull(StandardBundlerParam.LIMIT_MODULES.getID(), value); 158 } 159 160 public void setModulePath(String value) { 161 putUnlessNull(StandardBundlerParam.MODULE_PATH.getID(), value); 162 } 163 164 public void setMainModule(String value) { 165 putUnlessNull(StandardBundlerParam.MODULE.getID(), value); 166 } 167 168 public String getApplicationID() { 169 return fetchParam(IDENTIFIER); 170 } 171 172 public String getApplicationClass() { 173 return fetchParam(MAIN_CLASS); 174 } 175 176 public void setApplicationClass(String applicationClass) { 177 putUnlessNull(PARAM_APPLICATION_CLASS, applicationClass); 178 } 179 180 public String getAppVersion() { 181 return fetchParam(VERSION); 182 } 183 184 public void setAppVersion(String version) { 185 putUnlessNull(PARAM_VERSION, version); 186 } 187 188 public String getDescription() { 189 return fetchParam(DESCRIPTION); 190 } 191 192 public void setDescription(String s) { 193 putUnlessNull(PARAM_DESCRIPTION, s); 194 } 195 196 public void setInstalldirChooser(Boolean b) { 197 putUnlessNull(PARAM_INSTALLDIR_CHOOSER, b); 198 } 199 200 public String getName() { 201 return fetchParam(APP_NAME); 202 } 203 204 public void setName(String name) { 205 putUnlessNull(PARAM_NAME, name); 206 } 207 208 @SuppressWarnings("deprecation") 209 public BundlerType getType() { 210 return fetchParam(BundlerType.class, PARAM_TYPE); 211 } 212 213 @SuppressWarnings("deprecation") 214 public void setType(BundlerType type) { 215 putUnlessNull(PARAM_TYPE, type); 216 } 217 218 public String getBundleFormat() { 219 return fetchParam(String.class, PARAM_BUNDLE_FORMAT); 220 } 221 222 public void setBundleFormat(String t) { 223 putUnlessNull(PARAM_BUNDLE_FORMAT, t); 224 } 225 226 public boolean getVerbose() { 227 return fetchParam(VERBOSE); 228 } 229 230 public List<String> getJvmargs() { 231 return JAVA_OPTIONS.fetchFrom(params); 232 } 233 234 public jdk.jpackage.internal.RelativeFileSet getAppResource() { 235 return fetchParam(APP_RESOURCES); 236 } 237 238 public void setAppResource(jdk.jpackage.internal.RelativeFileSet fs) { 239 putUnlessNull(PARAM_APP_RESOURCES, fs); 240 } 241 242 public void setAppResourcesList( 243 List<jdk.jpackage.internal.RelativeFileSet> rfs) { 244 putUnlessNull(APP_RESOURCES_LIST.getID(), rfs); 245 } 246 247 public String getMainClassName() { 248 String applicationClass = getApplicationClass(); 249 250 if (applicationClass == null) { 251 return null; 252 } 253 254 int idx = applicationClass.lastIndexOf("."); 255 if (idx >= 0) { 256 return applicationClass.substring(idx+1); 257 } 258 return applicationClass; 259 } 260 261 public String getCopyright() { 262 return fetchParam(COPYRIGHT); 263 } 264 265 public void setCopyright(String c) { 266 putUnlessNull(PARAM_COPYRIGHT, c); 267 } 268 269 private String mainJar = null; 270 271 // assuming that application was packaged according to the rules 272 // we must have application jar, i.e. jar where we embed launcher 273 // and have main application class listed as main class! 274 // If there are more than one, or none - it will be treated as an error 275 276 public String getMainApplicationJar() { 277 jdk.jpackage.internal.RelativeFileSet appResources = getAppResource(); 278 if (mainJar != null) { 279 if (getApplicationClass() == null) try { 280 if (appResources != null) { 281 File srcdir = appResources.getBaseDirectory(); 282 JarFile jf = new JarFile(new File(srcdir, mainJar)); 283 Manifest m = jf.getManifest(); 284 Attributes attrs = (m != null) ? 285 m.getMainAttributes() : null; 286 if (attrs != null) { 287 setApplicationClass( 288 attrs.getValue(Attributes.Name.MAIN_CLASS)); 289 } 290 } 291 } catch (IOException ignore) { 292 } 293 return mainJar; 294 } 295 296 String applicationClass = getApplicationClass(); 297 298 if (appResources == null || applicationClass == null) { 299 return null; 300 } 301 File srcdir = appResources.getBaseDirectory(); 302 for (String fname : appResources.getIncludedFiles()) { 303 JarFile jf; 304 try { 305 jf = new JarFile(new File(srcdir, fname)); 306 Manifest m = jf.getManifest(); 307 Attributes attrs = (m != null) ? m.getMainAttributes() : null; 308 if (attrs != null) { 309 boolean javaMain = applicationClass.equals( 310 attrs.getValue(Attributes.Name.MAIN_CLASS)); 311 312 if (javaMain) { 313 mainJar = fname; 314 return mainJar; 315 } 316 } 317 } catch (IOException ignore) { 318 } 319 } 320 return null; 321 } 322 323 public String getVendor() { 324 return fetchParam(VENDOR); 325 } 326 327 public void setVendor(String vendor) { 328 putUnlessNull(PARAM_VENDOR, vendor); 329 } 330 331 public String getEmail() { 332 return fetchParam(String.class, PARAM_EMAIL); 333 } 334 335 public void setEmail(String email) { 336 putUnlessNull(PARAM_EMAIL, email); 337 } 338 339 public void putUnlessNull(String param, Object value) { 340 if (value != null) { 341 params.put(param, value); 342 } 343 } 344 345 public void putUnlessNullOrEmpty(String param, Collection<?> value) { 346 if (value != null && !value.isEmpty()) { 347 params.put(param, value); 348 } 349 } 350 351 public void putUnlessNullOrEmpty(String param, Map<?,?> value) { 352 if (value != null && !value.isEmpty()) { 353 params.put(param, value); 354 } 355 } 356 357 }