1 /* 2 * Copyright (c) 2012, 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.IOException; 30 import java.net.MalformedURLException; 31 import java.net.URL; 32 import java.text.MessageFormat; 33 import java.util.Arrays; 34 import java.util.Collection; 35 import java.util.Map; 36 import java.util.ResourceBundle; 37 38 import static jdk.jpackage.internal.StandardBundlerParam.*; 39 40 public class LinuxAppBundler extends AbstractImageBundler { 41 42 private static final ResourceBundle I18N = ResourceBundle.getBundle( 43 "jdk.jpackage.internal.resources.LinuxResources"); 44 45 static final BundlerParamInfo<File> ICON_PNG = 46 new StandardBundlerParam<>( 47 "icon.png", 48 File.class, 49 params -> { 50 File f = ICON.fetchFrom(params); 51 if (f != null && !f.getName().toLowerCase().endsWith(".png")) { 52 Log.error(MessageFormat.format( 53 I18N.getString("message.icon-not-png"), f)); 54 return null; 55 } 56 return f; 57 }, 58 (s, p) -> new File(s)); 59 60 static final BundlerParamInfo<String> LINUX_INSTALL_DIR = 61 new StandardBundlerParam<>( 62 "linux-install-dir", 63 String.class, 64 params -> { 65 String dir = INSTALL_DIR.fetchFrom(params); 66 if (dir != null) { 67 if (dir.endsWith("/")) { 68 dir = dir.substring(0, dir.length()-1); 69 } 70 return dir; 71 } 72 return "/opt"; 73 }, 74 (s, p) -> s 75 ); 76 77 static final BundlerParamInfo<String> LINUX_PACKAGE_DEPENDENCIES = 78 new StandardBundlerParam<>( 79 Arguments.CLIOptions.LINUX_PACKAGE_DEPENDENCIES.getId(), 80 String.class, 81 params -> { 82 return ""; 83 }, 84 (s, p) -> s 85 ); 86 87 @Override 88 public boolean validate(Map<String, ? super Object> params) 89 throws UnsupportedPlatformException, ConfigException { 90 try { 91 if (params == null) throw new ConfigException( 92 I18N.getString("error.parameters-null"), 93 I18N.getString("error.parameters-null.advice")); 94 95 return doValidate(params); 96 } catch (RuntimeException re) { 97 if (re.getCause() instanceof ConfigException) { 98 throw (ConfigException) re.getCause(); 99 } else { 100 throw new ConfigException(re); 101 } 102 } 103 } 104 105 private boolean doValidate(Map<String, ? super Object> params) 106 throws UnsupportedPlatformException, ConfigException { 107 if (Platform.getPlatform() != Platform.LINUX) { 108 throw new UnsupportedPlatformException(); 109 } 110 111 imageBundleValidation(params); 112 113 return true; 114 } 115 116 // it is static for the sake of sharing with "installer" bundlers 117 // that may skip calls to validate/bundle in this class! 118 static File getRootDir(File outDir, Map<String, ? super Object> params) { 119 return new File(outDir, APP_NAME.fetchFrom(params)); 120 } 121 122 static String getLauncherCfgName(Map<String, ? super Object> params) { 123 return "app/" + APP_NAME.fetchFrom(params) +".cfg"; 124 } 125 126 File doBundle(Map<String, ? super Object> params, File outputDirectory, 127 boolean dependentTask) throws PackagerException { 128 if (StandardBundlerParam.isRuntimeInstaller(params)) { 129 return PREDEFINED_RUNTIME_IMAGE.fetchFrom(params); 130 } else { 131 return doAppBundle(params, outputDirectory, dependentTask); 132 } 133 } 134 135 private File doAppBundle(Map<String, ? super Object> params, 136 File outputDirectory, boolean dependentTask) throws PackagerException { 137 try { 138 File rootDirectory = createRoot(params, outputDirectory, 139 dependentTask, APP_NAME.fetchFrom(params)); 140 AbstractAppImageBuilder appBuilder = new LinuxAppImageBuilder( 141 params, outputDirectory.toPath()); 142 if (PREDEFINED_RUNTIME_IMAGE.fetchFrom(params) == null ) { 143 JLinkBundlerHelper.execute(params, appBuilder); 144 } else { 145 StandardBundlerParam.copyPredefinedRuntimeImage( 146 params, appBuilder); 147 } 148 return rootDirectory; 149 } catch (PackagerException pe) { 150 throw pe; 151 } catch (Exception ex) { 152 Log.verbose(ex); 153 throw new PackagerException(ex); 154 } 155 } 156 157 @Override 158 public String getName() { 159 return I18N.getString("app.bundler.name"); 160 } 161 162 @Override 163 public String getDescription() { 164 return I18N.getString("app.bundler.description"); 165 } 166 167 @Override 168 public String getID() { 169 return "linux.app"; 170 } 171 172 @Override 173 public String getBundleType() { 174 return "IMAGE"; 175 } 176 177 @Override 178 public Collection<BundlerParamInfo<?>> getBundleParameters() { 179 return getAppBundleParameters(); 180 } 181 182 static Collection<BundlerParamInfo<?>> getAppBundleParameters() { 183 return Arrays.asList( 184 APP_NAME, 185 APP_RESOURCES, 186 ARGUMENTS, 187 CLASSPATH, 188 JAVA_OPTIONS, 189 MAIN_CLASS, 190 MAIN_JAR, 191 VERSION, 192 VERBOSE 193 ); 194 } 195 196 @Override 197 public File execute(Map<String, ? super Object> params, 198 File outputParentDir) throws PackagerException { 199 return doBundle(params, outputParentDir, false); 200 } 201 202 @Override 203 public boolean supported(boolean runtimeInstaller) { 204 return (Platform.getPlatform() == Platform.LINUX); 205 } 206 }