tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject tomee git commit: app class should be an observer by default and adding TomEEEmbeddedApplicationRunnerInjection to simplify custom injections
Date Tue, 04 Oct 2016 09:44:59 GMT
Repository: tomee
Updated Branches:
  refs/heads/master d5e01c0ee -> 3bb08cbe1


app class should be an observer by default and adding TomEEEmbeddedApplicationRunnerInjection
to simplify custom injections


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

Branch: refs/heads/master
Commit: 3bb08cbe19cb9c1ae3419ac40e9467ed8fcb8eae
Parents: d5e01c0
Author: rmannibucau <rmannibucau@apache.org>
Authored: Tue Oct 4 11:44:52 2016 +0200
Committer: rmannibucau <rmannibucau@apache.org>
Committed: Tue Oct 4 11:44:52 2016 +0200

----------------------------------------------------------------------
 .../TomEEEmbeddedApplicationRunner.java         |  4 ++
 ...TomEEEmbeddedApplicationRunnerInjection.java | 54 ++++++++++++++++++++
 .../embedded/SingleInstanceRunnerTest.java      | 19 ++++++-
 3 files changed, 76 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/3bb08cbe/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 b328d4b..34826c3 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
@@ -26,6 +26,7 @@ import org.apache.openejb.testing.Jars;
 import org.apache.openejb.testing.RandomPort;
 import org.apache.openejb.testing.WebResource;
 import org.apache.tomee.embedded.component.TomEEEmbeddedArgs;
+import org.apache.tomee.embedded.event.TomEEEmbeddedApplicationRunnerInjection;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.inject.OWBInjector;
 import org.apache.xbean.finder.AnnotationFinder;
@@ -255,6 +256,7 @@ public class TomEEEmbeddedApplicationRunner implements AutoCloseable {
             }
         }
 
+        SystemInstance.get().addObserver(app);
         composerInject(app);
 
         final AnnotationFinder appFinder = new AnnotationFinder(new ClassesArchive(appClass));
@@ -423,6 +425,8 @@ public class TomEEEmbeddedApplicationRunner implements AutoCloseable {
         if (superclass != Object.class) {
             composerInject(superclass);
         }
+
+        SystemInstance.get().fireEvent(new TomEEEmbeddedApplicationRunnerInjection(target));
     }
 
     @Retention(RUNTIME)

http://git-wip-us.apache.org/repos/asf/tomee/blob/3bb08cbe/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/event/TomEEEmbeddedApplicationRunnerInjection.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/event/TomEEEmbeddedApplicationRunnerInjection.java
b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/event/TomEEEmbeddedApplicationRunnerInjection.java
new file mode 100644
index 0000000..8be133a
--- /dev/null
+++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/event/TomEEEmbeddedApplicationRunnerInjection.java
@@ -0,0 +1,54 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.tomee.embedded.event;
+
+import org.apache.openejb.observer.Event;
+
+import java.lang.reflect.Field;
+
+@Event
+public class TomEEEmbeddedApplicationRunnerInjection {
+    private final Object instance;
+
+    public TomEEEmbeddedApplicationRunnerInjection(final Object instance) {
+        this.instance = instance;
+    }
+
+    public Object getInstance() {
+        return instance;
+    }
+
+    public <T> TomEEEmbeddedApplicationRunnerInjection inject(final Class<T>
type, final T value) {
+        Class<?> aClass = instance.getClass();
+        while (aClass != null) {
+            for (final Field f : aClass.getDeclaredFields()) {
+                if (f.getType().isAssignableFrom(type)) {
+                    if (!f.isAccessible()) {
+                        f.setAccessible(true);
+                    }
+                    try {
+                        f.set(instance, value);
+                    } catch (final IllegalAccessException e) {
+                        throw new IllegalStateException(e);
+                    }
+                }
+            }
+            aClass = aClass.getSuperclass();
+        }
+        return this;
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/3bb08cbe/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 3c5ae7d..8bb73e7 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
@@ -18,11 +18,13 @@ package org.apache.tomee.embedded;
 
 import org.apache.openejb.assembler.classic.Assembler;
 import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.observer.Observes;
 import org.apache.openejb.testing.Application;
 import org.apache.openejb.testing.Classes;
 import org.apache.openejb.testing.ContainerProperties;
 import org.apache.openejb.testing.RandomPort;
 import org.apache.openejb.testng.PropertiesBuilder;
+import org.apache.tomee.embedded.event.TomEEEmbeddedApplicationRunnerInjection;
 import org.apache.tomee.embedded.junit.TomEEEmbeddedSingleRunner;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -63,12 +65,14 @@ public class SingleInstanceRunnerTest {
         assertEquals(1, app.tasks.size());
         assertEquals(app.task, app.tasks.iterator().next());
         assertEquals(app.task, MyTask.instance);
+        assertNotNull(app.custom);
     }
 
     @Application
     @Classes(context = "app")
     @ContainerProperties(@ContainerProperties.Property(name = "t", value = "set"))
-    @TomEEEmbeddedApplicationRunner.LifecycleTasks(MyTask.class) // can start a ftp/sftp/elasticsearch/mongo/...
server before tomee
+    @TomEEEmbeddedApplicationRunner.LifecycleTasks(MyTask.class)
+    // can start a ftp/sftp/elasticsearch/mongo/... server before tomee
     @TomEEEmbeddedApplicationRunner.Configurers(SetMyProperty.class)
     public static class TheApp {
         @RandomPort("http")
@@ -87,6 +91,19 @@ public class SingleInstanceRunnerTest {
         public Properties add() {
             return new PropertiesBuilder().p("prog", "p").build();
         }
+
+        private Custom custom;
+
+        public void doInject(@Observes final TomEEEmbeddedApplicationRunnerInjection injector)
{
+            injector.inject(Custom.class, new Custom())
+                    .inject(NotHere.class, new NotHere());
+        }
+    }
+
+    public static class NotHere {
+    }
+
+    public static class Custom {
     }
 
     public static class MyTask implements LifecycleTask {


Mime
View raw message