Home » Spring-Framework-090522 » org.springframework » beans » factory » [javadoc | source]

    1   /*
    2    * Copyright 2002-2008 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.Iterator;
   22   import java.util.LinkedList;
   23   import java.util.List;
   24   
   25   import org.springframework.beans.FatalBeanException;
   26   import org.springframework.core.NestedRuntimeException;
   27   
   28   /**
   29    * Exception thrown when a BeanFactory encounters an error when
   30    * attempting to create a bean from a bean definition.
   31    *
   32    * @author Juergen Hoeller
   33    */
   34   public class BeanCreationException extends FatalBeanException {
   35   
   36   	private String beanName;
   37   
   38   	private String resourceDescription;
   39   
   40   	private List 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();
  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</code> if none
  140   	 */
  141   	public Throwable[] getRelatedCauses() {
  142   		if (this.relatedCauses == null) {
  143   			return null;
  144   		}
  145   		return (Throwable[]) this.relatedCauses.toArray(new Throwable[this.relatedCauses.size()]);
  146   	}
  147   
  148   
  149   	public String toString() {
  150   		StringBuffer sb = new StringBuffer(super.toString());
  151   		if (this.relatedCauses != null) {
  152   			for (Iterator it = this.relatedCauses.iterator(); it.hasNext();) {
  153   				Throwable relatedCause = (Throwable) it.next();
  154   				sb.append("\nRelated cause: ");
  155   				sb.append(relatedCause);
  156   			}
  157   		}
  158   		return sb.toString();
  159   	}
  160   
  161   	public void printStackTrace(PrintStream ps) {
  162   		synchronized (ps) {
  163   			super.printStackTrace(ps);
  164   			if (this.relatedCauses != null) {
  165   				for (Iterator it = this.relatedCauses.iterator(); it.hasNext();) {
  166   					Throwable relatedCause = (Throwable) it.next();
  167   					ps.println("Related cause:");
  168   					relatedCause.printStackTrace(ps);
  169   				}
  170   			}
  171   		}
  172   	}
  173   
  174   	public void printStackTrace(PrintWriter pw) {
  175   		synchronized (pw) {
  176   			super.printStackTrace(pw);
  177   			if (this.relatedCauses != null) {
  178   				for (Iterator it = this.relatedCauses.iterator(); it.hasNext();) {
  179   					Throwable relatedCause = (Throwable) it.next();
  180   					pw.println("Related cause:");
  181   					relatedCause.printStackTrace(pw);
  182   				}
  183   			}
  184   		}
  185   	}
  186   
  187   	public boolean contains(Class exClass) {
  188   		if (super.contains(exClass)) {
  189   			return true;
  190   		}
  191   		if (this.relatedCauses != null) {
  192   			for (Iterator it = this.relatedCauses.iterator(); it.hasNext();) {
  193   				Throwable relatedCause = (Throwable) it.next();
  194   				if (relatedCause instanceof NestedRuntimeException &&
  195   						((NestedRuntimeException) relatedCause).contains(exClass)) {
  196   					return true;
  197   				}
  198   			}
  199   		}
  200   		return false;
  201   	}
  202   
  203   }

Home » Spring-Framework-090522 » org.springframework » beans » factory » [javadoc | source]