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

org.infinispan.cdi.AdvancedCacheProducer Maven / Gradle / Ivy

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

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

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 org.infinispan.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.cdi.event.cache.CacheEventBridge;
import org.infinispan.cdi.event.cachemanager.CacheManagerEventBridge;
import org.infinispan.cdi.util.Reflections;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.EmbeddedCacheManager;

/**
 * This class is responsible to produce the {@link Cache} and {@link AdvancedCache}. This class use the
 * Generic Beans
 * mechanism provided by Seam Solder.
 *
 * @author Pete Muir
 * @author Kevin Pollet  (C) 2011 SERLI
 */
public class AdvancedCacheProducer {

   @Inject
   private CacheContainer defaultCacheContainer;

   @Inject
   private CacheEventBridge cacheEventBridge;

   @Inject
   private InfinispanExtension infinispanExtension;

   @Inject @Any
   private Instance cacheManagers;

   @Inject
   private BeanManager beanManager;

   @Inject
   private CacheManagerEventBridge eventBridge;

   private CacheContainer getCacheContainer(Set qualifiers) {
      Instance cacheContainer = cacheManagers.select(qualifiers.toArray(Reflections.EMPTY_ANNOTATION_ARRAY));
      if (cacheContainer.isUnsatisfied()) {
         return defaultCacheContainer;
      } else {
         return cacheContainer.get();
      }
   }

   public  AdvancedCache getAdvancedCache(String name, Set qualifiers) {

      // lazy register stuff
      infinispanExtension.getEmbeddedExtension().registerCacheConfigurations(eventBridge, cacheManagers, beanManager);

      Cache cache;
      CacheContainer container = getCacheContainer(qualifiers);
      if (name.isEmpty()) {
         cache = container.getCache();
      } else {
         cache = container.getCache(name);
      }

      cacheEventBridge.registerObservers(
            qualifiers,
            cache
      );

      return cache.getAdvancedCache();
   }

   @Produces
    AdvancedCache getDefaultAdvancedCache() {
       // lazy register stuff
       infinispanExtension.getEmbeddedExtension().registerCacheConfigurations(eventBridge, cacheManagers, beanManager);
       return defaultCacheContainer.getCache().getAdvancedCache();
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy