Skip to content

Instantly share code, notes, and snippets.

@MikeN123
Last active December 17, 2015 13:39
Show Gist options
  • Save MikeN123/5618297 to your computer and use it in GitHub Desktop.
Save MikeN123/5618297 to your computer and use it in GitHub Desktop.
MySpringEmbeddedCacheManagerFactoryBean
package mypackage;
import org.infinispan.config.ConfigurationException;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.infinispan.configuration.parsing.ConfigurationBuilderHolder;
import org.infinispan.configuration.parsing.ParserRegistry;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.spring.provider.SpringEmbeddedCacheManagerFactoryBean;
import org.springframework.core.io.Resource;
import java.io.IOException;
import java.util.Map;
/**
* Workaround class to overcome the issue that it is impossible with Infinispan to use _both_ a config file as well as
* override certain options from the Spring config. This class will first apply the config file, and then apply the
* specified overrides to both the global config, default config as well as configured named caches.
*/
public class MySpringEmbeddedCacheManagerFactoryBean extends SpringEmbeddedCacheManagerFactoryBean {
private Resource configurationFileLocation;
@Override
protected EmbeddedCacheManager createBackingEmbeddedCacheManager() throws ConfigurationException, IOException {
EmbeddedCacheManager cm;
if (configurationFileLocation != null) {
ConfigurationBuilderHolder configurationBuilderHolder =
new ParserRegistry(Thread.currentThread().getContextClassLoader())
.parse(configurationFileLocation.getInputStream());
this.globalConfigurationOverrides
.applyOverridesTo(configurationBuilderHolder.getGlobalConfigurationBuilder());
this.configurationOverrides.applyOverridesTo(configurationBuilderHolder.getDefaultConfigurationBuilder());
for (Map.Entry<String, ConfigurationBuilder> entry : configurationBuilderHolder
.getNamedConfigurationBuilders().entrySet()) {
this.configurationOverrides.applyOverridesTo(entry.getValue());
}
return new DefaultCacheManager(configurationBuilderHolder, true);
}
else {
final GlobalConfigurationBuilder globalCfgBuilder = new GlobalConfigurationBuilder();
final ConfigurationBuilder cacheCfgBuilder = new ConfigurationBuilder();
this.globalConfigurationOverrides.applyOverridesTo(globalCfgBuilder);
this.configurationOverrides.applyOverridesTo(cacheCfgBuilder);
cm = createCacheManager(globalCfgBuilder, cacheCfgBuilder);
return cm;
}
}
public void setConfigurationFileLocation(final Resource configurationFileLocation) {
this.configurationFileLocation = configurationFileLocation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment