org.ehcache.jcache.JCacheConfiguration Maven / Gradle / Ivy
/**
* Copyright 2003-2010 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.jcache;
import net.sf.ehcache.config.CacheConfiguration;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import javax.cache.configuration.CacheEntryListenerConfiguration;
import javax.cache.configuration.CompleteConfiguration;
import javax.cache.configuration.Configuration;
import javax.cache.configuration.Factory;
import javax.cache.expiry.Duration;
import javax.cache.expiry.EternalExpiryPolicy;
import javax.cache.expiry.ExpiryPolicy;
import javax.cache.integration.CacheLoader;
import javax.cache.integration.CacheWriter;
/**
* Configuration for a JSR107 Cache
*
* @author Ryan Gardner
* @since 1.4.0-beta1
*/
public class JCacheConfiguration implements javax.cache.configuration.CompleteConfiguration {
private final Class keyType;
private final Class valueType;
private final ExpiryPolicy expiryPolicy;
private final ConcurrentMap, JCacheListenerAdapter> cacheEntryListenerConfigurations = new ConcurrentHashMap, JCacheListenerAdapter>();
private final boolean storeByValue;
private final boolean readThrough;
private final boolean writeThrough;
private final Factory> cacheLoaderFactory;
private final Factory> cacheWristerFactory;
private final Factory expiryPolicyFactory;
private final Set> initialCacheEntryListenerConfigurations;
private boolean statisticsEnabled;
private boolean managementEnabled;
public JCacheConfiguration(final CacheConfiguration cacheConfiguration, final Configuration configuration, final Class keyType, final Class valueType) {
this.keyType = keyType;
this.valueType = valueType;
if (configuration instanceof CompleteConfiguration) {
CompleteConfiguration cConfiguration = (CompleteConfiguration) configuration;
Factory expiryPolicyFactory = cConfiguration.getExpiryPolicyFactory();
this.expiryPolicy = expiryPolicyFactory.create();
storeByValue = configuration.isStoreByValue();
readThrough = cConfiguration.isReadThrough();
writeThrough = cConfiguration.isWriteThrough();
statisticsEnabled = cConfiguration.isStatisticsEnabled();
managementEnabled = cConfiguration.isManagementEnabled();
cacheLoaderFactory = cConfiguration.getCacheLoaderFactory();
cacheWristerFactory = cConfiguration.getCacheWriterFactory();
this.expiryPolicyFactory = cConfiguration.getExpiryPolicyFactory();
final HashSet> set = new HashSet>();
for (CacheEntryListenerConfiguration kvCacheEntryListenerConfiguration : cConfiguration.getCacheEntryListenerConfigurations()) {
set.add(kvCacheEntryListenerConfiguration);
}
initialCacheEntryListenerConfigurations = Collections.unmodifiableSet(set);
} else {
if (cacheConfiguration == null) {
expiryPolicyFactory = EternalExpiryPolicy.factoryOf();
expiryPolicy = expiryPolicyFactory.create();
storeByValue = true;
readThrough = false;
writeThrough = false;
cacheLoaderFactory = null;
cacheWristerFactory = null;
initialCacheEntryListenerConfigurations = new HashSet>();
} else {
expiryPolicyFactory = null;
expiryPolicy = new ExpiryPolicy() {
@Override
public Duration getExpiryForCreation() {
return new Duration(TimeUnit.SECONDS, cacheConfiguration.getTimeToLiveSeconds());
}
@Override
public Duration getExpiryForAccess() {
return new Duration(TimeUnit.SECONDS, cacheConfiguration.getTimeToLiveSeconds());
}
@Override
public Duration getExpiryForUpdate() {
return getExpiryForCreation();
}
};
storeByValue = false;
readThrough = false;
writeThrough = false;
cacheLoaderFactory = null;
cacheWristerFactory = null;
initialCacheEntryListenerConfigurations = new HashSet>();
}
}
}
public JCacheConfiguration(final CacheConfiguration cacheConfiguration) {
this(cacheConfiguration, null, null, null);
}
public JCacheConfiguration(final Configuration configuration) {
this(null, configuration, configuration.getKeyType(), configuration.getValueType());
}
@Override
public Class getKeyType() {
return keyType == null ? (Class)Object.class : keyType;
}
@Override
public Class getValueType() {
return valueType == null ? (Class)Object.class : valueType;
}
@Override
public boolean isStoreByValue() {
return storeByValue;
}
@Override
public boolean isReadThrough() {
return readThrough;
}
@Override
public boolean isWriteThrough() {
return writeThrough;
}
@Override
public boolean isStatisticsEnabled() {
return statisticsEnabled;
}
@Override
public boolean isManagementEnabled() {
return managementEnabled;
}
@Override
public Iterable> getCacheEntryListenerConfigurations() {
return cacheEntryListenerConfigurations.keySet();
}
@Override
public Factory> getCacheLoaderFactory() {
return cacheLoaderFactory;
}
@Override
public Factory> getCacheWriterFactory() {
return cacheWristerFactory;
}
@Override
public Factory getExpiryPolicyFactory() {
return expiryPolicyFactory;
}
public ExpiryPolicy getExpiryPolicy() {
return expiryPolicy;
}
public boolean addCacheEntryListenerConfiguration(final CacheEntryListenerConfiguration cacheEntryListenerConfiguration, final JCacheListenerAdapter cacheEventListener) {
return cacheEntryListenerConfigurations.putIfAbsent(cacheEntryListenerConfiguration, cacheEventListener) == null;
}
public JCacheListenerAdapter removeCacheEntryListenerConfiguration(final CacheEntryListenerConfiguration cacheEntryListenerConfiguration) {
return cacheEntryListenerConfigurations.remove(cacheEntryListenerConfiguration);
}
void setStatisticsEnabled(final boolean statisticsEnabled) {
this.statisticsEnabled = statisticsEnabled;
}
void setManagementEnabled(final boolean managementEnabled) {
this.managementEnabled = managementEnabled;
}
public Iterable> getInitialCacheEntryListenerConfigurations() {
return initialCacheEntryListenerConfigurations;
}
public boolean overrideDefaultExpiry() {
return expiryPolicyFactory != null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy