org.infinispan.cdi.common.util.DelegatingContextualLifecycle Maven / Gradle / Ivy
The newest version!
package org.infinispan.cdi.common.util;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.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);
}
}
}