org.infinispan.cdi.InfinispanExtensionRemote Maven / Gradle / Ivy
package org.infinispan.cdi;
import static org.infinispan.cdi.util.Reflections.getMetaAnnotation;
import static org.infinispan.cdi.util.Reflections.getQualifiers;
import static org.infinispan.cdi.util.Reflections.getRawType;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedMember;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.ProcessInjectionTarget;
import javax.enterprise.inject.spi.ProcessProducer;
import javax.enterprise.inject.spi.Producer;
import javax.enterprise.util.AnnotationLiteral;
import org.infinispan.cdi.util.BeanBuilder;
import org.infinispan.cdi.util.ContextualLifecycle;
import org.infinispan.cdi.util.logging.Log;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.commons.logging.LogFactory;
public class InfinispanExtensionRemote implements Extension {
private static final Log log = LogFactory.getLog(InfinispanExtensionRemote.class, Log.class);
private final Map> remoteCacheInjectionPoints;
private Producer> remoteCacheProducer;
public InfinispanExtensionRemote() {
new ConfigurationBuilder(); // Attempt to initialize a hotrod client class
this.remoteCacheInjectionPoints = new HashMap>();
}
void processProducers(ProcessProducer, ?> event) {
AnnotatedMember> member = event.getAnnotatedMember();
if (RemoteCacheProducer.class.equals(member.getDeclaringType().getBaseType())) {
remoteCacheProducer = (Producer>) event.getProducer();
}
}
void saveRemoteInjectionPoints(@Observes ProcessInjectionTarget event, BeanManager beanManager) {
final InjectionTarget injectionTarget = event.getInjectionTarget();
for (InjectionPoint injectionPoint : injectionTarget.getInjectionPoints()) {
final Annotated annotated = injectionPoint.getAnnotated();
final Type type = annotated.getBaseType();
final Class> rawType = getRawType(annotated.getBaseType());
final Set qualifiers = getQualifiers(beanManager, annotated.getAnnotations());
if (rawType.equals(RemoteCache.class) && qualifiers.isEmpty()) {
qualifiers.add(new AnnotationLiteral() {});
addRemoteCacheInjectionPoint(type, qualifiers);
} else if (!annotated.isAnnotationPresent(Remote.class)
&& getMetaAnnotation(annotated, Remote.class) != null
&& rawType.isAssignableFrom(RemoteCache.class)) {
addRemoteCacheInjectionPoint(type, qualifiers);
}
}
}
private void addRemoteCacheInjectionPoint(Type type, Set qualifiers) {
final Set currentQualifiers = remoteCacheInjectionPoints.get(type);
if (currentQualifiers == null) {
remoteCacheInjectionPoints.put(type, qualifiers);
} else {
currentQualifiers.addAll(qualifiers);
}
}
@SuppressWarnings("unchecked")
void registerCacheBeans(@Observes AfterBeanDiscovery event, final BeanManager beanManager) {
for (Map.Entry> entry : remoteCacheInjectionPoints.entrySet()) {
event.addBean(new BeanBuilder(beanManager)
.readFromType(beanManager.createAnnotatedType(getRawType(entry.getKey())))
.addType(entry.getKey())
.addQualifiers(entry.getValue())
.beanLifecycle(new ContextualLifecycle>() {
@Override
public RemoteCache, ?> create(Bean> bean, CreationalContext> ctx) {
return remoteCacheProducer.produce(ctx);
}
@Override
public void destroy(Bean> bean, RemoteCache, ?> instance, CreationalContext> ctx) {
remoteCacheProducer.dispose(instance);
}
}).create());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy