tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject tomee git commit: ensuring lifecycle tasks instances can be retrieved by the application
Date Tue, 04 Oct 2016 09:30:05 GMT
Repository: tomee
Updated Branches:
  refs/heads/master 78b76fcae -> d5e01c0ee


ensuring lifecycle tasks instances can be retrieved by the application


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/d5e01c0e
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/d5e01c0e
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/d5e01c0e

Branch: refs/heads/master
Commit: d5e01c0ee492f2d5fda30171637e78e47a9753a7
Parents: 78b76fc
Author: rmannibucau <rmannibucau@apache.org>
Authored: Tue Oct 4 11:29:57 2016 +0200
Committer: rmannibucau <rmannibucau@apache.org>
Committed: Tue Oct 4 11:29:57 2016 +0200

----------------------------------------------------------------------
 .../TomEEEmbeddedApplicationRunner.java         | 48 ++++++++++++++++++--
 .../embedded/SingleInstanceRunnerTest.java      | 16 ++++++-
 2 files changed, 59 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/d5e01c0e/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/TomEEEmbeddedApplicationRunner.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/TomEEEmbeddedApplicationRunner.java
b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/TomEEEmbeddedApplicationRunner.java
index 12033d2..b328d4b 100644
--- a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/TomEEEmbeddedApplicationRunner.java
+++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/TomEEEmbeddedApplicationRunner.java
@@ -54,6 +54,7 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.concurrent.CountDownLatch;
 
+import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.TYPE;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 import static org.apache.openejb.loader.JarLocation.jarLocation;
@@ -151,11 +152,14 @@ public class TomEEEmbeddedApplicationRunner implements AutoCloseable
{
             }
         }
 
+        final Collection<org.apache.tomee.embedded.LifecycleTask> lifecycleTasks =
new ArrayList<>();
         final Collection<Closeable> postTasks = new ArrayList<>();
         final LifecycleTasks tasks = appClass.getAnnotation(LifecycleTasks.class);
         if (tasks != null) {
             for (final Class<? extends org.apache.tomee.embedded.LifecycleTask> type
: tasks.value()) {
-                postTasks.add(type.newInstance().beforeContainerStartup());
+                final org.apache.tomee.embedded.LifecycleTask lifecycleTask = type.newInstance();
+                lifecycleTasks.add(lifecycleTask);
+                postTasks.add(lifecycleTask.beforeContainerStartup());
             }
         }
 
@@ -229,6 +233,7 @@ public class TomEEEmbeddedApplicationRunner implements AutoCloseable {
 
         final Container container = new Container(configuration);
         SystemInstance.get().setComponent(TomEEEmbeddedArgs.class, new TomEEEmbeddedArgs(args,
null));
+        SystemInstance.get().setComponent(LifecycleTaskAccessor.class, new LifecycleTaskAccessor(lifecycleTasks));
         container.deploy(new Container.DeploymentRequest(
                 context,
                 // call ClasspathSearcher that lazily since container needs to be started
to not preload logging
@@ -281,9 +286,7 @@ public class TomEEEmbeddedApplicationRunner implements AutoCloseable {
                 }
 
                 try {
-                    if (container != null) {
-                        container.close();
-                    }
+                    container.close();
                 } catch (final Exception e) {
                     e.printStackTrace();
                 }
@@ -406,6 +409,14 @@ public class TomEEEmbeddedApplicationRunner implements AutoCloseable
{
                     f.setAccessible(true);
                 }
                 f.set(target, app);
+            } else if (f.isAnnotationPresent(LifecycleTask.class)) {
+                if (!f.isAccessible()) {
+                    f.setAccessible(true);
+                }
+                final LifecycleTaskAccessor accessor = SystemInstance.get().getComponent(LifecycleTaskAccessor.class);
+                final Class type = f.getType();
+                final Object taskByType = accessor.getTaskByType(type);
+                f.set(target, taskByType);
             }
         }
         final Class<?> superclass = aClass.getSuperclass();
@@ -421,6 +432,11 @@ public class TomEEEmbeddedApplicationRunner implements AutoCloseable
{
     }
 
     @Retention(RUNTIME)
+    @Target(FIELD)
+    public @interface LifecycleTask {
+    }
+
+    @Retention(RUNTIME)
     @Target(TYPE)
     public @interface Configurers {
         Class<? extends Configurer>[] value();
@@ -429,4 +445,28 @@ public class TomEEEmbeddedApplicationRunner implements AutoCloseable
{
     public interface Configurer {
         void configure(Configuration configuration);
     }
+
+    public static class LifecycleTaskAccessor {
+        private final Collection<org.apache.tomee.embedded.LifecycleTask> tasks;
+
+        private LifecycleTaskAccessor(final Collection<org.apache.tomee.embedded.LifecycleTask>
lifecycleTasks) {
+            this.tasks = lifecycleTasks;
+        }
+
+        public Collection<org.apache.tomee.embedded.LifecycleTask> getTasks() {
+            return tasks;
+        }
+
+        public <T> T getTaskByType(final Class<T> type) {
+            for (final org.apache.tomee.embedded.LifecycleTask task : tasks) {
+                if (type == task.getClass()) {
+                    return (T) task;
+                }
+            }
+            if (Collection.class.isAssignableFrom(type)) {
+                return (T) tasks;
+            }
+            return null;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/tomee/blob/d5e01c0e/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/SingleInstanceRunnerTest.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/SingleInstanceRunnerTest.java
b/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/SingleInstanceRunnerTest.java
index cf2d039..3c5ae7d 100644
--- a/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/SingleInstanceRunnerTest.java
+++ b/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/SingleInstanceRunnerTest.java
@@ -30,6 +30,7 @@ import org.junit.runner.RunWith;
 import java.io.Closeable;
 import java.io.IOException;
 import java.net.URL;
+import java.util.Collection;
 import java.util.Properties;
 
 import static org.junit.Assert.assertEquals;
@@ -39,7 +40,6 @@ import static org.junit.Assert.assertTrue;
 
 // just a manual test to check it works, can't be executed with the rest of the suite,
 // we could use a different surefire execution if we want to add it to the default run
-//@Ignore("can't run with by test containers")
 @RunWith(TomEEEmbeddedSingleRunner.class)
 public class SingleInstanceRunnerTest {
     @Application // app can have several injections/helpers
@@ -58,6 +58,11 @@ public class SingleInstanceRunnerTest {
         assertNotEquals(8080, app.port);
         assertTrue(app.base.toExternalForm().endsWith("/app"));
         assertEquals(app.port, port);
+        assertNotNull(app.task);
+        assertNotNull(app.tasks);
+        assertEquals(1, app.tasks.size());
+        assertEquals(app.task, app.tasks.iterator().next());
+        assertEquals(app.task, MyTask.instance);
     }
 
     @Application
@@ -72,6 +77,12 @@ public class SingleInstanceRunnerTest {
         @RandomPort("http")
         private URL base;
 
+        @TomEEEmbeddedApplicationRunner.LifecycleTask
+        private MyTask task;
+
+        @TomEEEmbeddedApplicationRunner.LifecycleTask
+        private Collection<LifecycleTask> tasks;
+
         @org.apache.openejb.testing.Configuration
         public Properties add() {
             return new PropertiesBuilder().p("prog", "p").build();
@@ -79,8 +90,11 @@ public class SingleInstanceRunnerTest {
     }
 
     public static class MyTask implements LifecycleTask {
+        private static MyTask instance;
+
         @Override
         public Closeable beforeContainerStartup() {
+            instance = this;
             System.out.println(">>> start");
             System.setProperty("my.server.port", "128463");
             return new Closeable() {


Mime
View raw message