View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config.spring.parsers.cache;
8   
9   import org.mule.cache.ObjectStoreCachingStrategy;
10  import org.mule.cache.keygenerator.ExpressionKeyGenerator;
11  import org.mule.config.spring.parsers.assembly.BeanAssembler;
12  import org.mule.config.spring.parsers.assembly.BeanAssemblerFactory;
13  import org.mule.config.spring.parsers.assembly.DefaultBeanAssembler;
14  import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
15  import org.mule.config.spring.parsers.assembly.configuration.SingleProperty;
16  import org.mule.config.spring.parsers.generic.OrphanDefinitionParser;
17  import org.mule.config.spring.parsers.processors.CheckExclusiveAttributes;
18  
19  import org.springframework.beans.MutablePropertyValues;
20  import org.springframework.beans.factory.config.BeanDefinition;
21  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
22  
23  public class CachingStrategyDefinitionParser extends OrphanDefinitionParser
24  {
25  
26      public CachingStrategyDefinitionParser(Class<ObjectStoreCachingStrategy> objectStoreCachingStrategyClass, boolean singleton)
27      {
28          super(objectStoreCachingStrategyClass, singleton);
29          setBeanAssemblerFactory(new LocalBeanAssemblerFactory());
30          registerPreProcessor(new CheckExclusiveAttributes(new String[][] {
31                  new String[] {"keyGenerationExpression"}, new String[] {"keyGeneration-ref"}}));
32      }
33  
34      private class LocalBeanAssembler extends DefaultBeanAssembler
35      {
36  
37          public LocalBeanAssembler(PropertyConfiguration beanConfig, BeanDefinitionBuilder bean,
38                                    PropertyConfiguration targetConfig, BeanDefinition target)
39          {
40              super(beanConfig, bean, targetConfig, target);
41          }
42  
43          protected void addPropertyWithReference(MutablePropertyValues properties, SingleProperty config, String name, Object value)
44          {
45              if ("keyGenerationExpression".equals(name))
46              {
47                  BeanDefinitionBuilder wrapper = BeanDefinitionBuilder.genericBeanDefinition(ExpressionKeyGenerator.class);
48                  wrapper.addPropertyValue("expression", value);
49  
50                  super.addPropertyWithReference(properties, config, "keyGenerator", wrapper.getBeanDefinition());
51              }
52              else
53              {
54                  super.addPropertyWithReference(properties, config, name, value);
55              }
56          }
57      }
58  
59      private class LocalBeanAssemblerFactory implements BeanAssemblerFactory
60      {
61  
62          public BeanAssembler newBeanAssembler(PropertyConfiguration beanConfig, BeanDefinitionBuilder bean,
63                                                PropertyConfiguration targetConfig, BeanDefinition target)
64          {
65              return new LocalBeanAssembler(beanConfig, bean, targetConfig, target);
66          }
67  
68      }
69  }