cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject [1/3] cxf git commit: Some refactoring of how property files are loaded
Date Thu, 19 Feb 2015 16:00:38 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 559c32c37 -> 030c22d80


Some refactoring of how property files are loaded


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

Branch: refs/heads/3.0.x-fixes
Commit: a33670388cd4314a75bde41d98336446ea073d4e
Parents: 559c32c
Author: Colm O hEigeartaigh <coheigea@apache.org>
Authored: Thu Feb 19 15:53:28 2015 +0000
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Thu Feb 19 15:56:57 2015 +0000

----------------------------------------------------------------------
 .../ws/security/trust/AbstractSTSClient.java    | 31 ++-----
 .../wss4j/AbstractWSS4JInterceptor.java         | 45 ++--------
 .../wss4j/AbstractWSS4JStaxInterceptor.java     | 90 +++-----------------
 .../wss4j/PolicyBasedWSS4JInInterceptor.java    | 56 ++----------
 .../ws/security/wss4j/SamlTokenInterceptor.java | 49 +----------
 .../cxf/ws/security/wss4j/WSS4JUtils.java       | 82 ++++++++++++++++++
 .../policyhandlers/AbstractBindingBuilder.java  | 49 ++---------
 7 files changed, 125 insertions(+), 277 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/a3367038/rt/ws/security/src/main/java/org/apache/cxf/ws/security/trust/AbstractSTSClient.java
----------------------------------------------------------------------
diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/trust/AbstractSTSClient.java
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/trust/AbstractSTSClient.java
index 4b4630e..0e757c6 100755
--- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/trust/AbstractSTSClient.java
+++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/trust/AbstractSTSClient.java
@@ -20,7 +20,6 @@
 package org.apache.cxf.ws.security.trust;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.StringReader;
 import java.net.URL;
 import java.security.PublicKey;
@@ -104,6 +103,7 @@ import org.apache.cxf.ws.security.SecurityConstants;
 import org.apache.cxf.ws.security.tokenstore.SecurityToken;
 import org.apache.cxf.ws.security.trust.claims.ClaimsCallback;
 import org.apache.cxf.ws.security.trust.delegation.DelegationCallback;
+import org.apache.cxf.ws.security.wss4j.WSS4JUtils;
 import org.apache.cxf.wsdl.WSDLManager;
 import org.apache.cxf.wsdl11.WSDLServiceFactory;
 import org.apache.neethi.All;
@@ -1591,30 +1591,11 @@ public abstract class AbstractSTSClient implements Configurable, InterceptorProv
         }
 
         Object o = getProperty(SecurityConstants.STS_TOKEN_PROPERTIES + (decrypt ? ".decrypt"
: ""));
-        Properties properties = null;
-        if (o instanceof Properties) {
-            properties = (Properties)o;
-        } else if (o instanceof String) {
-            ResourceManager rm = bus.getExtension(ResourceManager.class);
-            URL url = rm.resolveResource((String)o, URL.class);
-            if (url == null) {
-                url = ClassLoaderUtils.getResource((String)o, this.getClass());
-            }
-            if (url != null) {
-                properties = new Properties();
-                InputStream ins = url.openStream();
-                properties.load(ins);
-                ins.close();
-            } else {
-                throw new Fault("Could not find properties file " + (String)o, LOG);
-            }
-        } else if (o instanceof URL) {
-            properties = new Properties();
-            InputStream ins = ((URL)o).openStream();
-            properties.load(ins);
-            ins.close();
-        }
-
+        
+        ResourceManager manager = bus.getExtension(ResourceManager.class);
+        URL propsURL = WSS4JUtils.getPropertiesFileURL(o, manager, this.getClass());
+        Properties properties = WSS4JUtils.getProps(o, propsURL);
+        
         if (properties != null) {
             return CryptoFactory.getInstance(properties);
         }

http://git-wip-us.apache.org/repos/asf/cxf/blob/a3367038/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/AbstractWSS4JInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/AbstractWSS4JInterceptor.java
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/AbstractWSS4JInterceptor.java
index 10d731c..e50f6ee 100644
--- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/AbstractWSS4JInterceptor.java
+++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/AbstractWSS4JInterceptor.java
@@ -18,13 +18,10 @@
  */
 package org.apache.cxf.ws.security.wss4j;
 
-import java.io.InputStream;
 import java.net.URI;
-import java.net.URL;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Map;
-import java.util.Properties;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -32,17 +29,13 @@ import javax.xml.namespace.QName;
 
 import org.apache.cxf.binding.soap.SoapMessage;
 import org.apache.cxf.binding.soap.interceptor.SoapInterceptor;
-import org.apache.cxf.common.classloader.ClassLoaderUtils;
-import org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder;
 import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageUtils;
 import org.apache.cxf.phase.PhaseInterceptor;
-import org.apache.cxf.resource.ResourceManager;
 import org.apache.cxf.ws.security.SecurityConstants;
 import org.apache.wss4j.common.ConfigurationConstants;
 import org.apache.wss4j.common.crypto.Crypto;
-import org.apache.wss4j.common.crypto.CryptoFactory;
 import org.apache.wss4j.common.crypto.PasswordEncryptor;
 import org.apache.wss4j.common.ext.WSSecurityException;
 import org.apache.wss4j.dom.WSConstants;
@@ -207,37 +200,13 @@ public abstract class AbstractWSS4JInterceptor extends WSHandler implements
Soap
         String propFilename, 
         RequestData reqData
     ) throws WSSecurityException {
-        ClassLoaderHolder orig = null;
-        try {
-            try {
-                URL url = ClassLoaderUtils.getResource(propFilename, this.getClass());
-                if (url == null) {
-                    ResourceManager manager = ((Message)reqData.getMsgContext()).getExchange()
-                            .getBus().getExtension(ResourceManager.class);
-                    ClassLoader loader = manager.resolveResource("", ClassLoader.class);
-                    if (loader != null) {
-                        orig = ClassLoaderUtils.setThreadContextClassloader(loader);
-                    }
-                    url = manager.resolveResource(propFilename, URL.class);
-                }
-                if (url != null) {
-                    Properties props = new Properties();
-                    InputStream in = url.openStream(); 
-                    props.load(in);
-                    in.close();
-                    return CryptoFactory.getInstance(props,
-                                                     this.getClassLoader(reqData.getMsgContext()),
-                                                     getPasswordEncryptor(reqData));
-                }
-            } catch (Exception e) {
-                //ignore
-            } 
-            return CryptoFactory.getInstance(propFilename, this.getClassLoader(reqData.getMsgContext()));
-        } finally {
-            if (orig != null) {
-                orig.reset();
-            }
-        }
+        Message message = (Message)reqData.getMsgContext();
+        ClassLoader classLoader = this.getClassLoader(reqData.getMsgContext());
+        PasswordEncryptor passwordEncryptor = getPasswordEncryptor(reqData);
+        return 
+            WSS4JUtils.loadCryptoFromPropertiesFile(
+                message, propFilename, this.getClass(), classLoader, passwordEncryptor
+            );
     }
     
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/a3367038/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/AbstractWSS4JStaxInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/AbstractWSS4JStaxInterceptor.java
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/AbstractWSS4JStaxInterceptor.java
index bc54fed..07954f5 100644
--- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/AbstractWSS4JStaxInterceptor.java
+++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/AbstractWSS4JStaxInterceptor.java
@@ -19,7 +19,6 @@
 package org.apache.cxf.ws.security.wss4j;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.URI;
 import java.net.URL;
 import java.util.ArrayList;
@@ -40,11 +39,9 @@ import javax.security.auth.callback.CallbackHandler;
 import javax.security.auth.callback.UnsupportedCallbackException;
 import javax.xml.namespace.QName;
 
-import org.apache.cxf.Bus;
 import org.apache.cxf.binding.soap.SoapMessage;
 import org.apache.cxf.binding.soap.interceptor.SoapInterceptor;
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
-import org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.interceptor.Fault;
@@ -387,36 +384,11 @@ public abstract class AbstractWSS4JStaxInterceptor implements SoapInterceptor,
     protected Crypto loadCryptoFromPropertiesFile(
         SoapMessage soapMessage, String propFilename, WSSSecurityProperties securityProperties
     ) throws WSSecurityException {
-        ClassLoaderHolder orig = null;
-        try {
-            try {
-                URL url = ClassLoaderUtils.getResource(propFilename, this.getClass());
-                if (url == null) {
-                    ResourceManager manager = soapMessage.getExchange()
-                            .getBus().getExtension(ResourceManager.class);
-                    ClassLoader loader = manager.resolveResource("", ClassLoader.class);
-                    if (loader != null) {
-                        orig = ClassLoaderUtils.setThreadContextClassloader(loader);
-                    }
-                    url = manager.resolveResource(propFilename, URL.class);
-                }
-                if (url != null) {
-                    Properties props = new Properties();
-                    InputStream in = url.openStream(); 
-                    props.load(in);
-                    in.close();
-                    return CryptoFactory.getInstance(props, getClassLoader(), 
-                                                     getPasswordEncryptor(soapMessage, securityProperties));
-                }
-            } catch (Exception e) {
-                //ignore
-            } 
-            return CryptoFactory.getInstance(propFilename, getClassLoader());
-        } finally {
-            if (orig != null) {
-                orig.reset();
-            }
-        }
+        PasswordEncryptor passwordEncryptor = getPasswordEncryptor(soapMessage, securityProperties);
+        return 
+            WSS4JUtils.loadCryptoFromPropertiesFile(
+                soapMessage, propFilename, this.getClass(), getClassLoader(), passwordEncryptor
+            );
     }
     
     protected PasswordEncryptor getPasswordEncryptor(
@@ -458,46 +430,6 @@ public abstract class AbstractWSS4JStaxInterceptor implements SoapInterceptor,
         return null;
     }
     
-    private static Properties getProps(Object o, URL propsURL, SoapMessage message) {
-        Properties properties = null;
-        if (o instanceof Properties) {
-            properties = (Properties)o;
-        } else if (propsURL != null) {
-            try {
-                properties = new Properties();
-                InputStream ins = propsURL.openStream();
-                properties.load(ins);
-                ins.close();
-            } catch (IOException e) {
-                properties = null;
-            }
-        }
-        
-        return properties;
-    }
-    
-    private URL getPropertiesFileURL(Object o, SoapMessage message) {
-        if (o instanceof String) {
-            URL url = null;
-            ResourceManager rm = message.getExchange().get(Bus.class).getExtension(ResourceManager.class);
-            url = rm.resolveResource((String)o, URL.class);
-            try {
-                if (url == null) {
-                    url = ClassLoaderUtils.getResource((String)o, AbstractWSS4JInterceptor.class);
-                }
-                if (url == null) {
-                    url = new URL((String)o);
-                }
-                return url;
-            } catch (IOException e) {
-                // Do nothing
-            }
-        } else if (o instanceof URL) {
-            return (URL)o;        
-        }
-        return null;
-    }
-    
     protected Crypto getEncryptionCrypto(
             Object e, SoapMessage message, WSSSecurityProperties securityProperties
     ) throws WSSecurityException {
@@ -506,8 +438,10 @@ public abstract class AbstractWSS4JStaxInterceptor implements SoapInterceptor,
         } else if (e instanceof Crypto) {
             return (Crypto)e;
         } else {
-            URL propsURL = getPropertiesFileURL(e, message);
-            Properties props = getProps(e, propsURL, message);
+            ResourceManager manager = 
+                message.getExchange().getBus().getExtension(ResourceManager.class);
+            URL propsURL = WSS4JUtils.getPropertiesFileURL(e, manager, this.getClass());
+            Properties props = WSS4JUtils.getProps(e, propsURL);
             if (props == null) {
                 LOG.fine("Cannot find Crypto Encryption properties: " + e);
                 Exception ex = new Exception("Cannot find Crypto Encryption properties: "
+ e);
@@ -534,8 +468,10 @@ public abstract class AbstractWSS4JStaxInterceptor implements SoapInterceptor,
         } else if (s instanceof Crypto) {
             return (Crypto)s;
         } else {
-            URL propsURL = getPropertiesFileURL(s, message);
-            Properties props = getProps(s, propsURL, message);
+            ResourceManager manager = 
+                message.getExchange().getBus().getExtension(ResourceManager.class);
+            URL propsURL = WSS4JUtils.getPropertiesFileURL(s, manager, this.getClass());
+            Properties props = WSS4JUtils.getProps(s, propsURL);
             if (props == null) {
                 LOG.fine("Cannot find Crypto Signature properties: " + s);
                 Exception ex = new Exception("Cannot find Crypto Signature properties: "
+ s);

http://git-wip-us.apache.org/repos/asf/cxf/blob/a3367038/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/PolicyBasedWSS4JInInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/PolicyBasedWSS4JInInterceptor.java
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/PolicyBasedWSS4JInInterceptor.java
index fd636c3..47629d3 100644
--- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/PolicyBasedWSS4JInInterceptor.java
+++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/PolicyBasedWSS4JInInterceptor.java
@@ -19,8 +19,6 @@
 
 package org.apache.cxf.ws.security.wss4j;
 
-import java.io.IOException;
-import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -44,9 +42,7 @@ import javax.xml.xpath.XPathFactory;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
-import org.apache.cxf.Bus;
 import org.apache.cxf.binding.soap.SoapMessage;
-import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.helpers.CastUtils;
@@ -134,46 +130,6 @@ public class PolicyBasedWSS4JInInterceptor extends WSS4JInInterceptor
{
         }
     }
     
-    private static Properties getProps(Object o, URL propsURL, SoapMessage message) {
-        Properties properties = null;
-        if (o instanceof Properties) {
-            properties = (Properties)o;
-        } else if (propsURL != null) {
-            try {
-                properties = new Properties();
-                InputStream ins = propsURL.openStream();
-                properties.load(ins);
-                ins.close();
-            } catch (IOException e) {
-                properties = null;
-            }
-        }
-        
-        return properties;
-    }
-    
-    private URL getPropertiesFileURL(Object o, SoapMessage message) {
-        if (o instanceof String) {
-            URL url = null;
-            ResourceManager rm = message.getExchange().get(Bus.class).getExtension(ResourceManager.class);
-            url = rm.resolveResource((String)o, URL.class);
-            try {
-                if (url == null) {
-                    url = ClassLoaderUtils.getResource((String)o, AbstractWSS4JInterceptor.class);
-                }
-                if (url == null) {
-                    url = new URL((String)o);
-                }
-                return url;
-            } catch (IOException e) {
-                // Do nothing
-            }
-        } else if (o instanceof URL) {
-            return (URL)o;        
-        }
-        return null;
-    }
-    
     private void handleWSS11(AssertionInfoMap aim, SoapMessage message) {
         if (isRequestor(message)) {
             message.put(WSHandlerConstants.ENABLE_SIGNATURE_CONFIRMATION, "false");
@@ -471,8 +427,10 @@ public class PolicyBasedWSS4JInInterceptor extends WSS4JInInterceptor
{
         if (e instanceof Crypto) {
             encrCrypto = (Crypto)e;
         } else if (e != null) {
-            URL propsURL = getPropertiesFileURL(e, message);
-            Properties props = getProps(e, propsURL, message);
+            ResourceManager manager = 
+                message.getExchange().getBus().getExtension(ResourceManager.class);
+            URL propsURL = WSS4JUtils.getPropertiesFileURL(e, manager, this.getClass());
+            Properties props = WSS4JUtils.getProps(e, propsURL);
             if (props == null) {
                 LOG.fine("Cannot find Crypto Encryption properties: " + e);
                 Exception ex = new Exception("Cannot find Crypto Encryption properties: "
+ e);
@@ -518,8 +476,10 @@ public class PolicyBasedWSS4JInInterceptor extends WSS4JInInterceptor
{
         if (s instanceof Crypto) {
             signCrypto = (Crypto)s;
         } else if (s != null) {
-            URL propsURL = getPropertiesFileURL(s, message);
-            Properties props = getProps(s, propsURL, message);
+            ResourceManager manager = 
+                message.getExchange().getBus().getExtension(ResourceManager.class);
+            URL propsURL = WSS4JUtils.getPropertiesFileURL(s, manager, this.getClass());
+            Properties props = WSS4JUtils.getProps(s, propsURL);
             if (props == null) {
                 LOG.fine("Cannot find Crypto Signature properties: " + s);
                 Exception ex = new Exception("Cannot find Crypto Signature properties: "
+ s);

http://git-wip-us.apache.org/repos/asf/cxf/blob/a3367038/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java
index bcbc7eb..0c39dbf 100644
--- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java
+++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java
@@ -19,8 +19,6 @@
 
 package org.apache.cxf.ws.security.wss4j;
 
-import java.io.IOException;
-import java.io.InputStream;
 import java.net.URL;
 import java.security.Principal;
 import java.security.cert.Certificate;
@@ -33,7 +31,6 @@ import javax.security.auth.callback.CallbackHandler;
 import javax.xml.namespace.QName;
 
 import org.w3c.dom.Element;
-import org.apache.cxf.Bus;
 import org.apache.cxf.binding.soap.SoapMessage;
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.util.StringUtils;
@@ -331,48 +328,10 @@ public class SamlTokenInterceptor extends AbstractTokenInterceptor {
             return null;
         }
 
-        Properties properties = null;
-        if (o instanceof Properties) {
-            properties = (Properties)o;
-        } else if (o instanceof String) {
-            ResourceManager rm = message.getExchange().get(Bus.class).getExtension(ResourceManager.class);
-            URL url = rm.resolveResource((String)o, URL.class);
-            try {
-                if (url == null) {
-                    url = ClassLoaderUtils.getResource((String)o, this.getClass());
-                }
-                if (url == null) {
-                    try {
-                        url = new URL((String)o);
-                    } catch (Exception ex) {
-                        //ignore
-                    }
-                }
-                if (url != null) {
-                    InputStream ins = url.openStream();
-                    properties = new Properties();
-                    properties.load(ins);
-                    ins.close();
-                } else if (samlToken != null) {
-                    policyNotAsserted(samlToken, "Could not find properties file " + o, message);
-                }
-            } catch (IOException e) {
-                if (samlToken != null) {
-                    policyNotAsserted(samlToken, e.getMessage(), message);
-                }
-            }
-        } else if (o instanceof URL) {
-            properties = new Properties();
-            try {
-                InputStream ins = ((URL)o).openStream();
-                properties.load(ins);
-                ins.close();
-            } catch (IOException e) {
-                if (samlToken != null) {
-                    policyNotAsserted(samlToken, e.getMessage(), message);
-                }
-            }            
-        }
+        ResourceManager manager = 
+            message.getExchange().getBus().getExtension(ResourceManager.class);
+        URL propsURL = WSS4JUtils.getPropertiesFileURL(o, manager, this.getClass());
+        Properties properties = WSS4JUtils.getProps(o, propsURL);
 
         if (properties != null) {
             crypto = CryptoFactory.getInstance(properties);

http://git-wip-us.apache.org/repos/asf/cxf/blob/a3367038/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/WSS4JUtils.java
----------------------------------------------------------------------
diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/WSS4JUtils.java
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/WSS4JUtils.java
index b392e0d..0e891e6 100644
--- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/WSS4JUtils.java
+++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/WSS4JUtils.java
@@ -19,9 +19,11 @@
 package org.apache.cxf.ws.security.wss4j;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
 import java.security.Key;
 import java.util.Date;
+import java.util.Properties;
 
 import javax.crypto.SecretKey;
 
@@ -30,6 +32,7 @@ import org.apache.cxf.binding.soap.SoapFault;
 import org.apache.cxf.binding.soap.SoapMessage;
 import org.apache.cxf.binding.soap.SoapVersion;
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
+import org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder;
 import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageUtils;
@@ -42,6 +45,9 @@ import org.apache.cxf.ws.security.tokenstore.TokenStore;
 import org.apache.cxf.ws.security.tokenstore.TokenStoreFactory;
 import org.apache.wss4j.common.cache.ReplayCache;
 import org.apache.wss4j.common.cache.ReplayCacheFactory;
+import org.apache.wss4j.common.crypto.Crypto;
+import org.apache.wss4j.common.crypto.CryptoFactory;
+import org.apache.wss4j.common.crypto.PasswordEncryptor;
 import org.apache.wss4j.common.ext.WSSecurityException;
 import org.apache.wss4j.stax.ext.WSSConstants;
 import org.apache.wss4j.stax.securityToken.WSSecurityTokenConstants;
@@ -268,4 +274,80 @@ public final class WSS4JUtils {
         }
         return fault;
     }
+    
+    public static Properties getProps(Object o, URL propsURL) {
+        Properties properties = null;
+        if (o instanceof Properties) {
+            properties = (Properties)o;
+        } else if (propsURL != null) {
+            try {
+                properties = new Properties();
+                try (InputStream ins = propsURL.openStream()) {
+                    properties.load(ins);
+                }
+            } catch (IOException e) {
+                properties = null;
+            }
+        }
+        
+        return properties;
+    }
+    
+    public static URL getPropertiesFileURL(
+        Object o, ResourceManager manager, Class<?> callingClass
+    ) {
+        if (o instanceof String) {
+            ClassLoaderHolder orig = null;
+            try {
+                URL url = ClassLoaderUtils.getResource((String)o, callingClass);
+                if (url == null) {
+                    ClassLoader loader = manager.resolveResource((String)o, ClassLoader.class);
+                    if (loader != null) {
+                        orig = ClassLoaderUtils.setThreadContextClassloader(loader);
+                    }
+                    url = manager.resolveResource((String)o, URL.class);
+                }
+                if (url == null) {
+                    try {
+                        url = new URL((String)o);
+                    } catch (IOException e) {
+                        // Do nothing
+                    }
+                }
+                return url;
+            } finally {
+                if (orig != null) {
+                    orig.reset();
+                }
+            }
+        } else if (o instanceof URL) {
+            return (URL)o;        
+        }
+        return null;
+    }
+    
+    public static Crypto loadCryptoFromPropertiesFile(
+        Message message,
+        String propFilename, 
+        Class<?> callingClass,
+        ClassLoader classLoader,
+        PasswordEncryptor passwordEncryptor
+    ) throws WSSecurityException {
+        try {
+            ResourceManager manager = 
+                message.getExchange().getBus().getExtension(ResourceManager.class);
+            URL url = getPropertiesFileURL(propFilename, manager, callingClass);
+            if (url != null) {
+                Properties props = new Properties();
+                try (InputStream in = url.openStream()) { 
+                    props.load(in);
+                }
+                return CryptoFactory.getInstance(props, classLoader, passwordEncryptor);
+            }
+        } catch (Exception e) {
+            //ignore
+        } 
+        return CryptoFactory.getInstance(propFilename, classLoader);
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/a3367038/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/policyhandlers/AbstractBindingBuilder.java
----------------------------------------------------------------------
diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/policyhandlers/AbstractBindingBuilder.java
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/policyhandlers/AbstractBindingBuilder.java
index bb484a5..bb8f9bf 100644
--- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/policyhandlers/AbstractBindingBuilder.java
+++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/policyhandlers/AbstractBindingBuilder.java
@@ -19,8 +19,6 @@
 
 package org.apache.cxf.ws.security.wss4j.policyhandlers;
 
-import java.io.IOException;
-import java.io.InputStream;
 import java.net.URL;
 import java.security.cert.X509Certificate;
 import java.util.ArrayList;
@@ -1473,48 +1471,11 @@ public abstract class AbstractBindingBuilder extends AbstractCommonBindingHandle
         if (crypto != null) {
             return crypto;
         }
-        Properties properties = null;
-        if (o instanceof Properties) {
-            properties = (Properties)o;
-        } else if (o instanceof String) {
-            ResourceManager rm = message.getExchange().get(Bus.class).getExtension(ResourceManager.class);
-            URL url = rm.resolveResource((String)o, URL.class);
-            try {
-                if (url == null) {
-                    url = ClassLoaderUtils.getResource((String)o, this.getClass());
-                }
-                if (url == null) {
-                    try {
-                        url = new URL((String)o);
-                    } catch (Exception ex) {
-                        //ignore
-                    }
-                }
-                if (url != null) {
-                    InputStream ins = url.openStream();
-                    properties = new Properties();
-                    properties.load(ins);
-                    ins.close();
-                } else if (wrapper != null) {
-                    policyNotAsserted(wrapper, "Could not find properties file " + o);
-                }
-            } catch (IOException e) {
-                if (wrapper != null) {
-                    policyNotAsserted(wrapper, e);
-                }
-            }
-        } else if (o instanceof URL) {
-            properties = new Properties();
-            try {
-                InputStream ins = ((URL)o).openStream();
-                properties.load(ins);
-                ins.close();
-            } catch (IOException e) {
-                if (wrapper != null) {
-                    policyNotAsserted(wrapper, e);
-                }
-            }            
-        }
+        
+        ResourceManager manager = 
+            message.getExchange().get(Bus.class).getExtension(ResourceManager.class);
+        URL propsURL = WSS4JUtils.getPropertiesFileURL(o, manager, this.getClass());
+        Properties properties = WSS4JUtils.getProps(o, propsURL);
         
         if (properties != null) {
             crypto = CryptoFactory.getInstance(properties, 


Mime
View raw message