/* * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.jpackager.internal.builders.linux; import jdk.jpackager.internal.BundlerParamInfo; import jdk.jpackager.internal.IOUtils; import jdk.jpackager.internal.Log; import jdk.jpackager.internal.RelativeFileSet; import jdk.jpackager.internal.StandardBundlerParam; import jdk.jpackager.internal.resources.linux.LinuxResources; import jdk.jpackager.internal.builders.AbstractAppImageBuilder; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UncheckedIOException; import java.io.Writer; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.PosixFilePermission; import java.text.MessageFormat; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.ResourceBundle; import java.util.Set; import static jdk.jpackager.internal.StandardBundlerParam.*; public class LinuxAppImageBuilder extends AbstractAppImageBuilder { private static final ResourceBundle I18N = ResourceBundle.getBundle( "jdk.jpackager.internal.resources.builders.linux.LinuxAppImageBuilder"); protected static final String LINUX_BUNDLER_PREFIX = BUNDLER_PREFIX + "linux" + File.separator; private static final String EXECUTABLE_NAME = "JavaAppLauncher"; private static final String LIBRARY_NAME = "libjpackager.so"; private final Path root; private final Path appDir; private final Path runtimeDir; private final Path resourcesDir; private final Path mdir; private final Map params; public static final BundlerParamInfo ICON_PNG = new StandardBundlerParam<>( I18N.getString("param.icon-png.name"), I18N.getString("param.icon-png.description"), "icon.png", File.class, params -> { File f = ICON.fetchFrom(params); if (f != null && !f.getName().toLowerCase().endsWith(".png")) { Log.error(MessageFormat.format(I18N.getString( "message.icon-not-png"), f)); return null; } return f; }, (s, p) -> new File(s)); public LinuxAppImageBuilder(Map config, Path imageOutDir) throws IOException { super(config, imageOutDir.resolve(APP_NAME.fetchFrom(config) + "/runtime")); Objects.requireNonNull(imageOutDir); this.root = imageOutDir.resolve(APP_NAME.fetchFrom(config)); this.appDir = root.resolve("app"); this.runtimeDir = root.resolve("runtime"); this.resourcesDir = root.resolve("resources"); this.mdir = runtimeDir.resolve("lib"); this.params = new HashMap<>(); config.entrySet().stream().forEach(e -> params.put( e.getKey().toString(), e.getValue())); Files.createDirectories(appDir); Files.createDirectories(runtimeDir); Files.createDirectories(resourcesDir); } public LinuxAppImageBuilder(String appName, Path imageOutDir) throws IOException { super(null, imageOutDir.resolve(appName)); Objects.requireNonNull(imageOutDir); this.root = imageOutDir.resolve(appName); this.appDir = null; this.runtimeDir = null; this.resourcesDir = null; this.mdir = null; this.params = new HashMap<>(); } private Path destFile(String dir, String filename) { return runtimeDir.resolve(dir).resolve(filename); } private void writeEntry(InputStream in, Path dstFile) throws IOException { Files.createDirectories(dstFile.getParent()); Files.copy(in, dstFile); } private void writeSymEntry(Path dstFile, Path target) throws IOException { Files.createDirectories(dstFile.getParent()); Files.createLink(dstFile, target); } /** * chmod ugo+x file */ private void setExecutable(Path file) { try { Set perms = Files.getPosixFilePermissions(file); perms.add(PosixFilePermission.OWNER_EXECUTE); perms.add(PosixFilePermission.GROUP_EXECUTE); perms.add(PosixFilePermission.OTHERS_EXECUTE); Files.setPosixFilePermissions(file, perms); } catch (IOException ioe) { throw new UncheckedIOException(ioe); } } private static void createUtf8File(File file, String content) throws IOException { try (OutputStream fout = new FileOutputStream(file); Writer output = new OutputStreamWriter(fout, "UTF-8")) { output.write(content); } } // it is static for the sake of sharing with "installer" bundlers // that may skip calls to validate/bundle in this class! public static File getRootDir(File outDir, Map p) { return new File(outDir, APP_FS_NAME.fetchFrom(p)); } public static String getLauncherName(Map p) { return APP_FS_NAME.fetchFrom(p); } public static String getLauncherCfgName(Map p) { return "app/" + APP_FS_NAME.fetchFrom(p) + ".cfg"; } @Override public InputStream getResourceAsStream(String name) { return LinuxResources.class.getResourceAsStream(name); } @Override public void prepareApplicationFiles() throws IOException { Map originalParams = new HashMap<>(params); try { // create the primary launcher createLauncherForEntryPoint(params, root); // Copy library to the launcher folder try (InputStream is_lib = getResourceAsStream(LIBRARY_NAME)) { writeEntry(is_lib, root.resolve(LIBRARY_NAME)); } // create the secondary launchers, if any List> entryPoints = StandardBundlerParam.SECONDARY_LAUNCHERS.fetchFrom(params); for (Map entryPoint : entryPoints) { Map tmp = new HashMap<>(originalParams); tmp.putAll(entryPoint); // remove name.fs that was calculated for main launcher. // otherwise, wrong launcher name will be selected. tmp.remove(APP_FS_NAME.getID()); createLauncherForEntryPoint(tmp, root); } // Copy class path entries to Java folder copyApplication(); // Copy icon to Resources folder copyIcon(); } catch (IOException ex) { Log.error("Exception: " + ex); Log.debug(ex); } } @Override public void prepareServerJreFiles() throws IOException {} private void createLauncherForEntryPoint(Map p, Path rootDir) throws IOException { // Copy executable to Linux folder Path executableFile = root.resolve(getLauncherName(p)); try (InputStream is_launcher = getResourceAsStream("papplauncher")) { writeEntry(is_launcher, executableFile); } executableFile.toFile().setExecutable(true, false); executableFile.toFile().setWritable(true, true); writeCfgFile(p, root.resolve(getLauncherCfgName(p)).toFile(), "$APPDIR/runtime"); } private void copyIcon() throws IOException { File icon = ICON_PNG.fetchFrom(params); if (icon != null) { File iconTarget = new File(resourcesDir.toFile(), APP_FS_NAME.fetchFrom(params) + ".png"); IOUtils.copyFile(icon, iconTarget); } } private void copyApplication() throws IOException { for (RelativeFileSet appResources : APP_RESOURCES_LIST.fetchFrom(params)) { if (appResources == null) { throw new RuntimeException("Null app resources?"); } File srcdir = appResources.getBaseDirectory(); for (String fname : appResources.getIncludedFiles()) { copyEntry(appDir, srcdir, fname); } } } }