org.ehcache.core.EhcacheRuntimeConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ehcache Show documentation
Show all versions of ehcache Show documentation
End-user ehcache3 jar artifact
/*
* 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.core;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.CacheRuntimeConfiguration;
import org.ehcache.config.EvictionAdvisor;
import org.ehcache.config.ResourcePools;
import org.ehcache.core.config.ExpiryUtils;
import org.ehcache.core.internal.events.EventListenerWrapper;
import org.ehcache.event.CacheEventListener;
import org.ehcache.event.EventFiring;
import org.ehcache.event.EventOrdering;
import org.ehcache.event.EventType;
import org.ehcache.expiry.ExpiryPolicy;
import org.ehcache.spi.service.ServiceConfiguration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
class EhcacheRuntimeConfiguration implements CacheRuntimeConfiguration, InternalRuntimeConfiguration, HumanReadable {
private final Collection> serviceConfigurations;
private final CacheConfiguration config;
private final Class keyType;
private final Class valueType;
private final EvictionAdvisor evictionAdvisor;
private final ClassLoader classLoader;
private final ExpiryPolicy expiry;
private volatile ResourcePools resourcePools;
private final List cacheConfigurationListenerList
= new CopyOnWriteArrayList<>();
EhcacheRuntimeConfiguration(CacheConfiguration config) {
this.config = config;
this.serviceConfigurations = copy(config.getServiceConfigurations());
this.keyType = config.getKeyType();
this.valueType = config.getValueType();
this.evictionAdvisor = config.getEvictionAdvisor();
this.classLoader = config.getClassLoader();
this.expiry = config.getExpiryPolicy();
this.resourcePools = config.getResourcePools();
}
@Override
public synchronized void updateResourcePools(ResourcePools pools) {
if(pools == null) {
throw new NullPointerException("Pools to be updated cannot be null");
}
ResourcePools updatedResourcePools = config.getResourcePools().validateAndMerge(pools);
fireCacheConfigurationChange(CacheConfigurationProperty.UPDATE_SIZE, config.getResourcePools(), updatedResourcePools);
this.resourcePools = updatedResourcePools;
}
@Override
public Collection> getServiceConfigurations() {
return this.serviceConfigurations;
}
@Override
public Class getKeyType() {
return this.keyType;
}
@Override
public Class getValueType() {
return this.valueType;
}
@Override
public EvictionAdvisor getEvictionAdvisor() {
return this.evictionAdvisor;
}
@Override
public ClassLoader getClassLoader() {
return this.classLoader;
}
@SuppressWarnings("deprecation")
@Override
public org.ehcache.expiry.Expiry getExpiry() {
return ExpiryUtils.convertToExpiry(expiry);
}
@Override
public ExpiryPolicy getExpiryPolicy() {
return expiry;
}
@Override
public ResourcePools getResourcePools() {
return this.resourcePools;
}
@Override
public boolean addCacheConfigurationListener(List listeners) {
return this.cacheConfigurationListenerList.addAll(listeners);
}
@Override
public boolean removeCacheConfigurationListener(CacheConfigurationChangeListener listener) {
return this.cacheConfigurationListenerList.remove(listener);
}
@Override
public synchronized void deregisterCacheEventListener(CacheEventListener listener) {
fireCacheConfigurationChange(CacheConfigurationProperty.REMOVE_LISTENER, listener, listener);
}
@Override
public synchronized void registerCacheEventListener(CacheEventListener listener, EventOrdering ordering,
EventFiring firing, Set forEventTypes) {
EventListenerWrapper listenerWrapper = new EventListenerWrapper<>(listener, firing, ordering, EnumSet.copyOf(forEventTypes));
fireCacheConfigurationChange(CacheConfigurationProperty.ADD_LISTENER, listenerWrapper, listenerWrapper);
}
@Override
public void registerCacheEventListener(CacheEventListener listener, EventOrdering ordering, EventFiring firing, EventType eventType, EventType... eventTypes) {
EventListenerWrapper listenerWrapper = new EventListenerWrapper<>(listener, firing, ordering, EnumSet.of(eventType, eventTypes));
fireCacheConfigurationChange(CacheConfigurationProperty.ADD_LISTENER, listenerWrapper, listenerWrapper);
}
private Collection copy(Collection collection) {
if (collection == null) {
return null;
}
return Collections.unmodifiableCollection(new ArrayList<>(collection));
}
@SuppressWarnings("unchecked")
private void fireCacheConfigurationChange(CacheConfigurationProperty prop, final T oldValue, final T newValue) {
if ((oldValue != null && !oldValue.equals(newValue)) || newValue != null) {
for (CacheConfigurationChangeListener cacheConfigurationListener : cacheConfigurationListenerList) {
cacheConfigurationListener.cacheConfigurationChange(new CacheConfigurationChangeEvent(prop, oldValue, newValue));
}
}
}
@Override
public String readableString() {
StringBuilder serviceConfigurationsToStringBuilder = new StringBuilder();
for (ServiceConfiguration serviceConfiguration : serviceConfigurations) {
serviceConfigurationsToStringBuilder
.append("\n ")
.append("- ");
if(serviceConfiguration instanceof HumanReadable) {
serviceConfigurationsToStringBuilder
.append(((HumanReadable)serviceConfiguration).readableString())
.append("\n");
} else {
serviceConfigurationsToStringBuilder
.append(serviceConfiguration.getClass().getName())
.append("\n");
}
}
if(serviceConfigurationsToStringBuilder.length() > 0) {
serviceConfigurationsToStringBuilder.deleteCharAt(serviceConfigurationsToStringBuilder.length() -1);
} else {
serviceConfigurationsToStringBuilder.append(" None");
}
String expiryPolicy;
if (ExpiryPolicy.NO_EXPIRY == expiry) {
expiryPolicy = "NoExpiryPolicy";
} else {
expiryPolicy = expiry.toString();
}
return
"keyType: " + keyType.getName() + "\n" +
"valueType: " + valueType.getName() + "\n" +
"serviceConfigurations:" + serviceConfigurationsToStringBuilder.toString().replace("\n", "\n ") + "\n" +
"evictionAdvisor: " + ((evictionAdvisor != null) ? evictionAdvisor.getClass().getName() : "None") + "\n" +
"expiry: " + expiryPolicy + "\n" +
"resourcePools: " + "\n " + ((resourcePools instanceof HumanReadable) ? ((HumanReadable)resourcePools).readableString() : "").replace("\n", "\n ");
}
}