View Javadoc
1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.beans.factory;
18  
19  import java.io.PrintStream;
20  import java.io.PrintWriter;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import org.springframework.beans.FatalBeanException;
25  import org.springframework.core.NestedRuntimeException;
26  
27  /**
28   * Exception thrown when a BeanFactory encounters an error when
29   * attempting to create a bean from a bean definition.
30   *
31   * @author Juergen Hoeller
32   */
33  @SuppressWarnings("serial")
34  public class BeanCreationException extends FatalBeanException {
35  
36  	private String beanName;
37  
38  	private String resourceDescription;
39  
40  	private List<Throwable> relatedCauses;
41  
42  
43  	/**
44  	 * Create a new BeanCreationException.
45  	 * @param msg the detail message
46  	 */
47  	public BeanCreationException(String msg) {
48  		super(msg);
49  	}
50  
51  	/**
52  	 * Create a new BeanCreationException.
53  	 * @param msg the detail message
54  	 * @param cause the root cause
55  	 */
56  	public BeanCreationException(String msg, Throwable cause) {
57  		super(msg, cause);
58  	}
59  
60  	/**
61  	 * Create a new BeanCreationException.
62  	 * @param beanName the name of the bean requested
63  	 * @param msg the detail message
64  	 */
65  	public BeanCreationException(String beanName, String msg) {
66  		super("Error creating bean with name '" + beanName + "': " + msg);
67  		this.beanName = beanName;
68  	}
69  
70  	/**
71  	 * Create a new BeanCreationException.
72  	 * @param beanName the name of the bean requested
73  	 * @param msg the detail message
74  	 * @param cause the root cause
75  	 */
76  	public BeanCreationException(String beanName, String msg, Throwable cause) {
77  		this(beanName, msg);
78  		initCause(cause);
79  	}
80  
81  	/**
82  	 * Create a new BeanCreationException.
83  	 * @param resourceDescription description of the resource
84  	 * that the bean definition came from
85  	 * @param beanName the name of the bean requested
86  	 * @param msg the detail message
87  	 */
88  	public BeanCreationException(String resourceDescription, String beanName, String msg) {
89  		super("Error creating bean with name '" + beanName + "'" +
90  				(resourceDescription != null ? " defined in " + resourceDescription : "") + ": " + msg);
91  		this.resourceDescription = resourceDescription;
92  		this.beanName = beanName;
93  	}
94  
95  	/**
96  	 * Create a new BeanCreationException.
97  	 * @param resourceDescription description of the resource
98  	 * that the bean definition came from
99  	 * @param beanName the name of the bean requested
100 	 * @param msg the detail message
101 	 * @param cause the root cause
102 	 */
103 	public BeanCreationException(String resourceDescription, String beanName, String msg, Throwable cause) {
104 		this(resourceDescription, beanName, msg);
105 		initCause(cause);
106 	}
107 
108 
109 	/**
110 	 * Return the name of the bean requested, if any.
111 	 */
112 	public String getBeanName() {
113 		return this.beanName;
114 	}
115 
116 	/**
117 	 * Return the description of the resource that the bean
118 	 * definition came from, if any.
119 	 */
120 	public String getResourceDescription() {
121 		return this.resourceDescription;
122 	}
123 
124 	/**
125 	 * Add a related cause to this bean creation exception,
126 	 * not being a direct cause of the failure but having occured
127 	 * earlier in the creation of the same bean instance.
128 	 * @param ex the related cause to add
129 	 */
130 	public void addRelatedCause(Throwable ex) {
131 		if (this.relatedCauses == null) {
132 			this.relatedCauses = new LinkedList<Throwable>();
133 		}
134 		this.relatedCauses.add(ex);
135 	}
136 
137 	/**
138 	 * Return the related causes, if any.
139 	 * @return the array of related causes, or {@code null} if none
140 	 */
141 	public Throwable[] getRelatedCauses() {
142 		if (this.relatedCauses == null) {
143 			return null;
144 		}
145 		return this.relatedCauses.toArray(new Throwable[this.relatedCauses.size()]);
146 	}
147 
148 
149 	@Override
150 	public String toString() {
151 		StringBuilder sb = new StringBuilder(super.toString());
152 		if (this.relatedCauses != null) {
153 			for (Throwable relatedCause : this.relatedCauses) {
154 				sb.append("\nRelated cause: ");
155 				sb.append(relatedCause);
156 			}
157 		}
158 		return sb.toString();
159 	}
160 
161 	@Override
162 	public void printStackTrace(PrintStream ps) {
163 		synchronized (ps) {
164 			super.printStackTrace(ps);
165 			if (this.relatedCauses != null) {
166 				for (Throwable relatedCause : this.relatedCauses) {
167 					ps.println("Related cause:");
168 					relatedCause.printStackTrace(ps);
169 				}
170 			}
171 		}
172 	}
173 
174 	@Override
175 	public void printStackTrace(PrintWriter pw) {
176 		synchronized (pw) {
177 			super.printStackTrace(pw);
178 			if (this.relatedCauses != null) {
179 				for (Throwable relatedCause : this.relatedCauses) {
180 					pw.println("Related cause:");
181 					relatedCause.printStackTrace(pw);
182 				}
183 			}
184 		}
185 	}
186 
187 	@Override
188 	public boolean contains(Class<?> exClass) {
189 		if (super.contains(exClass)) {
190 			return true;
191 		}
192 		if (this.relatedCauses != null) {
193 			for (Throwable relatedCause : this.relatedCauses) {
194 				if (relatedCause instanceof NestedRuntimeException &&
195 						((NestedRuntimeException) relatedCause).contains(exClass)) {
196 					return true;
197 				}
198 			}
199 		}
200 		return false;
201 	}
202 
203 }