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;
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: synchronized (ps) {
110: ps.println(getClass().getName()
111: + "; nested PropertyAccessException details ("
112: + getExceptionCount() + ") are:");
113: for (int i = 0; i < this .propertyAccessExceptions.length; i++) {
114: ps.println("PropertyAccessException " + (i + 1) + ":");
115: this .propertyAccessExceptions[i].printStackTrace(ps);
116: }
117: }
118: }
119:
120: public void printStackTrace(PrintWriter pw) {
121: synchronized (pw) {
122: pw.println(getClass().getName()
123: + "; nested PropertyAccessException details ("
124: + getExceptionCount() + ") are:");
125: for (int i = 0; i < this .propertyAccessExceptions.length; i++) {
126: pw.println("PropertyAccessException " + (i + 1) + ":");
127: this .propertyAccessExceptions[i].printStackTrace(pw);
128: }
129: }
130: }
131:
132: public boolean contains(Class exType) {
133: if (exType == null) {
134: return false;
135: }
136: if (exType.isInstance(this )) {
137: return true;
138: }
139: for (int i = 0; i < this .propertyAccessExceptions.length; i++) {
140: PropertyAccessException pae = this .propertyAccessExceptions[i];
141: if (pae.contains(exType)) {
142: return true;
143: }
144: }
145: return false;
146: }
147:
148: }
|