View Javadoc

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