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.springframework.beans.factory.parsing.FailFastProblemReporter;
10  import org.springframework.beans.factory.parsing.Problem;
11  import org.w3c.dom.Element;
12  
13  /**
14   * A very simple extension to {@link org.springframework.beans.factory.parsing.FailFastProblemReporter}
15   * that intercepts errors related to missing definition parsers to give a more helpful message.
16   * In the future we may want to replace this by something more sophisticated that allows
17   * different problems to be resolved by different "pluggable" components...
18   */
19  public class MissingParserProblemReporter extends FailFastProblemReporter
20  {
21  
22      public static final String NO_PARSER_PREFIX = "Cannot locate BeanDefinitionParser";
23  
24      @Override
25      public void fatal(Problem problem)
26      {
27          if (isMissingParser(problem))
28          {
29              problem = extendProblemDetails(problem);
30          }
31          super.fatal(problem);
32      }
33  
34      protected boolean isMissingParser(Problem problem)
35      {
36          // Spring doesn't give us much useful data here - parseState and rootCause are null
37          String message = problem.getMessage();
38          return (null != message && message.startsWith(NO_PARSER_PREFIX));
39      }
40  
41      protected Problem extendProblemDetails(Problem problem)
42      {
43          try
44          {
45              String element = ((Element) problem.getLocation().getSource()).getLocalName();
46              String namespace = ((Element) problem.getLocation().getSource()).getNamespaceURI();
47              String message = "The element '" + element + "' does not have an associated Bean Definition Parser."
48                      +"  Is the module or transport associated with " + namespace + " present on the classpath?";
49              return new Problem(message, problem.getLocation(), problem.getParseState(), problem.getRootCause());
50          }
51          catch (Exception e)
52          {
53              // fall back to previous message
54              return problem;
55          }
56      }
57  
58  }