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.Collections;
  28 import java.util.Optional;
  29 import java.util.Set;
  30 import java.util.stream.Collectors;
  31 import java.util.stream.Stream;
  32 
  33 /**
  34  * jpackage type traits.
  35  */
  36 public enum PackageType {
  37     WIN_MSI(".msi",
  38             TKit.isWindows() ? "jdk.incubator.jpackage.internal.WinMsiBundler" : null),
  39     WIN_EXE(".exe",
  40             TKit.isWindows() ? "jdk.incubator.jpackage.internal.WinMsiBundler" : null),
  41     LINUX_DEB(".deb",
  42             TKit.isLinux() ? "jdk.incubator.jpackage.internal.LinuxDebBundler" : null),
  43     LINUX_RPM(".rpm",
  44             TKit.isLinux() ? "jdk.incubator.jpackage.internal.LinuxRpmBundler" : null),
  45     MAC_DMG(".dmg", TKit.isOSX() ? "jdk.incubator.jpackage.internal.MacDmgBundler" : null),
  46     MAC_PKG(".pkg", TKit.isOSX() ? "jdk.incubator.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             TKit.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("--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(
  99                     clazz.getConstructor().newInstance(), true));
 100         } catch (ClassNotFoundException | IllegalAccessException ex) {
 101         } catch (InstantiationException | NoSuchMethodException
 102                 | InvocationTargetException ex) {
 103             Functional.rethrowUnchecked(ex);
 104         }
 105         return false;
 106     }
 107 
 108     private final String name;
 109     private final String suffix;
 110     private final boolean supported;
 111 
 112     public final static Set<PackageType> LINUX = Set.of(LINUX_DEB, LINUX_RPM);
 113     public final static Set<PackageType> WINDOWS = Set.of(WIN_EXE, WIN_MSI);
 114     public final static Set<PackageType> MAC = Set.of(MAC_PKG, MAC_DMG);
 115     public final static Set<PackageType> NATIVE = Stream.concat(
 116             Stream.concat(LINUX.stream(), WINDOWS.stream()),
 117             MAC.stream()).collect(Collectors.toUnmodifiableSet());
 118 
 119     private final static class Inner {
 120 
 121         private final static Set<String> DISABLED_PACKAGERS = Optional.ofNullable(
 122                 TKit.tokenizeConfigProperty("disabledPackagers")).orElse(
 123                 Collections.emptySet());
 124     }
 125 }