org.infinispan.cdi.DelegatingDistributedTaskLifecycle Maven / Gradle / Ivy
package org.infinispan.cdi;
import org.infinispan.Cache;
import org.infinispan.cdi.util.CDIHelper;
import org.infinispan.distexec.spi.DistributedTaskLifecycle;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;
import java.util.concurrent.Callable;
public class DelegatingDistributedTaskLifecycle implements DistributedTaskLifecycle {
private final DistributedTaskLifecycle delegate;
public DelegatingDistributedTaskLifecycle() {
delegate = CDIHelper.isCDIAvailable() ? new CDIDistributedTaskLifecycle() : new NoCDIDistributedTaskLifecycle();
}
@Override
public void onPreExecute(Callable task, Cache inputDataCache) {
delegate.onPreExecute(task, inputDataCache);
}
@Override
public void onPostExecute(Callable task) {
delegate.onPostExecute(task);
}
static class NoCDIDistributedTaskLifecycle implements DistributedTaskLifecycle {
@Override
public void onPreExecute(Callable task, Cache inputDataCache) {
}
@Override
public void onPostExecute(Callable task) {
}
}
static class CDIDistributedTaskLifecycle implements DistributedTaskLifecycle {
@Override
public void onPreExecute(Callable task, Cache inputDataCache) {
BeanManager bm = CDIHelper.getBeanManager();
if (bm == null)
return;
ContextInputCache.set(inputDataCache);
Class> clazz = (Class>) task.getClass();
AnnotatedType> type = bm.createAnnotatedType(clazz);
InjectionTarget> it = bm.createInjectionTarget(type);
CreationalContext> ctx = bm.createCreationalContext(null);
it.inject(task, ctx);
it.postConstruct(task);
}
@Override
public void onPostExecute(Callable task) {
try {
BeanManager bm = CDIHelper.getBeanManager();
if (bm == null)
return;
Class> clazz = (Class>) task.getClass();
AnnotatedType> type = bm.createAnnotatedType(clazz);
InjectionTarget> it = bm.createInjectionTarget(type);
it.preDestroy(task);
it.dispose(task);
} finally {
ContextInputCache.clean();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy