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