View Javadoc

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