openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1781048 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java test/java/org/apache/webbeans/test/managed/ProxyFactoryTest.java
Date Tue, 31 Jan 2017 10:14:37 GMT
Author: rmannibucau
Date: Tue Jan 31 10:14:37 2017
New Revision: 1781048

URL: http://svn.apache.org/viewvc?rev=1781048&view=rev
Log:
OWB-1170 ensure we don't fail cause of LinkageError during proxy creation

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/managed/ProxyFactoryTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java?rev=1781048&r1=1781047&r2=1781048&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
Tue Jan 31 10:14:37 2017
@@ -353,8 +353,22 @@ public abstract class AbstractProxyFacto
         {
             Class<T> definedClass = (Class<T>) defineClassMethod.invoke(classLoader,
proxyName, proxyBytes, 0, proxyBytes.length);
 
-            Class<T> loadedClass = (Class<T>) Class.forName(definedClass.getName(),
true, classLoader);
-            return loadedClass;
+            return (Class<T>) Class.forName(definedClass.getName(), true, classLoader);
+        }
+        catch (InvocationTargetException le) // if concurrent calls are done then ensure
to just reload the created one
+        {
+            if (LinkageError.class.isInstance(le.getCause()))
+            {
+                try
+                {
+                    return (Class<T>) Class.forName(proxyName.replace('/', '.'), true,
classLoader);
+                }
+                catch (ClassNotFoundException e)
+                {
+                    // default error handling
+                }
+            }
+            throw new ProxyGenerationException(le.getCause());
         }
         catch (Throwable e)
         {

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/managed/ProxyFactoryTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/managed/ProxyFactoryTest.java?rev=1781048&r1=1781047&r2=1781048&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/managed/ProxyFactoryTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/managed/ProxyFactoryTest.java
Tue Jan 31 10:14:37 2017
@@ -22,21 +22,28 @@ package org.apache.webbeans.test.managed
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
 
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.spi.Bean;
 
 import junit.framework.Assert;
 
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.proxy.NormalScopeProxyFactory;
 import org.apache.webbeans.test.AbstractUnitTest;
 import org.apache.webbeans.test.managed.multipleinterfaces.MyEntityServiceImpl;
 import org.junit.Test;
 
-public class ProxyFactoryTest extends AbstractUnitTest {
+public class ProxyFactoryTest extends AbstractUnitTest
+{
 
     @SuppressWarnings("unchecked")
     @Test
-    public void testProxyFactoryWithMultipleInterfaces() {
+    public void testProxyFactoryWithMultipleInterfaces()
+    {
         Collection<String> beanXmls = new ArrayList<String>();
 
         Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
@@ -57,4 +64,60 @@ public class ProxyFactoryTest extends Ab
         
         shutDownContainer();
     }
+
+    @Test
+    public void threadSafe() throws Throwable
+    {
+        Collection<String> beanXmls = new ArrayList<>();
+
+        Collection<Class<?>> beanClasses = new ArrayList<>();
+        beanClasses.add(MyEntityServiceImpl.class);
+
+        startContainer(beanClasses, beanXmls);
+
+        final AtomicReference<Throwable> failed = new AtomicReference<>();
+        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
+        final NormalScopeProxyFactory normalScopeProxyFactory = WebBeansContext.currentInstance().getNormalScopeProxyFactory();
+        final Thread[] concurrentRequests = new Thread[16];
+        final CountDownLatch starter = new CountDownLatch(1);
+        for (int i = 0; i < concurrentRequests.length; i++)
+        {
+            final int idx = i;
+            concurrentRequests[i] = new Thread()
+            {
+                {
+                    setName(getClass().getName() + ".threadSafe-#" + (idx + 1));
+                }
+
+                @Override
+                public void run()
+                {
+                    try
+                    {
+                        starter.await();
+                        normalScopeProxyFactory.createProxyClass(loader, MyEntityServiceImpl.class);
+                    }
+                    catch (final Throwable t)
+                    {
+                        failed.compareAndSet(null, t);
+                    }
+                }
+            };
+        }
+
+        for (final Thread t : concurrentRequests)
+        {
+            t.start();
+        }
+        starter.countDown();
+        for (final Thread t : concurrentRequests)
+        {
+            t.join(TimeUnit.MINUTES.toMillis(1));
+        }
+        if (failed.get() != null) {
+            throw failed.get();
+        }
+
+        shutDownContainer();
+    }
 }



Mime
View raw message