Java Code Examples for org.ehcache.config.builders.CacheConfigurationBuilder#withService()

The following examples show how to use org.ehcache.config.builders.CacheConfigurationBuilder#withService() . 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: ServiceConfigurationParser.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
public <K, V> CacheConfigurationBuilder<K, V> parseConfiguration(CacheTemplate cacheDefinition, ClassLoader cacheClassLoader,
                                                                 CacheConfigurationBuilder<K, V> cacheBuilder) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
  for (CoreServiceConfigurationParser coreServiceConfigParser : CORE_SERVICE_CONFIGURATION_PARSERS) {
    cacheBuilder = coreServiceConfigParser.parseServiceConfiguration(cacheDefinition, cacheClassLoader, cacheBuilder);
  }

  Map<URI, CacheServiceConfigurationParser<?>> parsers = extensionParsers.values().stream().
    collect(toMap(CacheServiceConfigurationParser::getNamespace, identity()));
  for (Element element : cacheDefinition.serviceConfigExtensions()) {
    URI namespace = URI.create(element.getNamespaceURI());
    final CacheServiceConfigurationParser<?> xmlConfigurationParser = parsers.get(namespace);
    if(xmlConfigurationParser == null) {
      throw new IllegalArgumentException("Can't find parser for namespace: " + namespace);
    }
    cacheBuilder = cacheBuilder.withService(xmlConfigurationParser.parseServiceConfiguration(element, cacheClassLoader));
  }

  return cacheBuilder;
}
 
Example 2
Source File: DefaultCacheEventListenerConfigurationParser.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Override
public <K, V> CacheConfigurationBuilder<K, V> parseServiceConfiguration(CacheTemplate cacheDefinition, ClassLoader cacheClassLoader,
                                                                        CacheConfigurationBuilder<K, V> cacheBuilder) throws ClassNotFoundException {
  ListenersConfig listenersConfig = cacheDefinition.listenersConfig();
  if(listenersConfig != null && listenersConfig.listeners() != null) {
    for (ListenersType.Listener listener : listenersConfig.listeners()) {
      Set<org.ehcache.event.EventType> eventSetToFireOn = listener.getEventsToFireOn().stream()
        .map(EventType::value).map(org.ehcache.event.EventType::valueOf).collect(toSet());
      @SuppressWarnings("unchecked")
      Class<CacheEventListener<?, ?>> cacheEventListenerClass = (Class<CacheEventListener<?, ?>>) getClassForName(listener.getClazz(), cacheClassLoader);
      CacheEventListenerConfigurationBuilder listenerBuilder = CacheEventListenerConfigurationBuilder
        .newEventListenerConfiguration(cacheEventListenerClass, eventSetToFireOn)
        .firingMode(EventFiring.valueOf(listener.getEventFiringMode().value()))
        .eventOrdering(EventOrdering.valueOf(listener.getEventOrderingMode().value()));
      cacheBuilder = cacheBuilder.withService(listenerBuilder);
    }
  }

  return cacheBuilder;
}
 
Example 3
Source File: ConfigurationMerger.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static <K, V> CacheConfigurationBuilder<K, V> addDefaultCopiers(CacheConfigurationBuilder<K, V> builder, Class<K> keyType, Class<V> valueType ) {
  Set<Class<?>> immutableTypes = new HashSet<>();
  immutableTypes.add(String.class);
  immutableTypes.add(Long.class);
  immutableTypes.add(Float.class);
  immutableTypes.add(Double.class);
  immutableTypes.add(Character.class);
  immutableTypes.add(Integer.class);
  if (immutableTypes.contains(keyType)) {
    builder = builder.withService(new DefaultCopierConfiguration<K>((Class)Eh107IdentityCopier.class, DefaultCopierConfiguration.Type.KEY));
  } else {
    builder = builder.withService(new DefaultCopierConfiguration<>(SerializingCopier.<K>asCopierClass(), DefaultCopierConfiguration.Type.KEY));
  }

  if (immutableTypes.contains(valueType)) {
    builder = builder.withService(new DefaultCopierConfiguration<K>((Class)Eh107IdentityCopier.class, DefaultCopierConfiguration.Type.VALUE));
  } else {
    builder = builder.withService(new DefaultCopierConfiguration<>(SerializingCopier.<K>asCopierClass(), DefaultCopierConfiguration.Type.VALUE));
  }
  return builder;
}
 
Example 4
Source File: SimpleCoreServiceConfigurationParser.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Override
public final <K, V> CacheConfigurationBuilder<K, V> parseServiceConfiguration(CacheTemplate cacheDefinition, ClassLoader cacheClassLoader, CacheConfigurationBuilder<K, V> cacheBuilder) throws ClassNotFoundException {
  IN config = extractor.apply(cacheDefinition);
  if (config != null) {
    U configuration = parser.parse(config, cacheClassLoader);
    if (configuration != null) {
      return cacheBuilder.withService(configuration);
    }
  }
  return cacheBuilder;
}
 
Example 5
Source File: ConfigurationMerger.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private <K, V> CacheConfigurationBuilder<K, V> handleStoreByValue(Eh107CompleteConfiguration<K, V> jsr107Configuration, CacheConfigurationBuilder<K, V> builder, String cacheName) {
  @SuppressWarnings("unchecked")
  Collection<DefaultCopierConfiguration<?>> copierConfigs = builder.getServices((Class<DefaultCopierConfiguration<?>>) (Class) DefaultCopierConfiguration.class);
  if(copierConfigs.isEmpty()) {
    if(jsr107Configuration.isStoreByValue()) {
      if (xmlConfiguration != null) {
        DefaultCopyProviderConfiguration defaultCopyProviderConfiguration = findSingletonAmongst(DefaultCopyProviderConfiguration.class,
            xmlConfiguration.getServiceCreationConfigurations());
        if (defaultCopyProviderConfiguration != null) {
          Map<Class<?>, DefaultCopierConfiguration<?>> defaults = defaultCopyProviderConfiguration.getDefaults();
          handleCopierDefaultsforImmutableTypes(defaults);
          boolean matchingDefault = false;
          if (defaults.containsKey(jsr107Configuration.getKeyType())) {
            matchingDefault = true;
          } else {
            builder = builder.withService(new DefaultCopierConfiguration<>(SerializingCopier.<K>asCopierClass(), DefaultCopierConfiguration.Type.KEY));
          }
          if (defaults.containsKey(jsr107Configuration.getValueType())) {
            matchingDefault = true;
          } else {
            builder = builder.withService(new DefaultCopierConfiguration<>(SerializingCopier.<K>asCopierClass(), DefaultCopierConfiguration.Type.VALUE));
          }
          if (matchingDefault) {
            LOG.info("CacheManager level copier configuration overwriting JSR-107 by-value semantics for cache {}", cacheName);
          }
          return builder;
        }
      }
      builder = addDefaultCopiers(builder, jsr107Configuration.getKeyType(), jsr107Configuration.getValueType());
      LOG.debug("Using default Copier for JSR-107 store-by-value cache {}", cacheName);
    }
  } else {
    LOG.info("Cache level copier configuration overwriting JSR-107 by-value semantics for cache {}", cacheName);
  }
  return builder;
}