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.specific;
8   
9   import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser;
10  import org.mule.config.spring.parsers.MuleDefinitionParser;
11  import org.mule.config.spring.parsers.PreProcessor;
12  import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
13  import org.mule.config.spring.parsers.delegate.AbstractParallelDelegatingDefinitionParser;
14  import org.mule.config.spring.util.SpringXMLUtils;
15  import org.mule.util.StringUtils;
16  
17  import org.springframework.beans.factory.xml.ParserContext;
18  import org.w3c.dom.Attr;
19  import org.w3c.dom.Element;
20  import org.w3c.dom.NamedNodeMap;
21  import org.w3c.dom.Node;
22  
23  /**
24   * Allows for parsing either a shortcut component configuration by delegating to two
25   * different component parses depending on the existence of the class attribute. If
26   * the class attribute is used then an embedded object factory element cannot be
27   * used.
28   * 
29   * <pre>
30   * &lt;component class=&quot;&quot;&gt;
31   * </pre>
32   * 
33   * or one with an embedded object factory element.
34   * 
35   * <pre>
36   * &lt;component&gt;
37   *     &lt;singleon-object class=&quot;..&quot;/&gt;
38   * &lt;/component&gt;
39   * </pre>
40   */
41  public class ComponentDelegatingDefinitionParser extends AbstractParallelDelegatingDefinitionParser
42  {
43  
44      private MuleDefinitionParser normalConfig;
45      private MuleDefinitionParser shortcutConfig;
46  
47      public ComponentDelegatingDefinitionParser(Class clazz)
48      {
49          normalConfig = new ComponentDefinitionParser(clazz);
50          shortcutConfig = new ShortcutComponentDefinitionParser(clazz);
51          addDelegate(normalConfig);
52          addDelegate(shortcutConfig);
53          registerPreProcessor(new CheckExclusiveClassAttributeObjectFactory());
54      }
55  
56      @Override
57      protected MuleDefinitionParser getDelegate(Element element, ParserContext parserContext)
58      {
59          if (StringUtils.isEmpty(element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_CLASS)))
60          {
61              return normalConfig;
62          }
63          else
64          {
65              return shortcutConfig;
66          }
67      }
68  
69      /**
70       * Given that the service object-factory is extensible and new object factory
71       * types can be implemented and used by substitution, the only way of checking
72       * for the existence of an object-factory if by object factory element
73       * convention.<br>
74       * This pre-processor checks for the existence of a <i>"class"</i> attribute on
75       * the service, and throws an exception if the service has any elements that
76       * match the object factory element convention (i.e. that end in "object"). NOTE:
77       * We used to test by exclusion here allowing all other elements, but that no
78       * longer works now extensible interceptors elements can be used.
79       */
80      class CheckExclusiveClassAttributeObjectFactory implements PreProcessor
81      {
82  
83          private static final String OBJECT_FACTORY_ELEMENT_CONVENTION_SUFFIX = "object";
84  
85          public void preProcess(PropertyConfiguration config, Element element)
86          {
87              NamedNodeMap attributes = element.getAttributes();
88              for (int i = 0; i < attributes.getLength(); i++)
89              {
90                  String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
91                  if (alias.equals(AbstractMuleBeanDefinitionParser.ATTRIBUTE_CLASS))
92                  {
93                      for (int j = 0; j < element.getChildNodes().getLength(); j++)
94                      {
95                          Node child = element.getChildNodes().item(j);
96                          if (child instanceof Element
97                              && child.getLocalName().endsWith(OBJECT_FACTORY_ELEMENT_CONVENTION_SUFFIX))
98                          {
99                              StringBuffer message = new StringBuffer("The child element '");
100                             message.append(child.getLocalName());
101                             message.append("' cannot appear with the 'class' attribute");
102                             message.append(" in element ");
103                             message.append(SpringXMLUtils.elementToString(element));
104                             message.append(".");
105                             throw new CheckExclusiveClassAttributeObjectFactoryException(message.toString());
106                         }
107                     }
108                 }
109             }
110         }
111     }
112 
113     class CheckExclusiveClassAttributeObjectFactoryException extends IllegalStateException
114     {
115         private static final long serialVersionUID = 4625276914151932111L;
116 
117         CheckExclusiveClassAttributeObjectFactoryException(String message)
118         {
119             super(message);
120         }
121     }
122 
123 }