KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > PropertyBatchUpdateException


1 /*
2  * Copyright 2002-2006 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;
18
19 import java.io.PrintStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 import org.springframework.util.Assert;
23
24 /**
25  * Combined exception, composed of individual PropertyAccessException instances.
26  * An object of this class is created at the beginning of the binding
27  * process, and errors added to it as necessary.
28  *
29  * <p>The binding process continues when it encounters application-level
30  * PropertyAccessExceptions, applying those changes that can be applied
31  * and storing rejected changes in an object of this class.
32  *
33  * @author Rod Johnson
34  * @author Juergen Hoeller
35  * @since 18 April 2001
36  */

37 public class PropertyBatchUpdateException extends BeansException {
38
39     /** List of PropertyAccessException objects */
40     private PropertyAccessException[] propertyAccessExceptions;
41
42
43     /**
44      * Create a new PropertyBatchUpdateException.
45      * @param propertyAccessExceptions the List of PropertyAccessExceptions
46      */

47     public PropertyBatchUpdateException(PropertyAccessException[] propertyAccessExceptions) {
48         super(null);
49         Assert.notEmpty(propertyAccessExceptions, "At least 1 PropertyAccessException required");
50         this.propertyAccessExceptions = propertyAccessExceptions;
51     }
52
53
54     /**
55      * If this returns 0, no errors were encountered during binding.
56      */

57     public final int getExceptionCount() {
58         return this.propertyAccessExceptions.length;
59     }
60
61     /**
62      * Return an array of the propertyAccessExceptions stored in this object.
63      * Will return the empty array (not null) if there were no errors.
64      */

65     public final PropertyAccessException[] getPropertyAccessExceptions() {
66         return this.propertyAccessExceptions;
67     }
68
69     /**
70      * Return the exception for this field, or <code>null</code> if there isn't one.
71      */

72     public PropertyAccessException getPropertyAccessException(String JavaDoc propertyName) {
73         for (int i = 0; i < this.propertyAccessExceptions.length; i++) {
74             PropertyAccessException pae = this.propertyAccessExceptions[i];
75             if (propertyName.equals(pae.getPropertyChangeEvent().getPropertyName())) {
76                 return pae;
77             }
78         }
79         return null;
80     }
81
82
83     public String JavaDoc getMessage() {
84         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Failed properties: ");
85         for (int i = 0; i < this.propertyAccessExceptions.length; i++) {
86             sb.append(this.propertyAccessExceptions[i].getMessage());
87             if (i < this.propertyAccessExceptions.length - 1) {
88                 sb.append("; ");
89             }
90         }
91         return sb.toString();
92     }
93
94     public String JavaDoc toString() {
95         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
96         sb.append(getClass().getName()).append("; nested PropertyAccessExceptions (");
97         sb.append(getExceptionCount()).append(") are:");
98         for (int i = 0; i < this.propertyAccessExceptions.length; i++) {
99             sb.append('\n').append("PropertyAccessException ").append(i + 1).append(": ");
100             sb.append(this.propertyAccessExceptions[i]);
101         }
102         return sb.toString();
103     }
104
105     public void printStackTrace(PrintStream JavaDoc ps) {
106         ps.println(getClass().getName() + "; nested PropertyAccessException details (" +
107                 getExceptionCount() + ") are:");
108         for (int i = 0; i < this.propertyAccessExceptions.length; i++) {
109             ps.println("PropertyAccessException " + (i + 1) + ":");
110             this.propertyAccessExceptions[i].printStackTrace(ps);
111         }
112     }
113
114     public void printStackTrace(PrintWriter JavaDoc pw) {
115         pw.println(getClass().getName() + "; nested PropertyAccessException details (" +
116                 getExceptionCount() + ") are:");
117         for (int i = 0; i < this.propertyAccessExceptions.length; i++) {
118             pw.println("PropertyAccessException " + (i + 1) + ":");
119             this.propertyAccessExceptions[i].printStackTrace(pw);
120         }
121     }
122
123     public boolean contains(Class JavaDoc exClass) {
124         if (exClass == null) {
125             return false;
126         }
127         if (exClass.isInstance(this)) {
128             return true;
129         }
130         for (int i = 0; i < this.propertyAccessExceptions.length; i++) {
131             PropertyAccessException pae = this.propertyAccessExceptions[i];
132             if (pae.contains(exClass)) {
133                 return true;
134             }
135         }
136         return false;
137     }
138
139 }
140
Popular Tags