cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1336216 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/client/ test/java/org/apache/cxf/jaxrs/client/
Date Wed, 09 May 2012 14:11:16 GMT
Author: sergeyb
Date: Wed May  9 14:11:15 2012
New Revision: 1336216

URL: http://svn.apache.org/viewvc?rev=1336216&view=rev
Log:
[CXF-4290] Adding an option to specify a custom loader for creating proxies

Added:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java
  (with props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java?rev=1336216&r1=1336215&r2=1336216&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
Wed May  9 14:11:15 2012
@@ -78,22 +78,28 @@ public class ClientProxyImpl extends Abs
     private static final String SLASH = "/";
     
     private ClassResourceInfo cri;
+    private ClassLoader proxyLoader;
     private boolean inheritHeaders;
     private boolean isRoot;
     private Map<String, Object> valuesMap = Collections.emptyMap();
     
-    public ClientProxyImpl(URI baseURI, ClassResourceInfo cri, boolean isRoot, 
-                           boolean inheritHeaders, Object... varValues) {
-        super(baseURI);
-        this.cri = cri;
-        this.isRoot = isRoot;
-        this.inheritHeaders = inheritHeaders;
-        initValuesMap(varValues);
+    public ClientProxyImpl(URI baseURI,
+                           ClassLoader loader,
+                           ClassResourceInfo cri, 
+                           boolean isRoot, 
+                           boolean inheritHeaders, 
+                           Object... varValues) {
+        this(new LocalClientState(baseURI), loader, cri, isRoot, inheritHeaders, varValues);
     }
     
-    public ClientProxyImpl(ClientState initialState, ClassResourceInfo cri, boolean isRoot,

-                           boolean inheritHeaders, Object... varValues) {
+    public ClientProxyImpl(ClientState initialState,
+                           ClassLoader loader,
+                           ClassResourceInfo cri, 
+                           boolean isRoot, 
+                           boolean inheritHeaders, 
+                           Object... varValues) {
         super(initialState);
+        this.proxyLoader = loader;
         this.cri = cri;
         this.isRoot = isRoot;
         this.inheritHeaders = inheritHeaders;
@@ -174,9 +180,10 @@ public class ClientProxyImpl extends Abs
             
             ClientState newState = getState().newState(uri, subHeaders, 
                  getTemplateParametersMap(ori.getURITemplate(), pathParams));
-            ClientProxyImpl proxyImpl = new ClientProxyImpl(newState, subCri, false, inheritHeaders);
+            ClientProxyImpl proxyImpl = 
+                new ClientProxyImpl(newState, proxyLoader, subCri, false, inheritHeaders);
             proxyImpl.setConfiguration(getConfiguration());
-            return JAXRSClientFactory.create(m.getReturnType(), proxyImpl);
+            return JAXRSClientFactory.createProxy(m.getReturnType(), proxyLoader, proxyImpl);
         } 
         
         headers.putAll(paramHeaders);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java?rev=1336216&r1=1336215&r2=1336216&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
Wed May  9 14:11:15 2012
@@ -49,6 +49,19 @@ public final class JAXRSClientFactory {
     }
     
     /**
+     * Creates a proxy using a custom class loader
+     * @param baseAddress baseAddress
+     * @param loader class loader
+     * @param cls resource class, if not interface then a CGLIB proxy will be created
+     * @return typed proxy
+     */
+    public static <T> T create(String baseAddress, Class<T> cls, ClassLoader
loader) {
+        JAXRSClientFactoryBean bean = getBean(baseAddress, cls, null);
+        bean.setClassLoader(loader);
+        return bean.create(cls);
+    }
+    
+    /**
      * Creates a proxy
      * @param baseURI baseURI
      * @param cls resource class, if not interface then a CGLIB proxy will be created
@@ -282,9 +295,11 @@ public final class JAXRSClientFactory {
         return proxy;
     }
     
-    static <T> T create(Class<T> cls, InvocationHandler handler) {
+    static <T> T createProxy(Class<T> cls, ClassLoader loader, InvocationHandler
handler) {
         
-        return cls.cast(ProxyHelper.getProxy(cls.getClassLoader(), new Class[]{cls, Client.class},
handler));
+        return cls.cast(ProxyHelper.getProxy(loader == null ? cls.getClassLoader() : loader,

+                                             new Class[]{cls, Client.class}, 
+                                             handler));
     }
     
     private static JAXRSClientFactoryBean getBean(String baseAddress, Class<?> cls,
String configLocation) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java?rev=1336216&r1=1336215&r2=1336216&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBean.java
Wed May  9 14:11:15 2012
@@ -52,6 +52,7 @@ public class JAXRSClientFactoryBean exte
     private boolean threadSafe;
     private long timeToKeepState;
     private Class<?> serviceClass;
+    private ClassLoader proxyLoader;
     
     public JAXRSClientFactoryBean() {
         this(new JAXRSServiceFactoryBean());
@@ -64,6 +65,15 @@ public class JAXRSClientFactoryBean exte
     }
     
     /**
+     * Sets the custom class loader to be used 
+     * for creating proxies 
+     * @param loader
+     */
+    public void setClassLoader(ClassLoader loader) {
+        proxyLoader = loader;
+    }
+    
+    /**
      * Indicates if a single proxy or WebClient instance can be reused 
      * by multiple threads.
      *   
@@ -276,22 +286,28 @@ public class JAXRSClientFactoryBean exte
             ClientState actualState = getActualState();
             if (actualState == null) {
                 proxyImpl = 
-                    new ClientProxyImpl(URI.create(getAddress()), cri, isRoot, inheritHeaders,
varValues);
+                    new ClientProxyImpl(URI.create(getAddress()), proxyLoader, cri, isRoot,

+                                        inheritHeaders, varValues);
             } else {
                 proxyImpl = 
-                    new ClientProxyImpl(actualState, cri, isRoot, inheritHeaders, varValues);
+                    new ClientProxyImpl(actualState, proxyLoader, cri, isRoot, 
+                                        inheritHeaders, varValues);
             }
             initClient(proxyImpl, ep, actualState == null);    
             
             Client actualClient = null;
             try {
-                actualClient = (Client)ProxyHelper.getProxy(cri.getServiceClass().getClassLoader(),
-                                        new Class[]{cri.getServiceClass(), Client.class,

+                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,

+                                                    new Class[]{cri.getServiceClass(), 
+                                                                Client.class, 
                                                                 InvocationHandlerAware.class},

                                      proxyImpl);
             }

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java?rev=1336216&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java
Wed May  9 14:11:15 2012
@@ -0,0 +1,57 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.client;
+
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Utility class loader that can be used to create proxies in cases where
+ * the the JAX-RS client classes are not visible to the loader of the
+ * service class.    
+ */
+public class ProxyClassLoader extends ClassLoader {
+    private Set<ClassLoader> loaders = new HashSet<ClassLoader>();
+
+    public void addLoader(ClassLoader loader) {
+        loaders.add(loader);
+    }
+
+    public Class<?> findClass(String name) throws ClassNotFoundException {
+        for (ClassLoader loader : loaders) {
+            try {
+                return loader.loadClass(name);
+            } catch (ClassNotFoundException cnfe) {
+                // Try next
+            }
+        }
+        throw new ClassNotFoundException(name);
+    }
+    
+    public URL findResource(String name) {
+        for (ClassLoader loader : loaders) {
+            URL url = loader.getResource(name);
+            if (url != null) {
+                return url;
+            }
+        }
+        return null;
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ProxyClassLoader.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java?rev=1336216&r1=1336215&r2=1336216&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/JAXRSClientFactoryBeanTest.java
Wed May  9 14:11:15 2012
@@ -52,6 +52,22 @@ public class JAXRSClientFactoryBeanTest 
     }
     
     @Test
+    public void testCreateClientCustomLoader() throws Exception {
+        ProxyClassLoader loader = new ProxyClassLoader();
+        loader.addLoader(BookStore.class.getClassLoader());
+        
+        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
+        bean.setAddress("http://bar");
+        bean.setResourceClass(BookStore.class);
+        bean.setClassLoader(loader);
+        BookStore client = (BookStore)bean.createWithValues(BookStore.class);
+        assertNotNull(client);
+        // tricky to test the loader has been used correctly with Maven 
+        // given that the system loader loads all the test classes  
+        
+    }
+    
+    @Test
     public void testCreateClientWithUserResource() throws Exception {
         JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
         bean.setAddress("http://bar");
@@ -198,4 +214,6 @@ public class JAXRSClientFactoryBeanTest 
 
     }
     
-}
+
+    
+}
\ No newline at end of file



Mime
View raw message