org.ehcache.spi.serialization.Serializer Java Examples

The following examples show how to use org.ehcache.spi.serialization.Serializer. 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: ConditionalReplaceOperation.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuffer encode(final Serializer<K> keySerializer, final Serializer<V> valueSerializer) {
  ByteBuffer keyBuf = keySerializer.serialize(key);
  ByteBuffer oldValueBuf = oldValueHolder.encode(valueSerializer);
  ByteBuffer valueBuf = newValueHolder.encode(valueSerializer);

  ByteBuffer buffer = ByteBuffer.allocate(BYTE_SIZE_BYTES +   // Operation type
                                          INT_SIZE_BYTES +    // Size of the key payload
                                          LONG_SIZE_BYTES +   // Size of expiration time stamp
                                          keyBuf.remaining() + // the key payload itself
                                          INT_SIZE_BYTES +    // Size of the old value payload
                                          oldValueBuf.remaining() +  // The old value payload itself
                                          valueBuf.remaining());  // The value payload itself

  buffer.put(getOpCode().getValue());
  buffer.putLong(this.timeStamp);
  buffer.putInt(keyBuf.remaining());
  buffer.put(keyBuf);
  buffer.putInt(oldValueBuf.remaining());
  buffer.put(oldValueBuf);
  buffer.put(valueBuf);

  buffer.flip();
  return buffer;
}
 
Example #2
Source File: OffHeapDiskStore.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
private EhcachePersistentConcurrentOffHeapClockCache<K, OffHeapValueHolder<V>> createBackingMap(long size, Serializer<K> keySerializer, Serializer<V> valueSerializer, SwitchableEvictionAdvisor<K, OffHeapValueHolder<V>> evictionAdvisor) throws IOException {
  File metadataFile = getMetadataFile();
  try (FileOutputStream fos = new FileOutputStream(metadataFile)) {
    Properties properties = new Properties();
    properties.put(KEY_TYPE_PROPERTY_NAME, keyType.getName());
    properties.put(VALUE_TYPE_PROPERTY_NAME, valueType.getName());
    properties.store(fos, "Key and value types");
  }

  MappedPageSource source = new MappedPageSource(getDataFile(), size);
  PersistentPortability<K> keyPortability = persistent(new SerializerPortability<>(keySerializer));
  PersistentPortability<OffHeapValueHolder<V>> valuePortability = persistent(createValuePortability(valueSerializer));
  DiskWriteThreadPool writeWorkers = new DiskWriteThreadPool(executionService, threadPoolAlias, writerConcurrency);

  Factory<FileBackedStorageEngine<K, OffHeapValueHolder<V>>> storageEngineFactory = FileBackedStorageEngine.createFactory(source,
      max((size / diskSegments) / 10, 1024), BYTES, keyPortability, valuePortability, writeWorkers, true);

  EhcachePersistentSegmentFactory<K, OffHeapValueHolder<V>> factory = new EhcachePersistentSegmentFactory<>(
    source,
    storageEngineFactory,
    64,
    evictionAdvisor,
    mapEvictionListener, true);
  return new EhcachePersistentConcurrentOffHeapClockCache<>(evictionAdvisor, factory, diskSegments);

}
 
Example #3
Source File: BaseKeyValueOperation.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
BaseKeyValueOperation(ByteBuffer buffer, Serializer<K> keySerializer, Serializer<V> valueSerializer) {
  OperationCode opCode = OperationCode.valueOf(buffer.get());
  if (opCode != getOpCode()) {
    throw new IllegalArgumentException("Invalid operation: " + opCode);
  }
  this.timeStamp = buffer.getLong();
  int keySize = buffer.getInt();
  int maxLimit = buffer.limit();
  buffer.limit(buffer.position() + keySize);
  ByteBuffer keyBlob = buffer.slice();
  buffer.position(buffer.limit());
  buffer.limit(maxLimit);
  try {
    this.key = keySerializer.read(keyBlob);
  } catch (ClassNotFoundException e) {
    throw new CodecException(e);
  }
  this.valueHolder = new LazyValueHolder<>(buffer.slice(), valueSerializer);
}
 
Example #4
Source File: DefaultSerializationProviderConfigurationParserTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test @SuppressWarnings("unchecked")
public void unparseServiceCreationConfiguration() {
  DefaultSerializationProviderConfiguration providerConfig = new DefaultSerializationProviderConfiguration();
  providerConfig.addSerializerFor(Description.class, (Class) TestSerializer3.class);
  providerConfig.addSerializerFor(Person.class, (Class) TestSerializer4.class);

  Configuration config = ConfigurationBuilder.newConfigurationBuilder().withService(providerConfig).build();
  ConfigType configType = new DefaultSerializationProviderConfigurationParser().unparseServiceCreationConfiguration(config, new ConfigType());

  List<SerializerType.Serializer> serializers = configType.getDefaultSerializers().getSerializer();
  assertThat(serializers).hasSize(2);
  serializers.forEach(serializer -> {
    if (serializer.getType().equals(Description.class.getName())) {
      assertThat(serializer.getValue()).isEqualTo(TestSerializer3.class.getName());
    } else if (serializer.getType().equals(Person.class.getName())) {
      assertThat(serializer.getValue()).isEqualTo(TestSerializer4.class.getName());
    } else {
      throw new AssertionError("Not expected");
    }
  });
}
 
Example #5
Source File: DefaultSerializationProviderConfiguration.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a new {@link Serializer} mapping for the class {@code serializableClass}
 *
 * @param serializableClass the {@code Class} to add the mapping for
 * @param serializerClass the {@link Serializer} type to use
 * @param overwrite indicates if an existing mapping is to be overwritten
 * @param <T> the type of instances to be serialized / deserialized
 * @return this configuration object
 *
 * @throws NullPointerException if any argument is null
 * @throws IllegalArgumentException if a mapping for {@code serializableClass} already exists and {@code overwrite} is {@code false}
 */
public <T> DefaultSerializationProviderConfiguration addSerializerFor(Class<T> serializableClass, Class<? extends Serializer<T>> serializerClass, boolean overwrite) {
  if (serializableClass == null) {
    throw new NullPointerException("Serializable class cannot be null");
  }
  if (serializerClass == null) {
    throw new NullPointerException("Serializer class cannot be null");
  }

  if(!isConstructorPresent(serializerClass, ClassLoader.class)) {
    throw new IllegalArgumentException("The serializer: " + serializerClass.getName() + " does not have a constructor that takes in a ClassLoader.");
  }

  if (isConstructorPresent(serializerClass, ClassLoader.class, FileBasedPersistenceContext.class)) {
    LOGGER.warn(serializerClass.getName() + " class has a constructor that takes in a FileBasedPersistenceContext. " +
                "Support for this constructor has been removed since version 3.2. Consider removing it.");
  }

  if (defaultSerializers.containsKey(serializableClass) && !overwrite) {
    throw new IllegalArgumentException("Duplicate serializer for class : " + serializableClass.getName());
  } else {
    defaultSerializers.put(serializableClass, serializerClass);
  }

  return this;
}
 
Example #6
Source File: BaseKeyValueOperation.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
/**
 * Here we need to encode two objects of unknown size: the key and the value.
 * Encoding should be done in such a way that the key and value can be read
 * separately while decoding the bytes.
 * So the way it is done here is by writing the size of the payload along with
 * the payload. That is, the size of the key payload is written before the key
 * itself. The value payload is written after that.
 *
 * While decoding, the size is read first and then reading the same number of
 * bytes will get you the key payload. Whatever that is left is the value payload.
 */
@Override
public ByteBuffer encode(final Serializer<K> keySerializer, final Serializer<V> valueSerializer) {
  ByteBuffer keyBuf = keySerializer.serialize(key);
  ByteBuffer valueBuf = valueHolder.encode(valueSerializer);

  int size = BYTE_SIZE_BYTES +   // Operation type
             INT_SIZE_BYTES +    // Size of the key payload
             LONG_SIZE_BYTES +   // Size of expiration time stamp
             keyBuf.remaining() + // the key payload itself
             valueBuf.remaining();  // the value payload

  ByteBuffer buffer = ByteBuffer.allocate(size);

  buffer.put(getOpCode().getValue());
  buffer.putLong(this.timeStamp);
  buffer.putInt(keyBuf.remaining());
  buffer.put(keyBuf);
  buffer.put(valueBuf);
  buffer.flip();
  return buffer;
}
 
Example #7
Source File: DefaultSerializationProviderConfigurationParserTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void parseServiceCreationConfiguration() throws SAXException, JAXBException, ParserConfigurationException, IOException, ClassNotFoundException {
  Configuration xmlConfig = new XmlConfiguration(getClass().getResource("/configs/default-serializer.xml"));

  assertThat(xmlConfig.getServiceCreationConfigurations()).hasSize(1);

  ServiceCreationConfiguration<?, ?> configuration = xmlConfig.getServiceCreationConfigurations().iterator().next();

  assertThat(configuration).isExactlyInstanceOf(DefaultSerializationProviderConfiguration.class);

  DefaultSerializationProviderConfiguration factoryConfiguration = (DefaultSerializationProviderConfiguration) configuration;
  Map<Class<?>, Class<? extends Serializer<?>>> defaultSerializers = factoryConfiguration.getDefaultSerializers();
  assertThat(defaultSerializers).hasSize(4);
  assertThat(defaultSerializers.get(CharSequence.class)).isEqualTo(TestSerializer.class);
  assertThat(defaultSerializers.get(Number.class)).isEqualTo(TestSerializer2.class);
  assertThat(defaultSerializers.get(Long.class)).isEqualTo(TestSerializer3.class);
  assertThat(defaultSerializers.get(Integer.class)).isEqualTo(TestSerializer4.class);
}
 
Example #8
Source File: OffHeapStore.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
private EhcacheConcurrentOffHeapClockCache<K, OffHeapValueHolder<V>> createBackingMap(long size, Serializer<K> keySerializer, Serializer<V> valueSerializer, SwitchableEvictionAdvisor<K, OffHeapValueHolder<V>> evictionAdvisor) {
  HeuristicConfiguration config = new HeuristicConfiguration(size);
  PageSource source = new UpfrontAllocatingPageSource(getBufferSource(), config.getMaximumSize(), config.getMaximumChunkSize(), config.getMinimumChunkSize());
  Portability<K> keyPortability = new SerializerPortability<>(keySerializer);
  Portability<OffHeapValueHolder<V>> valuePortability = createValuePortability(valueSerializer);
  Factory<OffHeapBufferStorageEngine<K, OffHeapValueHolder<V>>> storageEngineFactory = OffHeapBufferStorageEngine.createFactory(PointerSize.INT, source, config
      .getSegmentDataPageSize(), keyPortability, valuePortability, false, true);

  Factory<? extends PinnableSegment<K, OffHeapValueHolder<V>>> segmentFactory = new EhcacheSegmentFactory<>(
    source,
    storageEngineFactory,
    config.getInitialSegmentTableSize(),
    evictionAdvisor,
    mapEvictionListener);
  return new EhcacheConcurrentOffHeapClockCache<>(evictionAdvisor, segmentFactory, config.getConcurrency());

}
 
Example #9
Source File: SerializersTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private void testSerializerWithByValueHeapCache(Serializer<Long> serializer) throws Exception {
  CacheManagerBuilder<CacheManager> cmBuilder =
    newCacheManagerBuilder()
      .withCache("heapByValueCache",
        newCacheConfigurationBuilder(Long.class, Person.class, newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES))
          .withKeyCopier(SerializingCopier.<Long>asCopierClass())
          .withKeySerializer(serializer)
      );
  cmBuilder.build(true);
}
 
Example #10
Source File: DefaultSerializationProviderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTransientSerializerWithoutConstructor() throws Exception {
  expectedException.expect(RuntimeException.class);
  expectedException.expectMessage("does not have a constructor that takes in a ClassLoader.");
  DefaultSerializationProvider provider = new DefaultSerializationProvider(null);
  provider.start(providerContaining());

  @SuppressWarnings("unchecked")
  Class<Serializer<Object>> serializerClass = (Class) BaseSerializer.class;
  DefaultSerializerConfiguration<Object> configuration = new DefaultSerializerConfiguration<>(serializerClass, DefaultSerializerConfiguration.Type.VALUE);
  provider.createValueSerializer(Object.class, ClassLoader.getSystemClassLoader(), configuration);
}
 
Example #11
Source File: DefaultSerializationProviderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void tesCreateTransientSerializersWithOverriddenSerializableType() throws Exception {
  DefaultSerializationProviderConfiguration dspfConfig = new DefaultSerializationProviderConfiguration();
  Class<Serializer<Serializable>> serializerClass = getSerializerClass();
  dspfConfig.addSerializerFor(Serializable.class, serializerClass);

  DefaultSerializationProvider dsp = new DefaultSerializationProvider(dspfConfig);
  dsp.start(providerContaining());

  assertThat(dsp.createKeySerializer(HashMap.class, getSystemClassLoader()), instanceOf(TestSerializer.class));
  assertThat(dsp.createKeySerializer(Serializable.class, getSystemClassLoader()), instanceOf(TestSerializer.class));
  assertThat(dsp.createKeySerializer(Integer.class, getSystemClassLoader()), instanceOf(IntegerSerializer.class));
}
 
Example #12
Source File: SerializersTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private void testSerializerWithHeapOffheapCache(Serializer<Long> serializer) throws Exception {
  CacheManagerBuilder<CacheManager> cmBuilder =
    newCacheManagerBuilder()
      .withCache("heapOffheapCache",
        newCacheConfigurationBuilder(Long.class, Person.class, newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).offheap(2, MemoryUnit.MB))
          .withKeySerializer(serializer)
      );
  cmBuilder.build(true);
}
 
Example #13
Source File: SerializersTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private void testSerializerWithOffheapCache(Serializer<Long> serializer) throws Exception {
  CacheManagerBuilder<CacheManager> cmBuilder =
    newCacheManagerBuilder()
      .withCache("offheapCache",
        newCacheConfigurationBuilder(Long.class, Person.class, newResourcePoolsBuilder().offheap(2, MemoryUnit.MB))
          .withKeySerializer(serializer)
      );
  cmBuilder.build(true);
}
 
Example #14
Source File: DefaultSerializationProviderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemembersCreationConfigurationAfterStopStart() throws UnsupportedTypeException {
  DefaultSerializationProviderConfiguration configuration = new DefaultSerializationProviderConfiguration();
  Class<Serializer<String>> serializerClass = getSerializerClass();
  configuration.addSerializerFor(String.class, serializerClass);
  DefaultSerializationProvider serializationProvider = new DefaultSerializationProvider(configuration);
  @SuppressWarnings("unchecked")
  ServiceProvider<Service> serviceProvider = mock(ServiceProvider.class);
  serializationProvider.start(serviceProvider);
  assertThat(serializationProvider.createKeySerializer(String.class, getSystemClassLoader()), instanceOf(TestSerializer.class));
  serializationProvider.stop();
  serializationProvider.start(serviceProvider);
  assertThat(serializationProvider.createKeySerializer(String.class, getSystemClassLoader()), instanceOf(TestSerializer.class));
}
 
Example #15
Source File: EhcacheSegmentTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private EhcacheSegmentFactory.EhcacheSegment<String, String> createTestSegmentWithAdvisorAndListener(final EvictionAdvisor<? super String, ? super String> evictionPredicate, EhcacheSegmentFactory.EhcacheSegment.EvictionListener<String, String> evictionListener) {
  try {
    HeuristicConfiguration configuration = new HeuristicConfiguration(1024 * 1024);
    SerializationProvider serializationProvider = new DefaultSerializationProvider(null);
    serializationProvider.start(providerContaining());
    PageSource pageSource = new UpfrontAllocatingPageSource(getBufferSource(), configuration.getMaximumSize(), configuration.getMaximumChunkSize(), configuration.getMinimumChunkSize());
    Serializer<String> keySerializer = serializationProvider.createKeySerializer(String.class, EhcacheSegmentTest.class.getClassLoader());
    Serializer<String> valueSerializer = serializationProvider.createValueSerializer(String.class, EhcacheSegmentTest.class.getClassLoader());
    Portability<String> keyPortability = new SerializerPortability<>(keySerializer);
    Portability<String> elementPortability = new SerializerPortability<>(valueSerializer);
    Factory<OffHeapBufferStorageEngine<String, String>> storageEngineFactory = OffHeapBufferStorageEngine.createFactory(PointerSize.INT, pageSource, configuration.getInitialSegmentTableSize(), keyPortability, elementPortability, false, true);
    SwitchableEvictionAdvisor<String, String> wrappedEvictionAdvisor = new SwitchableEvictionAdvisor<String, String>() {

      private volatile boolean enabled = true;

      @Override
      public boolean adviseAgainstEviction(String key, String value) {
        return evictionPredicate.adviseAgainstEviction(key, value);
      }

      @Override
      public boolean isSwitchedOn() {
        return enabled;
      }

      @Override
      public void setSwitchedOn(boolean switchedOn) {
        this.enabled = switchedOn;
      }
    };
    return new EhcacheSegmentFactory.EhcacheSegment<>(pageSource, storageEngineFactory.newInstance(), 1, wrappedEvictionAdvisor, evictionListener);
  } catch (UnsupportedTypeException e) {
    throw new AssertionError(e);
  }
}
 
Example #16
Source File: DefaultSerializationProviderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateKeySerializerWithActualInstanceInServiceConfig() throws Exception {
  DefaultSerializationProvider provider = new DefaultSerializationProvider(null);
  @SuppressWarnings("unchecked")
  TestSerializer<String> serializer = mock(TestSerializer.class);
  DefaultSerializerConfiguration<String> config = new DefaultSerializerConfiguration<>(serializer, DefaultSerializerConfiguration.Type.KEY);
  Serializer<?> created = provider.createKeySerializer(TestSerializer.class, getSystemClassLoader(), config);
  assertSame(serializer, created);
}
 
Example #17
Source File: RemoveOperation.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer encode(final Serializer<K> keySerializer, final Serializer<V> valueSerializer) {
  ByteBuffer keyBuf = keySerializer.serialize(key);

  int size = BYTE_SIZE_BYTES +   // Operation type
             LONG_SIZE_BYTES +   // Size of expiration time stamp
             keyBuf.remaining();   // the key payload itself

  ByteBuffer buffer = ByteBuffer.allocate(size);
  buffer.put(getOpCode().getValue());
  buffer.putLong(this.timeStamp);
  buffer.put(keyBuf);
  buffer.flip();
  return buffer;
}
 
Example #18
Source File: DefaultSerializationProviderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransientLegacySerializer() throws Exception {
  expectedException.expect(RuntimeException.class);
  expectedException.expectMessage("does not have a constructor that takes in a ClassLoader.");
  DefaultSerializationProvider provider = new DefaultSerializationProvider(null);
  provider.start(providerContaining());

  @SuppressWarnings("unchecked")
  Class<Serializer<Object>> serializerClass = (Class) LegacySerializer.class;
  DefaultSerializerConfiguration<Object> configuration = new DefaultSerializerConfiguration<>(serializerClass, DefaultSerializerConfiguration.Type.VALUE);
  provider.createValueSerializer(Object.class, ClassLoader.getSystemClassLoader(), configuration);
}
 
Example #19
Source File: DefaultSerializationProvider.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private static <T> Serializer<T> getUserProvidedSerializer(DefaultSerializerConfiguration<T> conf) {
  if(conf != null) {
    Serializer<T> instance = conf.getInstance();
    if(instance != null) {
      return instance;
    }
  }
  return null;
}
 
Example #20
Source File: SoftLock.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
protected SoftLock<V> copyAfterDeserialization(Serializer<V> valueSerializer, SoftLock<V> serializedSoftLock) throws ClassNotFoundException {
  V oldValue = null;
  if (serializedSoftLock.oldValueSerialized != null) {
    oldValue = valueSerializer.read(ByteBuffer.wrap(serializedSoftLock.oldValueSerialized));
  }
  XAValueHolder<V> newValueHolder = null;
  if (this.newValueHolder != null) {
    newValueHolder = this.newValueHolder.copyAfterDeserialization(valueSerializer);
  }
  return new SoftLock<>(transactionId, oldValue, newValueHolder);
}
 
Example #21
Source File: OffHeapDiskStoreTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Override
protected OffHeapDiskStore<String, byte[]> createAndInitStore(TimeSource timeSource, ExpiryPolicy<? super String, ? super byte[]> expiry, EvictionAdvisor<? super String, ? super byte[]> evictionAdvisor) {
  try {
    SerializationProvider serializationProvider = new DefaultSerializationProvider(null);
    serializationProvider.start(providerContaining(diskResourceService));
    ClassLoader classLoader = getClass().getClassLoader();
    Serializer<String> keySerializer = serializationProvider.createKeySerializer(String.class, classLoader);
    Serializer<byte[]> valueSerializer = serializationProvider.createValueSerializer(byte[].class, classLoader);
    StoreConfigurationImpl<String, byte[]> storeConfiguration = new StoreConfigurationImpl<>(String.class, byte[].class,
      evictionAdvisor, getClass().getClassLoader(), expiry, null, 0, true, keySerializer, valueSerializer, null, false);
    OffHeapDiskStore<String, byte[]> offHeapStore = new OffHeapDiskStore<String, byte[]>(
      getPersistenceContext(),
      new OnDemandExecutionService(), null, DEFAULT_WRITER_CONCURRENCY, DEFAULT_DISK_SEGMENTS,
      storeConfiguration, timeSource,
      new TestStoreEventDispatcher<>(),
      MB.toBytes(1), new DefaultStatisticsService()) {
      @Override
      protected OffHeapValueHolderPortability<byte[]> createValuePortability(Serializer<byte[]> serializer) {
        return new AssertingOffHeapValueHolderPortability<>(serializer);
      }
    };
    OffHeapDiskStore.Provider.init(offHeapStore);
    return offHeapStore;
  } catch (UnsupportedTypeException e) {
    throw new AssertionError(e);
  }
}
 
Example #22
Source File: CacheConfigurationBuilderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithoutKeySerializer() throws Exception {
  CacheConfiguration<Object, Object> cacheConfiguration = newCacheConfigurationBuilder(Object.class, Object.class, heap(10))
    .withKeySerializer(MockitoUtil.<Serializer<Object>>mock(Serializer.class))
    .withDefaultKeySerializer()
    .build();

  assertThat(cacheConfiguration.getServiceConfigurations(), not(hasItem(instanceOf(DefaultSerializerConfiguration.class))));
}
 
Example #23
Source File: DefaultSerializationProviderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultCharSerializer() throws Exception {
  DefaultSerializationProvider provider = getStartedProvider();
  Serializer<Character> keySerializer = provider.createKeySerializer(Character.class, getSystemClassLoader());
  assertThat(keySerializer, instanceOf(CharSerializer.class));

  keySerializer = provider.createKeySerializer(Character.class, getSystemClassLoader(), getPersistenceSpaceIdentifierMock());
  assertThat(keySerializer, instanceOf(CharSerializer.class));
}
 
Example #24
Source File: SerializingCopier.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new copier that will using the provided {@link Serializer}.
 *
 * @param serializer the serializer to use
 */
public SerializingCopier(Serializer<T> serializer) {
  if (serializer == null) {
    throw new NullPointerException("A " + SerializingCopier.class.getName() + " instance requires a "
                                   + Serializer.class.getName() + " instance to copy!");
  }
  this.serializer = serializer;
}
 
Example #25
Source File: DefaultSerializationProviderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultFloatSerializer() throws Exception {
  DefaultSerializationProvider provider = getStartedProvider();
  Serializer<Float> keySerializer = provider.createKeySerializer(Float.class, getSystemClassLoader());
  assertThat(keySerializer, instanceOf(FloatSerializer.class));

  keySerializer = provider.createKeySerializer(Float.class, getSystemClassLoader(), getPersistenceSpaceIdentifierMock());
  assertThat(keySerializer, instanceOf(FloatSerializer.class));
}
 
Example #26
Source File: DefaultSerializationProvider.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
public DefaultSerializationProvider(DefaultSerializationProviderConfiguration configuration) {
  if (configuration != null) {
    this.serializers = new LinkedHashMap<>(configuration.getDefaultSerializers());
  } else {
    this.serializers = new LinkedHashMap<>(Collections.<Class<?>, Class<? extends Serializer<?>>>emptyMap());
  }
}
 
Example #27
Source File: DefaultCopyProviderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testUserProvidedCloseableCopierInstanceDoesNotCloseOnRelease() throws Exception {
  DefaultCopyProvider copyProvider = new DefaultCopyProvider(null);
  TestCloseableCopier<Long> testCloseableCopier = new TestCloseableCopier<>();
  DefaultCopierConfiguration<Long> config = new DefaultCopierConfiguration<>(testCloseableCopier, DefaultCopierConfiguration.Type.KEY);

  @SuppressWarnings("unchecked")
  Serializer<Long> serializer = mock(Serializer.class);
  assertThat(copyProvider.createKeyCopier(Long.class, serializer, config), sameInstance((Copier)testCloseableCopier));

  copyProvider.releaseCopier(testCloseableCopier);

  assertFalse(testCloseableCopier.isInvoked());

}
 
Example #28
Source File: DefaultSerializationProvider.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Serializer<T> createValueSerializer(Class<T> clazz, ClassLoader classLoader, ServiceConfiguration<?, ?>... configs) throws UnsupportedTypeException {
  DefaultSerializerConfiguration<T> configuration = find(DefaultSerializerConfiguration.Type.VALUE, configs);
  Serializer<T> serializer = getUserProvidedSerializer(configuration);
  if (serializer == null) {
    serializer = createSerializer(clazz, classLoader, configuration, configs);
    instantiated.add(serializer);
  }
  updateProvidedInstanceCounts(serializer);
  return serializer;
}
 
Example #29
Source File: DefaultSerializationProviderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultStringSerializer() throws Exception {
  DefaultSerializationProvider provider = getStartedProvider();
  Serializer<String> keySerializer = provider.createKeySerializer(String.class, getSystemClassLoader());
  assertThat(keySerializer, instanceOf(StringSerializer.class));

  keySerializer = provider.createKeySerializer(String.class, getSystemClassLoader(), getPersistenceSpaceIdentifierMock());
  assertThat(keySerializer, instanceOf(StringSerializer.class));
}
 
Example #30
Source File: DefaultSerializationProviderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransientMinimalStatefulSerializer() throws Exception {
  DefaultSerializationProvider provider = new DefaultSerializationProvider(null);
  provider.start(providerContaining());

  MinimalStatefulSerializer.baseConstructorInvoked = false;
  @SuppressWarnings("unchecked")
  Class<Serializer<Object>> serializerClass = (Class) MinimalStatefulSerializer.class;
  DefaultSerializerConfiguration<Object> configuration = new DefaultSerializerConfiguration<>(serializerClass, DefaultSerializerConfiguration.Type.VALUE);
  Serializer<Object> valueSerializer =
    provider.createValueSerializer(Object.class, ClassLoader.getSystemClassLoader(), configuration);
  assertThat(valueSerializer, instanceOf(MinimalStatefulSerializer.class));
  assertThat(MinimalStatefulSerializer.baseConstructorInvoked, is(true));
}