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

    if (beanManager.getBeans(RemoteCacheManager.class).isEmpty()) {
      LOGGER.addDefaultRemoteCacheManager();
      event.addBean(createDefaultRemoteCacheManagerBean(beanManager));
    }

    for (Map.Entry<Type, Set<Annotation>> entry : remoteCacheInjectionPoints.entrySet()) {

      event.addBean(
          new BeanBuilder(beanManager)
              .readFromType(beanManager.createAnnotatedType(Reflections.getRawType(entry.getKey())))
              .addType(entry.getKey())
              .addQualifiers(entry.getValue())
              .beanLifecycle(
                  new ContextualLifecycle<RemoteCache<?, ?>>() {
                    @Override
                    public RemoteCache<?, ?> create(
                        Bean<RemoteCache<?, ?>> bean, CreationalContext<RemoteCache<?, ?>> ctx) {
                      return remoteCacheProducer.produce(ctx);
                    }

                    @Override
                    public void destroy(
                        Bean<RemoteCache<?, ?>> bean,
                        RemoteCache<?, ?> instance,
                        CreationalContext<RemoteCache<?, ?>> ctx) {
                      remoteCacheProducer.dispose(instance);
                    }
                  })
              .create());
    }
  }
 // This is a work around for CDI Uber Jar deployment. When Weld scans the classpath it  pick up
 // RemoteCacheManager
 // (this is an implementation, not an interface, so it gets instantiated). As a result we get
 // duplicated classes
 // in CDI BeanManager.
 @SuppressWarnings("unused")
 <T extends RemoteCacheManager> void removeDuplicatedRemoteCacheManager(
     @Observes ProcessAnnotatedType<T> bean) {
   if (RemoteCacheManager.class
       .getCanonicalName()
       .equals(bean.getAnnotatedType().getJavaClass().getCanonicalName())) {
     LOGGER.info("removing duplicated  RemoteCacheManager" + bean.getAnnotatedType());
     bean.veto();
   }
 }