/*
 * Copyright Terracotta, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ehcache.spi.serialization;

import org.ehcache.spi.service.Service;
import org.ehcache.spi.service.ServiceConfiguration;

A Service providing Serializer instances.

The CacheManager obtains an instance of this Service prior to creating any Cache instances. Before creating each Cache instance, the CacheManager calls the createKeySerializer(Class<Object>, ClassLoader, ServiceConfiguration<?>[]) and createValueSerializer(Class<Object>, ClassLoader, ServiceConfiguration<?>[]) methods to obtain Serializer instances for the Cache, either through explicit configuration or from CacheManager level configuration.

Some Cache configurations make serialization mandatory. If serialization is mandatory, failure to return a Serializer from a SerializationProvider results in a Cache initialization failure. For a Cache in which serialization is not mandatory, failing to return a Serializer will not cause Cache initialization failure.

/** * A {@link Service} providing {@link Serializer} instances. * <p> * The {@code CacheManager} {@link org.ehcache.spi.service.ServiceProvider obtains} an instance of this * {@code Service} prior to creating any {@code Cache} instances. Before creating each {@code Cache} * instance, the {@code CacheManager} calls the * {@link #createKeySerializer(Class, ClassLoader, ServiceConfiguration[])} and * {@link #createValueSerializer(Class, ClassLoader, ServiceConfiguration[])} methods to obtain * {@code Serializer} instances for the {@code Cache}, either through explicit configuration or from * {@code CacheManager} level configuration. * <p> * Some {@code Cache} configurations make serialization mandatory. If serialization is mandatory, * failure to return a {@code Serializer} from a {@code SerializationProvider} * results in a {@code Cache} initialization failure. For a {@code Cache} in which serialization * is not mandatory, failing to return a {@code Serializer} will not cause {@code Cache} initialization * failure. */
public interface SerializationProvider extends Service {
Creates a key Serializer with the given parameters.
Params:
  • clazz – the class of the type to serialize to/from
  • classLoader – the classloader used to load classes during deserialization; may be null
  • configs – the ServiceConfiguration instances available through the CacheManager
Type parameters:
  • <T> – the type serialized to serialize to/from
Throws:
Returns:a Serializer instance, possibly null
/** * Creates a key {@link Serializer} with the given parameters. * * @param clazz the class of the type to serialize to/from * @param classLoader the classloader used to load classes during deserialization; may be {@code null} * @param configs the {@link ServiceConfiguration} instances available through the * {@link org.ehcache.CacheManager CacheManager} * @param <T> the type serialized to serialize to/from * * @return a {@code Serializer} instance, possibly {@code null} * * @throws UnsupportedTypeException if a serializer cannot be created for the given type */
<T> Serializer<T> createKeySerializer(Class<T> clazz, ClassLoader classLoader, ServiceConfiguration<?>... configs) throws UnsupportedTypeException;
Creates a value Serializer with the given parameters.
Params:
  • clazz – the class of the type to serialize to/from
  • classLoader – the classloader used to load classes during deserialization; may be null
  • configs – the ServiceConfiguration instances available through the CacheManager
Type parameters:
  • <T> – the type serialized to serialize to/from
Throws:
Returns:a Serializer instance, possibly null
/** * Creates a value {@link Serializer} with the given parameters. * * @param clazz the class of the type to serialize to/from * @param classLoader the classloader used to load classes during deserialization; may be {@code null} * @param configs the {@link ServiceConfiguration} instances available through the * {@link org.ehcache.CacheManager CacheManager} * @param <T> the type serialized to serialize to/from * * @return a {@code Serializer} instance, possibly {@code null} * * @throws UnsupportedTypeException if a serializer cannot be created for the given type */
<T> Serializer<T> createValueSerializer(Class<T> clazz, ClassLoader classLoader, ServiceConfiguration<?>... configs) throws UnsupportedTypeException;
Releases the given Serializer instance. If the serializer is obtained from a SerializationProvider and implements Closeable, the close method is invoked.
Params:
  • serializer – the serializer to be released
Throws:
/** * Releases the given {@link Serializer} instance. * If the serializer is obtained from a {@code SerializationProvider} and implements * {@link java.io.Closeable Closeable}, the {@link java.io.Closeable#close() close} method is invoked. * * @param serializer the serializer to be released * * @throws Exception when the release fails */
void releaseSerializer(Serializer<?> serializer) throws Exception; }