openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1814572 - in /openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave: internal/ClassLoaderLock.java junit/MeecrowaveRuleBase.java junit/MonoMeecrowave.java testing/MonoBase.java
Date Wed, 08 Nov 2017 12:53:46 GMT
Author: rmannibucau
Date: Wed Nov  8 12:53:46 2017
New Revision: 1814572

URL: http://svn.apache.org/viewvc?rev=1814572&view=rev
Log:
avoid locking issues and share the classloader finding logic between mono and normal rules

Modified:
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRuleBase.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java?rev=1814572&r1=1814571&r2=1814572&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java
Wed Nov  8 12:53:46 2017
@@ -21,9 +21,42 @@ package org.apache.meecrowave.internal;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.config.WebBeansFinder;
+import org.apache.webbeans.corespi.DefaultSingletonService;
+import org.apache.webbeans.spi.SingletonService;
+
 public final class ClassLoaderLock {
     public static final Lock LOCK = new ReentrantLock();
 
+    public static ClassLoader getUsableContainerLoader() {
+        ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
+        if (currentCL == null) {
+            currentCL = ClassLoaderLock.class.getClassLoader();
+        }
+
+        final SingletonService<WebBeansContext> singletonService = WebBeansFinder.getSingletonService();
+        synchronized (singletonService) {
+            try {
+                if (singletonService instanceof DefaultSingletonService) {
+                    synchronized (singletonService) {
+                        ((DefaultSingletonService) singletonService).register(currentCL,
null);
+                        // all fine, it seems we do not have an OWB container for this ClassLoader
yet
+
+                        // let's reset it then ;
+                        singletonService.clear(currentCL);
+                    }
+                    return currentCL;
+                }
+            }
+            catch (IllegalArgumentException iae) {
+                // whoops there is already an OWB container registered for this very ClassLoader
+            }
+
+            return new ClassLoader(currentCL) {};
+        }
+    }
+
     private ClassLoaderLock() {
         // no-op
     }

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRuleBase.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRuleBase.java?rev=1814572&r1=1814571&r2=1814572&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRuleBase.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRuleBase.java
Wed Nov  8 12:53:46 2017
@@ -20,10 +20,6 @@ package org.apache.meecrowave.junit;
 
 import org.apache.meecrowave.Meecrowave;
 import org.apache.meecrowave.internal.ClassLoaderLock;
-import org.apache.webbeans.config.WebBeansContext;
-import org.apache.webbeans.config.WebBeansFinder;
-import org.apache.webbeans.corespi.DefaultSingletonService;
-import org.apache.webbeans.spi.SingletonService;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
@@ -51,15 +47,15 @@ public abstract class MeecrowaveRuleBase
             public void evaluate() throws Throwable {
                 final Thread thread = Thread.currentThread();
                 ClassLoader oldCL = thread.getContextClassLoader();
-                ClassLoaderLock.LOCK.lock();
                 boolean unlocked = false;
+                doLockContext();
                 try {
                     ClassLoader newCl = getClassLoader();
                     if (newCl != null) {
                         thread.setContextClassLoader(newCl);
                     }
                     try (final AutoCloseable closeable = onStart()) {
-                        ClassLoaderLock.LOCK.unlock();
+                        doUnlockContext(unlocked);
                         unlocked = true;
 
                         started.set(true);
@@ -76,14 +72,22 @@ public abstract class MeecrowaveRuleBase
                         thread.setContextClassLoader(oldCL);
                     }
                 } finally {
-                    if (!unlocked) {
-                        ClassLoaderLock.LOCK.unlock();
-                    }
+                    doUnlockContext(unlocked);
                 }
             }
         };
     }
 
+    protected void doUnlockContext(final boolean unlocked) {
+        if (!unlocked) {
+            ClassLoaderLock.LOCK.unlock();
+        }
+    }
+
+    protected void doLockContext() {
+        ClassLoaderLock.LOCK.lock();
+    }
+
     private static CreationalContext<Object> doInject(final Object instance) {
         final BeanManager bm = CDI.current().getBeanManager();
         final AnnotatedType<?> annotatedType = bm.createAnnotatedType(instance.getClass());
@@ -108,33 +112,7 @@ public abstract class MeecrowaveRuleBase
 
     protected ClassLoader getClassLoader() {
         if (meecrowaveCL == null) {
-            ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
-            if (currentCL == null) {
-                currentCL = this.getClass().getClassLoader();
-            }
-
-            final SingletonService<WebBeansContext> singletonService = WebBeansFinder.getSingletonService();
-            synchronized (singletonService) {
-                try {
-                    if (singletonService instanceof DefaultSingletonService) {
-                        synchronized (singletonService) {
-                            ((DefaultSingletonService) singletonService).register(currentCL,
null);
-                            // all fine, it seems we do not have an OWB container for this
ClassLoader yet
-
-                            // let's reset it then ;
-                            ((DefaultSingletonService) singletonService).clear(currentCL);
-                        }
-                        meecrowaveCL = currentCL;
-                    }
-                }
-                catch (IllegalArgumentException iae) {
-                    // whoops there is already an OWB container registered for this very
ClassLoader
-                }
-
-                if (meecrowaveCL == null) {
-                    meecrowaveCL = new ClassLoader(currentCL) {};
-                }
-            }
+            meecrowaveCL = ClassLoaderLock.getUsableContainerLoader();
         }
         return meecrowaveCL;
     }

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java?rev=1814572&r1=1814571&r2=1814572&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java
Wed Nov  8 12:53:46 2017
@@ -86,5 +86,15 @@ public class MonoMeecrowave {
             BASE.startIfNeeded();
             return NOOP_CLOSEABLE;
         }
+
+        @Override
+        protected void doUnlockContext(final boolean unlocked) {
+            // no-op: done in the monobase
+        }
+
+        @Override
+        protected void doLockContext() {
+            // no-op: done in the monobase
+        }
     }
 }

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java?rev=1814572&r1=1814571&r2=1814572&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
Wed Nov  8 12:53:46 2017
@@ -20,9 +20,6 @@ package org.apache.meecrowave.testing;
 
 import org.apache.meecrowave.Meecrowave;
 import org.apache.meecrowave.internal.ClassLoaderLock;
-import org.apache.webbeans.config.WebBeansContext;
-import org.apache.webbeans.config.WebBeansFinder;
-import org.apache.webbeans.spi.SingletonService;
 
 import java.io.File;
 import java.util.Comparator;
@@ -33,7 +30,6 @@ import java.util.stream.StreamSupport;
 public class MonoBase {
     private static final AtomicReference<Meecrowave> CONTAINER = new AtomicReference<>();
     private static final AtomicReference<Meecrowave.Builder> CONFIGURATION = new AtomicReference<>();
-    private static ClassLoader monoClassLoader = null;
 
     public Meecrowave.Builder doBoot() {
         final Meecrowave.Builder configuration = new Meecrowave.Builder().randomHttpPort().noShutdownHook(/*the
rule does*/);
@@ -42,20 +38,9 @@ public class MonoBase {
         boolean unlocked = false;
         ClassLoaderLock.LOCK.lock();
         try {
-            ClassLoader originalCL = Thread.currentThread()
-                                           .getContextClassLoader();
-            if (originalCL == null) {
-                originalCL = this.getClass()
-                                 .getClassLoader();
-            }
-            final SingletonService<WebBeansContext> singletonInstance = WebBeansFinder.getSingletonService();
-            synchronized (singletonInstance) {
-                try {
-                    singletonInstance.get(originalCL);
-                } catch (final IllegalArgumentException iae) {
-                    monoClassLoader = new ClassLoader(originalCL) {};
-                }
-            }
+            final ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
+            ClassLoaderLock.LOCK.lock();
+            final ClassLoader containerLoader = ClassLoaderLock.getUsableContainerLoader();
 
             final Meecrowave meecrowave = new Meecrowave(CONFIGURATION.get());
             if (CONTAINER.compareAndSet(null, meecrowave)) {
@@ -69,8 +54,8 @@ public class MonoBase {
 
                 final File war = runnerConfig.application();
                 final Thread thread = Thread.currentThread();
-                if (monoClassLoader != null) {
-                    thread.setContextClassLoader(monoClassLoader);
+                if (containerLoader != originalCL) {
+                    thread.setContextClassLoader(containerLoader);
                 }
                 try {
                     if (war == null) {
@@ -79,7 +64,7 @@ public class MonoBase {
                         meecrowave.deployWebapp(runnerConfig.context(), runnerConfig.application());
                     }
                 } finally {
-                    if (monoClassLoader != null) {
+                    if (containerLoader != originalCL) {
                         thread.setContextClassLoader(originalCL);
                     }
                 }



Mime
View raw message