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.File;
  29 import java.io.FileInputStream;
  30 import java.io.FileOutputStream;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 import java.io.OutputStream;
  34 import java.io.OutputStreamWriter;
  35 import java.io.UncheckedIOException;
  36 import java.io.Writer;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.attribute.PosixFilePermission;
  40 import java.text.MessageFormat;
  41 import java.util.HashMap;
  42 import java.util.List;
  43 import java.util.Map;
  44 import java.util.Objects;
  45 import java.util.ResourceBundle;
  46 import java.util.Set;
  47 
  48 import static jdk.jpackage.internal.StandardBundlerParam.*;
  49 
  50 public class LinuxAppImageBuilder extends AbstractAppImageBuilder {
  51 
  52     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  53         "jdk.jpackage.internal.resources.LinuxResources");
  54 
  55     private static final String LIBRARY_NAME = "libapplauncher.so";
  56 
  57     private final Path root;
  58     private final Path appDir;
  59     private final Path appModsDir;
  60     private final String relativeModsDir;
  61     private final Path runtimeDir;
  62     private final Path binDir;
  63     private final Path mdir;
  64 
  65     private final Map<String, ? super Object> params;
  66 
  67     public static final BundlerParamInfo<File> ICON_PNG =
  68             new StandardBundlerParam<>(
  69             "icon.png",
  70             File.class,
  71             params -> {
  72                 File f = ICON.fetchFrom(params);
  73                 if (f != null && !f.getName().toLowerCase().endsWith(".png")) {
  74                     Log.error(MessageFormat.format(I18N.getString(
  75                             "message.icon-not-png"), f));
  76                     return null;
  77                 }
  78                 return f;
  79             },
  80             (s, p) -> new File(s));
  81 
  82     public LinuxAppImageBuilder(Map<String, Object> config, Path imageOutDir)
  83             throws IOException {
  84         super(config,
  85                 imageOutDir.resolve(APP_NAME.fetchFrom(config) + "/runtime"));
  86 
  87         Objects.requireNonNull(imageOutDir);
  88 
  89         this.root = imageOutDir.resolve(APP_NAME.fetchFrom(config));
  90         this.appDir = root.resolve("app");
  91         this.appModsDir = appDir.resolve("mods");
  92         this.relativeModsDir = "app/mods";
  93         this.runtimeDir = root.resolve("runtime");
  94         this.binDir = root.resolve("bin");
  95         this.mdir = runtimeDir.resolve("lib");
  96         this.params = new HashMap<>();
  97         config.entrySet().stream().forEach(e -> params.put(
  98                 e.getKey().toString(), e.getValue()));
  99         Files.createDirectories(appDir);
 100         Files.createDirectories(runtimeDir);
 101     }
 102 
 103     public LinuxAppImageBuilder(String appName, Path imageOutDir)
 104             throws IOException {
 105         super(null, imageOutDir.resolve(appName));
 106 
 107         Objects.requireNonNull(imageOutDir);
 108 
 109         this.root = imageOutDir.resolve(appName);
 110         this.appDir = null;
 111         this.appModsDir = null;
 112         this.relativeModsDir = null;
 113         this.runtimeDir = null;
 114         this.binDir = null;
 115         this.mdir = null;
 116         this.params = new HashMap<>();
 117     }
 118 
 119     private void writeEntry(InputStream in, Path dstFile) throws IOException {
 120         Files.createDirectories(dstFile.getParent());
 121         Files.copy(in, dstFile);
 122     }
 123 
 124     // it is static for the sake of sharing with "installer" bundlers
 125     // that may skip calls to validate/bundle in this class!
 126     public static File getRootDir(File outDir,
 127             Map<String, ? super Object> params) {
 128         return new File(outDir, APP_NAME.fetchFrom(params));
 129     }
 130 
 131     public static String getLauncherRelativePath(
 132             Map<String, ? super Object> params) {
 133         return "bin" + File.separator + APP_NAME.fetchFrom(params);
 134     }
 135 
 136     private static String getLauncherName(Map<String, ? super Object> params) {
 137         return APP_NAME.fetchFrom(params);
 138     }
 139 
 140     public static String getLauncherCfgName(
 141             Map<String, ? super Object> params) {
 142         return "app" + File.separator + APP_NAME.fetchFrom(params) + ".cfg";
 143     }
 144 
 145     @Override
 146     public Path getAppDir() {
 147         return appDir;
 148     }
 149 
 150     @Override
 151     public Path getAppModsDir() {
 152         return appModsDir;
 153     }
 154 
 155     @Override
 156     public String getRelativeModsDir() {
 157         return relativeModsDir;
 158     }
 159 
 160     @Override
 161     public void prepareApplicationFiles() throws IOException {
 162         Map<String, ? super Object> originalParams = new HashMap<>(params);
 163 
 164         try {
 165             IOUtils.writableOutputDir(root);
 166             IOUtils.writableOutputDir(binDir);
 167         } catch (PackagerException pe) {
 168             throw new RuntimeException(pe);
 169         }
 170 
 171         // create the primary launcher
 172         createLauncherForEntryPoint(params);
 173 
 174         // Copy library to the launcher folder
 175         try (InputStream is_lib = getResourceAsStream(LIBRARY_NAME)) {
 176             writeEntry(is_lib, binDir.resolve(LIBRARY_NAME));
 177         }
 178 
 179         // create the additional launchers, if any
 180         List<Map<String, ? super Object>> entryPoints
 181                 = StandardBundlerParam.ADD_LAUNCHERS.fetchFrom(params);
 182         for (Map<String, ? super Object> entryPoint : entryPoints) {
 183             createLauncherForEntryPoint(
 184                     AddLauncherArguments.merge(originalParams, entryPoint));
 185         }
 186 
 187         // Copy class path entries to Java folder
 188         copyApplication();
 189 
 190         // Copy icon to Resources folder
 191         copyIcon();
 192     }
 193 
 194     @Override
 195     public void prepareJreFiles() throws IOException {}
 196 
 197     private void createLauncherForEntryPoint(
 198             Map<String, ? super Object> params) throws IOException {
 199         // Copy executable to Linux folder
 200         Path executableFile = binDir.resolve(getLauncherName(params));
 201         try (InputStream is_launcher =
 202                 getResourceAsStream("jpackageapplauncher")) {
 203             writeEntry(is_launcher, executableFile);
 204         }
 205 
 206         executableFile.toFile().setExecutable(true, false);
 207         executableFile.toFile().setWritable(true, true);
 208 
 209         writeCfgFile(params, root.resolve(getLauncherCfgName(params)).toFile(),
 210                 "$APPDIR/runtime");
 211     }
 212 
 213     private void copyIcon() throws IOException {
 214         File icon = ICON_PNG.fetchFrom(params);
 215         if (icon != null) {
 216             File iconTarget = new File(binDir.toFile(),
 217                     APP_NAME.fetchFrom(params) + ".png");
 218             IOUtils.copyFile(icon, iconTarget);
 219         }
 220     }
 221 
 222     private void copyApplication() throws IOException {
 223         for (RelativeFileSet appResources :
 224                 APP_RESOURCES_LIST.fetchFrom(params)) {
 225             if (appResources == null) {
 226                 throw new RuntimeException("Null app resources?");
 227             }
 228             File srcdir = appResources.getBaseDirectory();
 229             for (String fname : appResources.getIncludedFiles()) {
 230                 copyEntry(appDir, srcdir, fname);
 231             }
 232         }
 233     }
 234 
 235 }