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.config.spring.parsers.assembly.BeanAssembler;
10  import org.mule.config.spring.parsers.assembly.BeanAssemblerFactory;
11  import org.mule.config.spring.parsers.assembly.DefaultBeanAssembler;
12  import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
13  import org.mule.config.spring.parsers.assembly.configuration.SingleProperty;
14  import org.mule.config.spring.parsers.generic.ChildDefinitionParser;
15  import org.mule.config.spring.parsers.processors.CheckExclusiveAttributes;
16  import org.mule.routing.filters.ExpressionFilter;
17  
18  import org.springframework.beans.MutablePropertyValues;
19  import org.springframework.beans.factory.config.BeanDefinition;
20  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
21  
22  public class CacheDefinitionParser extends ChildDefinitionParser
23  {
24  
25      public CacheDefinitionParser(String setterMethod, Class<?> clazz)
26      {
27          super(setterMethod, clazz);
28          setBeanAssemblerFactory(new LocalBeanAssemblerFactory());
29          registerPreProcessor(new CheckExclusiveAttributes(new String[][] {
30                  new String[] {"filterExpression"}, new String[] {"filter-ref"}}));
31      }
32  
33      private class LocalBeanAssembler extends DefaultBeanAssembler
34      {
35  
36          public LocalBeanAssembler(PropertyConfiguration beanConfig, BeanDefinitionBuilder bean,
37                                    PropertyConfiguration targetConfig, BeanDefinition target)
38          {
39              super(beanConfig, bean, targetConfig, target);
40          }
41  
42          protected void addPropertyWithReference(MutablePropertyValues properties, SingleProperty config, String name, Object value)
43          {
44              if ("filterExpression".equals(name))
45              {
46                  BeanDefinitionBuilder wrapper = BeanDefinitionBuilder.genericBeanDefinition(ExpressionFilter.class);
47                  wrapper.addConstructorArgValue(value);
48  
49                  super.addPropertyWithReference(properties, config, "filter", wrapper.getBeanDefinition());
50              }
51              else
52              {
53                  super.addPropertyWithReference(properties, config, name, value);
54              }
55          }
56      }
57  
58      private class LocalBeanAssemblerFactory implements BeanAssemblerFactory
59      {
60  
61          public BeanAssembler newBeanAssembler(PropertyConfiguration beanConfig, BeanDefinitionBuilder bean,
62                                                PropertyConfiguration targetConfig, BeanDefinition target)
63          {
64              return new LocalBeanAssembler(beanConfig, bean, targetConfig, target);
65          }
66  
67      }
68  }