org.ehcache.impl.internal.spi.copy.DefaultCopyProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ehcache-impl Show documentation
Show all versions of ehcache-impl Show documentation
The implementation module of Ehcache 3
/*
* 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.impl.internal.spi.copy;
import org.ehcache.core.spi.service.ServiceUtils;
import org.ehcache.impl.config.copy.DefaultCopierConfiguration;
import org.ehcache.impl.config.copy.DefaultCopierConfiguration.Type;
import org.ehcache.impl.config.copy.DefaultCopyProviderConfiguration;
import org.ehcache.impl.internal.classes.ClassInstanceConfiguration;
import org.ehcache.impl.internal.classes.ClassInstanceProvider;
import org.ehcache.impl.copy.IdentityCopier;
import org.ehcache.impl.copy.SerializingCopier;
import org.ehcache.spi.copy.Copier;
import org.ehcache.spi.copy.CopyProvider;
import org.ehcache.spi.serialization.Serializer;
import org.ehcache.spi.service.ServiceConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
/**
* @author Albin Suresh
*/
public class DefaultCopyProvider extends ClassInstanceProvider, Copier>> implements CopyProvider {
private static final Logger LOG = LoggerFactory.getLogger(DefaultCopyProvider.class);
@SuppressWarnings("unchecked")
public DefaultCopyProvider(DefaultCopyProviderConfiguration configuration) {
super(configuration, (Class) DefaultCopierConfiguration.class);
}
@Override
public Copier createKeyCopier(final Class clazz, Serializer serializer, ServiceConfiguration>... configs) {
return createCopier(Type.KEY, clazz, serializer, configs);
}
@Override
public Copier createValueCopier(final Class clazz, Serializer serializer, ServiceConfiguration>... configs) {
return createCopier(Type.VALUE, clazz, serializer, configs);
}
@Override
public void releaseCopier(Copier> copier) throws Exception {
if (!(copier instanceof SerializingCopier)) {
releaseInstance(copier);
}
}
private Copier createCopier(Type type, Class clazz,
Serializer serializer, ServiceConfiguration>... configs) {
DefaultCopierConfiguration conf = find(type, configs);
Copier copier;
final ClassInstanceConfiguration> preConfigured = preconfigured.get(clazz);
if (conf != null && conf.getClazz().isAssignableFrom(SerializingCopier.class)) {
if (serializer == null) {
throw new IllegalStateException("No Serializer configured for type '" + clazz.getName()
+ "' which doesn't implement java.io.Serializable");
}
copier = new SerializingCopier<>(serializer);
} else if (conf == null && preConfigured != null && preConfigured.getClazz().isAssignableFrom(SerializingCopier.class)) {
if (serializer == null) {
throw new IllegalStateException("No Serializer configured for type '" + clazz.getName()
+ "' which doesn't implement java.io.Serializable");
}
copier = new SerializingCopier<>(serializer);
} else {
copier = createCopier(clazz, conf, type);
}
LOG.debug("Copier for <{}> : {}", clazz.getName(), copier);
return copier;
}
private Copier createCopier(Class clazz, DefaultCopierConfiguration config, Type type) {
@SuppressWarnings("unchecked")
Copier copier = (Copier) newInstance(clazz, config);
if (copier == null) {
@SuppressWarnings("unchecked")
Copier defaultInstance = (Copier) newInstance(clazz, new DefaultCopierConfiguration(IdentityCopier.class, type));
copier = defaultInstance;
}
return copier;
}
@SuppressWarnings("unchecked")
private static DefaultCopierConfiguration find(Type type, ServiceConfiguration>... serviceConfigurations) {
DefaultCopierConfiguration result = null;
Collection copierConfigurations =
ServiceUtils.findAmongst(DefaultCopierConfiguration.class, (Object[])serviceConfigurations);
for (DefaultCopierConfiguration copierConfiguration : copierConfigurations) {
if (copierConfiguration.getType() == type) {
if (result != null) {
throw new IllegalArgumentException("Duplicate " + type + " copier : " + copierConfiguration);
}
result = copierConfiguration;
}
}
return result;
}
}