[CXF-6094] Cache the default KeyManagers, only warn once if there is an issue, 
and check for file existence before even attempting so no warning at all if not 
there.

Conflicts:
        core/src/main/java/org/apache/cxf/configuration/jsse/SSLUtils.java


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/77bec2bd
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/77bec2bd
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/77bec2bd

Branch: refs/heads/3.0.x-fixes
Commit: 77bec2bd46fffc01bb7954ee6d0f56f82e7b599f
Parents: f912154
Author: Daniel Kulp <[email protected]>
Authored: Mon Nov 10 15:28:45 2014 -0500
Committer: Daniel Kulp <[email protected]>
Committed: Mon Nov 10 18:45:16 2014 -0500

----------------------------------------------------------------------
 .../apache/cxf/configuration/jsse/SSLUtils.java | 42 +++++++++++++++-----
 1 file changed, 32 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/77bec2bd/core/src/main/java/org/apache/cxf/configuration/jsse/SSLUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/configuration/jsse/SSLUtils.java 
b/core/src/main/java/org/apache/cxf/configuration/jsse/SSLUtils.java
index 0906818..396d53c 100644
--- a/core/src/main/java/org/apache/cxf/configuration/jsse/SSLUtils.java
+++ b/core/src/main/java/org/apache/cxf/configuration/jsse/SSLUtils.java
@@ -22,6 +22,7 @@ package org.apache.cxf.configuration.jsse;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.lang.reflect.Method;
@@ -75,6 +76,8 @@ public final class SSLUtils {
     private static final List<String> DEFAULT_CIPHERSUITE_FILTERS_EXCLUDE =
         Arrays.asList(new String[] {".*_NULL_.*",
                                     ".*_anon_.*"});
+    
+    private static volatile KeyManager[] defaultManagers;
 
     private SSLUtils() {
     }    
@@ -147,23 +150,42 @@ public final class SSLUtils {
     }
 
     public static KeyManager[] getDefaultKeyStoreManagers(Logger log) {
+        if (defaultManagers == null) {
+            loadDefaultKeyManagers(log);
+        }
+        if (defaultManagers.length == 0) {
+            return null;
+        }
+        return defaultManagers;
+    }
+    private static synchronized void loadDefaultKeyManagers(Logger log) {
+        if (defaultManagers != null) {
+            return;
+        }
+            
         String location = getKeystore(null, log);
         String keyStorePassword = getKeystorePassword(null, log);
         String keyPassword = getKeyPassword(null, log);
         FileInputStream fis = null;
         
         try {
-            KeyManagerFactory kmf = 
-                
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());  
-            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
-            
-            fis = new FileInputStream(location);
-            ks.load(fis, (keyStorePassword != null) ? 
keyStorePassword.toCharArray() : null);
-            kmf.init(ks, (keyPassword != null) ? keyPassword.toCharArray() : 
null);
-            return kmf.getKeyManagers();
+            File file = new File(location);
+            if (file.exists()) {
+                KeyManagerFactory kmf = 
+                    
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());  
+                KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
+                
+                fis = new FileInputStream(file);
+                ks.load(fis, (keyStorePassword != null) ? 
keyStorePassword.toCharArray() : null);
+                kmf.init(ks, (keyPassword != null) ? keyPassword.toCharArray() 
: null);
+                defaultManagers = kmf.getKeyManagers();
+            } else {
+                log.log(Level.FINER, "No default keystore {0}", location);
+                defaultManagers = new KeyManager[0];
+            }
         } catch (Exception e) {
-            log.warning("Default key managers cannot be initialized: " + 
e.getMessage());
-            return null;
+            log.log(Level.WARNING, "Default key managers cannot be 
initialized: " + e.getMessage(), e);
+            defaultManagers = new KeyManager[0];
         } finally {
             if (fis != null) {
                 try {

Reply via email to