cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1516747 - in /cxf/trunk: core/src/main/java/org/apache/cxf/common/util/ rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/
Date Fri, 23 Aug 2013 08:40:31 GMT
Author: cschneider
Date: Fri Aug 23 08:40:31 2013
New Revision: 1516747

URL: http://svn.apache.org/r1516747
Log:
CXF-5228 Extending ProxyHelper to support combined classloader

Added:
    cxf/trunk/core/src/main/java/org/apache/cxf/common/util/ProxyClassLoader.java
      - copied, changed from r1516383, cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java
Removed:
    cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java
Modified:
    cxf/trunk/core/src/main/java/org/apache/cxf/common/util/ProxyHelper.java
    cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
    cxf/trunk/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java

Copied: cxf/trunk/core/src/main/java/org/apache/cxf/common/util/ProxyClassLoader.java (from
r1516383, cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java)
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/common/util/ProxyClassLoader.java?p2=cxf/trunk/core/src/main/java/org/apache/cxf/common/util/ProxyClassLoader.java&p1=cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java&r1=1516383&r2=1516747&rev=1516747&view=diff
==============================================================================
--- cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java
(original)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/common/util/ProxyClassLoader.java Fri Aug
23 08:40:31 2013
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.cxf.jaxrs.client;
+package org.apache.cxf.common.util;
 
 import java.net.URL;
 import java.util.HashSet;

Modified: cxf/trunk/core/src/main/java/org/apache/cxf/common/util/ProxyHelper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/common/util/ProxyHelper.java?rev=1516747&r1=1516746&r2=1516747&view=diff
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/common/util/ProxyHelper.java (original)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/common/util/ProxyHelper.java Fri Aug 23 08:40:31
2013
@@ -42,10 +42,44 @@ public class ProxyHelper {
     }
     
     protected Object getProxyInternal(ClassLoader loader, Class<?>[] interfaces, InvocationHandler
handler) {
-        return Proxy.newProxyInstance(loader, interfaces, handler);
+        ClassLoader combinedLoader = getClassLoaderForInterfaces(loader, interfaces);
+        return Proxy.newProxyInstance(combinedLoader, interfaces, handler);
     }
-    
-    
+
+    /**
+     * Return a classloader that can see all the given interfaces If the given loader can
see all interfaces
+     * then it is used. If not then a combined classloader of all interface classloaders
is returned.
+     * 
+     * @param loader use supplied class loader
+     * @param interfaces
+     * @return classloader that sees all interfaces
+     */
+    private ClassLoader getClassLoaderForInterfaces(ClassLoader loader, Class<?>[]
interfaces) {
+        if (canSeeAllInterfaces(loader, interfaces)) {
+            return loader;
+        }
+        ProxyClassLoader combined = new ProxyClassLoader();
+        for (Class<?> currentInterface : interfaces) {
+            combined.addLoader(currentInterface.getClassLoader());
+        }
+        return combined;
+    }
+
+    private boolean canSeeAllInterfaces(ClassLoader loader, Class<?>[] interfaces)
{
+        for (Class<?> currentInterface : interfaces) {
+            String ifName = currentInterface.getName();
+            try {
+                Class<?> ifClass = Class.forName(ifName, false, loader);
+                if (ifClass != currentInterface) {
+                    return false;
+                }
+            } catch (ClassNotFoundException e) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     public static Object getProxy(ClassLoader loader, Class<?>[] interfaces, InvocationHandler
handler) {
         return HELPER.getProxyInternal(loader, interfaces, handler);
     }

Modified: cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java?rev=1516747&r1=1516746&r2=1516747&view=diff
==============================================================================
--- cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
(original)
+++ cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
Fri Aug 23 08:40:31 2013
@@ -66,8 +66,9 @@ public class JAXRSClientFactoryBean exte
     }
     
     /**
-     * Sets the custom class loader to be used 
-     * for creating proxies 
+     * Sets the custom class loader to be used for creating proxies.
+     * By default the class loader of the given serviceClass will be used.
+     * 
      * @param loader
      */
     public void setClassLoader(ClassLoader loader) {
@@ -306,25 +307,11 @@ public class JAXRSClientFactoryBean exte
             }
             initClient(proxyImpl, ep, actualState == null);    
             
-            Client actualClient = null;
-            try {
-                ClassLoader theLoader = proxyLoader == null ? cri.getServiceClass().getClassLoader()

-                                                            : proxyLoader;
-                actualClient = (Client)ProxyHelper.getProxy(theLoader,
-                                        new Class[]{cri.getServiceClass(), 
-                                                    Client.class, 
-                                                    InvocationHandlerAware.class}, 
-                                        proxyImpl);
-            } catch (Exception ex) {
-                actualClient = (Client)ProxyHelper.getProxy(Thread.currentThread().getContextClassLoader(),
-                                                    new Class[]{cri.getServiceClass(), 
-                                                                Client.class, 
-                                                                InvocationHandlerAware.class},

-                                     proxyImpl);
-            }
+            ClassLoader theLoader = proxyLoader == null ? cri.getServiceClass().getClassLoader()
: proxyLoader;
+            Class<?>[] ifaces = new Class[]{cri.getServiceClass(), Client.class, InvocationHandlerAware.class};
+            Client actualClient = (Client)ProxyHelper.getProxy(theLoader, ifaces, proxyImpl);
             notifyLifecycleManager(actualClient);
             this.getServiceFactory().sendEvent(FactoryBeanListener.Event.CLIENT_CREATED,
actualClient, ep);
-            
             return actualClient;
         } catch (IllegalArgumentException ex) {
             String message = ex.getLocalizedMessage();
@@ -341,9 +328,8 @@ public class JAXRSClientFactoryBean exte
             throw new RuntimeException(ex);
         }
         
-        
     }
-    
+
     protected ConduitSelector getConduitSelector(Endpoint ep) {
         ConduitSelector cs = getConduitSelector();
         if (cs == null) {

Modified: cxf/trunk/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java?rev=1516747&r1=1516746&r2=1516747&view=diff
==============================================================================
--- cxf/trunk/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java
(original)
+++ cxf/trunk/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java
Fri Aug 23 08:40:31 2013
@@ -27,6 +27,7 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 
 import org.apache.cxf.Bus;
+import org.apache.cxf.common.util.ProxyClassLoader;
 import org.apache.cxf.feature.AbstractFeature;
 import org.apache.cxf.feature.Feature;
 import org.apache.cxf.interceptor.Fault;
@@ -41,7 +42,6 @@ import org.apache.cxf.phase.AbstractPhas
 import org.apache.cxf.phase.Phase;
 import org.apache.cxf.transport.Conduit;
 import org.apache.cxf.transport.http.HTTPConduit;
-
 import org.junit.Assert;
 import org.junit.Test;
 



Mime
View raw message