57038
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 2018, 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 |
|
57039
|
26 |
package jdk.jpackage.internal;
|
57038
|
27 |
|
57039
|
28 |
import jdk.jpackage.internal.BundlerParamInfo;
|
|
29 |
import jdk.jpackage.internal.IOUtils;
|
|
30 |
import jdk.jpackage.internal.Log;
|
|
31 |
import jdk.jpackage.internal.RelativeFileSet;
|
|
32 |
import jdk.jpackage.internal.StandardBundlerParam;
|
|
33 |
import jdk.jpackage.internal.resources.LinuxResources;
|
|
34 |
import jdk.jpackage.internal.AbstractAppImageBuilder;
|
57038
|
35 |
|
|
36 |
import java.io.File;
|
|
37 |
import java.io.FileInputStream;
|
|
38 |
import java.io.FileOutputStream;
|
|
39 |
import java.io.IOException;
|
|
40 |
import java.io.InputStream;
|
|
41 |
import java.io.OutputStream;
|
|
42 |
import java.io.OutputStreamWriter;
|
|
43 |
import java.io.UncheckedIOException;
|
|
44 |
import java.io.Writer;
|
|
45 |
import java.nio.file.Files;
|
|
46 |
import java.nio.file.Path;
|
|
47 |
import java.nio.file.attribute.PosixFilePermission;
|
|
48 |
import java.text.MessageFormat;
|
|
49 |
import java.util.HashMap;
|
|
50 |
import java.util.List;
|
|
51 |
import java.util.Map;
|
|
52 |
import java.util.Objects;
|
|
53 |
import java.util.ResourceBundle;
|
|
54 |
import java.util.Set;
|
|
55 |
|
57039
|
56 |
import static jdk.jpackage.internal.StandardBundlerParam.*;
|
57038
|
57 |
|
|
58 |
public class LinuxAppImageBuilder extends AbstractAppImageBuilder {
|
|
59 |
|
|
60 |
private static final ResourceBundle I18N = ResourceBundle.getBundle(
|
57059
|
61 |
"jdk.jpackage.internal.resources.LinuxResources");
|
57038
|
62 |
|
|
63 |
private static final String EXECUTABLE_NAME = "JavaAppLauncher";
|
57039
|
64 |
private static final String LIBRARY_NAME = "libjpackage.so";
|
57038
|
65 |
|
|
66 |
private final Path root;
|
|
67 |
private final Path appDir;
|
|
68 |
private final Path runtimeDir;
|
|
69 |
private final Path resourcesDir;
|
|
70 |
private final Path mdir;
|
|
71 |
|
|
72 |
private final Map<String, ? super Object> params;
|
|
73 |
|
|
74 |
public static final BundlerParamInfo<File> ICON_PNG =
|
|
75 |
new StandardBundlerParam<>(
|
|
76 |
I18N.getString("param.icon-png.name"),
|
|
77 |
I18N.getString("param.icon-png.description"),
|
|
78 |
"icon.png",
|
|
79 |
File.class,
|
|
80 |
params -> {
|
|
81 |
File f = ICON.fetchFrom(params);
|
|
82 |
if (f != null && !f.getName().toLowerCase().endsWith(".png")) {
|
|
83 |
Log.error(MessageFormat.format(I18N.getString(
|
|
84 |
"message.icon-not-png"), f));
|
|
85 |
return null;
|
|
86 |
}
|
|
87 |
return f;
|
|
88 |
},
|
|
89 |
(s, p) -> new File(s));
|
|
90 |
|
|
91 |
public LinuxAppImageBuilder(Map<String, Object> config, Path imageOutDir)
|
|
92 |
throws IOException {
|
|
93 |
super(config,
|
|
94 |
imageOutDir.resolve(APP_NAME.fetchFrom(config) + "/runtime"));
|
|
95 |
|
|
96 |
Objects.requireNonNull(imageOutDir);
|
|
97 |
|
|
98 |
this.root = imageOutDir.resolve(APP_NAME.fetchFrom(config));
|
|
99 |
this.appDir = root.resolve("app");
|
|
100 |
this.runtimeDir = root.resolve("runtime");
|
|
101 |
this.resourcesDir = root.resolve("resources");
|
|
102 |
this.mdir = runtimeDir.resolve("lib");
|
|
103 |
this.params = new HashMap<>();
|
|
104 |
config.entrySet().stream().forEach(e -> params.put(
|
|
105 |
e.getKey().toString(), e.getValue()));
|
|
106 |
Files.createDirectories(appDir);
|
|
107 |
Files.createDirectories(runtimeDir);
|
|
108 |
Files.createDirectories(resourcesDir);
|
|
109 |
}
|
|
110 |
|
|
111 |
public LinuxAppImageBuilder(String appName, Path imageOutDir)
|
|
112 |
throws IOException {
|
|
113 |
super(null, imageOutDir.resolve(appName));
|
|
114 |
|
|
115 |
Objects.requireNonNull(imageOutDir);
|
|
116 |
|
|
117 |
this.root = imageOutDir.resolve(appName);
|
|
118 |
this.appDir = null;
|
|
119 |
this.runtimeDir = null;
|
|
120 |
this.resourcesDir = null;
|
|
121 |
this.mdir = null;
|
|
122 |
this.params = new HashMap<>();
|
|
123 |
}
|
|
124 |
|
|
125 |
private Path destFile(String dir, String filename) {
|
|
126 |
return runtimeDir.resolve(dir).resolve(filename);
|
|
127 |
}
|
|
128 |
|
|
129 |
private void writeEntry(InputStream in, Path dstFile) throws IOException {
|
|
130 |
Files.createDirectories(dstFile.getParent());
|
|
131 |
Files.copy(in, dstFile);
|
|
132 |
}
|
|
133 |
|
|
134 |
private void writeSymEntry(Path dstFile, Path target) throws IOException {
|
|
135 |
Files.createDirectories(dstFile.getParent());
|
|
136 |
Files.createLink(dstFile, target);
|
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
|
140 |
* chmod ugo+x file
|
|
141 |
*/
|
|
142 |
private void setExecutable(Path file) {
|
|
143 |
try {
|
|
144 |
Set<PosixFilePermission> perms =
|
|
145 |
Files.getPosixFilePermissions(file);
|
|
146 |
perms.add(PosixFilePermission.OWNER_EXECUTE);
|
|
147 |
perms.add(PosixFilePermission.GROUP_EXECUTE);
|
|
148 |
perms.add(PosixFilePermission.OTHERS_EXECUTE);
|
|
149 |
Files.setPosixFilePermissions(file, perms);
|
|
150 |
} catch (IOException ioe) {
|
|
151 |
throw new UncheckedIOException(ioe);
|
|
152 |
}
|
|
153 |
}
|
|
154 |
|
|
155 |
private static void createUtf8File(File file, String content)
|
|
156 |
throws IOException {
|
|
157 |
try (OutputStream fout = new FileOutputStream(file);
|
|
158 |
Writer output = new OutputStreamWriter(fout, "UTF-8")) {
|
|
159 |
output.write(content);
|
|
160 |
}
|
|
161 |
}
|
|
162 |
|
|
163 |
|
|
164 |
// it is static for the sake of sharing with "installer" bundlers
|
|
165 |
// that may skip calls to validate/bundle in this class!
|
|
166 |
public static File getRootDir(File outDir, Map<String, ? super Object> p) {
|
|
167 |
return new File(outDir, APP_FS_NAME.fetchFrom(p));
|
|
168 |
}
|
|
169 |
|
|
170 |
public static String getLauncherName(Map<String, ? super Object> p) {
|
|
171 |
return APP_FS_NAME.fetchFrom(p);
|
|
172 |
}
|
|
173 |
|
|
174 |
public static String getLauncherCfgName(Map<String, ? super Object> p) {
|
|
175 |
return "app/" + APP_FS_NAME.fetchFrom(p) + ".cfg";
|
|
176 |
}
|
|
177 |
|
|
178 |
@Override
|
|
179 |
public InputStream getResourceAsStream(String name) {
|
|
180 |
return LinuxResources.class.getResourceAsStream(name);
|
|
181 |
}
|
|
182 |
|
|
183 |
@Override
|
|
184 |
public void prepareApplicationFiles() throws IOException {
|
|
185 |
Map<String, ? super Object> originalParams = new HashMap<>(params);
|
|
186 |
|
|
187 |
// create the primary launcher
|
|
188 |
createLauncherForEntryPoint(params, root);
|
|
189 |
|
|
190 |
// Copy library to the launcher folder
|
|
191 |
try (InputStream is_lib = getResourceAsStream(LIBRARY_NAME)) {
|
|
192 |
writeEntry(is_lib, root.resolve(LIBRARY_NAME));
|
|
193 |
}
|
|
194 |
|
|
195 |
// create the secondary launchers, if any
|
|
196 |
List<Map<String, ? super Object>> entryPoints
|
|
197 |
= StandardBundlerParam.SECONDARY_LAUNCHERS.fetchFrom(params);
|
|
198 |
for (Map<String, ? super Object> entryPoint : entryPoints) {
|
|
199 |
Map<String, ? super Object> tmp = new HashMap<>(originalParams);
|
|
200 |
tmp.putAll(entryPoint);
|
|
201 |
// remove name.fs that was calculated for main launcher.
|
|
202 |
// otherwise, wrong launcher name will be selected.
|
|
203 |
tmp.remove(APP_FS_NAME.getID());
|
|
204 |
createLauncherForEntryPoint(tmp, root);
|
|
205 |
}
|
|
206 |
|
|
207 |
// Copy class path entries to Java folder
|
|
208 |
copyApplication();
|
|
209 |
|
|
210 |
// Copy icon to Resources folder
|
|
211 |
copyIcon();
|
|
212 |
}
|
|
213 |
|
|
214 |
@Override
|
|
215 |
public void prepareServerJreFiles() throws IOException {}
|
|
216 |
|
|
217 |
private void createLauncherForEntryPoint(Map<String, ? super Object> p,
|
|
218 |
Path rootDir) throws IOException {
|
|
219 |
// Copy executable to Linux folder
|
|
220 |
Path executableFile = root.resolve(getLauncherName(p));
|
|
221 |
try (InputStream is_launcher = getResourceAsStream("papplauncher")) {
|
|
222 |
writeEntry(is_launcher, executableFile);
|
|
223 |
}
|
|
224 |
|
|
225 |
executableFile.toFile().setExecutable(true, false);
|
|
226 |
executableFile.toFile().setWritable(true, true);
|
|
227 |
|
|
228 |
writeCfgFile(p, root.resolve(getLauncherCfgName(p)).toFile(),
|
|
229 |
"$APPDIR/runtime");
|
|
230 |
}
|
|
231 |
|
|
232 |
private void copyIcon() throws IOException {
|
|
233 |
File icon = ICON_PNG.fetchFrom(params);
|
|
234 |
if (icon != null) {
|
|
235 |
File iconTarget = new File(resourcesDir.toFile(),
|
|
236 |
APP_FS_NAME.fetchFrom(params) + ".png");
|
|
237 |
IOUtils.copyFile(icon, iconTarget);
|
|
238 |
}
|
|
239 |
}
|
|
240 |
|
|
241 |
private void copyApplication() throws IOException {
|
|
242 |
for (RelativeFileSet appResources :
|
|
243 |
APP_RESOURCES_LIST.fetchFrom(params)) {
|
|
244 |
if (appResources == null) {
|
|
245 |
throw new RuntimeException("Null app resources?");
|
|
246 |
}
|
|
247 |
File srcdir = appResources.getBaseDirectory();
|
|
248 |
for (String fname : appResources.getIncludedFiles()) {
|
|
249 |
copyEntry(appDir, srcdir, fname);
|
|
250 |
}
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
}
|