test/sun/security/pkcs11/PKCS11Test.java

Print this page
7191662: JCE providers should be located via ServiceLoader

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -24,11 +24,10 @@
 
 // common infrastructure for SunPKCS11 tests
 
 import java.io.*;
 import java.util.*;
-import java.lang.reflect.*;
 
 import java.security.*;
 import java.security.spec.ECGenParameterSpec;
 import java.security.spec.ECParameterSpec;
 

@@ -68,26 +67,55 @@
 
     // NSS versions of each library.  It is simplier to keep nss_version
     // for quick checking for generic testing than many if-else statements.
     static double softoken3_version = -1;
     static double nss3_version = -1;
+    static Provider sunpkcs11;
 
+    // Goes through ServiceLoader instead Provider.getInstance() since
+    // SunPKCS11 provider is NOT registered for non-Solaris platforms
+    static {
+        ServiceLoader sl = ServiceLoader.load(java.security.Provider.class);
+        Iterator<Provider> iter = sl.iterator();
+        Provider p = null;
+        while (iter.hasNext()) {
+            try {
+                p = iter.next();
+                if (p.getName().startsWith("SunPKCS11")) {
+                    break;
+                };
+            } catch (Exception e) {
+                // ignore and move on to the next one
+            }
+        }
+        sunpkcs11 = p;
+    }
+
+    // Return a SunPKCS11 provider with default config file (Solaris)
+    // or a dummy one which requires further configuration (non-Solaris)
+    static Provider getSunPKCS11() throws NoSuchProviderException {
+        if (sunpkcs11 == null) {
+            throw new NoSuchProviderException("No PKCS11 provider available");
+        }
+        return sunpkcs11;
+    }
+
+     
+    // Return a SunPKCS11 provider using the specified config file
     static Provider getSunPKCS11(String config) throws Exception {
-        Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
-        Constructor cons = clazz.getConstructor(new Class[] {String.class});
-        Object obj = cons.newInstance(new Object[] {config});
-        return (Provider)obj;
+        return getSunPKCS11().configure(config);
     }
 
     public abstract void main(Provider p) throws Exception;
 
     private void premain(Provider p) throws Exception {
         long start = System.currentTimeMillis();
         System.out.println("Running test with provider " + p.getName() + "...");
         main(p);
         long stop = System.currentTimeMillis();
-        System.out.println("Completed test with provider " + p.getName() + " (" + (stop - start) + " ms).");
+        System.out.println("Completed test with provider " + p.getName() +
+            " (" + (stop - start) + " ms).");
     }
 
     public static void main(PKCS11Test test) throws Exception {
         Provider[] oldProviders = Security.getProviders();
         try {

@@ -111,13 +139,15 @@
                     }
                 }
             }
             if (found) {
                 for (Provider p: newProviders) {
+                    System.out.println("PKCS11Test: remove new Provider: " + p.getName());
                     Security.removeProvider(p.getName());
                 }
                 for (Provider p: oldProviders) {
+                    System.out.println("PKCS11Test: add old Provider: " + p.getName());
                     Security.addProvider(p);
                 }
             }
         }
     }

@@ -574,12 +604,11 @@
 
     <T> T[] concat(T[] a, T[] b) {
         if ((b == null) || (b.length == 0)) {
             return a;
         }
-        T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
-        System.arraycopy(a, 0, r, 0, a.length);
+        T[] r = Arrays.copyOf(a, a.length + b.length);
         System.arraycopy(b, 0, r, a.length, b.length);
         return r;
     }
 
     /**