Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.xml;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.Configuration;
import org.ehcache.config.EvictionAdvisor;
import org.ehcache.config.ResourcePool;
import org.ehcache.config.ResourcePools;
import org.ehcache.config.Builder;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheEventListenerConfigurationBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.builders.WriteBehindConfigurationBuilder;
import org.ehcache.config.builders.WriteBehindConfigurationBuilder.BatchedWriteBehindConfigurationBuilder;
import org.ehcache.core.internal.util.ClassLoading;
import org.ehcache.event.CacheEventListener;
import org.ehcache.event.EventFiring;
import org.ehcache.event.EventOrdering;
import org.ehcache.expiry.Duration;
import org.ehcache.expiry.Expirations;
import org.ehcache.expiry.Expiry;
import org.ehcache.impl.config.copy.DefaultCopierConfiguration;
import org.ehcache.impl.config.copy.DefaultCopyProviderConfiguration;
import org.ehcache.impl.config.event.CacheEventDispatcherFactoryConfiguration;
import org.ehcache.impl.config.event.DefaultCacheEventDispatcherConfiguration;
import org.ehcache.impl.config.executor.PooledExecutionServiceConfiguration;
import org.ehcache.impl.config.loaderwriter.DefaultCacheLoaderWriterConfiguration;
import org.ehcache.impl.config.loaderwriter.writebehind.WriteBehindProviderConfiguration;
import org.ehcache.impl.config.persistence.CacheManagerPersistenceConfiguration;
import org.ehcache.impl.config.serializer.DefaultSerializationProviderConfiguration;
import org.ehcache.impl.config.serializer.DefaultSerializerConfiguration;
import org.ehcache.impl.config.store.heap.DefaultSizeOfEngineConfiguration;
import org.ehcache.impl.config.store.heap.DefaultSizeOfEngineProviderConfiguration;
import org.ehcache.impl.config.store.disk.OffHeapDiskStoreConfiguration;
import org.ehcache.impl.config.store.disk.OffHeapDiskStoreProviderConfiguration;
import org.ehcache.spi.copy.Copier;
import org.ehcache.spi.loaderwriter.CacheLoaderWriter;
import org.ehcache.spi.serialization.Serializer;
import org.ehcache.spi.service.ServiceConfiguration;
import org.ehcache.spi.service.ServiceCreationConfiguration;
import org.ehcache.xml.ConfigurationParser.Batching;
import org.ehcache.xml.ConfigurationParser.WriteBehind;
import org.ehcache.xml.exceptions.XmlConfigurationException;
import org.ehcache.xml.model.CopierType;
import org.ehcache.xml.model.EventType;
import org.ehcache.xml.model.SerializerType;
import org.ehcache.xml.model.ServiceType;
import org.ehcache.xml.model.ThreadPoolReferenceType;
import org.ehcache.xml.model.ThreadPoolsType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.ehcache.config.builders.CacheConfigurationBuilder.newCacheConfigurationBuilder;
import static org.ehcache.config.builders.ResourcePoolsBuilder.newResourcePoolsBuilder;
/**
* Exposes {@link org.ehcache.config.Configuration} and {@link CacheConfigurationBuilder} expressed
* in a XML file that obeys the core Ehcache schema.
*
* Instances of this class are not thread-safe.
*/
public class XmlConfiguration implements Configuration {
private static final Logger LOGGER = LoggerFactory.getLogger(XmlConfiguration.class);
private final URL xml;
private final ClassLoader classLoader;
private final Map cacheClassLoaders;
private final Collection> serviceConfigurations = new ArrayList>();
private final Map> cacheConfigurations = new HashMap>();
private final Map templates = new HashMap();
/**
* Constructs an instance of XmlConfiguration mapping to the XML file located at {@code url}
*
* Parses the XML file at the {@code url} provided.
*
* @param url URL pointing to the XML file's location
*
* @throws XmlConfigurationException if anything went wrong parsing the XML
*/
public XmlConfiguration(URL url)
throws XmlConfigurationException {
this(url, ClassLoading.getDefaultClassLoader());
}
/**
* Constructs an instance of XmlConfiguration mapping to the XML file located at {@code url} and using the provided
* {@code classLoader} to load user types (e.g. key and value Class instances).
*
* Parses the XML file at the {@code url} provided.
*
* @param url URL pointing to the XML file's location
* @param classLoader ClassLoader to use to load user types.
*
* @throws XmlConfigurationException if anything went wrong parsing the XML
*/
public XmlConfiguration(URL url, final ClassLoader classLoader)
throws XmlConfigurationException {
this(url, classLoader, Collections.emptyMap());
}
/**
* Constructs an instance of XmlConfiguration mapping to the XML file located at {@code url} and using the provided
* {@code classLoader} to load user types (e.g. key and value Class instances). The {@code cacheClassLoaders} will
* let you specify a different {@link java.lang.ClassLoader} to use for each {@link org.ehcache.Cache} managed by
* the {@link org.ehcache.CacheManager} configured using this {@link org.ehcache.xml.XmlConfiguration}
*
* Parses the XML file at the {@code url} provided.
*
* @param url URL pointing to the XML file's location
* @param classLoader ClassLoader to use to load user types.
* @param cacheClassLoaders the map with mappings between cache names and the corresponding class loaders
*
* @throws XmlConfigurationException if anything went wrong parsing the XML
*/
public XmlConfiguration(URL url, final ClassLoader classLoader, final Map cacheClassLoaders)
throws XmlConfigurationException {
if(url == null) {
throw new NullPointerException("The url can not be null");
}
if(classLoader == null) {
throw new NullPointerException("The classLoader can not be null");
}
if(cacheClassLoaders == null) {
throw new NullPointerException("The cacheClassLoaders map can not be null");
}
this.xml = url;
this.classLoader = classLoader;
this.cacheClassLoaders = new HashMap(cacheClassLoaders);
try {
parseConfiguration();
} catch (XmlConfigurationException e) {
throw e;
} catch (Exception e) {
throw new XmlConfigurationException("Error parsing XML configuration at " + url, e);
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void parseConfiguration()
throws ClassNotFoundException, IOException, SAXException, InstantiationException, IllegalAccessException, JAXBException, ParserConfigurationException {
LOGGER.info("Loading Ehcache XML configuration from {}.", xml.getPath());
ConfigurationParser configurationParser = new ConfigurationParser(xml.toExternalForm());
final ArrayList> serviceConfigs = new ArrayList>();
for (ServiceType serviceType : configurationParser.getServiceElements()) {
final ServiceCreationConfiguration> serviceConfiguration = configurationParser.parseExtension(serviceType.getServiceCreationConfiguration());
serviceConfigs.add(serviceConfiguration);
}
if (configurationParser.getDefaultSerializers() != null) {
DefaultSerializationProviderConfiguration configuration = new DefaultSerializationProviderConfiguration();
for (SerializerType.Serializer serializer : configurationParser.getDefaultSerializers().getSerializer()) {
configuration.addSerializerFor(getClassForName(serializer.getType(), classLoader), (Class) getClassForName(serializer.getValue(), classLoader));
}
serviceConfigs.add(configuration);
}
if (configurationParser.getDefaultCopiers() != null) {
DefaultCopyProviderConfiguration configuration = new DefaultCopyProviderConfiguration();
for (CopierType.Copier copier : configurationParser.getDefaultCopiers().getCopier()) {
configuration.addCopierFor(getClassForName(copier.getType(), classLoader), (Class)getClassForName(copier.getValue(), classLoader));
}
serviceConfigs.add(configuration);
}
if (configurationParser.getHeapStore() != null) {
DefaultSizeOfEngineProviderConfiguration configuration = new DefaultSizeOfEngineProviderConfiguration(
configurationParser.getHeapStore().getMaxObjectSize(), configurationParser.getHeapStore().getUnit(),
configurationParser.getHeapStore().getMaxObjectGraphSize());
serviceConfigs.add(configuration);
}
if (configurationParser.getPersistence() != null) {
serviceConfigs.add(new CacheManagerPersistenceConfiguration(new File(configurationParser.getPersistence().getDirectory())));
}
if (configurationParser.getThreadPools() != null) {
PooledExecutionServiceConfiguration poolsConfiguration = new PooledExecutionServiceConfiguration();
for (ThreadPoolsType.ThreadPool pool : configurationParser.getThreadPools().getThreadPool()) {
if (pool.isDefault()) {
poolsConfiguration.addDefaultPool(pool.getAlias(), pool.getMinSize().intValue(), pool.getMaxSize().intValue());
} else {
poolsConfiguration.addPool(pool.getAlias(), pool.getMinSize().intValue(), pool.getMaxSize().intValue());
}
}
serviceConfigs.add(poolsConfiguration);
}
if (configurationParser.getEventDispatch() != null) {
ThreadPoolReferenceType eventDispatchThreading = configurationParser.getEventDispatch();
serviceConfigs.add(new CacheEventDispatcherFactoryConfiguration(eventDispatchThreading.getThreadPool()));
}
if (configurationParser.getWriteBehind() != null) {
ThreadPoolReferenceType writeBehindThreading = configurationParser.getWriteBehind();
serviceConfigs.add(new WriteBehindProviderConfiguration(writeBehindThreading.getThreadPool()));
}
if (configurationParser.getDiskStore() != null) {
ThreadPoolReferenceType diskStoreThreading = configurationParser.getDiskStore();
serviceConfigs.add(new OffHeapDiskStoreProviderConfiguration(diskStoreThreading.getThreadPool()));
}
for (ServiceCreationConfiguration> serviceConfiguration : Collections.unmodifiableList(serviceConfigs)) {
serviceConfigurations.add(serviceConfiguration);
}
for (ConfigurationParser.CacheDefinition cacheDefinition : configurationParser.getCacheElements()) {
String alias = cacheDefinition.id();
ClassLoader cacheClassLoader = cacheClassLoaders.get(alias);
boolean classLoaderConfigured = false;
if (cacheClassLoader != null) {
classLoaderConfigured = true;
}
if (cacheClassLoader == null) {
if (classLoader != null) {
cacheClassLoader = classLoader;
} else {
cacheClassLoader = ClassLoading.getDefaultClassLoader();
}
}
Class keyType = getClassForName(cacheDefinition.keyType(), cacheClassLoader);
Class valueType = getClassForName(cacheDefinition.valueType(), cacheClassLoader);
ResourcePoolsBuilder resourcePoolsBuilder = newResourcePoolsBuilder();
for (ResourcePool resourcePool : cacheDefinition.resourcePools()) {
resourcePoolsBuilder = resourcePoolsBuilder.with(resourcePool);
}
CacheConfigurationBuilder