KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > util > DefaultOperationObserver


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access.util;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.cayenne.CayenneException;
30 import org.apache.cayenne.access.OperationObserver;
31 import org.apache.cayenne.access.ResultIterator;
32 import org.apache.cayenne.query.Query;
33 import org.apache.cayenne.util.Util;
34
35 /**
36  * Simple implementation of OperationObserver interface. Useful as a superclass of other
37  * implementations of OperationObserver. This implementation only tracks transaction
38  * events and exceptions.
39  * <p>
40  * <i>This operation observer is unsafe to use in application, since it doesn't rethrow
41  * the exceptions immediately, and may cause the database to hang.</i>
42  * </p>
43  *
44  * @author Andrus Adamchik
45  */

46 public class DefaultOperationObserver implements OperationObserver {
47
48     protected List JavaDoc globalExceptions = new ArrayList JavaDoc();
49     protected Map JavaDoc queryExceptions = new HashMap JavaDoc();
50
51     /**
52      * Prints the information about query and global exceptions.
53      */

54     public void printExceptions(PrintWriter JavaDoc out) {
55         if (globalExceptions.size() > 0) {
56             if (globalExceptions.size() == 1) {
57                 out.println("Global Exception:");
58             }
59             else {
60                 out.println("Global Exceptions:");
61             }
62
63             Iterator JavaDoc it = globalExceptions.iterator();
64             while (it.hasNext()) {
65                 Throwable JavaDoc th = (Throwable JavaDoc) it.next();
66                 th.printStackTrace(out);
67             }
68         }
69
70         if (queryExceptions.size() > 0) {
71             if (queryExceptions.size() == 1) {
72                 out.println("Query Exception:");
73             }
74             else {
75                 out.println("Query Exceptions:");
76             }
77
78             Iterator JavaDoc it = queryExceptions.keySet().iterator();
79             while (it.hasNext()) {
80                 Throwable JavaDoc th = (Throwable JavaDoc) queryExceptions.get(it.next());
81                 th.printStackTrace(out);
82             }
83         }
84     }
85
86     /** Returns a list of global exceptions that occured during data operation run. */
87     public List JavaDoc getGlobalExceptions() {
88         return globalExceptions;
89     }
90
91     /** Returns a list of exceptions that occured during data operation run by query. */
92     public Map JavaDoc getQueryExceptions() {
93         return queryExceptions;
94     }
95
96     /**
97      * Returns <code>true</code> if at least one exception was registered during query
98      * execution.
99      */

100     public boolean hasExceptions() {
101         return globalExceptions.size() > 0 || queryExceptions.size() > 0;
102     }
103
104     public void nextCount(Query query, int resultCount) {
105     }
106
107     public void nextBatchCount(Query query, int[] resultCount) {
108
109     }
110
111     public void nextDataRows(Query query, List JavaDoc dataRows) {
112         // noop
113
}
114
115     /**
116      * Closes ResultIterator without reading its data. If you implement a custom subclass,
117      * only call super if closing the iterator is what you need.
118      */

119     public void nextDataRows(Query query, ResultIterator it) {
120         if (it != null) {
121             try {
122                 it.close();
123             }
124             catch (CayenneException ex) {
125                 // don't throw here....
126
nextQueryException(query, ex);
127             }
128         }
129     }
130
131     /**
132      * Closes ResultIterator without reading its data. If you implement a custom subclass,
133      * only call super if closing the iterator is what you need.
134      *
135      * @since 1.2
136      */

137     public void nextGeneratedDataRows(Query query, ResultIterator keysIterator) {
138         if (keysIterator != null) {
139             try {
140                 keysIterator.close();
141             }
142             catch (CayenneException ex) {
143                 // don't throw here....
144
nextQueryException(query, ex);
145             }
146         }
147     }
148
149     public void nextQueryException(Query query, Exception JavaDoc ex) {
150         queryExceptions.put(query, Util.unwindException(ex));
151     }
152
153     public void nextGlobalException(Exception JavaDoc ex) {
154         globalExceptions.add(Util.unwindException(ex));
155     }
156
157     /**
158      * Returns <code>false</code>.
159      */

160     public boolean isIteratedResult() {
161         return false;
162     }
163 }
164
Popular Tags