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;
018:
019: import java.io.PrintStream;
020: import java.io.PrintWriter;
021:
022: import org.springframework.util.Assert;
023:
024: /**
025: * Combined exception, composed of individual PropertyAccessException instances.
026: * An object of this class is created at the beginning of the binding
027: * process, and errors added to it as necessary.
028: *
029: * <p>The binding process continues when it encounters application-level
030: * PropertyAccessExceptions, applying those changes that can be applied
031: * and storing rejected changes in an object of this class.
032: *
033: * @author Rod Johnson
034: * @author Juergen Hoeller
035: * @since 18 April 2001
036: */
037: public class PropertyBatchUpdateException extends BeansException {
038:
039: /** List of PropertyAccessException objects */
040: private PropertyAccessException[] propertyAccessExceptions;
041:
042: /**
043: * Create a new PropertyBatchUpdateException.
044: * @param propertyAccessExceptions the List of PropertyAccessExceptions
045: */
046: public PropertyBatchUpdateException(
047: PropertyAccessException[] propertyAccessExceptions) {
048: super (null);
049: Assert.notEmpty(propertyAccessExceptions,
050: "At least 1 PropertyAccessException required");
051: this .propertyAccessExceptions = propertyAccessExceptions;
052: }
053:
054: /**
055: * If this returns 0, no errors were encountered during binding.
056: */
057: public final int getExceptionCount() {
058: return this .propertyAccessExceptions.length;
059: }
060:
061: /**
062: * Return an array of the propertyAccessExceptions stored in this object.
063: * Will return the empty array (not null) if there were no errors.
064: */
065: public final PropertyAccessException[] getPropertyAccessExceptions() {
066: return this .propertyAccessExceptions;
067: }
068:
069: /**
070: * Return the exception for this field, or <code>null</code> if there isn't one.
071: */
072: public PropertyAccessException getPropertyAccessException(
073: String propertyName) {
074: for (int i = 0; i < this .propertyAccessExceptions.length; i++) {
075: PropertyAccessException pae = this .propertyAccessExceptions[i];
076: if (propertyName.equals(pae.getPropertyChangeEvent()
077: .getPropertyName())) {
078: return pae;
079: }
080: }
081: return null;
082: }
083:
084: public String getMessage() {
085: StringBuffer sb = new StringBuffer("Failed properties: ");
086: for (int i = 0; i < this .propertyAccessExceptions.length; i++) {
087: sb.append(this .propertyAccessExceptions[i].getMessage());
088: if (i < this .propertyAccessExceptions.length - 1) {
089: sb.append("; ");
090: }
091: }
092: return sb.toString();
093: }
094:
095: public String toString() {
096: StringBuffer sb = new StringBuffer();
097: sb.append(getClass().getName()).append(
098: "; nested PropertyAccessExceptions (");
099: sb.append(getExceptionCount()).append(") are:");
100: for (int i = 0; i < this .propertyAccessExceptions.length; i++) {
101: sb.append('\n').append("PropertyAccessException ").append(
102: i + 1).append(": ");
103: sb.append(this .propertyAccessExceptions[i]);
104: }
105: return sb.toString();
106: }
107:
108: public void printStackTrace(PrintStream ps) {
109: ps.println(getClass().getName()
110: + "; nested PropertyAccessException details ("
111: + getExceptionCount() + ") are:");
112: for (int i = 0; i < this .propertyAccessExceptions.length; i++) {
113: ps.println("PropertyAccessException " + (i + 1) + ":");
114: this .propertyAccessExceptions[i].printStackTrace(ps);
115: }
116: }
117:
118: public void printStackTrace(PrintWriter pw) {
119: pw.println(getClass().getName()
120: + "; nested PropertyAccessException details ("
121: + getExceptionCount() + ") are:");
122: for (int i = 0; i < this .propertyAccessExceptions.length; i++) {
123: pw.println("PropertyAccessException " + (i + 1) + ":");
124: this .propertyAccessExceptions[i].printStackTrace(pw);
125: }
126: }
127:
128: public boolean contains(Class exClass) {
129: if (exClass == null) {
130: return false;
131: }
132: if (exClass.isInstance(this )) {
133: return true;
134: }
135: for (int i = 0; i < this .propertyAccessExceptions.length; i++) {
136: PropertyAccessException pae = this .propertyAccessExceptions[i];
137: if (pae.contains(exClass)) {
138: return true;
139: }
140: }
141: return false;
142: }
143:
144: }
|