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 
  24 package jdk.jpackage.test;
  25 
  26 import java.lang.reflect.InvocationTargetException;
  27 import java.lang.reflect.Method;
  28 
  29 
  30 /**
  31  * jpackage package type traits.
  32  */
  33 public enum PackageType {
  34     WIN_MSI(".msi", "jdk.jpackage.internal.WinMsiBundler"),
  35     WIN_EXE(".exe", "jdk.jpackage.internal.WinMsiBundler"),
  36     LINUX_DEB(".deb", "jdk.jpackage.internal.LinuxDebBundler"),
  37     LINUX_RPM(".rpm", "jdk.jpackage.internal.LinuxRpmBundler"),
  38     OSX_DMG(".dmg", "jdk.jpackage.internal.MacDmgBundler"),
  39     IMAGE(null, null);
  40 
  41     PackageType(String bundleSuffix, String bundlerClass) {
  42         suffix = bundleSuffix;
  43         if (bundlerClass != null) {
  44             supported = isBundlerSupported(bundlerClass);
  45         } else {
  46             supported = false;
  47         }
  48 
  49         if (suffix != null && supported) {
  50             Test.trace(String.format("Bundler %s supported", getName()));
  51         }
  52     }
  53 
  54     void applyTo(JPackageCommand cmd) {
  55         cmd.addArguments("--package-type", getName());
  56     }
  57 
  58     String getSuffix() {
  59         return suffix;
  60     }
  61 
  62     boolean isSupported() {
  63         return supported;
  64     }
  65 
  66     String getName() {
  67         if (suffix == null) {
  68             return null;
  69         }
  70         return suffix.substring(1);
  71     }
  72 
  73     static PackageType fromSuffix(String packageFilename) {
  74         if (packageFilename != null) {
  75             for (PackageType v: values()) {
  76                 if (packageFilename.endsWith(v.getSuffix())) {
  77                     return v;
  78                 }
  79             }
  80         }
  81         return null;
  82     }
  83 
  84     private static boolean isBundlerSupported(String bundlerClass) {
  85         try {
  86             Class clazz = Class.forName(bundlerClass);
  87             Method isSupported = clazz.getDeclaredMethod("isSupported");
  88             return ((Boolean)isSupported.invoke(clazz));
  89         } catch (ClassNotFoundException ex) {
  90             return false;
  91         } catch (NoSuchMethodException|IllegalAccessException|InvocationTargetException ex) {
  92             throw new RuntimeException(ex);
  93         }
  94     }
  95 
  96     private final String suffix;
  97     private final boolean supported;
  98 }