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