View Javadoc

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