Coverage Report - org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleHierarchicalBeanDefinitionParserDelegate
0%
0/69
0%
0/50
4.25
 
 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  0
     protected static final Log logger = LogFactory.getLog(MuleHierarchicalBeanDefinitionParserDelegate.class);
 51  
 
 52  
     public MuleHierarchicalBeanDefinitionParserDelegate(XmlReaderContext readerContext,
 53  
                                                         DefaultBeanDefinitionDocumentReader spring)
 54  
     {
 55  0
         super(readerContext);
 56  0
         this.spring = spring;
 57  0
     }
 58  
 
 59  
     public BeanDefinition parseCustomElement(Element element, BeanDefinition parent)
 60  
     {
 61  0
         if (logger.isDebugEnabled())
 62  
         {
 63  0
             logger.debug("parsing: " + SpringXMLUtils.elementToString(element));
 64  
         }
 65  0
         if (SpringXMLUtils.isBeansNamespace(element))
 66  
         {
 67  0
             return handleSpringElements(element, parent);
 68  
         }
 69  
         else
 70  
         {
 71  0
             String namespaceUri = element.getNamespaceURI();
 72  0
             NamespaceHandler handler = getReaderContext().getNamespaceHandlerResolver().resolve(namespaceUri);
 73  0
             if (handler == null)
 74  
             {
 75  0
                 getReaderContext().error("Unable to locate NamespaceHandler for namespace [" + namespaceUri + "]", element);
 76  0
                 return null;
 77  
             }
 78  
 
 79  0
             boolean noRecurse = false;
 80  0
             boolean forceRecurse = false;
 81  
             BeanDefinition finalChild;
 82  
 
 83  
             do {
 84  0
                 ParserContext parserContext = new ParserContext(getReaderContext(), this, parent);
 85  0
                 finalChild = handler.parse(element, parserContext);
 86  0
                 registerBean(element, finalChild);
 87  0
                 noRecurse = noRecurse || testFlag(finalChild, MULE_NO_RECURSE);
 88  0
                 forceRecurse = forceRecurse || testFlag(finalChild, MULE_FORCE_RECURSE);
 89  0
             } 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  0
             if (noRecurse)
 103  
             {
 104  
                 // no recursion takes precedence, as recursion is set by default
 105  0
                 isRecurse = false;
 106  
             }
 107  
             else
 108  
             {
 109  0
                 if (forceRecurse)
 110  
                 {
 111  0
                     isRecurse = true;
 112  
                 }
 113  
                 else
 114  
                 {
 115  
                     // default behaviour if no control specified
 116  0
                     isRecurse = SpringXMLUtils.isMuleNamespace(element);
 117  
                 }
 118  
             }
 119  
 
 120  0
             if (isRecurse)
 121  
             {
 122  0
                 NodeList list = element.getChildNodes();
 123  0
                 for (int i = 0; i < list.getLength(); i++)
 124  
                 {
 125  0
                     if (list.item(i) instanceof Element)
 126  
                     {
 127  0
                         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  0
             if (testFlag(finalChild, MULE_POST_CHILDREN))
 135  
             {
 136  0
                 ParserContext parserContext = new ParserContext(getReaderContext(), this, parent);
 137  0
                 finalChild = handler.parse(element, parserContext);
 138  
             }
 139  
 
 140  0
             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  0
         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  0
                 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
 158  0
                 doc.appendChild(doc.importNode(element, true));
 159  0
                 spring.registerBeanDefinitions(doc, getReaderContext());
 160  
             }
 161  0
             catch (ParserConfigurationException e)
 162  
             {
 163  0
                 throw new RuntimeException(e);
 164  0
             }
 165  0
             return parent;
 166  
         }
 167  
 
 168  0
         else if (SpringXMLUtils.isLocalName(element, PROPERTY_ELEMENT))
 169  
         {
 170  0
             parsePropertyElement(element, parent);
 171  0
             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  0
         else if (SpringXMLUtils.isLocalName(element, BEAN_ELEMENT))
 194  
         {
 195  0
             BeanDefinitionHolder holder = parseBeanDefinitionElement(element, parent);
 196  0
             registerBeanDefinitionHolder(holder);
 197  0
             return holder.getBeanDefinition();
 198  
         }
 199  
         else
 200  
         {
 201  0
             throw new IllegalStateException("Unexpected Spring element: " + SpringXMLUtils.elementToString(element));
 202  
         }
 203  
     }
 204  
 
 205  
     protected void registerBean(Element ele, BeanDefinition bd)
 206  
     {
 207  0
         if (bd == null)
 208  
         {
 209  0
             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  0
         if (! testFlag(bd, MULE_NO_REGISTRATION))
 216  
         {
 217  0
             String name =  generateChildBeanName(ele);
 218  0
             logger.debug("register " + name + ": " + bd.getBeanClassName());
 219  0
             registerBeanDefinitionHolder(new BeanDefinitionHolder(bd, name));
 220  
         }
 221  0
     }
 222  
 
 223  
     protected void registerBeanDefinitionHolder(BeanDefinitionHolder bdHolder)
 224  
     {
 225  
         //bdHolder = decorateBeanDefinitionIfRequired(ele, bdHolder);
 226  
         // Register the final decorated instance.
 227  0
         BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
 228  
         // Send registration event.
 229  0
         getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
 230  0
     }
 231  
 
 232  
     protected String generateChildBeanName(Element e)
 233  
     {
 234  0
         String id = SpringXMLUtils.getNameOrId(e);
 235  0
         if (StringUtils.isBlank(id))
 236  
         {
 237  0
             String parentId = SpringXMLUtils.getNameOrId((Element) e.getParentNode());
 238  0
             return "." + parentId + ":" + e.getLocalName();
 239  
         }
 240  
         else
 241  
         {
 242  0
             return id;
 243  
         }
 244  
     }
 245  
 
 246  
     public static void setFlag(BeanDefinition bean, String flag)
 247  
     {
 248  0
         bean.setAttribute(flag, Boolean.TRUE);
 249  0
     }
 250  
 
 251  
     public static boolean testFlag(BeanDefinition bean, String flag)
 252  
     {
 253  0
         return null != bean
 254  
                 && bean.hasAttribute(flag)
 255  
                 && bean.getAttribute(flag) instanceof Boolean
 256  
                 && ((Boolean) bean.getAttribute(flag)).booleanValue();
 257  
     }
 258  
 
 259  
 }