51
52 public abstract class AbstractAppImageBuilder {
53
54 private static final ResourceBundle I18N = ResourceBundle.getBundle(
55 "jdk.jpackage.internal.resources.MainResources");
56
57 private final Path root;
58
59 public AbstractAppImageBuilder(Map<String, Object> unused, Path root) {
60 this.root = root;
61 }
62
63 public InputStream getResourceAsStream(String name) {
64 return ResourceLocator.class.getResourceAsStream(name);
65 }
66
67 public abstract void prepareApplicationFiles() throws IOException;
68 public abstract void prepareJreFiles() throws IOException;
69 public abstract Path getAppDir();
70 public abstract Path getAppModsDir();
71 public abstract String getRelativeModsDir();
72
73 public Path getRoot() {
74 return this.root;
75 }
76
77 protected void copyEntry(Path appDir, File srcdir, String fname)
78 throws IOException {
79 Path dest = appDir.resolve(fname);
80 Files.createDirectories(dest.getParent());
81 File src = new File(srcdir, fname);
82 if (src.isDirectory()) {
83 IOUtils.copyRecursive(src.toPath(), dest);
84 } else {
85 Files.copy(src.toPath(), dest);
86 }
87 }
88
89 protected InputStream locateResource(String publicName, String category,
90 String defaultName, File customFile,
91 boolean verbose, File publicRoot) throws IOException {
156 //read fully into memory
157 ByteArrayOutputStream baos = new ByteArrayOutputStream();
158 byte[] buffer = new byte[1024];
159 int length;
160 while ((length = is.read(buffer)) != -1) {
161 baos.write(buffer, 0, length);
162 }
163
164 //substitute
165 String result = new String(baos.toByteArray());
166 for (Map.Entry<String, String> e : pairs.entrySet()) {
167 if (e.getValue() != null) {
168 result = result.replace(e.getKey(), e.getValue());
169 }
170 }
171 return result;
172 }
173 }
174
175 public void writeCfgFile(Map<String, ? super Object> params,
176 File cfgFileName, String runtimeLocation) throws IOException {
177 cfgFileName.delete();
178 File mainJar = JLinkBundlerHelper.getMainJar(params);
179 ModFile.ModType mainJarType = ModFile.ModType.Unknown;
180
181 if (mainJar != null) {
182 mainJarType = new ModFile(mainJar).getModType();
183 }
184
185 String mainModule = StandardBundlerParam.MODULE.fetchFrom(params);
186
187 try (PrintStream out = new PrintStream(cfgFileName)) {
188
189 out.println("[Application]");
190 out.println("app.name=" + APP_NAME.fetchFrom(params));
191 out.println("app.version=" + VERSION.fetchFrom(params));
192 out.println("app.runtime=" + runtimeLocation);
193 out.println("app.identifier=" + IDENTIFIER.fetchFrom(params));
194 out.println("app.classpath=" + CLASSPATH.fetchFrom(params));
195
196 // The main app is required to be a jar, modular or unnamed.
197 if (mainModule != null &&
198 (mainJarType == ModFile.ModType.Unknown ||
199 mainJarType == ModFile.ModType.ModularJar)) {
200 out.println("app.mainmodule=" + mainModule);
201 } else {
202 String mainClass = JLinkBundlerHelper.getMainClass(params);
203 // If the app is contained in an unnamed jar then launch it the
204 // legacy way and the main class string must be
205 // of the format com/foo/Main
206 if (mainJar != null) {
207 out.println("app.mainjar="
208 + mainJar.toPath().getFileName().toString());
209 }
210 if (mainClass != null) {
211 out.println("app.mainclass="
212 + mainClass.replaceAll("\\.", "/"));
213 }
214 }
215
216 out.println();
217 out.println("[JavaOptions]");
218 List<String> jvmargs = JAVA_OPTIONS.fetchFrom(params);
219 for (String arg : jvmargs) {
220 out.println(arg);
221 }
222 Path modsDir = getAppModsDir();
223
224 if (modsDir != null && modsDir.toFile().exists()) {
225 out.println("--module-path");
226 out.println("$APPDIR/" + getRelativeModsDir());
227 }
228
229 out.println();
230 out.println("[ArgOptions]");
231 List<String> args = ARGUMENTS.fetchFrom(params);
232 for (String arg : args) {
233 if (arg.endsWith("=") &&
234 (arg.indexOf("=") == arg.lastIndexOf("="))) {
235 out.print(arg.substring(0, arg.length() - 1));
236 out.println("\\=");
237 } else {
238 out.println(arg);
239 }
240 }
241 }
242 }
243
244 }
|
51
52 public abstract class AbstractAppImageBuilder {
53
54 private static final ResourceBundle I18N = ResourceBundle.getBundle(
55 "jdk.jpackage.internal.resources.MainResources");
56
57 private final Path root;
58
59 public AbstractAppImageBuilder(Map<String, Object> unused, Path root) {
60 this.root = root;
61 }
62
63 public InputStream getResourceAsStream(String name) {
64 return ResourceLocator.class.getResourceAsStream(name);
65 }
66
67 public abstract void prepareApplicationFiles() throws IOException;
68 public abstract void prepareJreFiles() throws IOException;
69 public abstract Path getAppDir();
70 public abstract Path getAppModsDir();
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 {
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) 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
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=" + getCfgRuntimeDir());
192 out.println("app.identifier=" + IDENTIFIER.fetchFrom(params));
193 out.println("app.classpath="
194 + getCfgClassPath(CLASSPATH.fetchFrom(params)));
195
196 // The main app is required to be a jar, modular or unnamed.
197 if (mainModule != null &&
198 (mainJarType == ModFile.ModType.Unknown ||
199 mainJarType == ModFile.ModType.ModularJar)) {
200 out.println("app.mainmodule=" + mainModule);
201 } else {
202 String mainClass = JLinkBundlerHelper.getMainClass(params);
203 // If the app is contained in an unnamed jar then launch it the
204 // legacy way and the main class string must be
205 // of the format com/foo/Main
206 if (mainJar != null) {
207 out.println("app.mainjar=" + getCfgAppDir()
208 + mainJar.toPath().getFileName().toString());
209 }
210 if (mainClass != null) {
211 out.println("app.mainclass="
212 + mainClass.replace("\\", "/"));
213 }
214 }
215
216 out.println();
217 out.println("[JavaOptions]");
218 List<String> jvmargs = JAVA_OPTIONS.fetchFrom(params);
219 for (String arg : jvmargs) {
220 out.println(arg);
221 }
222 Path modsDir = getAppModsDir();
223
224 if (modsDir != null && modsDir.toFile().exists()) {
225 out.println("--module-path");
226 out.println(getCfgAppDir().replace("\\","/") + "mods");
227 }
228
229 out.println();
230 out.println("[ArgOptions]");
231 List<String> args = ARGUMENTS.fetchFrom(params);
232 for (String arg : args) {
233 if (arg.endsWith("=") &&
234 (arg.indexOf("=") == arg.lastIndexOf("="))) {
235 out.print(arg.substring(0, arg.length() - 1));
236 out.println("\\=");
237 } else {
238 out.println(arg);
239 }
240 }
241 }
242 }
243
244 String getCfgAppDir() {
245 return "$APPDIR" + File.separator
246 + getAppDir().getFileName() + File.separator;
247 }
248
249 String getCfgRuntimeDir() {
250 return "$APPDIR" + File.separator + "runtime";
251 }
252
253 String getCfgClassPath(String classpath) {
254 String cfgAppDir = getCfgAppDir();
255
256 StringBuilder sb = new StringBuilder();
257 for (String path : classpath.split("[:;]")) {
258 if (path.length() > 0) {
259 sb.append(cfgAppDir);
260 sb.append(path);
261 sb.append(File.pathSeparator);
262 }
263 }
264 if (sb.length() > 0) {
265 sb.deleteCharAt(sb.length() - 1);
266 }
267 return sb.toString();
268 }
269 }
|