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.Optional;
  28 import java.util.Set;
  29 import java.util.function.Supplier;
  30 import java.util.stream.Collectors;
  31 import java.util.stream.Stream;
  32 
  33 /**
  34  * jpackage package type traits.
  35  */
  36 public enum PackageType {
  37     WIN_MSI(".msi",
  38             Test.isWindows() ? "jdk.jpackage.internal.WinMsiBundler" : null),
  39     WIN_EXE(".exe",
  40             Test.isWindows() ? "jdk.jpackage.internal.WinMsiBundler" : null),
  41     LINUX_DEB(".deb",
  42             Test.isLinux() ? "jdk.jpackage.internal.LinuxDebBundler" : null),
  43     LINUX_RPM(".rpm",
  44             Test.isLinux() ? "jdk.jpackage.internal.LinuxRpmBundler" : null),
  45     MAC_DMG(".dmg", Test.isOSX() ? "jdk.jpackage.internal.MacDmgBundler" : null),
  46     MAC_PKG(".pkg", Test.isOSX() ? "jdk.jpackage.internal.MacPkgBundler" : null),
  47     IMAGE("app-image", null, null);
  48 
  49     PackageType(String packageName, String bundleSuffix, String bundlerClass) {
  50         name  = packageName;
  51         suffix = bundleSuffix;
  52         if (bundlerClass != null && !Inner.DISABLED_PACKAGERS.contains(getName())) {
  53             supported = isBundlerSupported(bundlerClass);
  54         } else {
  55             supported = false;
  56         }
  57 
  58         if (suffix != null && supported) {
  59             Test.trace(String.format("Bundler %s supported", getName()));
  60         }
  61     }
  62 
  63     PackageType(String bundleSuffix, String bundlerClass) {
  64         this(bundleSuffix.substring(1), bundleSuffix, bundlerClass);
  65     }
  66 
  67     void applyTo(JPackageCommand cmd) {
  68         cmd.addArguments("--package-type", getName());
  69     }
  70 
  71     String getSuffix() {
  72         return suffix;
  73     }
  74 
  75     boolean isSupported() {
  76         return supported;
  77     }
  78 
  79     String getName() {
  80         return name;
  81     }
  82 
  83     static PackageType fromSuffix(String packageFilename) {
  84         if (packageFilename != null) {
  85             for (PackageType v : values()) {
  86                 if (packageFilename.endsWith(v.getSuffix())) {
  87                     return v;
  88                 }
  89             }
  90         }
  91         return null;
  92     }
  93 
  94     private static boolean isBundlerSupported(String bundlerClass) {
  95         try {
  96             Class clazz = Class.forName(bundlerClass);
  97             Method supported = clazz.getMethod("supported", boolean.class);
  98             return ((Boolean) supported.invoke(clazz.newInstance(), true));
  99         } catch (ClassNotFoundException ex) {
 100             return false;
 101         } catch (InstantiationException | NoSuchMethodException
 102                 | IllegalAccessException | InvocationTargetException ex) {
 103             throw new RuntimeException(ex);
 104         }
 105     }
 106 
 107     private String name;
 108     private final String suffix;
 109     private final boolean supported;
 110 
 111     public final static Set<PackageType> LINUX = Set.of(LINUX_DEB, LINUX_RPM);
 112     public final static Set<PackageType> WINDOWS = Set.of(WIN_EXE, WIN_MSI);
 113     public final static Set<PackageType> MAC = Set.of(MAC_PKG, MAC_DMG);
 114     public final static Set<PackageType> NATIVE = Stream.concat(
 115             Stream.concat(LINUX.stream(), WINDOWS.stream()),
 116             MAC.stream()).collect(Collectors.toUnmodifiableSet());
 117 
 118     public final static PackageType DEFAULT = ((Supplier<PackageType>) () -> {
 119         if (Test.isLinux()) {
 120             return LINUX.stream().filter(v -> v.isSupported()).findFirst().orElseThrow();
 121         }
 122 
 123         if (Test.isWindows()) {
 124             return WIN_EXE;
 125         }
 126 
 127         if (Test.isOSX()) {
 128             return MAC_DMG;
 129         }
 130 
 131         throw new IllegalStateException("Unknwon platform");
 132     }).get();
 133 
 134     private final static class Inner {
 135 
 136         private final static Set<String> DISABLED_PACKAGERS = Stream.of(
 137                 Optional.ofNullable(
 138                         Test.getConfigProperty("disabledPackagers")).orElse(
 139                         "").split(",")).collect(Collectors.toUnmodifiableSet());
 140     }
 141 }