@Test
  public void testAddSerializerForPersistent() throws Exception {
    DefaultSerializationProviderConfiguration config =
        new DefaultSerializationProviderConfiguration();
    config.addSerializerFor(Long.class, PersistentSerializer.class);

    assertTrue(config.getTransientSerializers().isEmpty());
    assertSame(PersistentSerializer.class, config.getPersistentSerializers().get(Long.class));
  }
  @Test
  public void testAddSerializerForTransientPersistentCombo() throws Exception {
    DefaultSerializationProviderConfiguration config =
        new DefaultSerializationProviderConfiguration();
    config.addSerializerFor(Long.class, ComboSerializer.class);

    assertSame(ComboSerializer.class, config.getPersistentSerializers().get(Long.class));
    assertSame(ComboSerializer.class, config.getTransientSerializers().get(Long.class));
  }
 @Test(expected = IllegalArgumentException.class)
 public void testAddSerializerForUnusable() throws Exception {
   DefaultSerializationProviderConfiguration config =
       new DefaultSerializationProviderConfiguration();
   config.addSerializerFor(Long.class, UnusableSerializer.class);
 }
コード例 #4
0
  @SuppressWarnings({"rawtypes", "unchecked"})
  private void parseConfiguration()
      throws ClassNotFoundException, IOException, SAXException, InstantiationException,
          IllegalAccessException {
    LOGGER.info("Loading Ehcache XML configuration from {}.", xml.getPath());
    ConfigurationParser configurationParser =
        new ConfigurationParser(xml.toExternalForm(), CORE_SCHEMA_URL);

    final ArrayList<ServiceCreationConfiguration<?>> serviceConfigs =
        new ArrayList<ServiceCreationConfiguration<?>>();

    for (ServiceType serviceType : configurationParser.getServiceElements()) {
      if (serviceType.getDefaultSerializers() != null) {
        DefaultSerializationProviderConfiguration configuration =
            new DefaultSerializationProviderConfiguration();

        for (SerializerType.Serializer serializer :
            serviceType.getDefaultSerializers().getSerializer()) {
          try {
            configuration.addSerializerFor(
                getClassForName(serializer.getType(), classLoader),
                (Class) getClassForName(serializer.getValue(), classLoader));
          } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
          }
        }
        serviceConfigs.add(configuration);
      } else if (serviceType.getPersistence() != null) {
        serviceConfigs.add(
            new CacheManagerPersistenceConfiguration(
                new File(serviceType.getPersistence().getDirectory())));
      } else {
        final ServiceCreationConfiguration<?> serviceConfiguration1 =
            configurationParser.parseExtension((Element) serviceType.getAny());
        serviceConfigs.add(serviceConfiguration1);
      }
    }

    for (ServiceCreationConfiguration<?> serviceConfiguration :
        Collections.unmodifiableList(serviceConfigs)) {
      serviceConfigurations.add(serviceConfiguration);
    }

    for (ConfigurationParser.CacheDefinition cacheDefinition :
        configurationParser.getCacheElements()) {
      CacheConfigurationBuilder<Object, Object> builder =
          CacheConfigurationBuilder.newCacheConfigurationBuilder();
      String alias = cacheDefinition.id();

      ClassLoader cacheClassLoader = cacheClassLoaders.get(alias);
      if (cacheClassLoader != null) {
        builder = builder.withClassLoader(cacheClassLoader);
      }

      if (cacheClassLoader == null) {
        if (classLoader != null) {
          cacheClassLoader = classLoader;
        } else {
          cacheClassLoader = ClassLoading.getDefaultClassLoader();
        }
      }

      Class keyType = getClassForName(cacheDefinition.keyType(), cacheClassLoader);
      Class valueType = getClassForName(cacheDefinition.valueType(), cacheClassLoader);
      if (cacheDefinition.keySerializer() != null) {
        Class keySerializer = getClassForName(cacheDefinition.keySerializer(), cacheClassLoader);
        builder =
            builder.add(
                new DefaultSerializerConfiguration(
                    keySerializer, DefaultSerializerConfiguration.Type.KEY));
      }
      if (cacheDefinition.valueSerializer() != null) {
        Class valueSerializer =
            getClassForName(cacheDefinition.valueSerializer(), cacheClassLoader);
        builder =
            builder.add(
                new DefaultSerializerConfiguration(
                    valueSerializer, DefaultSerializerConfiguration.Type.VALUE));
      }
      EvictionVeto evictionVeto =
          getInstanceOfName(cacheDefinition.evictionVeto(), cacheClassLoader, EvictionVeto.class);
      EvictionPrioritizer evictionPrioritizer =
          getInstanceOfName(
              cacheDefinition.evictionPrioritizer(),
              cacheClassLoader,
              EvictionPrioritizer.class,
              Eviction.Prioritizer.class);
      final ConfigurationParser.Expiry parsedExpiry = cacheDefinition.expiry();
      if (parsedExpiry != null) {
        builder = builder.withExpiry(getExpiry(cacheClassLoader, parsedExpiry));
      }
      ResourcePoolsBuilder resourcePoolsBuilder = newResourcePoolsBuilder();
      for (ResourcePool resourcePool : cacheDefinition.resourcePools()) {
        resourcePoolsBuilder =
            resourcePoolsBuilder.with(
                resourcePool.getType(),
                resourcePool.getSize(),
                resourcePool.getUnit(),
                resourcePool.isPersistent());
      }
      builder = builder.withResourcePools(resourcePoolsBuilder);
      for (ServiceConfiguration<?> serviceConfig : cacheDefinition.serviceConfigs()) {
        builder = builder.add(serviceConfig);
      }
      if (cacheDefinition.loaderWriter() != null) {
        final Class<CacheLoaderWriter<?, ?>> cacheLoaderWriterClass =
            (Class<CacheLoaderWriter<?, ?>>)
                getClassForName(cacheDefinition.loaderWriter(), cacheClassLoader);
        builder = builder.add(new DefaultCacheLoaderWriterConfiguration(cacheLoaderWriterClass));
        if (cacheDefinition.writeBehind() != null) {
          WriteBehind writeBehind = cacheDefinition.writeBehind();
          WriteBehindConfigurationBuilder writeBehindConfigurationBuilder =
              WriteBehindConfigurationBuilder.newWriteBehindConfiguration()
                  .concurrencyLevel(writeBehind.concurrency())
                  .queueSize(writeBehind.maxQueueSize())
                  .rateLimit(writeBehind.rateLimitPerSecond())
                  .retry(writeBehind.retryAttempts(), writeBehind.retryAttemptsDelay())
                  .delay(writeBehind.minWriteDelay(), writeBehind.maxWriteDelay());
          if (writeBehind.isBatched()) {
            writeBehindConfigurationBuilder =
                writeBehindConfigurationBuilder.batchSize(writeBehind.batchSize());
          }
          if (writeBehind.isCoalesced()) {
            writeBehindConfigurationBuilder = writeBehindConfigurationBuilder.enableCoalescing();
          }
          builder = builder.add(writeBehindConfigurationBuilder);
        }
      }
      if (cacheDefinition.listeners() != null) {
        for (ConfigurationParser.Listener listener : cacheDefinition.listeners()) {
          final Class<CacheEventListener<?, ?>> cacheEventListenerClass =
              (Class<CacheEventListener<?, ?>>)
                  getClassForName(listener.className(), cacheClassLoader);
          final List<EventType> eventListToFireOn = listener.fireOn();
          Set<org.ehcache.event.EventType> eventSetToFireOn =
              new HashSet<org.ehcache.event.EventType>();
          for (EventType events : eventListToFireOn) {
            switch (events) {
              case CREATED:
                eventSetToFireOn.add(org.ehcache.event.EventType.CREATED);
                break;
              case EVICTED:
                eventSetToFireOn.add(org.ehcache.event.EventType.EVICTED);
                break;
              case EXPIRED:
                eventSetToFireOn.add(org.ehcache.event.EventType.EXPIRED);
                break;
              case UPDATED:
                eventSetToFireOn.add(org.ehcache.event.EventType.UPDATED);
                break;
              case REMOVED:
                eventSetToFireOn.add(org.ehcache.event.EventType.REMOVED);
                break;
              default:
                throw new IllegalArgumentException("Invalid Event Type provided");
            }
          }
          CacheEventListenerConfigurationBuilder listenerBuilder =
              CacheEventListenerConfigurationBuilder.newEventListenerConfiguration(
                      cacheEventListenerClass, eventSetToFireOn)
                  .firingMode(EventFiring.valueOf(listener.eventFiring().value()))
                  .eventOrdering(EventOrdering.valueOf(listener.eventOrdering().value()));
          builder = builder.add(listenerBuilder);
        }
      }
      if (cacheDefinition.storeByValueOnHeap() != null) {
        final OnHeapStoreServiceConfiguration onHeapStoreServiceConfig =
            new OnHeapStoreServiceConfiguration();
        onHeapStoreServiceConfig.storeByValue(cacheDefinition.storeByValueOnHeap());
        builder = builder.add(onHeapStoreServiceConfig);
      }
      final CacheConfiguration<?, ?> config =
          builder.buildConfig(keyType, valueType, evictionVeto, evictionPrioritizer);
      cacheConfigurations.put(alias, config);
    }

    templates.putAll(configurationParser.getTemplates());
  }