Skip to content

Commit

Permalink
made the lookup for the CacheOperationSource bean more robust and reg…
Browse files Browse the repository at this point in the history
…istered an alias to make accessing it easier
  • Loading branch information
Burt Beckwith committed May 25, 2012
1 parent 45abf4b commit 4df63ab
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
19 changes: 9 additions & 10 deletions CacheGrailsPlugin.groovy
Expand Up @@ -126,6 +126,7 @@ class CacheGrailsPlugin {
'proxy-target-class': proxyTargetClass)

// updates the AnnotationCacheOperationSource with a custom subclass
// and adds the 'cacheOperationSource' alias
cacheBeanPostProcessor(CacheBeanPostProcessor)

grailsCacheManager(GrailsConcurrentMapCacheManager)
Expand All @@ -138,8 +139,7 @@ class CacheGrailsPlugin {

grailsCacheFilter(MemoryPageFragmentCachingFilter) {
cacheManager = ref('grailsCacheManager')
// TODO this name might be brittle - perhaps do by type?
cacheOperationSource = ref(GrailsAnnotationCacheOperationSource.BEAN_NAME)
cacheOperationSource = ref('cacheOperationSource')
keyGenerator = ref('webCacheKeyGenerator')
expressionEvaluator = ref('webExpressionEvaluator')
}
Expand All @@ -155,22 +155,21 @@ class CacheGrailsPlugin {

def onChange = { event ->

if (!isEnabled(event.application)) {
def application = event.application
if (!isEnabled(application)) {
return
}

if (!event.source) {
def source = event.source
if (!source) {
return
}

if (event.application.isControllerClass(event.source) ||
event.application.isServiceClass(event.source)) {

def annotationCacheOperationSource = event.ctx.getBean(GrailsAnnotationCacheOperationSource.BEAN_NAME)
annotationCacheOperationSource.reset()
if (application.isControllerClass(source) || application.isServiceClass(source)) {
event.ctx.cacheOperationSource.reset()
log.debug 'Reset GrailsAnnotationCacheOperationSource cache'
}
else if (event.application.isCacheConfigClass(event.source)) {
else if (application.isCacheConfigClass(source)) {
reloadCaches event.ctx
}
}
Expand Down
41 changes: 38 additions & 3 deletions src/java/grails/plugin/cache/CacheBeanPostProcessor.java
Expand Up @@ -23,6 +23,7 @@
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.cache.annotation.AnnotationCacheOperationSource;

/**
* Changes the bean class of the org.springframework.cache.annotation.AnnotationCacheOperationSource#0
Expand All @@ -37,8 +38,11 @@ public class CacheBeanPostProcessor implements BeanDefinitionRegistryPostProcess
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
log.info("postProcessBeanDefinitionRegistry start");

AbstractBeanDefinition beanDef = (AbstractBeanDefinition)registry.getBeanDefinition(
GrailsAnnotationCacheOperationSource.BEAN_NAME);
AbstractBeanDefinition beanDef = findBeanDefinition(registry);
if (beanDef == null) {
log.error("Unable to find the AnnotationCacheOperationSource bean definition");
return;
}

// change the class to the plugin's subclass
beanDef.setBeanClass(GrailsAnnotationCacheOperationSource.class);
Expand All @@ -54,7 +58,38 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
log.debug("updated {}", beanDef);
}

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
protected AbstractBeanDefinition findBeanDefinition(BeanDefinitionRegistry registry) {

AbstractBeanDefinition beanDef = null;
String beanName = null;

if (registry.containsBeanDefinition(GrailsAnnotationCacheOperationSource.BEAN_NAME)) {
beanDef = (AbstractBeanDefinition)registry.getBeanDefinition(
GrailsAnnotationCacheOperationSource.BEAN_NAME);
beanName = GrailsAnnotationCacheOperationSource.BEAN_NAME;
}
else {
String className = AnnotationCacheOperationSource.class.getName();
for (String name : registry.getBeanDefinitionNames()) {
if (className.equals(registry.getBeanDefinition(name).getBeanClassName())) {
beanDef = (AbstractBeanDefinition)registry.getBeanDefinition(name);
beanName = name;
break;
}
}
}

if (beanDef != null) {
// make it easier to work with
if (!"cacheOperationSource".equals(beanName)) {
registry.registerAlias(beanName, "cacheOperationSource");
}
}

return beanDef;
}

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
log.info("postProcessBeanFactory");
}
}

0 comments on commit 4df63ab

Please sign in to comment.