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.IOUtils;
|
|
29 |
import jdk.jpackage.internal.Log;
|
|
30 |
import jdk.jpackage.internal.StandardBundlerParam;
|
|
31 |
import jdk.jpackage.internal.JLinkBundlerHelper;
|
|
32 |
import jdk.jpackage.internal.ModFile;
|
57038
|
33 |
|
|
34 |
import java.io.ByteArrayOutputStream;
|
|
35 |
import java.io.File;
|
|
36 |
import java.io.FileInputStream;
|
|
37 |
import java.io.IOException;
|
|
38 |
import java.io.InputStream;
|
|
39 |
import java.io.PrintStream;
|
|
40 |
import java.nio.file.Files;
|
|
41 |
import java.nio.file.Path;
|
|
42 |
import java.text.MessageFormat;
|
|
43 |
import java.util.List;
|
|
44 |
import java.util.Map;
|
|
45 |
import java.util.ResourceBundle;
|
|
46 |
import java.util.ArrayList;
|
|
47 |
|
57039
|
48 |
import static jdk.jpackage.internal.StandardBundlerParam.*;
|
|
49 |
import static jdk.jpackage.internal.StandardBundlerParam.ARGUMENTS;
|
57038
|
50 |
|
|
51 |
public abstract class AbstractAppImageBuilder {
|
|
52 |
|
57059
|
53 |
private static final ResourceBundle I18N = ResourceBundle.getBundle(
|
|
54 |
"jdk.jpackage.internal.resources.MainResources");
|
57038
|
55 |
|
|
56 |
//do not use file separator -
|
|
57 |
// we use it for classpath lookup and there / are not platform specific
|
|
58 |
public final static String BUNDLER_PREFIX = "package/";
|
|
59 |
|
|
60 |
private final Map<String, Object> properties;
|
|
61 |
private final Path root;
|
|
62 |
protected List<String> excludeFileList = new ArrayList<>();
|
|
63 |
|
|
64 |
public AbstractAppImageBuilder(Map<String, Object> properties,
|
|
65 |
Path root) throws IOException {
|
|
66 |
this.properties = properties;
|
|
67 |
this.root = root;
|
|
68 |
excludeFileList.add(".*\\.diz");
|
|
69 |
}
|
|
70 |
|
|
71 |
public abstract InputStream getResourceAsStream(String name);
|
|
72 |
public abstract void prepareApplicationFiles() throws IOException;
|
|
73 |
public abstract void prepareServerJreFiles() throws IOException;
|
|
74 |
|
|
75 |
public Map<String, Object> getProperties() {
|
|
76 |
return this.properties;
|
|
77 |
}
|
|
78 |
|
|
79 |
public Path getRoot() {
|
|
80 |
return this.root;
|
|
81 |
}
|
|
82 |
|
|
83 |
public String getExcludeFileList() {
|
|
84 |
return String.join(",", excludeFileList);
|
|
85 |
}
|
|
86 |
|
|
87 |
protected void copyEntry(Path appDir, File srcdir, String fname)
|
|
88 |
throws IOException {
|
|
89 |
Path dest = appDir.resolve(fname);
|
|
90 |
Files.createDirectories(dest.getParent());
|
|
91 |
File src = new File(srcdir, fname);
|
|
92 |
if (src.isDirectory()) {
|
|
93 |
IOUtils.copyRecursive(src.toPath(), dest);
|
|
94 |
} else {
|
|
95 |
Files.copy(src.toPath(), dest);
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
protected InputStream locateResource(String publicName, String category,
|
|
100 |
String defaultName, File customFile,
|
|
101 |
boolean verbose, File publicRoot) throws IOException {
|
|
102 |
InputStream is = null;
|
|
103 |
boolean customFromClasspath = false;
|
|
104 |
boolean customFromFile = false;
|
|
105 |
if (publicName != null) {
|
|
106 |
if (publicRoot != null) {
|
|
107 |
File publicResource = new File(publicRoot, publicName);
|
|
108 |
if (publicResource.exists() && publicResource.isFile()) {
|
|
109 |
is = new FileInputStream(publicResource);
|
|
110 |
}
|
|
111 |
} else {
|
|
112 |
is = getResourceAsStream(publicName);
|
|
113 |
}
|
|
114 |
customFromClasspath = (is != null);
|
|
115 |
}
|
|
116 |
if (is == null && customFile != null) {
|
|
117 |
is = new FileInputStream(customFile);
|
|
118 |
customFromFile = (is != null);
|
|
119 |
}
|
|
120 |
if (is == null && defaultName != null) {
|
|
121 |
is = getResourceAsStream(defaultName);
|
|
122 |
}
|
|
123 |
if (verbose) {
|
|
124 |
String msg = null;
|
|
125 |
if (customFromClasspath) {
|
|
126 |
msg = MessageFormat.format(I18N.getString(
|
|
127 |
"message.using-custom-resource-from-classpath"),
|
|
128 |
category == null ? "" : "[" + category + "] ", publicName);
|
|
129 |
} else if (customFromFile) {
|
|
130 |
msg = MessageFormat.format(I18N.getString(
|
|
131 |
"message.using-custom-resource-from-file"),
|
|
132 |
category == null ? "" : "[" + category + "] ",
|
|
133 |
customFile.getAbsoluteFile());
|
|
134 |
} else if (is != null) {
|
|
135 |
msg = MessageFormat.format(I18N.getString(
|
|
136 |
"message.using-default-resource-from-classpath"),
|
|
137 |
category == null ? "" : "[" + category + "] ", publicName);
|
|
138 |
} else {
|
|
139 |
msg = MessageFormat.format(I18N.getString(
|
|
140 |
"message.using-default-resource"),
|
|
141 |
category == null ? "" : "[" + category + "] ", publicName);
|
|
142 |
}
|
|
143 |
if (msg != null) {
|
|
144 |
Log.verbose(msg);
|
|
145 |
}
|
|
146 |
}
|
|
147 |
return is;
|
|
148 |
}
|
|
149 |
|
|
150 |
|
|
151 |
protected String preprocessTextResource(String publicName, String category,
|
|
152 |
String defaultName, Map<String, String> pairs,
|
|
153 |
boolean verbose, File publicRoot) throws IOException {
|
|
154 |
InputStream inp = locateResource(publicName, category,
|
|
155 |
defaultName, null, verbose, publicRoot);
|
|
156 |
if (inp == null) {
|
|
157 |
throw new RuntimeException(
|
|
158 |
"Module corrupt? No "+defaultName+" resource!");
|
|
159 |
}
|
|
160 |
|
|
161 |
try (InputStream is = inp) {
|
|
162 |
//read fully into memory
|
|
163 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
164 |
byte[] buffer = new byte[1024];
|
|
165 |
int length;
|
|
166 |
while ((length = is.read(buffer)) != -1) {
|
|
167 |
baos.write(buffer, 0, length);
|
|
168 |
}
|
|
169 |
|
|
170 |
//substitute
|
|
171 |
String result = new String(baos.toByteArray());
|
|
172 |
for (Map.Entry<String, String> e : pairs.entrySet()) {
|
|
173 |
if (e.getValue() != null) {
|
|
174 |
result = result.replace(e.getKey(), e.getValue());
|
|
175 |
}
|
|
176 |
}
|
|
177 |
return result;
|
|
178 |
}
|
|
179 |
}
|
|
180 |
|
|
181 |
public void writeCfgFile(Map<String, ? super Object> params,
|
|
182 |
File cfgFileName, String runtimeLocation) throws IOException {
|
|
183 |
cfgFileName.delete();
|
|
184 |
|
|
185 |
File mainJar = JLinkBundlerHelper.getMainJar(params);
|
|
186 |
ModFile.ModType mainJarType = ModFile.ModType.Unknown;
|
|
187 |
|
|
188 |
if (mainJar != null) {
|
|
189 |
mainJarType = new ModFile(mainJar).getModType();
|
|
190 |
}
|
|
191 |
|
|
192 |
String mainModule = StandardBundlerParam.MODULE.fetchFrom(params);
|
|
193 |
|
|
194 |
PrintStream out = new PrintStream(cfgFileName);
|
|
195 |
|
|
196 |
out.println("[Application]");
|
|
197 |
out.println("app.name=" + APP_NAME.fetchFrom(params));
|
|
198 |
out.println("app.version=" + VERSION.fetchFrom(params));
|
|
199 |
out.println("app.preferences.id=" + PREFERENCES_ID.fetchFrom(params));
|
|
200 |
out.println("app.runtime=" + runtimeLocation);
|
|
201 |
out.println("app.identifier=" + IDENTIFIER.fetchFrom(params));
|
|
202 |
out.println("app.classpath=" + String.join(File.pathSeparator,
|
|
203 |
CLASSPATH.fetchFrom(params).split("[ :;]")));
|
|
204 |
out.println("app.application.instance=" +
|
|
205 |
(SINGLETON.fetchFrom(params) ? "single" : "multiple"));
|
|
206 |
|
|
207 |
// The main app is required to be a jar, modular or unnamed.
|
|
208 |
if (mainModule != null &&
|
|
209 |
(mainJarType == ModFile.ModType.Unknown ||
|
|
210 |
mainJarType == ModFile.ModType.ModularJar)) {
|
|
211 |
out.println("app.mainmodule=" + mainModule);
|
|
212 |
} else {
|
|
213 |
String mainClass = JLinkBundlerHelper.getMainClass(params);
|
|
214 |
// If the app is contained in an unnamed jar then launch it the
|
|
215 |
// legacy way and the main class string must be
|
|
216 |
// of the format com/foo/Main
|
|
217 |
if (mainJar != null) {
|
|
218 |
out.println("app.mainjar="
|
|
219 |
+ mainJar.toPath().getFileName().toString());
|
|
220 |
}
|
|
221 |
if (mainClass != null) {
|
|
222 |
out.println("app.mainclass="
|
|
223 |
+ mainClass.replaceAll("\\.", "/"));
|
|
224 |
}
|
|
225 |
}
|
|
226 |
|
|
227 |
String version = JLinkBundlerHelper.getJDKVersion(params);
|
|
228 |
|
|
229 |
if (!version.isEmpty()) {
|
|
230 |
out.println("app.java.version=" + version);
|
|
231 |
}
|
|
232 |
|
57039
|
233 |
out.println("jpackage.java.version="
|
57038
|
234 |
+ System.getProperty("java.version"));
|
|
235 |
|
|
236 |
Integer port = JLinkBundlerHelper.DEBUG.fetchFrom(params);
|
|
237 |
|
|
238 |
if (port != null) {
|
|
239 |
out.println(
|
|
240 |
"app.debug=-agentlib:jdwp=transport=dt_socket,"
|
|
241 |
+ "server=y,suspend=y,address=localhost:"
|
|
242 |
+ port);
|
|
243 |
}
|
|
244 |
|
|
245 |
out.println();
|
|
246 |
out.println("[JVMOptions]");
|
|
247 |
List<String> jvmargs = JVM_OPTIONS.fetchFrom(params);
|
|
248 |
for (String arg : jvmargs) {
|
|
249 |
out.println(arg);
|
|
250 |
}
|
|
251 |
Map<String, String> jvmProps = JVM_PROPERTIES.fetchFrom(params);
|
|
252 |
for (Map.Entry<String, String> property : jvmProps.entrySet()) {
|
|
253 |
out.println("-D" + property.getKey() + "=" + property.getValue());
|
|
254 |
}
|
|
255 |
|
|
256 |
out.println();
|
|
257 |
out.println("[ArgOptions]");
|
|
258 |
List<String> args = ARGUMENTS.fetchFrom(params);
|
|
259 |
for (String arg : args) {
|
|
260 |
if (arg.endsWith("=") &&
|
|
261 |
(arg.indexOf("=") == arg.lastIndexOf("="))) {
|
|
262 |
out.print(arg.substring(0, arg.length() - 1));
|
|
263 |
out.println("\\=");
|
|
264 |
} else {
|
|
265 |
out.println(arg);
|
|
266 |
}
|
|
267 |
}
|
|
268 |
|
|
269 |
|
|
270 |
out.close();
|
|
271 |
}
|
|
272 |
|
|
273 |
public String getPlatformSpecificModulesFile() {
|
|
274 |
return null;
|
|
275 |
}
|
|
276 |
|
|
277 |
}
|