changeset 1460:ebb1725ebfd7

netx/net/sourceforge/jnlp/security/dialogs/CertsInfoPane.java: HexEncoder loaded by reflection to allow smooth jdk8 x 9 transition.
author Jiri Vanek <jvanek@redhat.com>
date Tue, 18 Jul 2017 17:28:21 +0200
parents dc5771ad1a85
children a1070aa6e46f
files ChangeLog netx/net/sourceforge/jnlp/security/dialogs/CertsInfoPane.java
diffstat 2 files changed, 34 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Jul 18 17:27:23 2017 +0200
+++ b/ChangeLog	Tue Jul 18 17:28:21 2017 +0200
@@ -1,3 +1,8 @@
+2017-07-18  Jiri Vanek <jvanek@redhat.com>
+
+	* netx/net/sourceforge/jnlp/security/dialogs/CertsInfoPane.java: HexEncoder loaded by reflection
+	to allow smooth jdk8 x 9 transition.
+
 2017-07-18  Jiri Vanek <jvanek@redhat.com>
 
 	* launcher/launchers.in: added exports for javax.jnlp to hook jdk9 one more times
--- a/netx/net/sourceforge/jnlp/security/dialogs/CertsInfoPane.java	Tue Jul 18 17:27:23 2017 +0200
+++ b/netx/net/sourceforge/jnlp/security/dialogs/CertsInfoPane.java	Tue Jul 18 17:28:21 2017 +0200
@@ -48,10 +48,6 @@
  * It is workaround to allow itw to run on jdk8 and older and also on jdk9 and newer
  */
 
-// jdk8 is using sun.misc.HexDumpEncoder, 
-import sun.misc.*;
-// jdk9 is using sun.security.util.HexDumpEncoder
-import sun.security.util.*;
 import sun.security.x509.*;
 import javax.swing.*;
 import javax.swing.event.*;
@@ -60,6 +56,7 @@
 import java.awt.event.*;
 import java.awt.datatransfer.Clipboard;
 import java.awt.datatransfer.StringSelection;
+import java.lang.reflect.Method;
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.TreeSelectionModel;
 import net.sourceforge.jnlp.security.CertVerifier;
@@ -68,6 +65,7 @@
 import net.sourceforge.jnlp.security.dialogresults.DialogResult;
 import net.sourceforge.jnlp.security.dialogresults.SetValueHandler;
 import net.sourceforge.jnlp.security.dialogresults.Yes;
+import net.sourceforge.jnlp.util.logging.OutputController;
 
 /**
  * Provides the panel for the Certificate Info dialog. This dialog displays data from
@@ -153,9 +151,7 @@
                             c.getNotAfter()).toString();
         String subject = c.getSubjectX500Principal().toString();
 
-        //convert our signature into a nice human-readable form.
-        HexDumpEncoder encoder = new HexDumpEncoder();
-        String signature = encoder.encodeBuffer(c.getSignature());
+        String signature = jdkIndependentHexEncoder(c.getSignature());
 
         String md5Hash = "";
         String sha1Hash = "";
@@ -183,6 +179,32 @@
                                                         };
         return cert;
     }
+    
+     private String jdkIndependentHexEncoder(byte[] signature) {
+        try {
+            return jdkIndependentHexEncoderImpl(signature);
+        } catch (Exception ex) {
+            String s = "Failed to encode signature: " + ex.toString();
+            OutputController.getLogger().log(s);
+            return s;
+        }
+    }
+
+    private String jdkIndependentHexEncoderImpl(byte[] signature) throws Exception {
+        // jdk8 is using sun.misc.HexDumpEncoder, 
+        // jdk9 is using sun.security.util.HexDumpEncoder
+        Class clazz;
+        try {
+            clazz = Class.forName("sun.security.util.HexDumpEncoder");
+        } catch (ClassNotFoundException ex) {
+            OutputController.getLogger().log("Using jdk8's HexDumpEncoder");
+            clazz = Class.forName("sun.misc.HexDumpEncoder");
+        }
+        Object encoder  = clazz.newInstance();
+        Method m = clazz.getDeclaredMethod("encodeBuffer", byte[].class);
+        //convert our signature into a nice human-readable form.
+        return (String) m.invoke(encoder, signature);
+    }
 
     /**
      * Constructs the GUI components of this panel