You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by bu...@apache.org on 2019/05/16 13:07:35 UTC

[cxf] branch master updated: ProxyClassLoaderCache: add isLoggable check

This is an automated email from the ASF dual-hosted git repository.

buhhunyx pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new e23579d  ProxyClassLoaderCache: add isLoggable check
e23579d is described below

commit e23579d37e839b479ab723f8c1bb9d7b6146ed10
Author: amarkevich <am...@talend.com>
AuthorDate: Thu May 16 16:07:03 2019 +0300

    ProxyClassLoaderCache: add isLoggable check
---
 .../cxf/common/util/ProxyClassLoaderCache.java     | 51 ++++++++--------
 .../cxf/common/util/ProxyClassLoaderCacheTest.java | 68 ++++++++--------------
 2 files changed, 49 insertions(+), 70 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/common/util/ProxyClassLoaderCache.java b/core/src/main/java/org/apache/cxf/common/util/ProxyClassLoaderCache.java
index 22e97a4..14a31c2 100644
--- a/core/src/main/java/org/apache/cxf/common/util/ProxyClassLoaderCache.java
+++ b/core/src/main/java/org/apache/cxf/common/util/ProxyClassLoaderCache.java
@@ -28,25 +28,22 @@ import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.logging.LogUtils;
 
 public class ProxyClassLoaderCache {
-    
+
     private static final Logger LOG = LogUtils.getL7dLogger(ProxyClassLoaderCache.class);
     private static final ThreadLocal<ClassLoader> PARENT_CLASSLOADER = new ThreadLocal<>();
     private static final ThreadLocal<Class<?>[]> PROXY_INTERFACES = new ThreadLocal<>();
-    
 
-    
     private final ClassValue<ClassLoader> backend = new ClassValue<ClassLoader>() {
         @Override
         protected ClassLoader computeValue(Class<?> proxyInterface) {
-            LOG.log(Level.FINE, "can't find ProxyClassLoader from ClassValue Cache, "
-                + "will create a new one");
-            LOG.log(Level.FINE, "interface for new created ProxyClassLoader is "
-                + proxyInterface.getName());
-            LOG.log(Level.FINE, "interface's classloader for new created ProxyClassLoader is "
-                + ClassLoaderUtils.getClassLoaderName(proxyInterface));
+            if (LOG.isLoggable(Level.FINE)) {
+                LOG.fine("can't find ProxyClassLoader from ClassValue Cache, will create a new one");
+                LOG.fine("interface for new created ProxyClassLoader is " + proxyInterface.getName());
+                LOG.fine("interface's classloader for new created ProxyClassLoader is "
+                    + ClassLoaderUtils.getClassLoaderName(proxyInterface));
+            }
             return createProxyClassLoader(proxyInterface); // Parameter 'proxyInterface' is not used inside method body
         }
-
     };
 
     private ClassLoader createProxyClassLoader(Class<?> proxyInterface) {
@@ -64,34 +61,38 @@ public class ProxyClassLoaderCache {
         }
         for (Class<?> currentInterface : PROXY_INTERFACES.get()) {
             ret.addLoader(getClassLoader(currentInterface));
-            LOG.log(Level.FINE, "interface for new created ProxyClassLoader is "
-                + currentInterface.getName());
-            LOG.log(Level.FINE, "interface's classloader for new created ProxyClassLoader is "
-                + getClassLoader(currentInterface));
+            if (LOG.isLoggable(Level.FINE)) {
+                LOG.fine("interface for new created ProxyClassLoader is " + currentInterface.getName());
+                LOG.fine("interface's classloader for new created ProxyClassLoader is "
+                    + getClassLoader(currentInterface));
+            }
         }
         return ret;
     }
 
-      
     public ClassLoader getProxyClassLoader(ClassLoader parent, Class<?>[] proxyInterfaces) {
         try {
             PARENT_CLASSLOADER.set(parent);
             PROXY_INTERFACES.set(proxyInterfaces);
             for (Class<?> currentInterface : proxyInterfaces) {
                 String ifName = currentInterface.getName();
-                LOG.log(Level.FINE, "the interface we are checking is " + currentInterface.getName());
-                LOG.log(Level.FINE, "the interface' classloader we are checking is " 
-                    + getClassLoader(currentInterface));
+                if (LOG.isLoggable(Level.FINE)) {
+                    LOG.fine("the interface we are checking is " + ifName);
+                    LOG.fine("the interface's classloader we are checking is " 
+                        + getClassLoader(currentInterface));
+                }
                 if (!ifName.startsWith("org.apache.cxf") && !ifName.startsWith("java")) {
                     // cache and retrieve customer interface
-                    LOG.log(Level.FINE, "the customer interface is " + currentInterface.getName()
-                                        + ". Will try to fetch it from Cache");
+                    if (LOG.isLoggable(Level.FINE)) {
+                        LOG.fine("the customer interface is " + ifName + ". Will try to fetch it from Cache");
+                    }
                     return backend.get(currentInterface);
                 }
             }
-            LOG.log(Level.FINE, "Non of interfaces are customer interface, "
-                + "retrive the last interface as key:" 
-                + proxyInterfaces[proxyInterfaces.length - 1].getName());
+            if (LOG.isLoggable(Level.FINE)) {
+                LOG.fine("Non of interfaces are customer interface, retrive the last interface as key:" 
+                        + proxyInterfaces[proxyInterfaces.length - 1].getName());
+            }
             //the last interface is the variable type
             return backend.get(proxyInterfaces[proxyInterfaces.length - 1]);
         } finally {
@@ -99,11 +100,11 @@ public class ProxyClassLoaderCache {
             PROXY_INTERFACES.remove();
         }
     }
-    
+
     public void removeStaleProxyClassLoader(Class<?> proxyInterface) {
         backend.remove(proxyInterface);
     }
-    
+
     private static ClassLoader getClassLoader(final Class<?> clazz) {
         final SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
diff --git a/core/src/test/java/org/apache/cxf/common/util/ProxyClassLoaderCacheTest.java b/core/src/test/java/org/apache/cxf/common/util/ProxyClassLoaderCacheTest.java
index dd7d974..9fed75e 100644
--- a/core/src/test/java/org/apache/cxf/common/util/ProxyClassLoaderCacheTest.java
+++ b/core/src/test/java/org/apache/cxf/common/util/ProxyClassLoaderCacheTest.java
@@ -29,69 +29,47 @@ import java.util.concurrent.CountDownLatch;
 
 import org.apache.cxf.endpoint.Client;
 
-import org.junit.Assert;
 import org.junit.Test;
 
-public class ProxyClassLoaderCacheTest extends Assert {
-    
-    private ProxyClassLoaderCache cache;
-    
+import static org.junit.Assert.assertSame;
+
+public class ProxyClassLoaderCacheTest {
+
+    private final ProxyClassLoaderCache cache = new ProxyClassLoaderCache();
+
     @Test
     public void testClassLoaderIdentical() throws Exception {
-        cache = new ProxyClassLoaderCache();
         ClassLoader cl1 = cache.getProxyClassLoader(
             this.getClass().getClassLoader(), 
             new Class<?>[]{Closeable.class, Client.class,  HelloWorld.class});
         ClassLoader cl2 = cache.getProxyClassLoader(
             this.getClass().getClassLoader(), 
             new Class<?>[]{Closeable.class, Client.class,  HelloWorld.class});
-        assertTrue(cl1 == cl2);
+        assertSame(cl1, cl2);
     }
-    
+
     @Test
     public void testClassLoaderIdenticalWithMultipleThreads() throws Exception {
-        cache = new ProxyClassLoaderCache();
-        Set<ClassLoader> clSet = Collections.synchronizedSet(new HashSet<>());
-        CountDownLatch countDownLatch = new CountDownLatch(50);
+        final Set<ClassLoader> clSet = Collections.synchronizedSet(new HashSet<>());
+        final CountDownLatch countDownLatch = new CountDownLatch(50);
         for (int i = 0; i < 50; i++) {
-            new Thread(new HelloWorker(clSet, countDownLatch)).start();
+            new Thread(() -> {
+                try {
+                    clSet.add(cache.getProxyClassLoader(
+                                            this.getClass().getClassLoader(), 
+                                            new Class<?>[]{Closeable.class, 
+                                            Client.class,  
+                                            HelloWorld.class}));
+                } finally {
+                    countDownLatch.countDown();
+                }
+            }).start();
         }
         countDownLatch.await(); 
-        assertTrue(clSet.size() == 1);
+        assertSame(1, clSet.size());
     }
-            
+
     interface HelloWorld {
-        void sayHello();
     }
-    
-    class HelloWorker implements Runnable {
-
-        private Set<ClassLoader> classLoaderSet;
-        
-        private CountDownLatch doneSignal;
-        HelloWorker(Set<ClassLoader> classLoaderSet,
-                           CountDownLatch doneSignal) {
-            this.classLoaderSet = classLoaderSet;
-            this.doneSignal = doneSignal;
-        }
-
-        public void run() {
-            
-
-            try {
-                this.classLoaderSet.add(cache.getProxyClassLoader(
-                                        this.getClass().getClassLoader(), 
-                                        new Class<?>[]{Closeable.class, 
-                                        Client.class,  
-                                        HelloWorld.class}));
-                doneSignal.countDown();
-           
-            } catch (RuntimeException ex) {
-                ex.printStackTrace();
-                
-            }
 
-        }
-
-    }
 }