01: /*
02: * $Id: MissingParserProblemReporter.java 10256 2008-01-08 15:20:25Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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
24: FailFastProblemReporter {
25:
26: public static final String NO_PARSER_PREFIX = "Cannot locate BeanDefinitionParser";
27:
28: // @Override
29: public void fatal(Problem problem) {
30: if (isMissingParser(problem)) {
31: problem = extendProblemDetails(problem);
32: }
33: super .fatal(problem);
34: }
35:
36: protected boolean isMissingParser(Problem problem) {
37: // Spring doesn't give us much useful data here - parseState and rootCause are null
38: String message = problem.getMessage();
39: return (null != message && message.startsWith(NO_PARSER_PREFIX));
40: }
41:
42: protected Problem extendProblemDetails(Problem problem) {
43: try {
44: String element = ((Element) problem.getLocation()
45: .getSource()).getLocalName();
46: String namespace = ((Element) problem.getLocation()
47: .getSource()).getNamespaceURI();
48: String message = "The element '"
49: + element
50: + "' does not have an associated Bean Definition Parser."
51: + " Is the module or transport associated with "
52: + namespace + " present on the classpath?";
53: return new Problem(message, problem.getLocation(), problem
54: .getParseState(), problem.getRootCause());
55: } catch (Exception e) {
56: // fall back to previous message
57: return problem;
58: }
59: }
60:
61: }
|