View Javadoc

1   /*
2    * $Id: ClassOrRefDefinitionParser.java 19191 2010-08-25 21:05:23Z tcarlson $
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;
12  
13  import org.mule.util.ClassUtils;
14  import org.mule.util.StringUtils;
15  
16  import org.springframework.beans.MutablePropertyValues;
17  import org.springframework.beans.factory.config.RuntimeBeanReference;
18  import org.springframework.beans.factory.support.AbstractBeanDefinition;
19  import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
20  import org.springframework.beans.factory.xml.ParserContext;
21  import org.w3c.dom.Element;
22  
23  public class ClassOrRefDefinitionParser extends AbstractBeanDefinitionParser
24  {
25      private String propertyName;
26  
27      public ClassOrRefDefinitionParser(String propertyName)
28      {
29          super();
30          
31          if (StringUtils.isEmpty(propertyName))
32          {
33              throw new IllegalArgumentException("propertyName cannot be null");
34          }
35          this.propertyName = propertyName;
36      }
37      
38      @Override
39      protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext)
40      {
41          MutablePropertyValues parentProps = parserContext.getContainingBeanDefinition().getPropertyValues();
42  
43          String ref = element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF);
44          String className = element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_CLASS);
45  
46          if (StringUtils.isBlank(ref) && StringUtils.isBlank(className))
47          {
48              String elementName = element.getLocalName();
49              throw new IllegalArgumentException("Neither ref nor class attribute specified for the "
50                  + elementName + " element");
51          }
52          
53          if (StringUtils.isNotBlank(ref))
54          {
55              // add a ref to other bean
56              parentProps.addPropertyValue(propertyName, new RuntimeBeanReference(ref));
57          }
58          else
59          {
60              // class attributed specified, instantiate and set directly
61              Object instance;
62              try
63              {
64                  instance = ClassUtils.instanciateClass(className, ClassUtils.NO_ARGS, getClass());
65              }
66              catch (Exception e)
67              {
68                  throw new RuntimeException(e);
69              }
70  
71              parentProps.addPropertyValue(propertyName, instance);
72          }
73  
74          return null;
75      }
76  }
77  
78