author | herrick |
Fri, 21 Dec 2018 07:40:47 -0500 | |
branch | JDK-8200758-branch |
changeset 57091 | 06bc4bd64599 |
parent 57078 | db003bfc5bf7 |
child 57106 | ea870b9ce89a |
permissions | -rw-r--r-- |
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 |
|
28 |
import java.io.File; |
|
29 |
import java.io.FileInputStream; |
|
30 |
import java.io.FileOutputStream; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.InputStream; |
|
33 |
import java.io.OutputStream; |
|
34 |
import java.io.OutputStreamWriter; |
|
35 |
import java.io.UncheckedIOException; |
|
36 |
import java.io.Writer; |
|
37 |
import java.nio.file.Files; |
|
38 |
import java.nio.file.Path; |
|
39 |
import java.nio.file.attribute.PosixFilePermission; |
|
40 |
import java.text.MessageFormat; |
|
41 |
import java.util.HashMap; |
|
42 |
import java.util.List; |
|
43 |
import java.util.Map; |
|
44 |
import java.util.Objects; |
|
45 |
import java.util.ResourceBundle; |
|
46 |
import java.util.Set; |
|
47 |
||
57039 | 48 |
import static jdk.jpackage.internal.StandardBundlerParam.*; |
57038 | 49 |
|
50 |
public class LinuxAppImageBuilder extends AbstractAppImageBuilder { |
|
51 |
||
52 |
private static final ResourceBundle I18N = ResourceBundle.getBundle( |
|
57059 | 53 |
"jdk.jpackage.internal.resources.LinuxResources"); |
57038 | 54 |
|
57064
a7fdadf67a92
8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
57059
diff
changeset
|
55 |
private static final String LIBRARY_NAME = "libapplauncher.so"; |
57038 | 56 |
|
57 |
private final Path root; |
|
58 |
private final Path appDir; |
|
57077
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
59 |
private final Path appModsDir; |
57038 | 60 |
private final Path runtimeDir; |
61 |
private final Path resourcesDir; |
|
62 |
private final Path mdir; |
|
63 |
||
64 |
private final Map<String, ? super Object> params; |
|
65 |
||
66 |
public static final BundlerParamInfo<File> ICON_PNG = |
|
67 |
new StandardBundlerParam<>( |
|
68 |
I18N.getString("param.icon-png.name"), |
|
69 |
I18N.getString("param.icon-png.description"), |
|
70 |
"icon.png", |
|
71 |
File.class, |
|
72 |
params -> { |
|
73 |
File f = ICON.fetchFrom(params); |
|
74 |
if (f != null && !f.getName().toLowerCase().endsWith(".png")) { |
|
75 |
Log.error(MessageFormat.format(I18N.getString( |
|
76 |
"message.icon-not-png"), f)); |
|
77 |
return null; |
|
78 |
} |
|
79 |
return f; |
|
80 |
}, |
|
81 |
(s, p) -> new File(s)); |
|
82 |
||
83 |
public LinuxAppImageBuilder(Map<String, Object> config, Path imageOutDir) |
|
84 |
throws IOException { |
|
85 |
super(config, |
|
86 |
imageOutDir.resolve(APP_NAME.fetchFrom(config) + "/runtime")); |
|
87 |
||
88 |
Objects.requireNonNull(imageOutDir); |
|
89 |
||
90 |
this.root = imageOutDir.resolve(APP_NAME.fetchFrom(config)); |
|
91 |
this.appDir = root.resolve("app"); |
|
57077
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
92 |
this.appModsDir = appDir.resolve("mods"); |
57038 | 93 |
this.runtimeDir = root.resolve("runtime"); |
94 |
this.resourcesDir = root.resolve("resources"); |
|
95 |
this.mdir = runtimeDir.resolve("lib"); |
|
96 |
this.params = new HashMap<>(); |
|
97 |
config.entrySet().stream().forEach(e -> params.put( |
|
98 |
e.getKey().toString(), e.getValue())); |
|
99 |
Files.createDirectories(appDir); |
|
100 |
Files.createDirectories(runtimeDir); |
|
101 |
Files.createDirectories(resourcesDir); |
|
102 |
} |
|
103 |
||
104 |
public LinuxAppImageBuilder(String appName, Path imageOutDir) |
|
105 |
throws IOException { |
|
106 |
super(null, imageOutDir.resolve(appName)); |
|
107 |
||
108 |
Objects.requireNonNull(imageOutDir); |
|
109 |
||
110 |
this.root = imageOutDir.resolve(appName); |
|
111 |
this.appDir = null; |
|
57077
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
112 |
this.appModsDir = null; |
57038 | 113 |
this.runtimeDir = null; |
114 |
this.resourcesDir = null; |
|
115 |
this.mdir = null; |
|
116 |
this.params = new HashMap<>(); |
|
117 |
} |
|
118 |
||
119 |
private Path destFile(String dir, String filename) { |
|
120 |
return runtimeDir.resolve(dir).resolve(filename); |
|
121 |
} |
|
122 |
||
123 |
private void writeEntry(InputStream in, Path dstFile) throws IOException { |
|
124 |
Files.createDirectories(dstFile.getParent()); |
|
125 |
Files.copy(in, dstFile); |
|
126 |
} |
|
127 |
||
128 |
private void writeSymEntry(Path dstFile, Path target) throws IOException { |
|
129 |
Files.createDirectories(dstFile.getParent()); |
|
130 |
Files.createLink(dstFile, target); |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* chmod ugo+x file |
|
135 |
*/ |
|
136 |
private void setExecutable(Path file) { |
|
137 |
try { |
|
138 |
Set<PosixFilePermission> perms = |
|
139 |
Files.getPosixFilePermissions(file); |
|
140 |
perms.add(PosixFilePermission.OWNER_EXECUTE); |
|
141 |
perms.add(PosixFilePermission.GROUP_EXECUTE); |
|
142 |
perms.add(PosixFilePermission.OTHERS_EXECUTE); |
|
143 |
Files.setPosixFilePermissions(file, perms); |
|
144 |
} catch (IOException ioe) { |
|
145 |
throw new UncheckedIOException(ioe); |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
private static void createUtf8File(File file, String content) |
|
150 |
throws IOException { |
|
151 |
try (OutputStream fout = new FileOutputStream(file); |
|
152 |
Writer output = new OutputStreamWriter(fout, "UTF-8")) { |
|
153 |
output.write(content); |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
||
158 |
// it is static for the sake of sharing with "installer" bundlers |
|
159 |
// that may skip calls to validate/bundle in this class! |
|
160 |
public static File getRootDir(File outDir, Map<String, ? super Object> p) { |
|
161 |
return new File(outDir, APP_FS_NAME.fetchFrom(p)); |
|
162 |
} |
|
163 |
||
164 |
public static String getLauncherName(Map<String, ? super Object> p) { |
|
165 |
return APP_FS_NAME.fetchFrom(p); |
|
166 |
} |
|
167 |
||
168 |
public static String getLauncherCfgName(Map<String, ? super Object> p) { |
|
169 |
return "app/" + APP_FS_NAME.fetchFrom(p) + ".cfg"; |
|
170 |
} |
|
171 |
||
172 |
@Override |
|
57077
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
173 |
public Path getAppDir() { |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
174 |
return appDir; |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
175 |
} |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
176 |
|
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
177 |
@Override |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
178 |
public Path getAppModsDir() { |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
179 |
return appModsDir; |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
180 |
} |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
181 |
|
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
182 |
@Override |
57038 | 183 |
public void prepareApplicationFiles() throws IOException { |
184 |
Map<String, ? super Object> originalParams = new HashMap<>(params); |
|
185 |
||
186 |
// create the primary launcher |
|
187 |
createLauncherForEntryPoint(params, root); |
|
188 |
||
189 |
// Copy library to the launcher folder |
|
190 |
try (InputStream is_lib = getResourceAsStream(LIBRARY_NAME)) { |
|
191 |
writeEntry(is_lib, root.resolve(LIBRARY_NAME)); |
|
192 |
} |
|
193 |
||
194 |
// create the secondary launchers, if any |
|
195 |
List<Map<String, ? super Object>> entryPoints |
|
196 |
= StandardBundlerParam.SECONDARY_LAUNCHERS.fetchFrom(params); |
|
197 |
for (Map<String, ? super Object> entryPoint : entryPoints) { |
|
198 |
Map<String, ? super Object> tmp = new HashMap<>(originalParams); |
|
199 |
tmp.putAll(entryPoint); |
|
200 |
// remove name.fs that was calculated for main launcher. |
|
201 |
// otherwise, wrong launcher name will be selected. |
|
202 |
tmp.remove(APP_FS_NAME.getID()); |
|
203 |
createLauncherForEntryPoint(tmp, root); |
|
204 |
} |
|
205 |
||
206 |
// Copy class path entries to Java folder |
|
207 |
copyApplication(); |
|
208 |
||
209 |
// Copy icon to Resources folder |
|
210 |
copyIcon(); |
|
211 |
} |
|
212 |
||
213 |
@Override |
|
57070 | 214 |
public void prepareJreFiles() throws IOException {} |
57038 | 215 |
|
216 |
private void createLauncherForEntryPoint(Map<String, ? super Object> p, |
|
217 |
Path rootDir) throws IOException { |
|
218 |
// Copy executable to Linux folder |
|
219 |
Path executableFile = root.resolve(getLauncherName(p)); |
|
57064
a7fdadf67a92
8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
57059
diff
changeset
|
220 |
try (InputStream is_launcher = |
a7fdadf67a92
8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
57059
diff
changeset
|
221 |
getResourceAsStream("jpackageapplauncher")) { |
57038 | 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 |
} |