1 /* 2 * Copyright (c) 2015, 2017, 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.packager.builders; 27 28 29 import jdk.packager.internal.JLinkBundlerHelper; 30 import com.oracle.tools.packager.RelativeFileSet; 31 32 import com.oracle.tools.packager.Log; 33 import com.oracle.tools.packager.StandardBundlerParam; 34 import jdk.packager.internal.Module; 35 36 import java.io.ByteArrayOutputStream; 37 import java.io.File; 38 import java.io.FileInputStream; 39 import java.io.FileOutputStream; 40 import java.io.IOException; 41 import java.io.InputStream; 42 import java.io.PrintStream; 43 import java.nio.file.Files; 44 import java.nio.file.Path; 45 import java.text.MessageFormat; 46 import java.util.Arrays; 47 import java.util.List; 48 import java.util.Map; 49 import java.util.ResourceBundle; 50 import java.util.Set; 51 52 import static com.oracle.tools.packager.StandardBundlerParam.*; 53 import static com.oracle.tools.packager.StandardBundlerParam.ARGUMENTS; 54 import static com.oracle.tools.packager.StandardBundlerParam.USER_JVM_OPTIONS; 55 import java.util.ArrayList; 56 57 58 public abstract class AbstractAppImageBuilder { 59 60 private static final ResourceBundle I18N = 61 ResourceBundle.getBundle(AbstractAppImageBuilder.class.getName()); 62 63 //do not use file separator - 64 // we use it for classpath lookup and there / are not platform specific 65 public final static String BUNDLER_PREFIX = "package/"; 66 67 private Map<String, Object> properties; 68 private Path root; 69 protected List<String> excludeFileList = new ArrayList<>(); 70 71 public AbstractAppImageBuilder(Map<String, Object> properties, Path root) throws IOException { 72 this.properties = properties; 73 this.root = root; 74 excludeFileList.add(".*\\.diz"); 75 } 76 77 public abstract InputStream getResourceAsStream(String name); 78 public abstract void prepareApplicationFiles() throws IOException; 79 80 public Map<String, Object> getProperties() { 81 return this.properties; 82 } 83 84 public Path getRoot() { 85 return this.root; 86 } 87 88 public String getExcludeFileList() { 89 String result = ""; 90 91 for (String item : excludeFileList) { 92 if (!result.isEmpty()) { 93 result += ","; 94 } 95 96 result += item; 97 } 98 99 return result; 100 } 101 102 protected InputStream locateResource(String publicName, String category, 103 String defaultName, File customFile, 104 boolean verbose, File publicRoot) throws IOException { 105 InputStream is = null; 106 boolean customFromClasspath = false; 107 boolean customFromFile = false; 108 if (publicName != null) { 109 if (publicRoot != null) { 110 File publicResource = new File(publicRoot, publicName); 111 if (publicResource.exists() && publicResource.isFile()) { 112 is = new FileInputStream(publicResource); 113 } 114 } else { 115 is = getResourceAsStream(publicName); 116 } 117 customFromClasspath = (is != null); 118 } 119 if (is == null && customFile != null) { 120 is = new FileInputStream(customFile); 121 customFromFile = (is != null); 122 } 123 if (is == null && defaultName != null) { 124 is = getResourceAsStream(defaultName); 125 } 126 String msg = null; 127 if (customFromClasspath) { 128 msg = MessageFormat.format(I18N.getString("message.using-custom-resource-from-classpath"), category == null ? "" : "[" + category + "] ", publicName); 129 } else if (customFromFile) { 130 msg = MessageFormat.format(I18N.getString("message.using-custom-resource-from-file"), category == null ? "" : "[" + category + "] ", customFile.getAbsoluteFile()); 131 } else if (is != null) { 132 msg = MessageFormat.format(I18N.getString("message.using-default-resource-from-classpath"), category == null ? "" : "[" + category + "] ", publicName); 133 } else { 134 msg = MessageFormat.format(I18N.getString("message.using-default-resource"), category == null ? "" : "[" + category + "] ", publicName); 135 } 136 if (verbose) { 137 Log.info(msg); 138 } 139 return is; 140 } 141 142 143 protected String preprocessTextResource(String publicName, String category, 144 String defaultName, Map<String, String> pairs, 145 boolean verbose, File publicRoot) throws IOException { 146 InputStream inp = locateResource(publicName, category, defaultName, null, verbose, publicRoot); 147 if (inp == null) { 148 throw new RuntimeException("Module corrupt? No "+defaultName+" resource!"); 149 } 150 151 try (InputStream is = inp) { 152 //read fully into memory 153 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 154 byte[] buffer = new byte[1024]; 155 int length; 156 while ((length = is.read(buffer)) != -1) { 157 baos.write(buffer, 0, length); 158 } 159 160 //substitute 161 String result = new String(baos.toByteArray()); 162 for (Map.Entry<String, String> e : pairs.entrySet()) { 163 if (e.getValue() != null) { 164 result = result.replace(e.getKey(), e.getValue()); 165 } 166 } 167 return result; 168 } 169 } 170 171 public void writeCfgFile(Map<String, ? super Object> params, File cfgFileName, String runtimeLocation) throws IOException { 172 cfgFileName.delete(); 173 174 boolean appCDEnabled = UNLOCK_COMMERCIAL_FEATURES.fetchFrom(params) && ENABLE_APP_CDS.fetchFrom(params); 175 String appCDSCacheMode = APP_CDS_CACHE_MODE.fetchFrom(params); 176 File mainJar = JLinkBundlerHelper.getMainJar(params); 177 Module.ModuleType mainJarType = Module.ModuleType.Unknown; 178 179 if (mainJar != null) { 180 mainJarType = new Module(mainJar).getModuleType(); 181 } 182 183 String mainModule = StandardBundlerParam.MODULE.fetchFrom(params); 184 185 PrintStream out = new PrintStream(cfgFileName); 186 187 out.println("[Application]"); 188 out.println("app.name=" + APP_NAME.fetchFrom(params)); 189 out.println("app.version=" + VERSION.fetchFrom(params)); 190 out.println("app.preferences.id=" + PREFERENCES_ID.fetchFrom(params)); 191 out.println("app.runtime=" + runtimeLocation); 192 out.println("app.identifier=" + IDENTIFIER.fetchFrom(params)); 193 out.println("app.classpath=" + String.join(File.pathSeparator, CLASSPATH.fetchFrom(params).split("[ :;]"))); 194 out.println("app.application.instance=" + (SINGLETON.fetchFrom(params) ? "single" : "multiple")); 195 196 // The main app is required to be a jar, modular or unnamed. 197 if (mainJarType == Module.ModuleType.Unknown || mainJarType == Module.ModuleType.ModularJar) { 198 if (mainModule != null) { 199 out.println("app.mainmodule=" + mainModule); // TODO get app class from main module mainifest. 200 } 201 } 202 else { 203 String mainClass = JLinkBundlerHelper.getMainClass(params); 204 205 if (mainJar != null && mainClass != null) { 206 // If the app is contained in an unnamed jar then launch it the 207 // legacy way and the main class string must be of the format com/foo/Main 208 out.println("app.mainclass=" + mainClass.replaceAll("\\.", "/")); 209 out.println("app.mainjar=" + mainJar.toPath().getFileName().toString()); 210 } 211 } 212 213 String version = JLinkBundlerHelper.getJDKVersion(params); 214 215 if (!version.isEmpty()) { 216 out.println("app.java.version=" + version); 217 } 218 219 out.println("packager.java.version=" + System.getProperty("java.version")); 220 221 Integer port = JLinkBundlerHelper.DEBUG.fetchFrom(params); 222 223 if (port != null) { 224 out.println("app.debug=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:" + port); 225 } 226 227 if (appCDEnabled) { 228 out.println("app.appcds.cache=" + appCDSCacheMode.split("\\+")[0]); 229 } 230 231 232 out.println(); 233 out.println("[JVMOptions]"); 234 List<String> jvmargs = JVM_OPTIONS.fetchFrom(params); 235 for (String arg : jvmargs) { 236 out.println(arg); 237 } 238 Map<String, String> jvmProps = JVM_PROPERTIES.fetchFrom(params); 239 for (Map.Entry<String, String> property : jvmProps.entrySet()) { 240 out.println("-D" + property.getKey() + "=" + property.getValue()); 241 } 242 String preloader = PRELOADER_CLASS.fetchFrom(params); 243 if (preloader != null) { 244 out.println("-Djavafx.preloader="+preloader); 245 } 246 247 248 out.println(); 249 out.println("[JVMUserOptions]"); 250 Map<String, String> overridableJVMOptions = USER_JVM_OPTIONS.fetchFrom(params); 251 for (Map.Entry<String, String> arg: overridableJVMOptions.entrySet()) { 252 if (arg.getKey() == null || arg.getValue() == null) { 253 Log.info(I18N.getString("message.jvm-user-arg-is-null")); 254 } else { 255 out.println(arg.getKey().replaceAll("([\\=])", "\\\\$1") + "=" + arg.getValue()); 256 } 257 } 258 259 if (appCDEnabled) { 260 prepareAppCDS(params, out); 261 } 262 263 out.println(); 264 out.println("[ArgOptions]"); 265 List<String> args = ARGUMENTS.fetchFrom(params); 266 for (String arg : args) { 267 if (arg.endsWith("=") && (arg.indexOf("=") == arg.lastIndexOf("="))) { 268 out.print(arg.substring(0, arg.length() - 1)); 269 out.println("\\="); 270 } else { 271 out.println(arg); 272 } 273 } 274 275 276 out.close(); 277 } 278 279 protected abstract String getCacheLocation(Map<String, ? super Object> params); 280 281 void prepareAppCDS(Map<String, ? super Object> params, PrintStream out) throws IOException { 282 File tempDir = Files.createTempDirectory("javapackager").toFile(); 283 tempDir.deleteOnExit(); 284 File classList = new File(tempDir, APP_FS_NAME.fetchFrom(params) + ".classlist"); 285 286 try (FileOutputStream fos = new FileOutputStream(classList); 287 PrintStream ps = new PrintStream(fos)) { 288 for (String className : APP_CDS_CLASS_ROOTS.fetchFrom(params)) { 289 String slashyName = className.replace(".", "/"); 290 ps.println(slashyName); 291 } 292 } 293 APP_RESOURCES_LIST.fetchFrom(params).add(new RelativeFileSet(classList.getParentFile(), Arrays.asList(classList))); 294 295 out.println(); 296 out.println("[AppCDSJVMOptions]"); 297 out.println("-XX:+UnlockCommercialFeatures"); 298 out.print("-XX:SharedArchiveFile="); 299 out.print(getCacheLocation(params)); 300 out.print(APP_FS_NAME.fetchFrom(params)); 301 out.println(".jpa"); 302 out.println("-Xshare:auto"); 303 out.println("-XX:+UseAppCDS"); 304 if (Log.isDebug()) { 305 out.println("-verbose:class"); 306 out.println("-XX:+TraceClassPaths"); 307 out.println("-XX:+UnlockDiagnosticVMOptions"); 308 } 309 out.println(""); 310 311 out.println("[AppCDSGenerateCacheJVMOptions]"); 312 out.println("-XX:+UnlockCommercialFeatures"); 313 out.println("-Xshare:dump"); 314 out.println("-XX:+UseAppCDS"); 315 out.print("-XX:SharedArchiveFile="); 316 out.print(getCacheLocation(params)); 317 out.print(APP_FS_NAME.fetchFrom(params)); 318 out.println(".jpa"); 319 out.println("-XX:SharedClassListFile=$PACKAGEDIR/" + APP_FS_NAME.fetchFrom(params) + ".classlist"); 320 if (Log.isDebug()) { 321 out.println("-XX:+UnlockDiagnosticVMOptions"); 322 } 323 } 324 }