Search in sources :

Example 6 with InjectionTarget

use of javax.enterprise.inject.spi.InjectionTarget in project meecrowave by apache.

the class Injector method inject.

public static CreationalContext<?> inject(final Object testInstance) {
    if (testInstance == null) {
        return null;
    }
    final BeanManager bm = CDI.current().getBeanManager();
    final AnnotatedType<?> annotatedType = bm.createAnnotatedType(testInstance.getClass());
    final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
    final CreationalContext<?> creationalContext = bm.createCreationalContext(null);
    injectionTarget.inject(testInstance, creationalContext);
    return creationalContext;
}
Also used : InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 7 with InjectionTarget

use of javax.enterprise.inject.spi.InjectionTarget in project meecrowave by apache.

the class Meecrowave method inject.

public <T> AutoCloseable inject(final T instance) {
    final BeanManager bm = CDI.current().getBeanManager();
    final AnnotatedType<?> annotatedType = bm.createAnnotatedType(instance.getClass());
    final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
    final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
    injectionTarget.inject(instance, creationalContext);
    return creationalContext::release;
}
Also used : InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 8 with InjectionTarget

use of javax.enterprise.inject.spi.InjectionTarget in project meecrowave by apache.

the class CDIInstanceManager method newInstance.

@Override
public void newInstance(final Object o) throws IllegalAccessException, InvocationTargetException, NamingException {
    if (WebBeansConfigurationListener.class.isInstance(o) || o.getClass().getName().startsWith("org.apache.catalina.servlets.")) {
        return;
    }
    final BeanManager bm = CDI.current().getBeanManager();
    final AnnotatedType<?> annotatedType = bm.createAnnotatedType(o.getClass());
    final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
    final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
    injectionTarget.inject(o, creationalContext);
    try {
        injectionTarget.postConstruct(o);
    } catch (final RuntimeException e) {
        creationalContext.release();
        throw e;
    }
    destroyables.put(o, () -> {
        try {
            injectionTarget.preDestroy(o);
        } finally {
            creationalContext.release();
        }
    });
}
Also used : WebBeansConfigurationListener(org.apache.webbeans.servlet.WebBeansConfigurationListener) InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 9 with InjectionTarget

use of javax.enterprise.inject.spi.InjectionTarget in project ovirt-engine by oVirt.

the class Injector method injectMembers.

/**
 * This method will take an instance and will fulfill all its dependencies, which are members
 * annotated with <code>@Inject</code>.
 * @param instance unmanaged CDI bean, essentially a regular object which is not managed by
 *                  the CDI container.
 * @param <T> an unmanaged CDI instance with some members containing <code>@Inject</code> annotated
 *           members
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T injectMembers(T instance) {
    AnnotatedType type = CDI.current().getBeanManager().createAnnotatedType(instance.getClass());
    InjectionTarget injectionTarget = CDI.current().getBeanManager().createInjectionTarget(type);
    injectionTarget.inject(instance, CDI.current().getBeanManager().createCreationalContext(null));
    injectionTarget.postConstruct(instance);
    return instance;
}
Also used : AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) InjectionTarget(javax.enterprise.inject.spi.InjectionTarget)

Example 10 with InjectionTarget

use of javax.enterprise.inject.spi.InjectionTarget in project datanucleus-core by datanucleus.

the class CDIHandler method close.

public void close() {
    if (!injectionTargets.isEmpty()) {
        Set keys = new HashSet<>();
        synchronized (injectionTargets) {
            keys.addAll(injectionTargets.keySet());
            for (Object key : keys) {
                try {
                    InjectionTarget target = injectionTargets.get(key);
                    target.preDestroy(key);
                    target.dispose(key);
                    injectionTargets.remove(key);
                } catch (Exception e) {
                }
            }
        }
    }
    if (!creationalContexts.isEmpty()) {
        Iterator<CreationalContext> ctxIter = creationalContexts.iterator();
        while (ctxIter.hasNext()) {
            CreationalContext ctx = ctxIter.next();
            ctx.release();
            ctxIter.remove();
        }
    }
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) HashSet(java.util.HashSet) Set(java.util.Set) InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) HashSet(java.util.HashSet)

Aggregations

InjectionTarget (javax.enterprise.inject.spi.InjectionTarget)12 BeanManager (javax.enterprise.inject.spi.BeanManager)6 CreationalContext (javax.enterprise.context.spi.CreationalContext)4 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Field (java.lang.reflect.Field)1 Member (java.lang.reflect.Member)1 Method (java.lang.reflect.Method)1 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 InjectionTargetFactory (javax.enterprise.inject.spi.InjectionTargetFactory)1 ProcessInjectionTarget (javax.enterprise.inject.spi.ProcessInjectionTarget)1 BeanAttributesImpl (org.apache.webbeans.component.BeanAttributesImpl)1 WebBeansContext (org.apache.webbeans.config.WebBeansContext)1 InjectionTargetFactoryImpl (org.apache.webbeans.container.InjectionTargetFactoryImpl)1 AnnotatedElementFactory (org.apache.webbeans.portable.AnnotatedElementFactory)1 WebBeansConfigurationListener (org.apache.webbeans.servlet.WebBeansConfigurationListener)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 ManagedReferenceFactory (org.jboss.as.naming.ManagedReferenceFactory)1 ComponentSupport (org.jboss.as.weld.spi.ComponentSupport)1