author | herrick |
Wed, 13 Mar 2019 13:37:26 -0400 | |
branch | JDK-8200758-branch |
changeset 57264 | 2624a38ca35a |
parent 57263 | 86e75b8f99c4 |
child 57392 | 46d4b0aa4542 |
permissions | -rw-r--r-- |
57038 | 1 |
/* |
57106
ea870b9ce89a
8216492: Update copyright of all new jpackage fils to 2019
kcr
parents:
57091
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
57038 | 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 |
"icon.png", |
|
69 |
File.class, |
|
70 |
params -> { |
|
71 |
File f = ICON.fetchFrom(params); |
|
72 |
if (f != null && !f.getName().toLowerCase().endsWith(".png")) { |
|
73 |
Log.error(MessageFormat.format(I18N.getString( |
|
74 |
"message.icon-not-png"), f)); |
|
75 |
return null; |
|
76 |
} |
|
77 |
return f; |
|
78 |
}, |
|
79 |
(s, p) -> new File(s)); |
|
80 |
||
81 |
public LinuxAppImageBuilder(Map<String, Object> config, Path imageOutDir) |
|
82 |
throws IOException { |
|
83 |
super(config, |
|
84 |
imageOutDir.resolve(APP_NAME.fetchFrom(config) + "/runtime")); |
|
85 |
||
86 |
Objects.requireNonNull(imageOutDir); |
|
87 |
||
88 |
this.root = imageOutDir.resolve(APP_NAME.fetchFrom(config)); |
|
89 |
this.appDir = root.resolve("app"); |
|
57077
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
90 |
this.appModsDir = appDir.resolve("mods"); |
57038 | 91 |
this.runtimeDir = root.resolve("runtime"); |
92 |
this.resourcesDir = root.resolve("resources"); |
|
93 |
this.mdir = runtimeDir.resolve("lib"); |
|
94 |
this.params = new HashMap<>(); |
|
95 |
config.entrySet().stream().forEach(e -> params.put( |
|
96 |
e.getKey().toString(), e.getValue())); |
|
97 |
Files.createDirectories(appDir); |
|
98 |
Files.createDirectories(runtimeDir); |
|
99 |
Files.createDirectories(resourcesDir); |
|
100 |
} |
|
101 |
||
102 |
public LinuxAppImageBuilder(String appName, Path imageOutDir) |
|
103 |
throws IOException { |
|
104 |
super(null, imageOutDir.resolve(appName)); |
|
105 |
||
106 |
Objects.requireNonNull(imageOutDir); |
|
107 |
||
108 |
this.root = imageOutDir.resolve(appName); |
|
109 |
this.appDir = null; |
|
57077
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
110 |
this.appModsDir = null; |
57038 | 111 |
this.runtimeDir = null; |
112 |
this.resourcesDir = null; |
|
113 |
this.mdir = null; |
|
114 |
this.params = new HashMap<>(); |
|
115 |
} |
|
116 |
||
117 |
private Path destFile(String dir, String filename) { |
|
118 |
return runtimeDir.resolve(dir).resolve(filename); |
|
119 |
} |
|
120 |
||
121 |
private void writeEntry(InputStream in, Path dstFile) throws IOException { |
|
122 |
Files.createDirectories(dstFile.getParent()); |
|
123 |
Files.copy(in, dstFile); |
|
124 |
} |
|
125 |
||
126 |
private void writeSymEntry(Path dstFile, Path target) throws IOException { |
|
127 |
Files.createDirectories(dstFile.getParent()); |
|
128 |
Files.createLink(dstFile, target); |
|
129 |
} |
|
130 |
||
131 |
/** |
|
132 |
* chmod ugo+x file |
|
133 |
*/ |
|
134 |
private void setExecutable(Path file) { |
|
135 |
try { |
|
136 |
Set<PosixFilePermission> perms = |
|
137 |
Files.getPosixFilePermissions(file); |
|
138 |
perms.add(PosixFilePermission.OWNER_EXECUTE); |
|
139 |
perms.add(PosixFilePermission.GROUP_EXECUTE); |
|
140 |
perms.add(PosixFilePermission.OTHERS_EXECUTE); |
|
141 |
Files.setPosixFilePermissions(file, perms); |
|
142 |
} catch (IOException ioe) { |
|
143 |
throw new UncheckedIOException(ioe); |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
private static void createUtf8File(File file, String content) |
|
148 |
throws IOException { |
|
149 |
try (OutputStream fout = new FileOutputStream(file); |
|
150 |
Writer output = new OutputStreamWriter(fout, "UTF-8")) { |
|
151 |
output.write(content); |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
||
156 |
// it is static for the sake of sharing with "installer" bundlers |
|
157 |
// that may skip calls to validate/bundle in this class! |
|
158 |
public static File getRootDir(File outDir, Map<String, ? super Object> p) { |
|
57119 | 159 |
return new File(outDir, APP_NAME.fetchFrom(p)); |
57038 | 160 |
} |
161 |
||
162 |
public static String getLauncherName(Map<String, ? super Object> p) { |
|
57119 | 163 |
return APP_NAME.fetchFrom(p); |
57038 | 164 |
} |
165 |
||
166 |
public static String getLauncherCfgName(Map<String, ? super Object> p) { |
|
57119 | 167 |
return "app/" + APP_NAME.fetchFrom(p) + ".cfg"; |
57038 | 168 |
} |
169 |
||
170 |
@Override |
|
57077
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
171 |
public Path getAppDir() { |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
172 |
return appDir; |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
173 |
} |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
174 |
|
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
175 |
@Override |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
176 |
public Path getAppModsDir() { |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
177 |
return appModsDir; |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
178 |
} |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
179 |
|
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
180 |
@Override |
57038 | 181 |
public void prepareApplicationFiles() throws IOException { |
182 |
Map<String, ? super Object> originalParams = new HashMap<>(params); |
|
183 |
||
184 |
// create the primary launcher |
|
57263 | 185 |
createLauncherForEntryPoint(params); |
57038 | 186 |
|
187 |
// Copy library to the launcher folder |
|
188 |
try (InputStream is_lib = getResourceAsStream(LIBRARY_NAME)) { |
|
189 |
writeEntry(is_lib, root.resolve(LIBRARY_NAME)); |
|
190 |
} |
|
191 |
||
57256 | 192 |
// create the additional launchers, if any |
57038 | 193 |
List<Map<String, ? super Object>> entryPoints |
57256 | 194 |
= StandardBundlerParam.ADD_LAUNCHERS.fetchFrom(params); |
57038 | 195 |
for (Map<String, ? super Object> entryPoint : entryPoints) { |
57259
a84c657c713d
8171959: add-launcher is not working when normal jar is used for first launcher and module is used for second launcher
herrick
parents:
57256
diff
changeset
|
196 |
createLauncherForEntryPoint( |
a84c657c713d
8171959: add-launcher is not working when normal jar is used for first launcher and module is used for second launcher
herrick
parents:
57256
diff
changeset
|
197 |
AddLauncherArguments.merge(originalParams, entryPoint)); |
57038 | 198 |
} |
199 |
||
200 |
// Copy class path entries to Java folder |
|
201 |
copyApplication(); |
|
202 |
||
203 |
// Copy icon to Resources folder |
|
204 |
copyIcon(); |
|
205 |
} |
|
206 |
||
207 |
@Override |
|
57070 | 208 |
public void prepareJreFiles() throws IOException {} |
57038 | 209 |
|
57264 | 210 |
private void createLauncherForEntryPoint(Map<String, ? super Object> p) |
57263 | 211 |
throws IOException { |
57038 | 212 |
// Copy executable to Linux folder |
213 |
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
|
214 |
try (InputStream is_launcher = |
a7fdadf67a92
8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
57059
diff
changeset
|
215 |
getResourceAsStream("jpackageapplauncher")) { |
57038 | 216 |
writeEntry(is_launcher, executableFile); |
217 |
} |
|
218 |
||
219 |
executableFile.toFile().setExecutable(true, false); |
|
220 |
executableFile.toFile().setWritable(true, true); |
|
221 |
||
222 |
writeCfgFile(p, root.resolve(getLauncherCfgName(p)).toFile(), |
|
223 |
"$APPDIR/runtime"); |
|
224 |
} |
|
225 |
||
226 |
private void copyIcon() throws IOException { |
|
227 |
File icon = ICON_PNG.fetchFrom(params); |
|
228 |
if (icon != null) { |
|
229 |
File iconTarget = new File(resourcesDir.toFile(), |
|
57119 | 230 |
APP_NAME.fetchFrom(params) + ".png"); |
57038 | 231 |
IOUtils.copyFile(icon, iconTarget); |
232 |
} |
|
233 |
} |
|
234 |
||
235 |
private void copyApplication() throws IOException { |
|
236 |
for (RelativeFileSet appResources : |
|
237 |
APP_RESOURCES_LIST.fetchFrom(params)) { |
|
238 |
if (appResources == null) { |
|
239 |
throw new RuntimeException("Null app resources?"); |
|
240 |
} |
|
241 |
File srcdir = appResources.getBaseDirectory(); |
|
242 |
for (String fname : appResources.getIncludedFiles()) { |
|
243 |
copyEntry(appDir, srcdir, fname); |
|
244 |
} |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
} |