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