public class MockDynamicCacheConfig extends DynamicCacheConfig {
@SuppressWarnings("rawtypes")
@Override
public EhCacheCacheManager dynamicCacheManager() {
EhCacheCacheManager cacheManager = new EhCacheCacheManager();
Configuration cacheManagerConfig;
InputStream inputStream = null;
try {
CoreLogger.LOGGER.info("In cluster mode.");
inputStream = new ClassPathResource("ehcache-dist.xml").getInputStream();
cacheManagerConfig = ConfigurationFactory.parseConfiguration(inputStream);
FactoryConfiguration peerProviderConfig = new FactoryConfiguration();
peerProviderConfig.setClass(RMICacheManagerPeerProviderFactory.class.getName());
List<String> replicatedCacheNames = getReplicatedCacheNames(cacheManagerConfig);
Pair<NetworkUtils.IPPortPair, String> properties = createManualDiscoveryCacheProperties(replicatedCacheNames);
NetworkUtils.IPPortPair currentListener = properties.getFirst();
String peerProperty = properties.getSecond();
peerProviderConfig.setProperties(peerProperty);
cacheManagerConfig.addCacheManagerPeerProviderFactory(peerProviderConfig);
System.setProperty("java.rmi.server.hostname", currentListener.getFormattedIP());
FactoryConfiguration peerListenerConfig = new FactoryConfiguration();
peerListenerConfig.setClass(RMICacheManagerPeerListenerFactory.class.getName());
String peerListenerProperty = String.format("hostName=%s, port=%d, socketTimeoutMillis=1000",
currentListener.getFormattedIP(), currentListener.getPort());
peerListenerConfig.setProperties(peerListenerProperty);
cacheManagerConfig.addCacheManagerPeerListenerFactory(peerListenerConfig);
CoreLogger.LOGGER.info("clusterURLs:{}", peerListenerProperty);
cacheManagerConfig.setName("TestCluster");
CacheManager mgr = CacheManager.create(cacheManagerConfig);
cacheManager.setCacheManager(mgr);
} catch (IOException e) {
CoreLogger.LOGGER.error("Error while setting up cache", e);
} finally {
IOUtils.closeQuietly(inputStream);
}