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( 68 Map<String, ? super Object> params) throws IOException; 69 public abstract void prepareJreFiles( 70 Map<String, ? super Object> params) throws IOException; 71 public abstract Path getAppDir(); 72 public abstract Path getAppModsDir(); 73 74 public Path getRuntimeRoot() { 75 return this.root; 76 } 77 78 protected void copyEntry(Path appDir, File srcdir, String fname) 79 throws IOException { 80 Path dest = appDir.resolve(fname); 81 Files.createDirectories(dest.getParent()); 82 File src = new File(srcdir, fname); 83 if (src.isDirectory()) { 84 IOUtils.copyRecursive(src.toPath(), dest); 85 } else { 86 Files.copy(src.toPath(), dest); 87 } 88 } 89 90 protected InputStream locateResource(String publicName, String category, 91 String defaultName, File customFile, 92 boolean verbose, File publicRoot) throws IOException { 93 InputStream is = null; 94 boolean customFromClasspath = false; 95 boolean customFromFile = false; 96 if (publicName != null) { 97 if (publicRoot != null) { 98 File publicResource = new File(publicRoot, publicName); 99 if (publicResource.exists() && publicResource.isFile()) { 100 is = new FileInputStream(publicResource); 101 } 102 } else { 103 is = getResourceAsStream(publicName); 104 } 105 customFromClasspath = (is != null); 106 } 107 if (is == null && customFile != null) { 108 is = new FileInputStream(customFile); 109 customFromFile = (is != null); 110 } 111 if (is == null && defaultName != null) { 112 is = getResourceAsStream(defaultName); 113 } 114 if (verbose) { 115 String msg = null; 116 if (customFromClasspath) { 117 msg = MessageFormat.format(I18N.getString( 118 "message.using-custom-resource"), 119 category == null ? "" : "[" + category + "] ", publicName); 120 } else if (customFromFile) { 121 msg = MessageFormat.format(I18N.getString( 122 "message.using-custom-resource-from-file"), 123 category == null ? "" : "[" + category + "] ", 124 customFile.getAbsoluteFile()); 125 } else if (is != null) { 126 msg = MessageFormat.format(I18N.getString( 127 "message.using-default-resource"), 128 defaultName, 129 category == null ? "" : "[" + category + "] ", 130 publicName); 131 } else { 132 msg = MessageFormat.format(I18N.getString( 133 "message.no-default-resource"), 134 defaultName == null ? "" : defaultName, 135 category == null ? "" : "[" + category + "] ", 136 publicName); 137 } 138 if (msg != null) { 139 Log.verbose(msg); 140 } 141 } 142 return is; 143 } 144 145 146 protected String preprocessTextResource(String publicName, String category, 147 String defaultName, Map<String, String> pairs, 148 boolean verbose, File publicRoot) throws IOException { 149 InputStream inp = locateResource(publicName, category, 150 defaultName, null, verbose, publicRoot); 151 if (inp == null) { 152 throw new RuntimeException( 153 "Module corrupt? No "+defaultName+" resource!"); 154 } 155 156 try (InputStream is = inp) { 157 //read fully into memory 158 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 159 byte[] buffer = new byte[1024]; 160 int length; 161 while ((length = is.read(buffer)) != -1) { 162 baos.write(buffer, 0, length); 163 } 164 165 //substitute 166 String result = new String(baos.toByteArray()); 167 for (Map.Entry<String, String> e : pairs.entrySet()) { 168 if (e.getValue() != null) { 169 result = result.replace(e.getKey(), e.getValue()); 170 } 171 } 172 return result; 173 } 174 } 175 176 public void writeCfgFile(Map<String, ? super Object> params, 177 File cfgFileName) throws IOException { 178 cfgFileName.getParentFile().mkdirs(); 179 cfgFileName.delete(); 180 File mainJar = JLinkBundlerHelper.getMainJar(params); 181 ModFile.ModType mainJarType = ModFile.ModType.Unknown; 182 183 if (mainJar != null) { 184 mainJarType = new ModFile(mainJar).getModType(); 185 } 186 187 String mainModule = StandardBundlerParam.MODULE.fetchFrom(params); 188 189 try (PrintStream out = new PrintStream(cfgFileName)) { 190 191 out.println("[Application]"); 192 out.println("app.name=" + APP_NAME.fetchFrom(params)); 193 out.println("app.version=" + VERSION.fetchFrom(params)); 194 out.println("app.runtime=" + getCfgRuntimeDir()); 195 out.println("app.identifier=" + IDENTIFIER.fetchFrom(params)); 196 out.println("app.classpath=" 197 + getCfgClassPath(CLASSPATH.fetchFrom(params))); 198 199 // The main app is required to be a jar, modular or unnamed. 200 if (mainModule != null && 201 (mainJarType == ModFile.ModType.Unknown || 202 mainJarType == ModFile.ModType.ModularJar)) { 203 out.println("app.mainmodule=" + mainModule); 204 } else { 205 String mainClass = JLinkBundlerHelper.getMainClass(params); 206 // If the app is contained in an unnamed jar then launch it the 207 // legacy way and the main class string must be 208 // of the format com/foo/Main 209 if (mainJar != null) { 210 out.println("app.mainjar=" + getCfgAppDir() 211 + mainJar.toPath().getFileName().toString()); 212 } 213 if (mainClass != null) { 214 out.println("app.mainclass=" 215 + mainClass.replace("\\", "/")); 216 } 217 } 218 219 out.println(); 220 out.println("[JavaOptions]"); 221 List<String> jvmargs = JAVA_OPTIONS.fetchFrom(params); 222 for (String arg : jvmargs) { 223 out.println(arg); 224 } 225 Path modsDir = getAppModsDir(); 226 227 if (modsDir != null && modsDir.toFile().exists()) { 228 out.println("--module-path"); 229 out.println(getCfgAppDir().replace("\\","/") + "mods"); 230 } 231 232 out.println(); 233 out.println("[ArgOptions]"); 234 List<String> args = ARGUMENTS.fetchFrom(params); 235 for (String arg : args) { 236 if (arg.endsWith("=") && 237 (arg.indexOf("=") == arg.lastIndexOf("="))) { 238 out.print(arg.substring(0, arg.length() - 1)); 239 out.println("\\="); 240 } else { 241 out.println(arg); 242 } 243 } 244 } 245 } 246 247 File getRuntimeImageDir(File runtimeImageTop) { 248 return runtimeImageTop; 249 } 250 251 protected String getCfgAppDir() { 252 return "$APPDIR" + File.separator 253 + getAppDir().getFileName() + File.separator; 254 } 255 256 protected String getCfgRuntimeDir() { 257 return "$APPDIR" + File.separator + "runtime"; 258 } 259 260 String getCfgClassPath(String classpath) { 261 String cfgAppDir = getCfgAppDir(); 262 263 StringBuilder sb = new StringBuilder(); 264 for (String path : classpath.split("[:;]")) { 265 if (path.length() > 0) { 266 sb.append(cfgAppDir); 267 sb.append(path); 268 sb.append(File.pathSeparator); 269 } 270 } 271 if (sb.length() > 0) { 272 sb.deleteCharAt(sb.length() - 1); 273 } 274 return sb.toString(); 275 } 276 }