Java Code Examples for org.ehcache.config.CacheConfiguration#getServiceConfigurations()

The following examples show how to use org.ehcache.config.CacheConfiguration#getServiceConfigurations() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Eh107CompleteConfiguration.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
private static <K, V> boolean isStoreByValue(Configuration<K, V> config, CacheConfiguration<K, V> ehcacheConfig) {
  if(ehcacheConfig != null) {
    Collection<ServiceConfiguration<?, ?>> serviceConfigurations = ehcacheConfig.getServiceConfigurations();
    for (ServiceConfiguration<?, ?> serviceConfiguration : serviceConfigurations) {
      if (serviceConfiguration instanceof DefaultCopierConfiguration) {
        DefaultCopierConfiguration<?> copierConfig = (DefaultCopierConfiguration)serviceConfiguration;
        if(copierConfig.getType().equals(DefaultCopierConfiguration.Type.VALUE)) {
          if(copierConfig.getClazz().isAssignableFrom(IdentityCopier.class)) {
            return false;
          } else {
            return true;
          }
        }
      }
    }
  }
  return config.isStoreByValue();
}
 
Example 2
Source File: IntegrationConfigurationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCacheEventListenerThreadPoolName() throws Exception {
  Configuration configuration = new XmlConfiguration(this.getClass().getResource("/configs/ehcache-cacheEventListener.xml"));
  CacheConfiguration<?, ?> template1 = configuration.getCacheConfigurations().get("template1");
  DefaultCacheEventDispatcherConfiguration eventDispatcherConfig = null;
  for (ServiceConfiguration<?, ?> serviceConfiguration : template1.getServiceConfigurations()) {
    if (serviceConfiguration instanceof DefaultCacheEventDispatcherConfiguration) {
      eventDispatcherConfig = (DefaultCacheEventDispatcherConfiguration) serviceConfiguration;
    }
  }
  assertThat(eventDispatcherConfig.getThreadPoolAlias(), is("listeners-pool"));
}
 
Example 3
Source File: CacheConfigurationBuilder.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance ready to produce a {@link CacheConfiguration} functionally equivalent to the supplied configuration.
 *
 * @param configuration seed configuration
 * @param <K> the key type
 * @param <V> the value type
 * @return a {@code CacheConfigurationBuilder}
 */
public static <K, V> CacheConfigurationBuilder<K, V> newCacheConfigurationBuilder(CacheConfiguration<K, V> configuration) {
  CacheConfigurationBuilder<K, V> builder = newCacheConfigurationBuilder(configuration.getKeyType(), configuration.getValueType(), configuration.getResourcePools())
    .withEvictionAdvisor(configuration.getEvictionAdvisor())
    .withExpiry(configuration.getExpiryPolicy());
  ClassLoader classLoader = configuration.getClassLoader();
  if (classLoader != null) {
    builder = builder.withClassLoader(classLoader);
  }
  for (ServiceConfiguration<?, ?> serviceConfig : configuration.getServiceConfigurations()) {
    builder = builder.withService(serviceConfig);
  }
  return builder;
}
 
Example 4
Source File: EhcacheManager.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
<K, V> InternalCache<K, V> createNewEhcache(String alias, CacheConfiguration<K, V> config,
                                      Class<K> keyType, Class<V> valueType) {
  Collection<ServiceConfiguration<?, ?>> adjustedServiceConfigs = new ArrayList<>(config.getServiceConfigurations());

  List<ServiceConfiguration<?, ?>> unknownServiceConfigs = new ArrayList<>();
  for (ServiceConfiguration<?, ?> serviceConfig : adjustedServiceConfigs) {
    if (!serviceLocator.knowsServiceFor(serviceConfig)) {
      unknownServiceConfigs.add(serviceConfig);
    }
  }
  if (!unknownServiceConfigs.isEmpty()) {
    throw new IllegalStateException("Cannot find service(s) that can handle following configuration(s) : " + unknownServiceConfigs);
  }

  List<LifeCycled> lifeCycledList = new ArrayList<>();

  CacheLoaderWriterProvider cacheLoaderWriterProvider = serviceLocator.getService(CacheLoaderWriterProvider.class);
  CacheLoaderWriter<? super K, V> loaderWriter;
  if(cacheLoaderWriterProvider != null) {
    loaderWriter = cacheLoaderWriterProvider.createCacheLoaderWriter(alias, config);

    if (loaderWriter != null) {
      lifeCycledList.add(new LifeCycledAdapter() {
        @Override
        public void close() throws Exception {
          cacheLoaderWriterProvider.releaseCacheLoaderWriter(alias, loaderWriter);
        }
      });
    }
  } else {
    loaderWriter = null;
  }

  Store<K, V> store = getStore(alias, config, keyType, valueType, adjustedServiceConfigs, lifeCycledList, loaderWriter);


  CacheEventDispatcherFactory cenlProvider = serviceLocator.getService(CacheEventDispatcherFactory.class);
  CacheEventDispatcher<K, V> evtService =
      cenlProvider.createCacheEventDispatcher(store, adjustedServiceConfigs.toArray(new ServiceConfiguration<?, ?>[adjustedServiceConfigs.size()]));
  lifeCycledList.add(new LifeCycledAdapter() {
    @Override
    public void close() {
      cenlProvider.releaseCacheEventDispatcher(evtService);
    }
  });
  evtService.setStoreEventSource(store.getStoreEventSource());

  ResilienceStrategyProvider resilienceProvider = serviceLocator.getService(ResilienceStrategyProvider.class);
  ResilienceStrategy<K, V> resilienceStrategy;
  if (loaderWriter == null) {
    resilienceStrategy = resilienceProvider.createResilienceStrategy(alias, config, new DefaultRecoveryStore<>(store));
  } else {
    resilienceStrategy = resilienceProvider.createResilienceStrategy(alias, config, new DefaultRecoveryStore<>(store), loaderWriter);
  }
  InternalCache<K, V> cache = new Ehcache<>(config, store, resilienceStrategy, evtService, LoggerFactory.getLogger(Ehcache.class + "-" + alias), loaderWriter);

  CacheEventListenerProvider evntLsnrFactory = serviceLocator.getService(CacheEventListenerProvider.class);
  if (evntLsnrFactory != null) {
    @SuppressWarnings("unchecked")
    Collection<CacheEventListenerConfiguration<?>> evtLsnrConfigs =
        ServiceUtils.<Class<CacheEventListenerConfiguration<?>>>findAmongst((Class) CacheEventListenerConfiguration.class, config.getServiceConfigurations());
    for (CacheEventListenerConfiguration<?> lsnrConfig: evtLsnrConfigs) {
      CacheEventListener<K, V> lsnr = evntLsnrFactory.createEventListener(alias, lsnrConfig);
      if (lsnr != null) {
        cache.getRuntimeConfiguration().registerCacheEventListener(lsnr, lsnrConfig.orderingMode(), lsnrConfig.firingMode(),
            lsnrConfig.fireOn());
        lifeCycledList.add(new LifeCycled() {
          @Override
          public void init() {
            // no-op for now
          }

          @Override
          public void close() throws Exception {
            evntLsnrFactory.releaseEventListener(lsnr);
          }
        });
      }
    }
    evtService.setListenerSource(cache);
  }

  for (LifeCycled lifeCycled : lifeCycledList) {
    cache.addHook(lifeCycled);
  }

  return cache;
}