1 /* 2 * Copyright (c) 2020, 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 package jdk.incubator.jpackage.internal; 26 27 import java.io.IOException; 28 import java.io.File; 29 import java.nio.file.Files; 30 import java.nio.file.Path; 31 import java.util.ArrayList; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.stream.Stream; 35 import static jdk.incubator.jpackage.internal.StandardBundlerParam.LAUNCHER_DATA; 36 import static jdk.incubator.jpackage.internal.StandardBundlerParam.APP_NAME; 37 import static jdk.incubator.jpackage.internal.StandardBundlerParam.JAVA_OPTIONS; 38 import static jdk.incubator.jpackage.internal.StandardBundlerParam.ARGUMENTS; 39 import static jdk.incubator.jpackage.internal.StandardBundlerParam.VERSION; 40 41 /** 42 * App launcher's config file. 43 */ 44 final class CfgFile { 45 CfgFile() { 46 appLayout = ApplicationLayout.platformAppImage(); 47 } 48 49 CfgFile initFromParams(Map<String, ? super Object> params) { 50 launcherData = LAUNCHER_DATA.fetchFrom(params); 51 launcherName = APP_NAME.fetchFrom(params); 52 javaOptions = JAVA_OPTIONS.fetchFrom(params); 53 arguments = ARGUMENTS.fetchFrom(params); 54 version = VERSION.fetchFrom(params); 55 return this; 56 } 57 58 void create(Path appImage) throws IOException { 59 List<Map.Entry<String, Object>> content = new ArrayList<>(); 60 61 ApplicationLayout appCfgLayout = createAppCfgLayout(); 62 63 content.add(Map.entry("[Application]", SECTION_TAG)); 64 65 if (launcherData.isModular()) { 66 content.add(Map.entry("app.mainmodule", launcherData.moduleName() 67 + "/" + launcherData.qualifiedClassName())); 68 } else { 69 // If the app is contained in an unnamed jar then launch it the 70 // legacy way and the main class string must be 71 // of the format com/foo/Main 72 if (launcherData.mainJarName() != null) { 73 content.add(Map.entry("app.classpath", 74 appCfgLayout.appDirectory().resolve( 75 launcherData.mainJarName()))); 76 } 77 content.add(Map.entry("app.mainclass", 78 launcherData.qualifiedClassName())); 79 } 80 81 for (var value : launcherData.classPath()) { 82 content.add(Map.entry("app.classpath", 83 appCfgLayout.appDirectory().resolve(value).toString())); 84 } 85 86 ApplicationLayout appImagelayout = appLayout.resolveAt(appImage); 87 Path modsDir = appImagelayout.appModsDirectory(); 88 89 content.add(Map.entry("[JavaOptions]", SECTION_TAG)); 90 91 // always let app know it's name and version 92 content.add(Map.entry( 93 "java-options", "-Djpackage.app-name=" + launcherName)); 94 content.add(Map.entry( 95 "java-options", "-Djpackage.app-version=" + version)); 96 content.add(Map.entry( 97 "java-options", "-Djpackage.app-path=" + "$BINDIR" 98 + File.separator + launcherName)); 99 100 // add user supplied java options if there are any 101 for (var value : javaOptions) { 102 content.add(Map.entry("java-options", value)); 103 } 104 105 // add module path if there is one 106 if (Files.isDirectory(modsDir)) { 107 content.add(Map.entry("java-options", "--module-path")); 108 content.add(Map.entry("java-options", 109 appCfgLayout.appModsDirectory())); 110 } 111 112 if (!arguments.isEmpty()) { 113 content.add(Map.entry("[ArgOptions]", SECTION_TAG)); 114 for (var value : arguments) { 115 content.add(Map.entry("arguments", value)); 116 } 117 } 118 119 Path cfgFile = appImagelayout.appDirectory().resolve(launcherName + ".cfg"); 120 Files.createDirectories(cfgFile.getParent()); 121 122 boolean[] addLineBreakAtSection = new boolean[1]; 123 Stream<String> lines = content.stream().map(entry -> { 124 if (entry.getValue() == SECTION_TAG) { 125 if (!addLineBreakAtSection[0]) { 126 addLineBreakAtSection[0] = true; 127 return entry.getKey(); 128 } 129 return "\n" + entry.getKey(); 130 } 131 return entry.getKey() + "=" + entry.getValue(); 132 }); 133 Files.write(cfgFile, (Iterable<String>) lines::iterator); 134 } 135 136 private ApplicationLayout createAppCfgLayout() { 137 ApplicationLayout appCfgLayout = appLayout.resolveAt(Path.of("$ROOTDIR")); 138 appCfgLayout.pathGroup().setPath(ApplicationLayout.PathRole.APP, 139 Path.of("$APPDIR")); 140 appCfgLayout.pathGroup().setPath(ApplicationLayout.PathRole.MODULES, 141 appCfgLayout.appDirectory().resolve(appCfgLayout.appModsDirectory().getFileName())); 142 return appCfgLayout; 143 } 144 145 private String launcherName; 146 private String version; 147 private LauncherData launcherData; 148 List<String> arguments; 149 List<String> javaOptions; 150 private final ApplicationLayout appLayout; 151 152 private final static Object SECTION_TAG = new Object(); 153 }