/* * Copyright (c) 2012, 2016, 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 com.oracle.tools.packager.linux; import com.oracle.tools.packager.AbstractImageBundler; import com.oracle.tools.packager.BundlerParamInfo; import com.oracle.tools.packager.ConfigException; import com.oracle.tools.packager.IOUtils; import com.oracle.tools.packager.JreUtils; import com.oracle.tools.packager.JreUtils.Rule; import com.oracle.tools.packager.Log; import com.oracle.tools.packager.Platform; import com.oracle.tools.packager.RelativeFileSet; import com.oracle.tools.packager.StandardBundlerParam; import com.oracle.tools.packager.UnsupportedPlatformException; import com.sun.javafx.tools.packager.bundlers.BundleParams; import jdk.packager.internal.legacy.builders.linux.LinuxAppImageBuilder; import jdk.packager.internal.legacy.JLinkBundlerHelper; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.text.MessageFormat; import java.util.Arrays; import java.util.Collection; import java.util.Map; import java.util.ResourceBundle; import static com.oracle.tools.packager.StandardBundlerParam.*; import jdk.packager.internal.legacy.builders.AbstractAppImageBuilder; public class LinuxAppBundler extends AbstractImageBundler { private static final ResourceBundle I18N = ResourceBundle.getBundle(LinuxAppBundler.class.getName()); protected static final String LINUX_BUNDLER_PREFIX = BUNDLER_PREFIX + "linux" + File.separator; private static final String EXECUTABLE_NAME = "JavaAppLauncher"; 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.info(MessageFormat.format(I18N.getString("message.icon-not-png"), f)); return null; } return f; }, (s, p) -> new File(s)); public static final BundlerParamInfo RAW_EXECUTABLE_URL = new StandardBundlerParam<>( I18N.getString("param.raw-executable-url.name"), I18N.getString("param.raw-executable-url.description"), "linux.launcher.url", URL.class, params -> LinuxResources.class.getResource(EXECUTABLE_NAME), (s, p) -> { try { return new URL(s); } catch (MalformedURLException e) { Log.info(e.toString()); return null; } }); //Subsetting of JRE is restricted. //JRE README defines what is allowed to strip: // http://www.oracle.com/technetwork/java/javase/jre-8-readme-2095710.html // public static final BundlerParamInfo LINUX_JRE_RULES = new StandardBundlerParam<>( "", "", ".linux.runtime.rules", Rule[].class, params -> new Rule[]{ Rule.prefixNeg("/bin"), Rule.prefixNeg("/plugin"), //Rule.prefixNeg("/lib/ext"), //need some of jars there for https to work Rule.suffix("deploy.jar"), //take deploy.jar Rule.prefixNeg("/lib/deploy"), Rule.prefixNeg("/lib/desktop"), Rule.substrNeg("libnpjp2.so") }, (s, p) -> null ); public static final BundlerParamInfo LINUX_RUNTIME = new StandardBundlerParam<>( I18N.getString("param.runtime.name"), I18N.getString("param.runtime.description"), BundleParams.PARAM_RUNTIME, RelativeFileSet.class, params -> JreUtils.extractJreAsRelativeFileSet(System.getProperty("java.home"), LINUX_JRE_RULES.fetchFrom(params)), (s, p) -> JreUtils.extractJreAsRelativeFileSet(s, LINUX_JRE_RULES.fetchFrom(p)) ); @Override public boolean validate(Map p) throws UnsupportedPlatformException, ConfigException { try { if (p == null) throw new ConfigException( I18N.getString("error.parameters-null"), I18N.getString("error.parameters-null.advice")); return doValidate(p); } catch (RuntimeException re) { if (re.getCause() instanceof ConfigException) { throw (ConfigException) re.getCause(); } else { throw new ConfigException(re); } } } //used by chained bundlers to reuse validation logic boolean doValidate(Map p) throws UnsupportedPlatformException, ConfigException { if (Platform.getPlatform() != Platform.LINUX) { throw new UnsupportedPlatformException(); } imageBundleValidation(p); if (RAW_EXECUTABLE_URL.fetchFrom(p) == null) { throw new ConfigException( I18N.getString("error.no-linux-resources"), I18N.getString("error.no-linux-resources.advice")); } return true; } //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 getLauncherCfgName(Map p) { return "app/" + APP_FS_NAME.fetchFrom(p) +".cfg"; } File doBundle(Map p, File outputDirectory, boolean dependentTask) { try { if (!outputDirectory.isDirectory() && !outputDirectory.mkdirs()) { throw new RuntimeException(MessageFormat.format(I18N.getString("error.cannot-create-output-dir"), outputDirectory.getAbsolutePath())); } if (!outputDirectory.canWrite()) { throw new RuntimeException(MessageFormat.format(I18N.getString("error.cannot-write-to-output-dir"), outputDirectory.getAbsolutePath())); } // Create directory structure File rootDirectory = getRootDir(outputDirectory, p); IOUtils.deleteRecursive(rootDirectory); rootDirectory.mkdirs(); if (!dependentTask) { Log.info(MessageFormat.format(I18N.getString("message.creating-bundle-location"), rootDirectory.getAbsolutePath())); } if (!p.containsKey(JLinkBundlerHelper.JLINK_BUILDER.getID())) { p.put(JLinkBundlerHelper.JLINK_BUILDER.getID(), "linuxapp-image-builder"); } AbstractAppImageBuilder appBuilder = new LinuxAppImageBuilder(p, outputDirectory.toPath()); JLinkBundlerHelper.execute(p, appBuilder); return rootDirectory; } catch (IOException ex) { Log.info("Exception: "+ex); Log.debug(ex); return null; } catch (Exception ex) { Log.info("Exception: "+ex); Log.debug(ex); return null; } } @Override public String getName() { return I18N.getString("bundler.name"); } @Override public String getDescription() { return I18N.getString("bundler.description"); } @Override public String getID() { return "linux.app"; } @Override public String getBundleType() { return "IMAGE"; } @Override public Collection> getBundleParameters() { return getAppBundleParameters(); } public static Collection> getAppBundleParameters() { return Arrays.asList( APP_NAME, APP_RESOURCES, // APP_RESOURCES_LIST, // ?? ARGUMENTS, CLASSPATH, JVM_OPTIONS, JVM_PROPERTIES, LINUX_RUNTIME, MAIN_CLASS, MAIN_JAR, PREFERENCES_ID, PRELOADER_CLASS, USER_JVM_OPTIONS, VERSION ); } @Override public File execute(Map params, File outputParentDir) { return doBundle(params, outputParentDir, false); } }