1 /*
   2  * Copyright (c) 2017, 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 import java.security.KeyFactory;
  25 import java.security.NoSuchAlgorithmException;
  26 
  27 import javax.net.ssl.SSLParameters;
  28 
  29 /*
  30  * This class is used for returning some specific JDK information.
  31  */
  32 public class JdkUtils {
  33 
  34     public static final String JAVA_RUNTIME_VERSION = "javaRuntimeVersion";
  35     public static final String SUPPORTS_EC_KEY = "supportsECKey";
  36     public static final String SUPPORTS_SNI = "supportsSNI";
  37     public static final String SUPPORTS_ALPN = "supportsALPN";
  38 
  39     // Returns the JDK build version.
  40     public static String javaRuntimeVersion() {
  41         return System.getProperty("java.runtime.version");
  42     }
  43 
  44     // Checks if EC key algorithm is supported by the JDK build.
  45     private static boolean supportsECKey() {
  46         boolean isSupported = true;
  47         try {
  48             KeyFactory.getInstance("EC");
  49         } catch (NoSuchAlgorithmException e) {
  50             isSupported = false;
  51         }
  52         return isSupported;
  53     }
  54 
  55     // Checks if SNI is supported by the JDK build.
  56     private static boolean supportsSNI() {
  57         boolean isSupported = true;
  58         try {
  59             SSLParameters.class.getMethod("getServerNames");
  60         } catch (NoSuchMethodException e) {
  61             isSupported = false;
  62         }
  63         return isSupported;
  64     }
  65 
  66     // Checks if ALPN is supported by the JDK build.
  67     private static boolean supportsALPN() {
  68         boolean isSupported = true;
  69         try {
  70             SSLParameters.class.getMethod("getApplicationProtocols");
  71         } catch (NoSuchMethodException e) {
  72             isSupported = false;
  73         }
  74         return isSupported;
  75     }
  76 
  77     public static void main(String[] args) {
  78         System.out.print(Utils.join(Utils.PARAM_DELIMITER,
  79                 attr(JAVA_RUNTIME_VERSION, javaRuntimeVersion()),
  80                 attr(SUPPORTS_EC_KEY, supportsECKey()),
  81                 attr(SUPPORTS_SNI, supportsSNI()),
  82                 attr(SUPPORTS_ALPN, supportsALPN())));
  83     }
  84 
  85     private static String attr(String name, Object value) {
  86         return name + "=" + String.valueOf(value);
  87     }
  88 }