1 /* 2 * Copyright (c) 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package jdk.jpackage.test; 24 25 import java.lang.reflect.InvocationTargetException; 26 import java.lang.reflect.Method; 27 import java.util.HashMap; 28 import java.util.Map; 29 import java.util.Optional; 30 import java.util.Set; 31 import java.util.function.Supplier; 32 import java.util.stream.Collectors; 33 import java.util.stream.Stream; 34 35 /** 36 * jpackage package type traits. 37 */ 38 public enum PackageType { 39 WIN_MSI(".msi", 40 Test.isWindows() ? "jdk.jpackage.internal.WinMsiBundler" : null), 41 WIN_EXE(".exe", 42 Test.isWindows() ? "jdk.jpackage.internal.WinMsiBundler" : null), 43 LINUX_DEB(".deb", 44 Test.isLinux() ? "jdk.jpackage.internal.LinuxDebBundler" : null), 45 LINUX_RPM(".rpm", 46 Test.isLinux() ? "jdk.jpackage.internal.LinuxRpmBundler" : null), 47 MAC_DMG(".dmg", Test.isOSX() ? "jdk.jpackage.internal.MacDmgBundler" : null), 48 MAC_PKG(".pkg", Test.isOSX() ? "jdk.jpackage.internal.MacPkgBundler" : null), 49 IMAGE("app-image", null, null); 50 51 PackageType(String packageName, String bundleSuffix, String bundlerClass) { 52 name = packageName; 53 suffix = bundleSuffix; 54 if (bundlerClass != null && !Inner.DISABLED_PACKAGERS.contains(getName())) { 55 supported = isBundlerSupported(bundlerClass); 56 } else { 57 supported = false; 58 } 59 60 if (suffix != null && supported) { 61 Test.trace(String.format("Bundler %s supported", getName())); 62 } 63 } 64 65 PackageType(String bundleSuffix, String bundlerClass) { 66 this(bundleSuffix.substring(1), bundleSuffix, bundlerClass); 67 } 68 69 void applyTo(JPackageCommand cmd) { 70 cmd.addArguments("--package-type", getName()); 71 } 72 73 String getSuffix() { 74 return suffix; 75 } 76 77 boolean isSupported() { 78 return supported; 79 } 80 81 String getName() { 82 return name; 83 } 84 85 static PackageType fromSuffix(String packageFilename) { 86 if (packageFilename != null) { 87 for (PackageType v : values()) { 88 if (packageFilename.endsWith(v.getSuffix())) { 89 return v; 90 } 91 } 92 } 93 return null; 94 } 95 96 private static boolean isBundlerSupported(String bundlerClass) { 97 try { 98 Class clazz = Class.forName(bundlerClass); 99 Method isSupported = clazz.getDeclaredMethod("isSupported"); 100 return ((Boolean) isSupported.invoke(clazz)); 101 } catch (ClassNotFoundException ex) { 102 return false; 103 } catch (IllegalAccessException | InvocationTargetException ex) { 104 throw new RuntimeException(ex); 105 } catch (NoSuchMethodException ex) { 106 // Not all bundler classes has isSupported() method. 107 return true; 108 } 109 } 110 111 private String name; 112 private final String suffix; 113 private final boolean supported; 114 115 public final static Set<PackageType> LINUX = Set.of(LINUX_DEB, LINUX_RPM); 116 public final static Set<PackageType> WINDOWS = Set.of(WIN_EXE, WIN_MSI); 117 public final static Set<PackageType> MAC = Set.of(MAC_PKG, MAC_DMG); 118 public final static Set<PackageType> NATIVE = Stream.concat( 119 Stream.concat(LINUX.stream(), WINDOWS.stream()), 120 MAC.stream()).collect(Collectors.toUnmodifiableSet()); 121 122 public final static PackageType DEFAULT = ((Supplier<PackageType>) () -> { 123 if (Test.isLinux()) { 124 return LINUX.stream().filter(v -> v.isSupported()).findFirst().orElseThrow(); 125 } 126 127 if (Test.isWindows()) { 128 return WIN_EXE; 129 } 130 131 if (Test.isOSX()) { 132 return MAC_DMG; 133 } 134 135 throw new IllegalStateException("Unknwon platform"); 136 }).get(); 137 138 private final static class Inner { 139 140 private final static Set<String> DISABLED_PACKAGERS = Stream.of( 141 Optional.ofNullable( 142 System.getProperty("jpackage.test.disabledPackagers")).orElse( 143 "").split(",")).collect(Collectors.toUnmodifiableSet()); 144 } 145 }