KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.apache.cayenne.access.util;
22
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.cayenne.CayenneException;
29 import org.apache.cayenne.CayenneRuntimeException;
30 import org.apache.cayenne.access.ResultIterator;
31 import org.apache.cayenne.query.Query;
32
33 /**
34  * OperationObserver that is used to track the execution
35  * of SelectQueries with results returned as ResultIterator.
36  *
37  * @author Andrus Adamchik
38  */

39 public class IteratedSelectObserver extends DefaultOperationObserver {
40     protected ResultIterator resultIterator;
41
42     public boolean isIteratedResult() {
43         return true;
44     }
45
46     public void nextDataRows(Query query, List JavaDoc dataRows) {
47         throw new CayenneRuntimeException("Results unexpectedly returned as list.");
48     }
49
50     public void nextDataRows(Query q, ResultIterator it) {
51         // don't call super - it closes the iterator
52
resultIterator = it;
53     }
54
55     public ResultIterator getResultIterator() throws CayenneException {
56         if (super.hasExceptions()) {
57             StringWriter JavaDoc str = new StringWriter JavaDoc();
58             PrintWriter JavaDoc out = new PrintWriter JavaDoc(str);
59             super.printExceptions(out);
60
61             try {
62                 out.close();
63                 str.close();
64             } catch (IOException JavaDoc ioex) {
65                 // this should never happen
66
}
67
68             throw new CayenneException(
69                 "Error getting ResultIterator: " + str.getBuffer());
70         }
71
72         return resultIterator;
73     }
74
75 }
76
Popular Tags