Author: rmannibucau
Date: Mon Sep 10 15:55:29 2012
New Revision: 1382955

URL: http://svn.apache.org/viewvc?rev=1382955&view=rev
Log:
OPENEJB-1898 support annotation finder and classesarchive as module type for 
application composer

Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/reflection/Reflections.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java?rev=1382955&r1=1382954&r2=1382955&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
 Mon Sep 10 15:55:29 2012
@@ -52,6 +52,8 @@ import org.apache.openejb.util.Join;
 import org.apache.openejb.util.ServiceManagerProxy;
 import org.apache.webbeans.inject.OWBInjector;
 import org.apache.xbean.finder.AnnotationFinder;
+import org.apache.xbean.finder.IAnnotationFinder;
+import org.apache.xbean.finder.archive.Archive;
 import org.apache.xbean.finder.archive.ClassesArchive;
 import org.junit.rules.MethodRule;
 import org.junit.runners.BlockJUnit4ClassRunner;
@@ -75,6 +77,15 @@ import static org.apache.openejb.config.
 public class ApplicationComposer extends BlockJUnit4ClassRunner {
 
     public static final String OPENEJB_APPLICATION_COMPOSER_CONTEXT = 
"openejb.application.composer.context";
+    private static final Class[] MODULE_TYPES = { IAnnotationFinder.class, 
ClassesArchive.class,
+            AppModule.class, WebModule.class, EjbModule.class,
+            Application.class,
+            EjbJar.class, EnterpriseBean.class,
+            Persistence.class, PersistenceUnit.class,
+            Connector.class, Beans.class,
+            Class[].class
+    };
+
     private final TestClass testClass;
     private ServiceManagerProxy serviceManager = null;
 
@@ -108,7 +119,7 @@ public class ApplicationComposer extends
 
         int appModules = 0;
         int modules = 0;
-        Class[] moduleTypes = { AppModule.class, WebModule.class, 
EjbModule.class, EjbJar.class, EnterpriseBean.class, Persistence.class, 
PersistenceUnit.class, Connector.class, Beans.class, Application.class, 
Class[].class};
+
         for (FrameworkMethod method : 
testClass.getAnnotatedMethods(Module.class)) {
 
             modules++;
@@ -119,8 +130,8 @@ public class ApplicationComposer extends
 
                 appModules++;
 
-            } else if (!isValidModuleType(type, moduleTypes)) {
-                final String gripe = "@Module method must return " + 
Join.join(" or ", moduleTypes).replaceAll("(class|interface) ", "");
+            } else if (!isValidModuleType(type, MODULE_TYPES)) {
+                final String gripe = "@Module method must return " + 
Join.join(" or ", MODULE_TYPES).replaceAll("(class|interface) ", "");
                 errors.add(new Exception(gripe));
             }
         }
@@ -188,6 +199,16 @@ public class ApplicationComposer extends
                 appModule.getEjbModules().add(new EjbModule(ejbJar, 
openejbJar));
             }
 
+            // call the mock injector before module method to be able to use 
mocked classes
+            FallbackPropertyInjector mockInjector = null;
+            final List<FrameworkMethod> mockInjectors = 
testClass.getAnnotatedMethods(MockInjector.class);
+            for (FrameworkMethod method : mockInjectors) { // max == 1 so no 
need to break
+                final Object o = method.invokeExplosively(testInstance);
+                if (o instanceof FallbackPropertyInjector) {
+                    mockInjector = (FallbackPropertyInjector) o;
+                }
+            }
+
             Application application = null;
 
             // Invoke the @Module producer methods to build out the AppModule
@@ -259,6 +280,18 @@ public class ApplicationComposer extends
                     ejbModule.setFinder(new AnnotationFinder(new 
ClassesArchive(bean)).link());
                     ejbModule.setBeans(new Beans());
                     appModule.getEjbModules().add(ejbModule);
+                } else if (obj instanceof IAnnotationFinder) {
+
+                    final EjbModule ejbModule = new EjbModule(new 
EjbJar(method.getName()));
+                    ejbModule.setFinder((IAnnotationFinder) obj);
+                    ejbModule.setBeans(new Beans());
+                    appModule.getEjbModules().add(ejbModule);
+                } else if (obj instanceof ClassesArchive) {
+
+                    final EjbModule ejbModule = new EjbModule(new 
EjbJar(method.getName()));
+                    ejbModule.setFinder(new AnnotationFinder((Archive) 
obj).link());
+                    ejbModule.setBeans(new Beans());
+                    appModule.getEjbModules().add(ejbModule);
                 } else if (obj instanceof AppModule) {
 
                     // we can probably go further here
@@ -312,12 +345,8 @@ public class ApplicationComposer extends
 
             SystemInstance.init(configuration);
 
-            final List<FrameworkMethod> mockInjectors = 
testClass.getAnnotatedMethods(MockInjector.class);
-            for (FrameworkMethod method : mockInjectors) { // max == 1 so no 
need to break
-                final Object o = method.invokeExplosively(testInstance);
-                if (o instanceof FallbackPropertyInjector) {
-                    
SystemInstance.get().setComponent(FallbackPropertyInjector.class, 
(FallbackPropertyInjector) o);
-                }
+            if (mockInjector instanceof FallbackPropertyInjector) {
+                
SystemInstance.get().setComponent(FallbackPropertyInjector.class, mockInjector);
             }
 
             try {

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/reflection/Reflections.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/reflection/Reflections.java?rev=1382955&r1=1382954&r2=1382955&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/reflection/Reflections.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/reflection/Reflections.java
 Mon Sep 10 15:55:29 2012
@@ -25,28 +25,50 @@ public final class Reflections {
     }
 
     public static Object invokeByReflection(final Object obj, final String 
mtdName, final Class<?>[] paramTypes, final Object[] args) {
-        Method mtd;
-        try {
-            mtd = obj.getClass().getDeclaredMethod(mtdName, paramTypes);
-            return mtd.invoke(obj, args);
-        } catch (Exception e) {
-            throw new IllegalArgumentException(e);
+        Class<?> clazz = obj.getClass();
+        while (clazz != null) {
+            boolean acc = true;
+            Method mtd = null;
+            try {
+                mtd = clazz.getDeclaredMethod(mtdName, paramTypes);
+                acc = mtd.isAccessible();
+                mtd.setAccessible(true);
+                return mtd.invoke(obj, args);
+            } catch (NoSuchMethodException nsme) {
+                // no-op
+            } catch (Exception e) {
+                throw new IllegalArgumentException(e);
+            } finally {
+                if (mtd != null) {
+                    mtd.setAccessible(acc);
+                }
+            }
+
+            clazz = clazz.getSuperclass();
         }
+        throw new IllegalArgumentException(new 
NoSuchMethodException(obj.getClass().getName() + " ." + mtdName));
     }
 
     public static void set(final Object instance, final String field, final 
Object value) {
-        Field f;
-        try {
-            f = instance.getClass().getDeclaredField(field);
-            boolean acc = f.isAccessible();
-            f.setAccessible(true);
+        Class<?> clazz = instance.getClass();
+        while (clazz != null) {
             try {
-                f.set(instance, value);
-            } finally {
-                f.setAccessible(acc);
+                final Field f = clazz.getDeclaredField(field);
+                boolean acc = f.isAccessible();
+                f.setAccessible(true);
+                try {
+                    f.set(instance, value);
+                    return;
+                } finally {
+                    f.setAccessible(acc);
+                }
+            } catch (NoSuchFieldException nsfe) {
+                // no-op
+            } catch (Exception e) {
+                throw new IllegalArgumentException(e);
             }
-        } catch (Exception e) {
-            throw new IllegalArgumentException(e);
+
+            clazz = clazz.getSuperclass();
         }
     }
 }


Reply via email to