Search in sources :

Example 1 with TomEEEmbeddedApplicationRunnerInjection

use of org.apache.tomee.embedded.event.TomEEEmbeddedApplicationRunnerInjection in project tomee by apache.

the class TomEEEmbeddedApplicationRunner method composerInject.

public void composerInject(final Object target) throws IllegalAccessException {
    WebBeansContext webBeansContext = null;
    try {
        webBeansContext = WebBeansContext.currentInstance();
    } catch (final IllegalStateException ise) {
    // no-op
    }
    if (webBeansContext != null) {
        OWBInjector.inject(webBeansContext.getBeanManagerImpl(), target, null);
    }
    Class<?> aClass = target.getClass();
    while (aClass != null && aClass != Object.class) {
        for (final Field f : aClass.getDeclaredFields()) {
            final RandomPort randomPort = f.getAnnotation(RandomPort.class);
            if (randomPort != null) {
                for (final Field field : app.getClass().getDeclaredFields()) {
                    final RandomPort appPort = field.getAnnotation(RandomPort.class);
                    if (field.getType() == f.getType() && appPort != null && appPort.value().equals(randomPort.value())) {
                        if (!field.isAccessible()) {
                            field.setAccessible(true);
                        }
                        if (!f.isAccessible()) {
                            f.setAccessible(true);
                        }
                        final Object value = field.get(app);
                        f.set(target, value);
                        break;
                    }
                }
            } else if (f.isAnnotationPresent(Application.class)) {
                if (!f.isAccessible()) {
                    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);
            } else if (f.isAnnotationPresent(Args.class)) {
                if (String[].class != f.getType()) {
                    throw new IllegalArgumentException("@Args can only be used for String[] field, not on " + f.getType());
                }
                if (!f.isAccessible()) {
                    f.setAccessible(true);
                }
                final TomEEEmbeddedArgs args = SystemInstance.get().getComponent(TomEEEmbeddedArgs.class);
                f.set(target, args == null ? new String[0] : args.getArgs());
            }
        }
        aClass = aClass.getSuperclass();
    }
    SystemInstance.get().fireEvent(new TomEEEmbeddedApplicationRunnerInjection(target));
}
Also used : TomEEEmbeddedArgs(org.apache.tomee.embedded.component.TomEEEmbeddedArgs) TomEEEmbeddedArgs(org.apache.tomee.embedded.component.TomEEEmbeddedArgs) RandomPort(org.apache.openejb.testing.RandomPort) Field(java.lang.reflect.Field) TomEEEmbeddedApplicationRunnerInjection(org.apache.tomee.embedded.event.TomEEEmbeddedApplicationRunnerInjection) WebBeansContext(org.apache.webbeans.config.WebBeansContext) Application(org.apache.openejb.testing.Application)

Aggregations

Field (java.lang.reflect.Field)1 Application (org.apache.openejb.testing.Application)1 RandomPort (org.apache.openejb.testing.RandomPort)1 TomEEEmbeddedArgs (org.apache.tomee.embedded.component.TomEEEmbeddedArgs)1 TomEEEmbeddedApplicationRunnerInjection (org.apache.tomee.embedded.event.TomEEEmbeddedApplicationRunnerInjection)1 WebBeansContext (org.apache.webbeans.config.WebBeansContext)1