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