View Javadoc

1   /*
2    * $Id: CachingStrategyDefinitionParser.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.cache.ObjectStoreCachingStrategy;
14  import org.mule.cache.keygenerator.ExpressionKeyGenerator;
15  import org.mule.config.spring.parsers.assembly.BeanAssembler;
16  import org.mule.config.spring.parsers.assembly.BeanAssemblerFactory;
17  import org.mule.config.spring.parsers.assembly.DefaultBeanAssembler;
18  import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
19  import org.mule.config.spring.parsers.assembly.configuration.SingleProperty;
20  import org.mule.config.spring.parsers.generic.OrphanDefinitionParser;
21  import org.mule.config.spring.parsers.processors.CheckExclusiveAttributes;
22  
23  import org.springframework.beans.MutablePropertyValues;
24  import org.springframework.beans.factory.config.BeanDefinition;
25  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
26  
27  public class CachingStrategyDefinitionParser extends OrphanDefinitionParser
28  {
29  
30      public CachingStrategyDefinitionParser(Class<ObjectStoreCachingStrategy> objectStoreCachingStrategyClass, boolean singleton)
31      {
32          super(objectStoreCachingStrategyClass, singleton);
33          setBeanAssemblerFactory(new LocalBeanAssemblerFactory());
34          registerPreProcessor(new CheckExclusiveAttributes(new String[][] {
35                  new String[] {"keyGenerationExpression"}, new String[] {"keyGeneration-ref"}}));
36      }
37  
38      private class LocalBeanAssembler extends DefaultBeanAssembler
39      {
40  
41          public LocalBeanAssembler(PropertyConfiguration beanConfig, BeanDefinitionBuilder bean,
42                                    PropertyConfiguration targetConfig, BeanDefinition target)
43          {
44              super(beanConfig, bean, targetConfig, target);
45          }
46  
47          protected void addPropertyWithReference(MutablePropertyValues properties, SingleProperty config, String name, Object value)
48          {
49              if ("keyGenerationExpression".equals(name))
50              {
51                  BeanDefinitionBuilder wrapper = BeanDefinitionBuilder.genericBeanDefinition(ExpressionKeyGenerator.class);
52                  wrapper.addPropertyValue("expression", value);
53  
54                  super.addPropertyWithReference(properties, config, "keyGenerator", wrapper.getBeanDefinition());
55              }
56              else
57              {
58                  super.addPropertyWithReference(properties, config, name, value);
59              }
60          }
61      }
62  
63      private class LocalBeanAssemblerFactory implements BeanAssemblerFactory
64      {
65  
66          public BeanAssembler newBeanAssembler(PropertyConfiguration beanConfig, BeanDefinitionBuilder bean,
67                                                PropertyConfiguration targetConfig, BeanDefinition target)
68          {
69              return new LocalBeanAssembler(beanConfig, bean, targetConfig, target);
70          }
71  
72      }
73  }