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 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 { 92 InputStream is = null; 93 boolean customFromClasspath = false; 94 boolean customFromFile = false; 95 if (publicName != null) { 96 if (publicRoot != null) { 97 File publicResource = new File(publicRoot, publicName); 98 if (publicResource.exists() && publicResource.isFile()) { 99 is = new FileInputStream(publicResource); 100 } 101 } else { 102 is = getResourceAsStream(publicName); 103 } 104 customFromClasspath = (is != null); 105 } 106 if (is == null && customFile != null) { 107 is = new FileInputStream(customFile); 108 customFromFile = (is != null); 109 } 110 if (is == null && defaultName != null) { 111 is = getResourceAsStream(defaultName); 112 } 113 if (verbose) { 114 String msg = null; 115 if (customFromClasspath) { 116 msg = MessageFormat.format(I18N.getString( 117 "message.using-custom-resource"), 118 category == null ? "" : "[" + category + "] ", publicName); 119 } else if (customFromFile) { 120 msg = MessageFormat.format(I18N.getString( 121 "message.using-custom-resource-from-file"), 122 category == null ? "" : "[" + category + "] ", 123 customFile.getAbsoluteFile()); 124 } else if (is != null) { 125 msg = MessageFormat.format(I18N.getString( 126 "message.using-default-resource"), 127 defaultName, 128 category == null ? "" : "[" + category + "] ", 129 publicName); 130 } else { 131 msg = MessageFormat.format(I18N.getString( 132 "message.no-default-resource"), 133 defaultName == null ? "" : defaultName, 134 category == null ? "" : "[" + category + "] ", 135 publicName); 136 } 137 if (msg != null) { 138 Log.verbose(msg); 139 } 140 } 141 return is; 142 } 143 144 145 protected String preprocessTextResource(String publicName, String category, 146 String defaultName, Map<String, String> pairs, 147 boolean verbose, File publicRoot) throws IOException { 148 InputStream inp = locateResource(publicName, category, 149 defaultName, null, verbose, publicRoot); 150 if (inp == null) { 151 throw new RuntimeException( 152 "Module corrupt? No "+defaultName+" resource!"); 153 } 154 155 try (InputStream is = inp) { 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 }