package org.ehcache.core.config;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.CacheRuntimeConfiguration;
import org.ehcache.config.Configuration;
import org.ehcache.config.FluentConfigurationBuilder;
import org.ehcache.core.HumanReadable;
import org.ehcache.core.util.ClassLoading;
import org.ehcache.spi.service.ServiceCreationConfiguration;
import static java.util.Collections.unmodifiableCollection;
import static java.util.Collections.unmodifiableMap;
import static org.ehcache.core.config.CoreConfigurationBuilder.newConfigurationBuilder;
public final class DefaultConfiguration implements Configuration, HumanReadable {
private final ConcurrentMap<String,CacheConfiguration<?, ?>> caches;
private final Collection<ServiceCreationConfiguration<?, ?>> services;
private final ClassLoader classLoader;
public DefaultConfiguration(Configuration cfg) {
if (cfg.getClassLoader() == null) {
throw new NullPointerException();
}
this.caches = new ConcurrentHashMap<>(cfg.getCacheConfigurations());
this.services = unmodifiableCollection(cfg.getServiceCreationConfigurations());
this.classLoader = cfg.getClassLoader();
}
public DefaultConfiguration(ClassLoader classLoader, ServiceCreationConfiguration<?, ?>... services) {
this(emptyCacheMap(), classLoader, services);
}
public DefaultConfiguration(Map<String, CacheConfiguration<?, ?>> caches, ClassLoader classLoader, ServiceCreationConfiguration<?, ?>... services) {
this.services = unmodifiableCollection(Arrays.asList(services));
this.caches = new ConcurrentHashMap<>(caches);
this.classLoader = classLoader == null ? ClassLoading.getDefaultClassLoader() : classLoader;
}
@Override
public Map<String, CacheConfiguration<?, ?>> getCacheConfigurations() {
return unmodifiableMap(caches);
}
@Override
public Collection<ServiceCreationConfiguration<?, ?>> getServiceCreationConfigurations() {
return services;
}
@Override
public ClassLoader getClassLoader() {
return classLoader;
}
@Override
public FluentConfigurationBuilder<?> derive() {
return newConfigurationBuilder(this);
}
private static Map<String, CacheConfiguration<?, ?>> emptyCacheMap() {
return Collections.emptyMap();
}
public void addCacheConfiguration(final String alias, final CacheConfiguration<?, ?> config) {
if (caches.put(alias, config) != null) {
throw new IllegalStateException("Cache '" + alias + "' already present!");
}
}
public void removeCacheConfiguration(final String alias) {
caches.remove(alias);
}
public <K, V> void replaceCacheConfiguration(final String alias, final CacheConfiguration<K, V> config, final CacheRuntimeConfiguration<K, V> runtimeConfiguration) {
if (!caches.replace(alias, config, runtimeConfiguration)) {
throw new IllegalStateException("The expected configuration doesn't match!");
}
}
@Override
public String readableString() {
StringBuilder cachesToStringBuilder = new StringBuilder();
for (Map.Entry<String, CacheConfiguration<?, ?>> cacheConfigurationEntry : caches.entrySet()) {
if(cacheConfigurationEntry.getValue() instanceof HumanReadable) {
cachesToStringBuilder
.append(cacheConfigurationEntry.getKey())
.append(":\n ")
.append(((HumanReadable)cacheConfigurationEntry.getValue()).readableString().replace("\n","\n "))
.append("\n");
}
}
if(cachesToStringBuilder.length() > 0) {
cachesToStringBuilder.deleteCharAt(cachesToStringBuilder.length() -1);
}
StringBuilder serviceCreationConfigurationsToStringBuilder = new StringBuilder();
for (ServiceCreationConfiguration<?, ?> serviceCreationConfiguration : services) {
serviceCreationConfigurationsToStringBuilder.append("- ");
if(serviceCreationConfiguration instanceof HumanReadable) {
serviceCreationConfigurationsToStringBuilder
.append(((HumanReadable)serviceCreationConfiguration).readableString())
.append("\n");
} else {
serviceCreationConfigurationsToStringBuilder
.append(serviceCreationConfiguration.getClass().getName())
.append("\n");
}
}
if(serviceCreationConfigurationsToStringBuilder.length() > 0) {
serviceCreationConfigurationsToStringBuilder.deleteCharAt(serviceCreationConfigurationsToStringBuilder.length() -1);
}
return "caches:\n " + cachesToStringBuilder.toString().replace("\n","\n ") + "\n" +
"services: \n " + serviceCreationConfigurationsToStringBuilder.toString().replace("\n","\n ") ;
}
}