All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.cdi.util.DelegatingContextualLifecycle Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.cdi.util;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionTarget;

/**
 * An implementation of {@link ContextualLifecycle} that is backed by an
 * {@link InjectionTarget}.
 *
 * @param 
 * @author Pete Muir
 * @author Stuart Douglas
 */
public class DelegatingContextualLifecycle implements ContextualLifecycle {

    private final InjectionTarget injectionTarget;

    /**
     * Instantiate a new {@link ContextualLifecycle} backed by an
     * {@link InjectionTarget}.
     *
     * @param injectionTarget the {@link InjectionTarget} used to create and
     *                        destroy instances
     */
    public DelegatingContextualLifecycle(InjectionTarget injectionTarget) {
        this.injectionTarget = injectionTarget;
    }

    public T create(Bean bean, CreationalContext creationalContext) {
        T instance = injectionTarget.produce(creationalContext);
        injectionTarget.inject(instance, creationalContext);
        injectionTarget.postConstruct(instance);
        return instance;
    }

    public void destroy(Bean bean, T instance, CreationalContext creationalContext) {
        try {
            injectionTarget.preDestroy(instance);
            creationalContext.release();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy