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