author | herrick |
Thu, 27 Jun 2019 17:44:18 -0400 | |
branch | JDK-8200758-branch |
changeset 57438 | 4a31db8d42bd |
parent 57393 | 85998c745ee9 |
child 57445 | 405ddd80496e |
permissions | -rw-r--r-- |
57038 | 1 |
/* |
57106
ea870b9ce89a
8216492: Update copyright of all new jpackage fils to 2019
kcr
parents:
57099
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.ByteArrayOutputStream; |
|
29 |
import java.io.File; |
|
30 |
import java.io.FileInputStream; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.InputStream; |
|
33 |
import java.io.PrintStream; |
|
34 |
import java.nio.file.Files; |
|
35 |
import java.nio.file.Path; |
|
36 |
import java.text.MessageFormat; |
|
37 |
import java.util.List; |
|
38 |
import java.util.Map; |
|
39 |
import java.util.ResourceBundle; |
|
40 |
import java.util.ArrayList; |
|
41 |
||
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
42 |
import jdk.jpackage.internal.resources.ResourceLocator; |
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
43 |
|
57039 | 44 |
import static jdk.jpackage.internal.StandardBundlerParam.*; |
57038 | 45 |
|
57392 | 46 |
/* |
47 |
* AbstractAppImageBuilder |
|
48 |
* This is sub-classed by each of the platform dependent AppImageBuilder |
|
49 |
* classes, and contains resource processing code common to all platforms. |
|
50 |
*/ |
|
51 |
||
57038 | 52 |
public abstract class AbstractAppImageBuilder { |
53 |
||
57059 | 54 |
private static final ResourceBundle I18N = ResourceBundle.getBundle( |
55 |
"jdk.jpackage.internal.resources.MainResources"); |
|
57038 | 56 |
|
57 |
private final Path root; |
|
58 |
||
57438
4a31db8d42bd
8226599: use code coverage results to remove dead code
herrick
parents:
57393
diff
changeset
|
59 |
public AbstractAppImageBuilder(Map<String, Object> unused, Path root) { |
57038 | 60 |
this.root = root; |
61 |
} |
|
62 |
||
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
63 |
public InputStream getResourceAsStream(String name) { |
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
64 |
return ResourceLocator.class.getResourceAsStream(name); |
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
65 |
} |
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
66 |
|
57038 | 67 |
public abstract void prepareApplicationFiles() throws IOException; |
57070 | 68 |
public abstract void prepareJreFiles() throws IOException; |
57077
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
69 |
public abstract Path getAppDir(); |
8f9cf6ad59f0
8213962: JPackageCreateImageRuntimeModuleTest fails
herrick
parents:
57070
diff
changeset
|
70 |
public abstract Path getAppModsDir(); |
57038 | 71 |
|
72 |
public Path getRoot() { |
|
73 |
return this.root; |
|
74 |
} |
|
75 |
||
76 |
protected void copyEntry(Path appDir, File srcdir, String fname) |
|
77 |
throws IOException { |
|
78 |
Path dest = appDir.resolve(fname); |
|
79 |
Files.createDirectories(dest.getParent()); |
|
80 |
File src = new File(srcdir, fname); |
|
81 |
if (src.isDirectory()) { |
|
82 |
IOUtils.copyRecursive(src.toPath(), dest); |
|
83 |
} else { |
|
84 |
Files.copy(src.toPath(), dest); |
|
85 |
} |
|
86 |
} |
|
87 |
||
88 |
protected InputStream locateResource(String publicName, String category, |
|
89 |
String defaultName, File customFile, |
|
90 |
boolean verbose, File publicRoot) throws IOException { |
|
91 |
InputStream is = null; |
|
92 |
boolean customFromClasspath = false; |
|
93 |
boolean customFromFile = false; |
|
94 |
if (publicName != null) { |
|
95 |
if (publicRoot != null) { |
|
96 |
File publicResource = new File(publicRoot, publicName); |
|
97 |
if (publicResource.exists() && publicResource.isFile()) { |
|
98 |
is = new FileInputStream(publicResource); |
|
99 |
} |
|
100 |
} else { |
|
101 |
is = getResourceAsStream(publicName); |
|
102 |
} |
|
103 |
customFromClasspath = (is != null); |
|
104 |
} |
|
105 |
if (is == null && customFile != null) { |
|
106 |
is = new FileInputStream(customFile); |
|
107 |
customFromFile = (is != null); |
|
108 |
} |
|
109 |
if (is == null && defaultName != null) { |
|
110 |
is = getResourceAsStream(defaultName); |
|
111 |
} |
|
112 |
if (verbose) { |
|
113 |
String msg = null; |
|
114 |
if (customFromClasspath) { |
|
115 |
msg = MessageFormat.format(I18N.getString( |
|
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
116 |
"message.using-custom-resource"), |
57038 | 117 |
category == null ? "" : "[" + category + "] ", publicName); |
118 |
} else if (customFromFile) { |
|
119 |
msg = MessageFormat.format(I18N.getString( |
|
120 |
"message.using-custom-resource-from-file"), |
|
121 |
category == null ? "" : "[" + category + "] ", |
|
122 |
customFile.getAbsoluteFile()); |
|
123 |
} else if (is != null) { |
|
124 |
msg = MessageFormat.format(I18N.getString( |
|
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
125 |
"message.using-default-resource"), |
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
126 |
defaultName, |
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
127 |
category == null ? "" : "[" + category + "] ", |
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
128 |
publicName); |
57038 | 129 |
} else { |
130 |
msg = MessageFormat.format(I18N.getString( |
|
57091
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
131 |
"message.no-default-resource"), |
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
132 |
defaultName == null ? "" : defaultName, |
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
133 |
category == null ? "" : "[" + category + "] ", |
06bc4bd64599
8215515: Add a command line option to override internal resources.
herrick
parents:
57078
diff
changeset
|
134 |
publicName); |
57038 | 135 |
} |
136 |
if (msg != null) { |
|
137 |
Log.verbose(msg); |
|
138 |
} |
|
139 |
} |
|
140 |
return is; |
|
141 |
} |
|
142 |
||
143 |
||
144 |
protected String preprocessTextResource(String publicName, String category, |
|
145 |
String defaultName, Map<String, String> pairs, |
|
146 |
boolean verbose, File publicRoot) throws IOException { |
|
147 |
InputStream inp = locateResource(publicName, category, |
|
148 |
defaultName, null, verbose, publicRoot); |
|
149 |
if (inp == null) { |
|
150 |
throw new RuntimeException( |
|
151 |
"Module corrupt? No "+defaultName+" resource!"); |
|
152 |
} |
|
153 |
||
154 |
try (InputStream is = inp) { |
|
155 |
//read fully into memory |
|
156 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
157 |
byte[] buffer = new byte[1024]; |
|
158 |
int length; |
|
159 |
while ((length = is.read(buffer)) != -1) { |
|
160 |
baos.write(buffer, 0, length); |
|
161 |
} |
|
162 |
||
163 |
//substitute |
|
164 |
String result = new String(baos.toByteArray()); |
|
165 |
for (Map.Entry<String, String> e : pairs.entrySet()) { |
|
166 |
if (e.getValue() != null) { |
|
167 |
result = result.replace(e.getKey(), e.getValue()); |
|
168 |
} |
|
169 |
} |
|
170 |
return result; |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
public void writeCfgFile(Map<String, ? super Object> params, |
|
175 |
File cfgFileName, String runtimeLocation) throws IOException { |
|
176 |
cfgFileName.delete(); |
|
177 |
File mainJar = JLinkBundlerHelper.getMainJar(params); |
|
178 |
ModFile.ModType mainJarType = ModFile.ModType.Unknown; |
|
179 |
||
180 |
if (mainJar != null) { |
|
181 |
mainJarType = new ModFile(mainJar).getModType(); |
|
182 |
} |
|
183 |
||
184 |
String mainModule = StandardBundlerParam.MODULE.fetchFrom(params); |
|
185 |
||
57390 | 186 |
try (PrintStream out = new PrintStream(cfgFileName)) { |
187 |
||
188 |
out.println("[Application]"); |
|
189 |
out.println("app.name=" + APP_NAME.fetchFrom(params)); |
|
190 |
out.println("app.version=" + VERSION.fetchFrom(params)); |
|
191 |
out.println("app.runtime=" + runtimeLocation); |
|
192 |
out.println("app.identifier=" + IDENTIFIER.fetchFrom(params)); |
|
57393
85998c745ee9
8223953: Fix CLASSPATH parsing for sub-directorys containing spaces
herrick
parents:
57392
diff
changeset
|
193 |
out.println("app.classpath=" + CLASSPATH.fetchFrom(params)); |
57038 | 194 |
|
57390 | 195 |
// The main app is required to be a jar, modular or unnamed. |
196 |
if (mainModule != null && |
|
197 |
(mainJarType == ModFile.ModType.Unknown || |
|
198 |
mainJarType == ModFile.ModType.ModularJar)) { |
|
199 |
out.println("app.mainmodule=" + mainModule); |
|
200 |
} else { |
|
201 |
String mainClass = JLinkBundlerHelper.getMainClass(params); |
|
202 |
// If the app is contained in an unnamed jar then launch it the |
|
203 |
// legacy way and the main class string must be |
|
204 |
// of the format com/foo/Main |
|
205 |
if (mainJar != null) { |
|
206 |
out.println("app.mainjar=" |
|
207 |
+ mainJar.toPath().getFileName().toString()); |
|
208 |
} |
|
209 |
if (mainClass != null) { |
|
210 |
out.println("app.mainclass=" |
|
211 |
+ mainClass.replaceAll("\\.", "/")); |
|
212 |
} |
|
213 |
} |
|
57038 | 214 |
|
57390 | 215 |
out.println(); |
216 |
out.println("[JavaOptions]"); |
|
217 |
List<String> jvmargs = JAVA_OPTIONS.fetchFrom(params); |
|
218 |
for (String arg : jvmargs) { |
|
219 |
out.println(arg); |
|
57038 | 220 |
} |
57390 | 221 |
Path modsDir = getAppModsDir(); |
222 |
if (modsDir != null && modsDir.toFile().exists()) { |
|
223 |
out.println("--module-path"); |
|
224 |
out.println(getAppDir().relativize(modsDir)); |
|
225 |
} |
|
226 |
||
227 |
out.println(); |
|
228 |
out.println("[ArgOptions]"); |
|
229 |
List<String> args = ARGUMENTS.fetchFrom(params); |
|
230 |
for (String arg : args) { |
|
231 |
if (arg.endsWith("=") && |
|
232 |
(arg.indexOf("=") == arg.lastIndexOf("="))) { |
|
233 |
out.print(arg.substring(0, arg.length() - 1)); |
|
234 |
out.println("\\="); |
|
235 |
} else { |
|
236 |
out.println(arg); |
|
237 |
} |
|
57038 | 238 |
} |
239 |
} |
|
240 |
} |
|
241 |
||
242 |
} |