001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.parsing;
018:
019: import org.springframework.util.Assert;
020:
021: /**
022: * Represents a problem with a bean definition configuration.
023: * Mainly serves as common argument passed into a {@link ProblemReporter}.
024: *
025: * <p>May indicate a potentially fatal problem (an error) or just a warning.
026: *
027: * @author Rob Harrop
028: * @author Juergen Hoeller
029: * @since 2.0
030: * @see ProblemReporter
031: */
032: public class Problem {
033:
034: private final String message;
035:
036: private final Location location;
037:
038: private final ParseState parseState;
039:
040: private final Throwable rootCause;
041:
042: /**
043: * Create a new instance of the {@link Problem} class.
044: * @param message a message detailing the problem
045: * @param location the location within a bean configuration source that triggered the error
046: */
047: public Problem(String message, Location location) {
048: this (message, location, null, null);
049: }
050:
051: /**
052: * Create a new instance of the {@link Problem} class.
053: * @param message a message detailing the problem
054: * @param parseState the {@link ParseState} at the time of the error
055: * @param location the location within a bean configuration source that triggered the error
056: */
057: public Problem(String message, Location location,
058: ParseState parseState) {
059: this (message, location, parseState, null);
060: }
061:
062: /**
063: * Create a new instance of the {@link Problem} class.
064: * @param message a message detailing the problem
065: * @param rootCause the underlying expection that caused the error (may be <code>null</code>)
066: * @param parseState the {@link ParseState} at the time of the error
067: * @param location the location within a bean configuration source that triggered the error
068: */
069: public Problem(String message, Location location,
070: ParseState parseState, Throwable rootCause) {
071: Assert.notNull(message, "Message must not be null");
072: Assert.notNull(location, "Location must not be null");
073: this .message = message;
074: this .location = location;
075: this .parseState = parseState;
076: this .rootCause = rootCause;
077: }
078:
079: /**
080: * Get the message detailing the problem.
081: */
082: public String getMessage() {
083: return message;
084: }
085:
086: /**
087: * Get the location within a bean configuration source that triggered the error.
088: */
089: public Location getLocation() {
090: return location;
091: }
092:
093: /**
094: * Get the description of the bean configuration source that triggered the error,
095: * as contained within this Problem's Location object.
096: * @see #getLocation()
097: */
098: public String getResourceDescription() {
099: return getLocation().getResource().getDescription();
100: }
101:
102: /**
103: * Get the {@link ParseState} at the time of the error (may be <code>null</code>).
104: */
105: public ParseState getParseState() {
106: return this .parseState;
107: }
108:
109: /**
110: * Get the underlying expection that caused the error (may be <code>null</code>).
111: */
112: public Throwable getRootCause() {
113: return rootCause;
114: }
115:
116: public String toString() {
117: StringBuffer sb = new StringBuffer();
118: sb.append("Configuration problem: ");
119: sb.append(getMessage());
120: sb.append("\nOffending resource: ").append(
121: getResourceDescription());
122: if (getParseState() != null) {
123: sb.append('\n').append(getParseState());
124: }
125: return sb.toString();
126: }
127:
128: }
|