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