openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1814562 - in /openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave: internal/ internal/ClassLoaderLock.java junit/MeecrowaveRule.java junit/MeecrowaveRuleBase.java testing/MonoBase.java
Date Wed, 08 Nov 2017 10:04:14 GMT
Author: rmannibucau
Date: Wed Nov  8 10:04:14 2017
New Revision: 1814562

URL: http://svn.apache.org/viewvc?rev=1814562&view=rev
Log:
trying to not break flat classpath assumption in our junit integration

Added:
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java
Modified:
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRule.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/testing/MonoBase.java

Added: 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=1814562&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java
(added)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java
Wed Nov  8 10:04:14 2017
@@ -0,0 +1,30 @@
+/*
+ * 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.meecrowave.internal;
+
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+public final class ClassLoaderLock {
+    public static final Lock LOCK = new ReentrantLock();
+
+    private ClassLoaderLock() {
+        // no-op
+    }
+}

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRule.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRule.java?rev=1814562&r1=1814561&r2=1814562&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRule.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRule.java
Wed Nov  8 10:04:14 2017
@@ -18,17 +18,12 @@
  */
 package org.apache.meecrowave.junit;
 
-import java.net.URL;
-import java.net.URLClassLoader;
-
 import org.apache.meecrowave.Meecrowave;
 
 public class MeecrowaveRule extends MeecrowaveRuleBase<MeecrowaveRule> {
     private final Meecrowave.Builder configuration;
     private final String context;
 
-    private ClassLoader meecrowaveCL;
-
     public MeecrowaveRule() {
         this(new Meecrowave.Builder().randomHttpPort(), "");
     }
@@ -47,17 +42,4 @@ public class MeecrowaveRule extends Meec
     protected AutoCloseable onStart() {
         return new Meecrowave(configuration).bake(context);
     }
-
-    @Override
-    protected ClassLoader getClassLoader() {
-        if (meecrowaveCL == null) {
-            ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
-            if (currentCL == null) {
-                this.getClass().getClassLoader();
-            }
-
-            meecrowaveCL = new URLClassLoader(new URL[0], currentCL);
-        }
-        return meecrowaveCL;
-    }
 }

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=1814562&r1=1814561&r2=1814562&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 10:04:14 2017
@@ -19,6 +19,10 @@
 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.spi.SingletonService;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
@@ -37,29 +41,43 @@ import static java.util.stream.Collector
 public abstract class MeecrowaveRuleBase<T extends MeecrowaveRuleBase> implements TestRule
{
     private final Collection<Object> toInject = new ArrayList<>();
     private final AtomicBoolean started = new AtomicBoolean(false);
+    private ClassLoader meecrowaveCL;
 
     @Override
     public Statement apply(final Statement base, final Description description) {
         return new Statement() {
             @Override
             public void evaluate() throws Throwable {
-                ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
-                ClassLoader newCl = getClassLoader();
-                if (newCl != null) {
-                    Thread.currentThread().setContextClassLoader(newCl);
-                }
-                try (final AutoCloseable closeable = onStart()) {
-                    started.set(true);
-                    final Collection<CreationalContext<?>> contexts = toInject.stream().map(MeecrowaveRuleBase::doInject).collect(toList());
-                    try {
-                        base.evaluate();
+                final Thread thread = Thread.currentThread();
+                ClassLoader oldCL = thread.getContextClassLoader();
+                ClassLoaderLock.LOCK.lock();
+                boolean unlocked = false;
+                try {
+                    ClassLoader newCl = getClassLoader();
+                    if (newCl != null) {
+                        thread.setContextClassLoader(newCl);
+                    }
+                    try (final AutoCloseable closeable = onStart()) {
+                        ClassLoaderLock.LOCK.unlock();
+                        unlocked = true;
+
+                        started.set(true);
+                        final Collection<CreationalContext<?>> contexts = toInject.stream()
+                                                                                  .map(MeecrowaveRuleBase::doInject)
+                                                                                  .collect(toList());
+                        try {
+                            base.evaluate();
+                        } finally {
+                            contexts.forEach(CreationalContext::release);
+                            started.set(false);
+                        }
                     } finally {
-                        contexts.forEach(CreationalContext::release);
-                        started.set(false);
+                        thread.setContextClassLoader(oldCL);
+                    }
+                } finally {
+                    if (!unlocked) {
+                        ClassLoaderLock.LOCK.unlock();
                     }
-                }
-                finally {
-                    Thread.currentThread().setContextClassLoader(oldCL);
                 }
             }
         };
@@ -88,6 +106,22 @@ public abstract class MeecrowaveRuleBase
     protected abstract AutoCloseable onStart();
 
     protected ClassLoader getClassLoader() {
-        return null;
+        if (meecrowaveCL == null) {
+            ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
+            if (currentCL == null) {
+                currentCL = this.getClass().getClassLoader();
+            }
+
+            final SingletonService<WebBeansContext> singletonInstance = WebBeansFinder.getSingletonService();
+            synchronized (singletonInstance) {
+                try {
+                    singletonInstance.get(currentCL);
+                    meecrowaveCL = currentCL;
+                } catch (final IllegalArgumentException iae) {
+                    meecrowaveCL = new ClassLoader(currentCL) {};
+                }
+            }
+        }
+        return meecrowaveCL;
     }
 }

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=1814562&r1=1814561&r2=1814562&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 10:04:14 2017
@@ -19,10 +19,12 @@
 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.net.URL;
-import java.net.URLClassLoader;
 import java.util.Comparator;
 import java.util.ServiceLoader;
 import java.util.concurrent.atomic.AtomicReference;
@@ -37,46 +39,79 @@ public class MonoBase {
         final Meecrowave.Builder configuration = new Meecrowave.Builder().randomHttpPort().noShutdownHook(/*the
rule does*/);
         CONFIGURATION.compareAndSet(null, configuration);
 
-        ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
-        monoClassLoader = new URLClassLoader(new URL[0], originalCL);
-
-        final Meecrowave meecrowave = new Meecrowave(CONFIGURATION.get());
-        if (CONTAINER.compareAndSet(null, meecrowave)) {
-            final Configuration runnerConfig = StreamSupport.stream(ServiceLoader.load(Configuration.class).spliterator(),
false)
-                    .sorted(Comparator.comparingInt(Configuration::order))
-                    .findFirst()
-                    .orElseGet(() -> new Configuration() {
-                    });
-
-            runnerConfig.beforeStarts();
-
-            final File war = runnerConfig.application();
-            if (war == null) {
-                meecrowave.bake(runnerConfig.context());
-            } else {
-                meecrowave.deployWebapp(runnerConfig.context(), runnerConfig.application());
+        boolean unlocked = false;
+        ClassLoaderLock.LOCK.lock();
+        try {
+            ClassLoader originalCL = Thread.currentThread()
+                                           .getContextClassLoader();
+            if (originalCL == null) {
+                originalCL = this.getClass()
+                                 .getClassLoader();
             }
-
-            runnerConfig.afterStarts();
-
-            Runtime.getRuntime().addShutdownHook(new Thread() {
-                {
-                    setName("Meecrowave-mono-rue-stopping");
+            final SingletonService<WebBeansContext> singletonInstance = WebBeansFinder.getSingletonService();
+            synchronized (singletonInstance) {
+                try {
+                    singletonInstance.get(originalCL);
+                } catch (final IllegalArgumentException iae) {
+                    monoClassLoader = new ClassLoader(originalCL) {};
                 }
+            }
 
-                @Override
-                public void run() {
-                    try {
-                        runnerConfig.beforeStops();
-                    } finally {
-                        try {
-                            meecrowave.close();
-                        } finally {
-                            runnerConfig.afterStops();
-                        }
+            final Meecrowave meecrowave = new Meecrowave(CONFIGURATION.get());
+            if (CONTAINER.compareAndSet(null, meecrowave)) {
+                final Configuration runnerConfig = StreamSupport.stream(ServiceLoader.load(Configuration.class)
+                                                                                     .spliterator(),
false)
+                                                                .sorted(Comparator.comparingInt(Configuration::order))
+                                                                .findFirst()
+                                                                .orElseGet(() -> new Configuration()
{});
+
+                runnerConfig.beforeStarts();
+
+                final File war = runnerConfig.application();
+                final Thread thread = Thread.currentThread();
+                if (monoClassLoader != null) {
+                    thread.setContextClassLoader(monoClassLoader);
+                }
+                try {
+                    if (war == null) {
+                        meecrowave.bake(runnerConfig.context());
+                    } else {
+                        meecrowave.deployWebapp(runnerConfig.context(), runnerConfig.application());
+                    }
+                } finally {
+                    if (monoClassLoader != null) {
+                        thread.setContextClassLoader(originalCL);
                     }
                 }
-            });
+                ClassLoaderLock.LOCK.unlock();
+                unlocked = true;
+
+                runnerConfig.afterStarts();
+
+                Runtime.getRuntime()
+                       .addShutdownHook(new Thread() {
+                           {
+                               setName("Meecrowave-mono-rule-stopping");
+                           }
+
+                           @Override
+                           public void run() {
+                               try {
+                                   runnerConfig.beforeStops();
+                               } finally {
+                                   try {
+                                       meecrowave.close();
+                                   } finally {
+                                       runnerConfig.afterStops();
+                                   }
+                               }
+                           }
+                       });
+            }
+        } finally {
+            if (!unlocked) {
+                ClassLoaderLock.LOCK.unlock();
+            }
         }
         return getConfiguration();
     }



Mime
View raw message