KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.access.util;
58
59 import java.io.PrintWriter JavaDoc;
60 import java.util.ArrayList JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.List JavaDoc;
64 import java.util.Map JavaDoc;
65
66 import org.apache.log4j.Level;
67 import org.apache.log4j.Logger;
68 import org.objectstyle.cayenne.CayenneException;
69 import org.objectstyle.cayenne.access.OperationObserver;
70 import org.objectstyle.cayenne.access.ResultIterator;
71 import org.objectstyle.cayenne.query.Query;
72 import org.objectstyle.cayenne.util.Util;
73
74 /**
75  * Simple implementation of OperationObserver interface. Useful as a superclass of other
76  * implementations of OperationObserver. This implementation only tracks transaction
77  * events and exceptions.
78  * <p>
79  * <i>This operation observer is unsafe to use in application, since it doesn't rethrow
80  * the exceptions immediately, and may cause the database to hang. Use
81  * {@link org.objectstyle.cayenne.access.QueryResult}instead. </i>
82  * </p>
83  *
84  * @author Andrei Adamchik
85  */

86 public class DefaultOperationObserver implements OperationObserver {
87
88     private static Logger logObj = Logger.getLogger(DefaultOperationObserver.class);
89
90     public static final Level DEFAULT_LOG_LEVEL = Query.DEFAULT_LOG_LEVEL;
91
92     protected List JavaDoc globalExceptions = new ArrayList JavaDoc();
93     protected Map JavaDoc queryExceptions = new HashMap JavaDoc();
94     protected Level loggingLevel = DEFAULT_LOG_LEVEL;
95
96     /**
97      * Prints the information about query and global exceptions.
98      */

99     public void printExceptions(PrintWriter JavaDoc out) {
100         if (globalExceptions.size() > 0) {
101             if (globalExceptions.size() == 1) {
102                 out.println("Global Exception:");
103             }
104             else {
105                 out.println("Global Exceptions:");
106             }
107
108             Iterator JavaDoc it = globalExceptions.iterator();
109             while (it.hasNext()) {
110                 Throwable JavaDoc th = (Throwable JavaDoc) it.next();
111                 th.printStackTrace(out);
112             }
113         }
114
115         if (queryExceptions.size() > 0) {
116             if (queryExceptions.size() == 1) {
117                 out.println("Query Exception:");
118             }
119             else {
120                 out.println("Query Exceptions:");
121             }
122
123             Iterator JavaDoc it = queryExceptions.keySet().iterator();
124             while (it.hasNext()) {
125                 Throwable JavaDoc th = (Throwable JavaDoc) queryExceptions.get(it.next());
126                 th.printStackTrace(out);
127             }
128         }
129     }
130
131     /** Returns a list of global exceptions that occured during data operation run. */
132     public List JavaDoc getGlobalExceptions() {
133         return globalExceptions;
134     }
135
136     /** Returns a list of exceptions that occured during data operation run by query. */
137     public Map JavaDoc getQueryExceptions() {
138         return queryExceptions;
139     }
140
141     /**
142      * Returns <code>true</code> if at least one exception was registered during query
143      * execution.
144      */

145     public boolean hasExceptions() {
146         return globalExceptions.size() > 0 || queryExceptions.size() > 0;
147     }
148
149     /**
150      * Returns a log level level that should be used when logging query execution.
151      */

152     public Level getLoggingLevel() {
153         return loggingLevel;
154     }
155
156     /**
157      * Sets log level that should be used for queries. If <code>level</code> argument is
158      * null, level is set to DEFAULT_LOG_LEVEL. If <code>level</code> is equal or higher
159      * than log level configured for QueryLogger, query SQL statements will be logged.
160      */

161     public void setLoggingLevel(Level level) {
162         this.loggingLevel = (level == null) ? DEFAULT_LOG_LEVEL : level;
163     }
164
165     public void nextCount(Query query, int resultCount) {
166     }
167
168     public void nextBatchCount(Query query, int[] resultCount) {
169         if (logObj.isDebugEnabled()) {
170             for (int i = 0; i < resultCount.length; i++) {
171                 logObj.debug("batch count: " + resultCount[i]);
172             }
173         }
174     }
175
176     public void nextDataRows(Query query, List JavaDoc dataRows) {
177         // noop
178
}
179
180     /**
181      * Closes ResultIterator without reading its data. If you implement a custom subclass,
182      * only call super if closing the iterator is what you need.
183      */

184     public void nextDataRows(Query query, ResultIterator it) {
185         if (it != null) {
186             try {
187                 it.close();
188             }
189             catch (CayenneException ex) {
190                 // don't throw here....
191
nextQueryException(query, ex);
192             }
193         }
194     }
195
196     /**
197      * Closes ResultIterator without reading its data. If you implement a custom subclass,
198      * only call super if closing the iterator is what you need.
199      *
200      * @since 1.2
201      */

202     public void nextGeneratedDataRows(Query query, ResultIterator keysIterator) {
203         if (keysIterator != null) {
204             try {
205                 keysIterator.close();
206             }
207             catch (CayenneException ex) {
208                 // don't throw here....
209
nextQueryException(query, ex);
210             }
211         }
212     }
213
214     public void nextQueryException(Query query, Exception JavaDoc ex) {
215         queryExceptions.put(query, Util.unwindException(ex));
216     }
217
218     public void nextGlobalException(Exception JavaDoc ex) {
219         globalExceptions.add(Util.unwindException(ex));
220     }
221
222     /**
223      * Returns <code>false</code>.
224      */

225     public boolean isIteratedResult() {
226         return false;
227     }
228 }
Popular Tags