001: /*
002: * Copyright 2002-2007 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;
018:
019: import java.io.PrintStream;
020: import java.io.PrintWriter;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024:
025: import org.springframework.beans.FatalBeanException;
026: import org.springframework.core.NestedRuntimeException;
027:
028: /**
029: * Exception thrown when a BeanFactory encounters an error when
030: * attempting to create a bean from a bean definition.
031: *
032: * @author Juergen Hoeller
033: */
034: public class BeanCreationException extends FatalBeanException {
035:
036: private String beanName;
037:
038: private String resourceDescription;
039:
040: private List relatedCauses;
041:
042: /**
043: * Create a new BeanCreationException.
044: * @param msg the detail message
045: */
046: public BeanCreationException(String msg) {
047: super (msg);
048: }
049:
050: /**
051: * Create a new BeanCreationException.
052: * @param msg the detail message
053: * @param cause the root cause
054: */
055: public BeanCreationException(String msg, Throwable cause) {
056: super (msg, cause);
057: }
058:
059: /**
060: * Create a new BeanCreationException.
061: * @param beanName the name of the bean requested
062: * @param msg the detail message
063: */
064: public BeanCreationException(String beanName, String msg) {
065: this (beanName, msg, (Throwable) null);
066: }
067:
068: /**
069: * Create a new BeanCreationException.
070: * @param beanName the name of the bean requested
071: * @param msg the detail message
072: * @param cause the root cause
073: */
074: public BeanCreationException(String beanName, String msg,
075: Throwable cause) {
076: super ("Error creating bean with name '" + beanName + "': "
077: + msg, cause);
078: this .beanName = beanName;
079: }
080:
081: /**
082: * Create a new BeanCreationException.
083: * @param resourceDescription description of the resource
084: * that the bean definition came from
085: * @param beanName the name of the bean requested
086: * @param msg the detail message
087: */
088: public BeanCreationException(String resourceDescription,
089: String beanName, String msg) {
090: this (resourceDescription, beanName, msg, null);
091: }
092:
093: /**
094: * Create a new BeanCreationException.
095: * @param resourceDescription description of the resource
096: * that the bean definition came from
097: * @param beanName the name of the bean requested
098: * @param msg the detail message
099: * @param cause the root cause
100: */
101: public BeanCreationException(String resourceDescription,
102: String beanName, String msg, Throwable cause) {
103: super ("Error creating bean with name '"
104: + beanName
105: + "'"
106: + (resourceDescription != null ? " defined in "
107: + resourceDescription : "") + ": " + msg, cause);
108: this .resourceDescription = resourceDescription;
109: this .beanName = beanName;
110: }
111:
112: /**
113: * Return the name of the bean requested, if any.
114: */
115: public String getBeanName() {
116: return this .beanName;
117: }
118:
119: /**
120: * Return the description of the resource that the bean
121: * definition came from, if any.
122: */
123: public String getResourceDescription() {
124: return this .resourceDescription;
125: }
126:
127: /**
128: * Add a related cause to this bean creation exception,
129: * not being a direct cause of the failure but having occured
130: * earlier in the creation of the same bean instance.
131: * @param ex the related cause to add
132: */
133: public void addRelatedCause(Throwable ex) {
134: if (this .relatedCauses == null) {
135: this .relatedCauses = new LinkedList();
136: }
137: this .relatedCauses.add(ex);
138: }
139:
140: /**
141: * Return the related causes, if any.
142: * @return the array of related causes, or <code>null</code> if none
143: */
144: public Throwable[] getRelatedCauses() {
145: if (this .relatedCauses == null) {
146: return null;
147: }
148: return (Throwable[]) this .relatedCauses
149: .toArray(new Throwable[this .relatedCauses.size()]);
150: }
151:
152: public String toString() {
153: StringBuffer sb = new StringBuffer(super .toString());
154: if (this .relatedCauses != null) {
155: for (Iterator it = this .relatedCauses.iterator(); it
156: .hasNext();) {
157: Throwable relatedCause = (Throwable) it.next();
158: sb.append("\nRelated cause: ");
159: sb.append(relatedCause);
160: }
161: }
162: return sb.toString();
163: }
164:
165: public void printStackTrace(PrintStream ps) {
166: synchronized (ps) {
167: super .printStackTrace(ps);
168: if (this .relatedCauses != null) {
169: for (Iterator it = this .relatedCauses.iterator(); it
170: .hasNext();) {
171: Throwable relatedCause = (Throwable) it.next();
172: ps.println("Related cause:");
173: relatedCause.printStackTrace(ps);
174: }
175: }
176: }
177: }
178:
179: public void printStackTrace(PrintWriter pw) {
180: synchronized (pw) {
181: super .printStackTrace(pw);
182: if (this .relatedCauses != null) {
183: for (Iterator it = this .relatedCauses.iterator(); it
184: .hasNext();) {
185: Throwable relatedCause = (Throwable) it.next();
186: pw.println("Related cause:");
187: relatedCause.printStackTrace(pw);
188: }
189: }
190: }
191: }
192:
193: public boolean contains(Class exClass) {
194: if (super .contains(exClass)) {
195: return true;
196: }
197: if (this .relatedCauses != null) {
198: for (Iterator it = this .relatedCauses.iterator(); it
199: .hasNext();) {
200: Throwable relatedCause = (Throwable) it.next();
201: if (relatedCause instanceof NestedRuntimeException
202: && ((NestedRuntimeException) relatedCause)
203: .contains(exClass)) {
204: return true;
205: }
206: }
207: }
208: return false;
209: }
210:
211: }
|