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;
8   
9   import org.mule.config.spring.util.SpringXMLUtils;
10  import org.mule.util.StringUtils;
11  
12  import javax.xml.parsers.DocumentBuilderFactory;
13  import javax.xml.parsers.ParserConfigurationException;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.springframework.beans.factory.config.BeanDefinition;
18  import org.springframework.beans.factory.config.BeanDefinitionHolder;
19  import org.springframework.beans.factory.parsing.BeanComponentDefinition;
20  import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
21  import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
22  import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
23  import org.springframework.beans.factory.xml.NamespaceHandler;
24  import org.springframework.beans.factory.xml.ParserContext;
25  import org.springframework.beans.factory.xml.XmlReaderContext;
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  import org.w3c.dom.NodeList;
29  
30  
31  /**
32   * This parser enables Mule to parse heirarchical bean structures using spring Namespace handling
33   * There are 4 base DefinitionParsers supplied in Mule that most Parsers will extend from, these are
34   * {@link org.mule.config.spring.parsers.AbstractChildDefinitionParser}
35   * {@link org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser}
36   * {@link org.mule.config.spring.parsers.generic.ChildDefinitionParser}
37   * {@link org.mule.config.spring.parsers.generic.MuleOrphanDefinitionParser}
38   */
39  public class MuleHierarchicalBeanDefinitionParserDelegate extends BeanDefinitionParserDelegate
40  {
41  
42      public static final String BEANS = "beans"; // cannot find this in Spring api!
43      public static final String MULE_REPEAT_PARSE = "org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate.MULE_REPEAT_PARSE";
44      public static final String MULE_NO_RECURSE = "org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate.MULE_NO_RECURSE";
45      public static final String MULE_FORCE_RECURSE = "org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate.MULE_FORCE_RECURSE";
46      public static final String MULE_NO_REGISTRATION = "org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate.MULE_NO_REGISTRATION";
47      public static final String MULE_POST_CHILDREN = "org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate.MULE_POST_CHILDREN";
48      private DefaultBeanDefinitionDocumentReader spring;
49  
50      protected static final Log logger = LogFactory.getLog(MuleHierarchicalBeanDefinitionParserDelegate.class);
51  
52      public MuleHierarchicalBeanDefinitionParserDelegate(XmlReaderContext readerContext,
53                                                          DefaultBeanDefinitionDocumentReader spring)
54      {
55          super(readerContext);
56          this.spring = spring;
57      }
58  
59      public BeanDefinition parseCustomElement(Element element, BeanDefinition parent)
60      {
61          if (logger.isDebugEnabled())
62          {
63              logger.debug("parsing: " + SpringXMLUtils.elementToString(element));
64          }
65          if (SpringXMLUtils.isBeansNamespace(element))
66          {
67              return handleSpringElements(element, parent);
68          }
69          else
70          {
71              String namespaceUri = element.getNamespaceURI();
72              NamespaceHandler handler = getReaderContext().getNamespaceHandlerResolver().resolve(namespaceUri);
73              if (handler == null)
74              {
75                  getReaderContext().error("Unable to locate NamespaceHandler for namespace [" + namespaceUri + "]", element);
76                  return null;
77              }
78  
79              boolean noRecurse = false;
80              boolean forceRecurse = false;
81              BeanDefinition finalChild;
82  
83              do {
84                  ParserContext parserContext = new ParserContext(getReaderContext(), this, parent);
85                  finalChild = handler.parse(element, parserContext);
86                  registerBean(element, finalChild);
87                  noRecurse = noRecurse || testFlag(finalChild, MULE_NO_RECURSE);
88                  forceRecurse = forceRecurse || testFlag(finalChild, MULE_FORCE_RECURSE);
89              } while (null != finalChild && testFlag(finalChild, MULE_REPEAT_PARSE));
90  
91              // Only iterate and parse child mule name-spaced elements. Spring does not do
92              // hierarchical parsing by default so we need to maintain this behavior
93              // for non-mule elements to ensure that we don't break the parsing of any
94              // other custom name-spaces e.g spring-jee.
95  
96              // We also avoid parsing inside elements that have constructed a factory bean
97              // because that means we're dealing with (something like) ChildMapDefinitionParser,
98              // which handles iteration internally (this is a hack needed because Spring doesn't
99              // expose the DP for "<spring:entry>" elements directly).
100 
101             boolean isRecurse;
102             if (noRecurse)
103             {
104                 // no recursion takes precedence, as recursion is set by default
105                 isRecurse = false;
106             }
107             else
108             {
109                 if (forceRecurse)
110                 {
111                     isRecurse = true;
112                 }
113                 else
114                 {
115                     // default behaviour if no control specified
116                     isRecurse = SpringXMLUtils.isMuleNamespace(element);
117                 }
118             }
119 
120             if (isRecurse)
121             {
122                 NodeList list = element.getChildNodes();
123                 for (int i = 0; i < list.getLength(); i++)
124                 {
125                     if (list.item(i) instanceof Element)
126                     {
127                         parseCustomElement((Element) list.item(i), finalChild);
128                     }
129                 }
130             }
131 
132             // If a parser requests post-processing we call again after children called
133 
134             if (testFlag(finalChild, MULE_POST_CHILDREN))
135             {
136                 ParserContext parserContext = new ParserContext(getReaderContext(), this, parent);
137                 finalChild = handler.parse(element, parserContext);
138             }
139 
140             return finalChild;
141         }
142     }
143 
144     protected BeanDefinition handleSpringElements(Element element, BeanDefinition parent)
145     {
146 
147         // these are only called if they are at a "top level" - if they are nested inside
148         // other spring elements then spring will handle them itself
149 
150         if (SpringXMLUtils.isLocalName(element, BEANS))
151         {
152             // the delegate doesn't support the full spring schema, but it seems that
153             // we can invoke the DefaultBeanDefinitionDocumentReader via registerBeanDefinitions
154             // but we need to create a new DOM document from the element first
155             try
156             {
157                 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
158                 doc.appendChild(doc.importNode(element, true));
159                 spring.registerBeanDefinitions(doc, getReaderContext());
160             }
161             catch (ParserConfigurationException e)
162             {
163                 throw new RuntimeException(e);
164             }
165             return parent;
166         }
167 
168         else if (SpringXMLUtils.isLocalName(element, PROPERTY_ELEMENT))
169         {
170             parsePropertyElement(element, parent);
171             return parent;
172         }
173 
174         // i am trying to keep these to a minimum - using anything but "bean" is a recipe
175         // for disaster - we already have problems with "property", for example.
176 
177 //        else if (isLocalName(element, MAP_ELEMENT))
178 //        {
179 //            // currently unused?
180 //            parseMapElement(element, bd);
181 //        }
182 //        else if (isLocalName(element, LIST_ELEMENT))
183 //        {
184 //            // currently unused?
185 //            parseListElement(element, bd);
186 //        }
187 //        else if (isLocalName(element, SET_ELEMENT))
188 //        {
189 //            // currently unused?
190 //            parseSetElement(element, bd);
191 //        }
192 
193         else if (SpringXMLUtils.isLocalName(element, BEAN_ELEMENT))
194         {
195             BeanDefinitionHolder holder = parseBeanDefinitionElement(element, parent);
196             registerBeanDefinitionHolder(holder);
197             return holder.getBeanDefinition();
198         }
199         else
200         {
201             throw new IllegalStateException("Unexpected Spring element: " + SpringXMLUtils.elementToString(element));
202         }
203     }
204 
205     protected void registerBean(Element ele, BeanDefinition bd)
206     {
207         if (bd == null)
208         {
209             return;
210         }
211 
212         // Check to see if the Bean Definition represents a compound element - one represents a subset of
213         // configuration for the parent bean. Compound bean definitions should not be registered since the properties
214         // set on them are really set on the parent bean.
215         if (! testFlag(bd, MULE_NO_REGISTRATION))
216         {
217             String name =  generateChildBeanName(ele);
218             logger.debug("register " + name + ": " + bd.getBeanClassName());
219             registerBeanDefinitionHolder(new BeanDefinitionHolder(bd, name));
220         }
221     }
222 
223     protected void registerBeanDefinitionHolder(BeanDefinitionHolder bdHolder)
224     {
225         //bdHolder = decorateBeanDefinitionIfRequired(ele, bdHolder);
226         // Register the final decorated instance.
227         BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
228         // Send registration event.
229         getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
230     }
231 
232     protected String generateChildBeanName(Element e)
233     {
234         String id = SpringXMLUtils.getNameOrId(e);
235         if (StringUtils.isBlank(id))
236         {
237             String parentId = SpringXMLUtils.getNameOrId((Element) e.getParentNode());
238             return "." + parentId + ":" + e.getLocalName();
239         }
240         else
241         {
242             return id;
243         }
244     }
245 
246     public static void setFlag(BeanDefinition bean, String flag)
247     {
248         bean.setAttribute(flag, Boolean.TRUE);
249     }
250 
251     public static boolean testFlag(BeanDefinition bean, String flag)
252     {
253         return null != bean
254                 && bean.hasAttribute(flag)
255                 && bean.getAttribute(flag) instanceof Boolean
256                 && ((Boolean) bean.getAttribute(flag)).booleanValue();
257     }
258 
259 }