Skip to content
Permalink
Browse files

ISPN-6242 Migrate to CDI 1.2 with annotation bean discovery

  • Loading branch information...
slaskawi authored and tristantarrant committed Jul 20, 2016
1 parent 5da4c65 commit 57ebf7b8700fbaa8661c4f7a3a65e1a27d0f8879
@@ -1,18 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:weld="http://jboss.org/schema/weld/beans"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd
http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd">
<weld:scan>
<weld:exclude name="org.infinispan.cdi.**">
<weld:if-class-available name="!javax.cache.Cache"/>
</weld:exclude>
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.2" bean-discovery-mode="annotated">

<weld:exclude name="org.infinispan.jcache.**">
<weld:if-class-available name="!javax.cache.Cache"/>
</weld:exclude>
<weld:exclude name="org.apache.logging.log4j.**" />
<weld:exclude name="infinispan.**" />
</weld:scan>
<scan>
<exclude name="org.infinispan.cdi.**">
<if-class-available name="!javax.cache.Cache"/>
</exclude>

<exclude name="org.infinispan.jcache.**">
<if-class-available name="!javax.cache.Cache"/>
</exclude>
</scan>
</beans>
@@ -1,21 +1,22 @@
package org.infinispan.cdi.embedded;

import java.lang.annotation.Annotation;
import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;

import org.infinispan.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.cdi.embedded.event.cache.CacheEventBridge;
import org.infinispan.cdi.common.util.Reflections;
import org.infinispan.cdi.embedded.event.cache.CacheEventBridge;
import org.infinispan.cdi.embedded.event.cachemanager.CacheManagerEventBridge;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.EmbeddedCacheManager;

import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import java.lang.annotation.Annotation;
import java.util.Set;

/**
* This class is responsible to produce the {@link Cache} and {@link AdvancedCache}. This class use the
* <a href="http://docs.jboss.org/seam/3/solder/latest/reference/en-US/html_single/#genericbeans">Generic Beans</a>
@@ -24,10 +25,11 @@
* @author Pete Muir
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
*/
@ApplicationScoped
public class AdvancedCacheProducer {

@Inject
private CacheContainer defaultCacheContainer;
private EmbeddedCacheManager defaultCacheContainer;

@Inject
private CacheEventBridge cacheEventBridge;
@@ -73,11 +75,4 @@ private CacheContainer getCacheContainer(Set<Annotation> qualifiers) {

return cache.getAdvancedCache();
}

@Produces
<K, V> AdvancedCache<K, V> getDefaultAdvancedCache() {
// lazy register stuff
infinispanExtension.registerCacheConfigurations(eventBridge, cacheManagers, beanManager);
return defaultCacheContainer.<K, V>getCache().getAdvancedCache();
}
}
@@ -1,5 +1,23 @@
package org.infinispan.cdi.embedded;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessProducer;
import javax.enterprise.inject.spi.Producer;
import javax.enterprise.util.TypeLiteral;

import org.infinispan.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.cdi.common.util.AnyLiteral;
@@ -20,24 +38,6 @@
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessProducer;
import javax.enterprise.inject.spi.Producer;
import javax.enterprise.util.TypeLiteral;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

/**
* The Infinispan CDI extension for embedded caches
*
@@ -63,35 +63,43 @@ public InfinispanExtensionEmbedded() {
this.configurations = new HashSet<>();
}

@SuppressWarnings("unchecked")
void processProducers(@Observes ProcessProducer<?, ?> event, BeanManager beanManager) {
final ConfigureCache annotation = event.getAnnotatedMember().getAnnotation(ConfigureCache.class);
if (annotation != null) {
configurations.add(new ConfigurationHolder((Producer<Configuration>) event.getProducer(), annotation.value(),
Reflections.getQualifiers(beanManager, event.getAnnotatedMember().getAnnotations())));
}
}

// This is a work around for CDI Uber Jar deployment. When Weld scans the classpath it pick up DefaultCacheManager
// (this is an implementation, not an interface, so it gets instantiated). As a result we get duplicated classes
// in CDI BeanManager.
<T extends DefaultCacheManager> void removeDuplicatedRemoteCacheManager(@Observes ProcessAnnotatedType<T> bean) {
if(DefaultCacheManager.class.getCanonicalName().equals(bean.getAnnotatedType().getJavaClass().getCanonicalName())) {
LOGGER.info("removing duplicated DefaultCacheManager" + bean.getAnnotatedType());
bean.veto();
}
}
@SuppressWarnings("unchecked")
void observeConfigurationProducers(@Observes ProcessProducer<?, Configuration> event, BeanManager beanManager) {
final ConfigureCache annotation = event.getAnnotatedMember().getAnnotation(ConfigureCache.class);
String configurationName = "";
if (annotation != null) {
configurationName = annotation.value();
}
configurations.add(new ConfigurationHolder((Producer<Configuration>) event.getProducer(), configurationName,
Reflections.getQualifiers(beanManager, event.getAnnotatedMember().getAnnotations())));
}

public void observeEmbeddedCacheManagerBean(@Observes ProcessBean<?> processBean) {
if (processBean.getBean().getTypes().contains(EmbeddedCacheManager.class)) {
installedEmbeddedCacheManagers.add(processBean.getBean().getQualifiers());
}
}

@SuppressWarnings("unchecked")
<T, X>void registerBeans(@Observes AfterBeanDiscovery event, final BeanManager beanManager) {

for (final ConfigurationHolder holder : configurations) {
if (beanManager.getBeans(Configuration.class).isEmpty()) {
LOGGER.addDefaultEmbeddedConfiguration();
final Configuration defaultConfiguration = new ConfigurationBuilder().build();
// Must be added after AdvancedCache producer registration - see also AdvancedCacheProducer.getDefaultAdvancedCache()
configurations.add(new ConfigurationHolder(defaultConfiguration, "", defaultQualifiers()));
event.addBean(createDefaultEmbeddedConfigurationBean(beanManager, defaultConfiguration));
}

for (final ConfigurationHolder holder : configurations) {
// register a AdvancedCache producer for each configuration
Bean<?> advancedCacheBean = new BeanBuilder(beanManager)
.readFromType(beanManager.createAnnotatedType(AdvancedCache.class))
.qualifiers(Beans.buildQualifiers(holder.getQualifiers()))
.addType(new TypeLiteral<AdvancedCache<T, X>>() {}.getType())
.addType(new TypeLiteral<Cache<T, X>>() {}.getType())
.passivationCapable(true)
.id(InfinispanExtensionEmbedded.class.getSimpleName() + "#" + AdvancedCache.class.getSimpleName() + "#" + Cache.class.getSimpleName())
.beanLifecycle(new ContextualLifecycle<AdvancedCache<?, ?>>() {
@Override
public AdvancedCache<?, ?> create(Bean<AdvancedCache<?, ?>> bean,
@@ -102,13 +110,6 @@ void processProducers(@Observes ProcessProducer<?, ?> event, BeanManager beanMan
event.addBean(advancedCacheBean);
}

if (beanManager.getBeans(Configuration.class).isEmpty()) {
LOGGER.addDefaultEmbeddedConfiguration();
final Configuration defaultConfiguration = new ConfigurationBuilder().build();
// Must be added after AdvancedCache producer registration - see also AdvancedCacheProducer.getDefaultAdvancedCache()
configurations.add(new ConfigurationHolder(defaultConfiguration, "", defaultQualifiers()));
event.addBean(createDefaultEmbeddedConfigurationBean(beanManager, defaultConfiguration));
}
if (beanManager.getBeans(EmbeddedCacheManager.class).isEmpty()) {
LOGGER.addDefaultEmbeddedCacheManager();
event.addBean(createDefaultEmbeddedCacheManagerBean(beanManager));
@@ -130,12 +131,6 @@ void processProducers(@Observes ProcessProducer<?, ?> event, BeanManager beanMan
CreationalContext<Cache<K, V>> creationalContext) {
return ContextInputCache.get();
}

@Override
public void destroy(Bean<Cache<K, V>> bean, Cache<K, V> instance,
CreationalContext<Cache<K, V>> creationalContext) {

}
}).create());
}

@@ -149,12 +144,6 @@ public void destroy(Bean<Cache<K, V>> bean, Cache<K, V> instance,
return installedCacheManagers;
}

public void observeEmbeddedCacheManagerBean(@Observes ProcessBean<?> processBean) {
if (processBean.getBean().getTypes().contains(EmbeddedCacheManager.class)) {
installedEmbeddedCacheManagers.add(processBean.getBean().getQualifiers());
}
}

public void registerCacheConfigurations(CacheManagerEventBridge eventBridge, Instance<EmbeddedCacheManager> cacheManagers, BeanManager beanManager) {
if (!registered) {
synchronized (registerLock) {
@@ -208,6 +197,8 @@ public void registerCacheConfigurations(CacheManagerEventBridge eventBridge, Ins
.addTypes(Object.class, Configuration.class)
.scope(Dependent.class)
.qualifiers(defaultQualifiers())
.passivationCapable(true)
.id(InfinispanExtensionEmbedded.class.getSimpleName() + "#" + Configuration.class.getSimpleName())
.beanLifecycle(new ContextualLifecycle<Configuration>() {
@Override
public Configuration create(Bean<Configuration> bean,
@@ -233,6 +224,8 @@ public Configuration create(Bean<Configuration> bean,
.addTypes(Object.class, EmbeddedCacheManager.class)
.scope(ApplicationScoped.class)
.qualifiers(defaultQualifiers())
.passivationCapable(true)
.id(InfinispanExtensionEmbedded.class.getSimpleName() + "#" + EmbeddedCacheManager.class.getSimpleName())
.beanLifecycle(new ContextualLifecycle<EmbeddedCacheManager>() {

@Override
@@ -1,18 +1,21 @@
package org.infinispan.cdi.embedded.event.cache;

import java.lang.annotation.Annotation;
import java.util.Set;

import javax.enterprise.context.Dependent;

import org.infinispan.cdi.embedded.event.AbstractEventBridge;
import org.infinispan.notifications.Listenable;
import org.infinispan.notifications.cachelistener.event.Event;

import java.lang.annotation.Annotation;
import java.util.Set;

/**
* Bridges Infinispan with CDI events.
*
* @author Pete Muir
* @author Sebastian Laskawiec
*/
@Dependent
public class CacheEventBridge extends AbstractEventBridge<Event<?, ?>> {

public void registerObservers(Set<Annotation> qualifierSet,
@@ -1,18 +1,21 @@
package org.infinispan.cdi.embedded.event.cachemanager;

import java.lang.annotation.Annotation;
import java.util.Set;

import javax.enterprise.context.Dependent;

import org.infinispan.cdi.embedded.event.AbstractEventBridge;
import org.infinispan.notifications.Listenable;
import org.infinispan.notifications.cachemanagerlistener.event.CacheStartedEvent;
import org.infinispan.notifications.cachemanagerlistener.event.CacheStoppedEvent;
import org.infinispan.notifications.cachemanagerlistener.event.Event;
import org.infinispan.notifications.cachemanagerlistener.event.ViewChangedEvent;

import java.lang.annotation.Annotation;
import java.util.Set;

/**
* @author Pete Muir
*/
@Dependent
public class CacheManagerEventBridge extends AbstractEventBridge<Event> {

public void registerObservers(Set<Annotation> qualifierSet,
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"/>
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.2" bean-discovery-mode="annotated"/>
@@ -1,5 +1,13 @@
package org.infinispan.cdi.embedded.test.cachemanager;

import static org.infinispan.cdi.embedded.test.testutil.Deployments.baseDeployment;
import static org.infinispan.commons.api.BasicCacheContainer.DEFAULT_CACHE_NAME;
import static org.testng.Assert.assertEquals;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import org.infinispan.Cache;
import org.infinispan.cdi.embedded.test.DefaultTestEmbeddedCacheManagerProducer;
import org.infinispan.configuration.cache.Configuration;
@@ -9,13 +17,6 @@
import org.jboss.shrinkwrap.api.Archive;
import org.testng.annotations.Test;

import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import static org.infinispan.cdi.embedded.test.testutil.Deployments.baseDeployment;
import static org.infinispan.commons.api.BasicCacheContainer.DEFAULT_CACHE_NAME;
import static org.testng.Assert.assertEquals;

/**
* Tests that the default embedded cache configuration can be overridden.
*
@@ -43,6 +44,7 @@ public void testDefaultConfiguration() {
* Overrides the default embedded cache configuration used for the initialization of the default embedded cache
* manager.
*/
@ApplicationScoped
public static class Config {
@Produces
public Configuration customDefaultConfiguration() {
@@ -42,7 +42,6 @@
<artifactId>infinispan-query-dsl</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>infinispan-commons-test</artifactId>
@@ -77,7 +76,6 @@
<artifactId>infinispan-server-hotrod</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.testng</groupId>
<artifactId>arquillian-testng-container</artifactId>
@@ -104,8 +102,6 @@
<artifactId>shrinkwrap-resolver-impl-maven</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Oops, something went wrong.

0 comments on commit 57ebf7b

Please sign in to comment.
You can’t perform that action at this time.