1 /* 2 * Copyright (c) 2015, 2019, 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 26 package jdk.jpackage.internal; 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 42 import jdk.jpackage.internal.resources.ResourceLocator; 43 44 import static jdk.jpackage.internal.StandardBundlerParam.*; 45 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 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 { 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( 116 "message.using-custom-resource"), 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( 125 "message.using-default-resource"), 126 defaultName, 127 category == null ? "" : "[" + category + "] ", 128 publicName); 129 } else { 130 msg = MessageFormat.format(I18N.getString( 131 "message.no-default-resource"), 132 defaultName == null ? "" : defaultName, 133 category == null ? "" : "[" + category + "] ", 134 publicName); 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 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)); 193 out.println("app.classpath=" + CLASSPATH.fetchFrom(params)); 194 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 } 214 215 out.println(); 216 out.println("[JavaOptions]"); 217 List<String> jvmargs = JAVA_OPTIONS.fetchFrom(params); 218 for (String arg : jvmargs) { 219 out.println(arg); 220 } 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 } 238 } 239 } 240 } 241 242 }