1 /*
   2  * Copyright (c) 2015, 2018, 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.jpackager.internal.builders.linux;
  27 
  28 import jdk.jpackager.internal.BundlerParamInfo;
  29 import jdk.jpackager.internal.IOUtils;
  30 import jdk.jpackager.internal.Log;
  31 import jdk.jpackager.internal.RelativeFileSet;
  32 import jdk.jpackager.internal.StandardBundlerParam;
  33 import jdk.jpackager.internal.resources.linux.LinuxResources;
  34 import jdk.jpackager.internal.builders.AbstractAppImageBuilder;
  35 
  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.OutputStream;
  42 import java.io.OutputStreamWriter;
  43 import java.io.UncheckedIOException;
  44 import java.io.Writer;
  45 import java.nio.file.Files;
  46 import java.nio.file.Path;
  47 import java.nio.file.attribute.PosixFilePermission;
  48 import java.text.MessageFormat;
  49 import java.util.HashMap;
  50 import java.util.List;
  51 import java.util.Map;
  52 import java.util.Objects;
  53 import java.util.ResourceBundle;
  54 import java.util.Set;
  55 
  56 import static jdk.jpackager.internal.StandardBundlerParam.*;
  57 
  58 public class LinuxAppImageBuilder extends AbstractAppImageBuilder {
  59 
  60     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  61         "jdk.jpackager.internal.resources.builders.linux.LinuxAppImageBuilder");
  62 
  63     protected static final String LINUX_BUNDLER_PREFIX =
  64             BUNDLER_PREFIX + "linux" + File.separator;
  65     private static final String EXECUTABLE_NAME = "JavaAppLauncher";
  66     private static final String LIBRARY_NAME = "libjpackager.so";
  67 
  68     private final Path root;
  69     private final Path appDir;
  70     private final Path runtimeDir;
  71     private final Path resourcesDir;
  72     private final Path mdir;
  73 
  74     private final Map<String, ? super Object> params;
  75 
  76     public static final BundlerParamInfo<File> ICON_PNG =
  77             new StandardBundlerParam<>(
  78             I18N.getString("param.icon-png.name"),
  79             I18N.getString("param.icon-png.description"),
  80             "icon.png",
  81             File.class,
  82             params -> {
  83                 File f = ICON.fetchFrom(params);
  84                 if (f != null && !f.getName().toLowerCase().endsWith(".png")) {
  85                     Log.error(MessageFormat.format(I18N.getString(
  86                             "message.icon-not-png"), f));
  87                     return null;
  88                 }
  89                 return f;
  90             },
  91             (s, p) -> new File(s));
  92 
  93     public LinuxAppImageBuilder(Map<String, Object> config, Path imageOutDir)
  94             throws IOException {
  95         super(config,
  96                 imageOutDir.resolve(APP_NAME.fetchFrom(config) + "/runtime"));
  97 
  98         Objects.requireNonNull(imageOutDir);
  99 
 100         this.root = imageOutDir.resolve(APP_NAME.fetchFrom(config));
 101         this.appDir = root.resolve("app");
 102         this.runtimeDir = root.resolve("runtime");
 103         this.resourcesDir = root.resolve("resources");
 104         this.mdir = runtimeDir.resolve("lib");
 105         this.params = new HashMap<>();
 106         config.entrySet().stream().forEach(e -> params.put(
 107                 e.getKey().toString(), e.getValue()));
 108         Files.createDirectories(appDir);
 109         Files.createDirectories(runtimeDir);
 110         Files.createDirectories(resourcesDir);
 111     }
 112     
 113     public LinuxAppImageBuilder(String appName, Path imageOutDir)
 114             throws IOException {
 115         super(null, imageOutDir.resolve(appName));
 116 
 117         Objects.requireNonNull(imageOutDir);
 118 
 119         this.root = imageOutDir.resolve(appName);
 120         this.appDir = null;
 121         this.runtimeDir = null;
 122         this.resourcesDir = null;
 123         this.mdir = null;
 124         this.params = new HashMap<>();
 125     }
 126 
 127     private Path destFile(String dir, String filename) {
 128         return runtimeDir.resolve(dir).resolve(filename);
 129     }
 130 
 131     private void writeEntry(InputStream in, Path dstFile) throws IOException {
 132         Files.createDirectories(dstFile.getParent());
 133         Files.copy(in, dstFile);
 134     }
 135 
 136     private void writeSymEntry(Path dstFile, Path target) throws IOException {
 137         Files.createDirectories(dstFile.getParent());
 138         Files.createLink(dstFile, target);
 139     }
 140 
 141     /**
 142      * chmod ugo+x file
 143      */
 144     private void setExecutable(Path file) {
 145         try {
 146             Set<PosixFilePermission> perms =
 147                     Files.getPosixFilePermissions(file);
 148             perms.add(PosixFilePermission.OWNER_EXECUTE);
 149             perms.add(PosixFilePermission.GROUP_EXECUTE);
 150             perms.add(PosixFilePermission.OTHERS_EXECUTE);
 151             Files.setPosixFilePermissions(file, perms);
 152         } catch (IOException ioe) {
 153             throw new UncheckedIOException(ioe);
 154         }
 155     }
 156 
 157     private static void createUtf8File(File file, String content)
 158             throws IOException {
 159         try (OutputStream fout = new FileOutputStream(file);
 160             Writer output = new OutputStreamWriter(fout, "UTF-8")) {
 161             output.write(content);
 162         }
 163     }
 164 
 165 
 166     // it is static for the sake of sharing with "installer" bundlers
 167     // that may skip calls to validate/bundle in this class!
 168     public static File getRootDir(File outDir, Map<String, ? super Object> p) {
 169         return new File(outDir, APP_FS_NAME.fetchFrom(p));
 170     }
 171 
 172     public static String getLauncherName(Map<String, ? super Object> p) {
 173         return APP_FS_NAME.fetchFrom(p);
 174     }
 175 
 176     public static String getLauncherCfgName(Map<String, ? super Object> p) {
 177         return "app/" + APP_FS_NAME.fetchFrom(p) + ".cfg";
 178     }
 179 
 180     @Override
 181     public InputStream getResourceAsStream(String name) {
 182         return LinuxResources.class.getResourceAsStream(name);
 183     }
 184 
 185     @Override
 186     public void prepareApplicationFiles() throws IOException {
 187         Map<String, ? super Object> originalParams = new HashMap<>(params);
 188 
 189         try {
 190             // create the primary launcher
 191             createLauncherForEntryPoint(params, root);
 192 
 193             // Copy library to the launcher folder
 194             try (InputStream is_lib = getResourceAsStream(LIBRARY_NAME)) {
 195                writeEntry(is_lib,  root.resolve(LIBRARY_NAME));
 196             }
 197 
 198             // create the secondary launchers, if any
 199             List<Map<String, ? super Object>> entryPoints =
 200                     StandardBundlerParam.SECONDARY_LAUNCHERS.fetchFrom(params);
 201             for (Map<String, ? super Object> entryPoint : entryPoints) {
 202                 Map<String, ? super Object> tmp = new HashMap<>(originalParams);
 203                 tmp.putAll(entryPoint);
 204                 // remove name.fs that was calculated for main launcher.
 205                 // otherwise, wrong launcher name will be selected.
 206                 tmp.remove(APP_FS_NAME.getID());
 207                 createLauncherForEntryPoint(tmp, root);
 208             }
 209 
 210             // Copy class path entries to Java folder
 211             copyApplication();
 212 
 213             // Copy icon to Resources folder
 214             copyIcon();
 215 
 216         } catch (IOException ex) {
 217             Log.error("Exception: " + ex);
 218             Log.debug(ex);
 219         }
 220     }
 221 
 222     @Override
 223     public void prepareServerJreFiles() throws IOException {}
 224 
 225     private void createLauncherForEntryPoint(Map<String, ? super Object> p,
 226             Path rootDir) throws IOException {
 227         // Copy executable to Linux folder
 228         Path executableFile = root.resolve(getLauncherName(p));
 229         try (InputStream is_launcher = getResourceAsStream("papplauncher")) {
 230             writeEntry(is_launcher, executableFile);
 231         }
 232 
 233         executableFile.toFile().setExecutable(true, false);
 234         executableFile.toFile().setWritable(true, true);
 235 
 236         writeCfgFile(p, root.resolve(getLauncherCfgName(p)).toFile(),
 237                 "$APPDIR/runtime");
 238     }
 239 
 240     private void copyIcon() throws IOException {
 241         File icon = ICON_PNG.fetchFrom(params);
 242         if (icon != null) {
 243             File iconTarget = new File(resourcesDir.toFile(),
 244                     APP_FS_NAME.fetchFrom(params) + ".png");
 245             IOUtils.copyFile(icon, iconTarget);
 246         }
 247     }
 248 
 249     private void copyApplication() throws IOException {
 250         for (RelativeFileSet appResources :
 251                 APP_RESOURCES_LIST.fetchFrom(params)) {
 252             if (appResources == null) {
 253                 throw new RuntimeException("Null app resources?");
 254             }
 255             File srcdir = appResources.getBaseDirectory();
 256             for (String fname : appResources.getIncludedFiles()) {
 257                 copyEntry(appDir, srcdir, fname);
 258             }
 259         }
 260     }
 261 
 262 }