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.delegate;
8   
9   import org.mule.config.spring.parsers.MuleDefinitionParser;
10  
11  import org.springframework.beans.factory.support.AbstractBeanDefinition;
12  import org.springframework.beans.factory.xml.ParserContext;
13  import org.w3c.dom.Element;
14  
15  /**
16   * Extend {@link org.mule.config.spring.parsers.delegate.AbstractSerialDelegatingDefinitionParser}
17   * to return the first definition as the final result
18   */
19  public class AbstractFirstResultSerialDefinitionParser extends AbstractSerialDelegatingDefinitionParser
20  {
21  
22      protected AbstractBeanDefinition firstDefinition;
23      private boolean returnFirstResult = true;
24  
25      public AbstractFirstResultSerialDefinitionParser()
26      {
27          super();
28      }
29  
30      public AbstractFirstResultSerialDefinitionParser(boolean doReset)
31      {
32          super(doReset);
33      }
34  
35      public void setReturnFirstResult(boolean returnFirstResult)
36      {
37          this.returnFirstResult = returnFirstResult;
38      }
39  
40      protected AbstractBeanDefinition doSingleBean(int index, MuleDefinitionParser parser, Element element, ParserContext parserContext)
41      {
42          try
43          {
44              AbstractBeanDefinition result = null;
45              try
46              {
47                  result = super.doSingleBean(index, parser, element, parserContext);
48              }
49              catch (RuntimeException e)
50              {
51                  if (!isExceptionHandled(e))
52                  {
53                      throw e;
54                  }
55              }
56              if (0 == index)
57              {
58                  firstDefinition = result;
59              }
60              if (size() == index + 1)
61              {
62                  if (returnFirstResult)
63                  {
64                      return firstDefinition;
65                  }
66                  else
67                  {
68                      return result;
69                  }
70              }
71              else
72              {
73                  return null;
74              }
75          }
76          catch (RuntimeException e)
77          {
78              firstDefinition = null;
79              throw e;
80          }
81      }
82  
83  }