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.ByteArrayOutputStream; 29 import java.io.File; 30 import java.io.IOException; 31 import java.io.PrintStream; 32 import java.nio.file.Files; 33 import java.text.MessageFormat; 34 import java.util.ArrayList; 35 import java.util.Arrays; 36 import java.util.Collection; 37 import java.util.LinkedHashSet; 38 import java.util.List; 39 import java.util.Map; 40 import java.util.ResourceBundle; 41 import java.util.regex.Matcher; 42 import java.util.regex.Pattern; 43 44 import static jdk.jpackage.internal.StandardBundlerParam.*; 45 46 public abstract class MacBaseInstallerBundler extends AbstractBundler { 47 48 private static final ResourceBundle I18N = ResourceBundle.getBundle( 49 "jdk.jpackage.internal.resources.MacResources"); 50 51 // This could be generalized more to be for any type of Image Bundler 52 public static final BundlerParamInfo<MacAppBundler> APP_BUNDLER = 53 new StandardBundlerParam<>( 54 "mac.app.bundler", 55 MacAppBundler.class, 56 params -> new MacAppBundler(), 57 (s, p) -> null); 58 59 public final BundlerParamInfo<File> APP_IMAGE_TEMP_ROOT = 60 new StandardBundlerParam<>( 61 "mac.app.imageRoot", 62 File.class, 63 params -> { 64 File imageDir = IMAGES_ROOT.fetchFrom(params); 65 if (!imageDir.exists()) imageDir.mkdirs(); 66 try { 67 return Files.createTempDirectory( 68 imageDir.toPath(), "image-").toFile(); 69 } catch (IOException e) { 87 null); 88 89 public static final BundlerParamInfo<String> INSTALLER_NAME = 90 new StandardBundlerParam<> ( 91 "mac.installerName", 92 String.class, 93 params -> { 94 String nm = APP_NAME.fetchFrom(params); 95 if (nm == null) return null; 96 97 String version = VERSION.fetchFrom(params); 98 if (version == null) { 99 return nm; 100 } else { 101 return nm + "-" + version; 102 } 103 }, 104 (s, p) -> s); 105 106 protected void validateAppImageAndBundeler( 107 Map<String, ? super Object> params) 108 throws ConfigException, UnsupportedPlatformException { 109 if (PREDEFINED_APP_IMAGE.fetchFrom(params) != null) { 110 File applicationImage = PREDEFINED_APP_IMAGE.fetchFrom(params); 111 if (!applicationImage.exists()) { 112 throw new ConfigException( 113 MessageFormat.format(I18N.getString( 114 "message.app-image-dir-does-not-exist"), 115 PREDEFINED_APP_IMAGE.getID(), 116 applicationImage.toString()), 117 MessageFormat.format(I18N.getString( 118 "message.app-image-dir-does-not-exist.advice"), 119 PREDEFINED_APP_IMAGE.getID())); 120 } 121 if (APP_NAME.fetchFrom(params) == null) { 122 throw new ConfigException( 123 I18N.getString("message.app-image-requires-app-name"), 124 I18N.getString( 125 "message.app-image-requires-app-name.advice")); 126 } 127 if (IDENTIFIER.fetchFrom(params) == null) { 128 throw new ConfigException( 129 I18N.getString("message.app-image-requires-identifier"), 130 I18N.getString( 131 "message.app-image-requires-identifier.advice")); 132 } 133 } else { 134 APP_BUNDLER.fetchFrom(params).validate(params); 135 } 136 } 137 138 protected File prepareAppBundle(Map<String, ? super Object> p, 139 boolean pkg) throws PackagerException { 140 File predefinedImage = StandardBundlerParam.getPredefinedAppImage(p); 141 if (predefinedImage != null) { 142 return predefinedImage; 143 } 144 File appImageRoot = APP_IMAGE_TEMP_ROOT.fetchFrom(p); 145 if (pkg) { 146 // create pkg in dmg 147 return new MacPkgBundler().bundle(p, appImageRoot); 148 } else { 149 return APP_BUNDLER.fetchFrom(p).doBundle(p, appImageRoot, true); 150 } 151 } 152 153 @Override 154 public Collection<BundlerParamInfo<?>> getBundleParameters() { 155 Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>(); 156 157 results.addAll(MacAppBundler.getAppBundleParameters()); 158 results.addAll(Arrays.asList( 159 APP_BUNDLER, 160 CONFIG_ROOT, 161 APP_IMAGE_TEMP_ROOT, 162 PREDEFINED_APP_IMAGE 163 )); 164 165 return results; 166 } 167 168 @Override 169 public String getBundleType() { 170 return "INSTALLER"; 171 } 172 173 public static String findKey(String key, String keychainName, 174 boolean verbose) { 175 if (Platform.getPlatform() != Platform.MAC) { 176 return null; 177 } 178 179 try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); 180 PrintStream ps = new PrintStream(baos)) { 181 List<String> searchOptions = new ArrayList<>(); 182 searchOptions.add("security"); 183 searchOptions.add("find-certificate"); 184 searchOptions.add("-c"); 185 searchOptions.add(key); 186 searchOptions.add("-a"); 187 if (keychainName != null && !keychainName.isEmpty()) { 188 searchOptions.add(keychainName); 189 } 190 191 ProcessBuilder pb = new ProcessBuilder(searchOptions); 192 193 IOUtils.exec(pb, verbose, false, ps); 194 Pattern p = Pattern.compile("\"alis\"<blob>=\"([^\"]+)\""); 195 Matcher m = p.matcher(baos.toString()); 196 if (!m.find()) { 197 Log.error("Did not find a key matching '" + key + "'"); 198 return null; 199 } 200 String matchedKey = m.group(1); 201 if (m.find()) { 202 Log.error("Found more than one key matching '" + key + "'"); 203 return null; 204 } 205 Log.debug("Using key '" + matchedKey + "'"); 206 return matchedKey; 207 } catch (IOException ioe) { 208 Log.verbose(ioe); 209 return null; 210 } 211 } 212 } | 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.incubator.jpackage.internal; 27 28 import java.io.ByteArrayOutputStream; 29 import java.io.File; 30 import java.io.IOException; 31 import java.io.PrintStream; 32 import java.nio.file.Files; 33 import java.text.MessageFormat; 34 import java.util.ArrayList; 35 import java.util.List; 36 import java.util.Map; 37 import java.util.ResourceBundle; 38 import java.util.regex.Matcher; 39 import java.util.regex.Pattern; 40 41 import static jdk.incubator.jpackage.internal.StandardBundlerParam.*; 42 43 public abstract class MacBaseInstallerBundler extends AbstractBundler { 44 45 private static final ResourceBundle I18N = ResourceBundle.getBundle( 46 "jdk.incubator.jpackage.internal.resources.MacResources"); 47 48 // This could be generalized more to be for any type of Image Bundler 49 public static final BundlerParamInfo<MacAppBundler> APP_BUNDLER = 50 new StandardBundlerParam<>( 51 "mac.app.bundler", 52 MacAppBundler.class, 53 params -> new MacAppBundler(), 54 (s, p) -> null); 55 56 public final BundlerParamInfo<File> APP_IMAGE_TEMP_ROOT = 57 new StandardBundlerParam<>( 58 "mac.app.imageRoot", 59 File.class, 60 params -> { 61 File imageDir = IMAGES_ROOT.fetchFrom(params); 62 if (!imageDir.exists()) imageDir.mkdirs(); 63 try { 64 return Files.createTempDirectory( 65 imageDir.toPath(), "image-").toFile(); 66 } catch (IOException e) { 84 null); 85 86 public static final BundlerParamInfo<String> INSTALLER_NAME = 87 new StandardBundlerParam<> ( 88 "mac.installerName", 89 String.class, 90 params -> { 91 String nm = APP_NAME.fetchFrom(params); 92 if (nm == null) return null; 93 94 String version = VERSION.fetchFrom(params); 95 if (version == null) { 96 return nm; 97 } else { 98 return nm + "-" + version; 99 } 100 }, 101 (s, p) -> s); 102 103 protected void validateAppImageAndBundeler( 104 Map<String, ? super Object> params) throws ConfigException { 105 if (PREDEFINED_APP_IMAGE.fetchFrom(params) != null) { 106 File applicationImage = PREDEFINED_APP_IMAGE.fetchFrom(params); 107 if (!applicationImage.exists()) { 108 throw new ConfigException( 109 MessageFormat.format(I18N.getString( 110 "message.app-image-dir-does-not-exist"), 111 PREDEFINED_APP_IMAGE.getID(), 112 applicationImage.toString()), 113 MessageFormat.format(I18N.getString( 114 "message.app-image-dir-does-not-exist.advice"), 115 PREDEFINED_APP_IMAGE.getID())); 116 } 117 if (APP_NAME.fetchFrom(params) == null) { 118 throw new ConfigException( 119 I18N.getString("message.app-image-requires-app-name"), 120 I18N.getString( 121 "message.app-image-requires-app-name.advice")); 122 } 123 } else { 124 APP_BUNDLER.fetchFrom(params).validate(params); 125 } 126 } 127 128 protected File prepareAppBundle(Map<String, ? super Object> params) 129 throws PackagerException { 130 File predefinedImage = 131 StandardBundlerParam.getPredefinedAppImage(params); 132 if (predefinedImage != null) { 133 return predefinedImage; 134 } 135 File appImageRoot = APP_IMAGE_TEMP_ROOT.fetchFrom(params); 136 137 return APP_BUNDLER.fetchFrom(params).doBundle( 138 params, appImageRoot, true); 139 } 140 141 @Override 142 public String getBundleType() { 143 return "INSTALLER"; 144 } 145 146 public static String findKey(String key, String keychainName, 147 boolean verbose) { 148 if (Platform.getPlatform() != Platform.MAC) { 149 return null; 150 } 151 152 try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); 153 PrintStream ps = new PrintStream(baos)) { 154 List<String> searchOptions = new ArrayList<>(); 155 searchOptions.add("security"); 156 searchOptions.add("find-certificate"); 157 searchOptions.add("-c"); 158 searchOptions.add(key); 159 searchOptions.add("-a"); 160 if (keychainName != null && !keychainName.isEmpty()) { 161 searchOptions.add(keychainName); 162 } 163 164 ProcessBuilder pb = new ProcessBuilder(searchOptions); 165 166 IOUtils.exec(pb, false, ps); 167 Pattern p = Pattern.compile("\"alis\"<blob>=\"([^\"]+)\""); 168 Matcher m = p.matcher(baos.toString()); 169 if (!m.find()) { 170 Log.error("Did not find a key matching '" + key + "'"); 171 return null; 172 } 173 String matchedKey = m.group(1); 174 if (m.find()) { 175 Log.error("Found more than one key matching '" + key + "'"); 176 return null; 177 } 178 Log.verbose("Using key '" + matchedKey + "'"); 179 return matchedKey; 180 } catch (IOException ioe) { 181 Log.verbose(ioe); 182 return null; 183 } 184 } 185 } |