001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.controls.system.jdbc;
020:
021: import org.apache.beehive.controls.api.ControlException;
022: import org.apache.beehive.controls.api.context.ControlBeanContext;
023:
024: import java.lang.reflect.Method;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.util.Calendar;
028: import java.util.NoSuchElementException;
029:
030: /**
031: * Used by DefaultIteratorResultSetMapper for mapping a ResultSet to an Iterator type.
032: */
033: public class ResultSetIterator implements java.util.Iterator {
034:
035: private final Class _returnClass;
036: private final ResultSet _rs;
037: private final RowMapper _rowMapper;
038:
039: private boolean _primed = false;
040:
041: /**
042: * Create a new ResultSetIterator.
043: * @param context A ControlBeanContext.
044: * @param method The annotated method.
045: * @param rs The ResultSet to map.
046: * @param cal A Calendar instance for mapping date/time values.
047: */
048: ResultSetIterator(ControlBeanContext context, Method method,
049: ResultSet rs, Calendar cal) {
050: _rs = rs;
051:
052: JdbcControl.SQL methodSQL = (JdbcControl.SQL) context
053: .getMethodPropertySet(method, JdbcControl.SQL.class);
054: _returnClass = methodSQL.iteratorElementType();
055:
056: if (_returnClass == null) {
057: throw new ControlException(
058: "Invalid return class declared for Iterator:"
059: + _returnClass.getName());
060: }
061:
062: _rowMapper = RowMapperFactory.getRowMapper(rs, _returnClass,
063: cal);
064: }
065:
066: /**
067: * Does this iterater have more elements?
068: * @return true if there is another element
069: */
070: public boolean hasNext() {
071: if (_primed) {
072: return true;
073: }
074:
075: try {
076: _primed = _rs.next();
077: } catch (SQLException sqle) {
078: return false;
079: }
080: return _primed;
081: }
082:
083: /**
084: * Get the next element in the iteration.
085: * @return The next element in the iteration.
086: */
087: public Object next() {
088: try {
089: if (!_primed) {
090: _primed = _rs.next();
091: if (!_primed) {
092: throw new NoSuchElementException();
093: }
094: }
095: // reset upon consumption
096: _primed = false;
097: return _rowMapper.mapRowToReturnType();
098: } catch (SQLException e) {
099: // Since Iterator interface is locked, all we can do
100: // is put the real exception inside an expected one.
101: NoSuchElementException xNoSuch = new NoSuchElementException(
102: "ResultSet exception: " + e);
103: xNoSuch.initCause(e);
104: throw xNoSuch;
105: }
106: }
107:
108: /**
109: * Remove is currently not supported.
110: */
111: public void remove() {
112: throw new UnsupportedOperationException("remove not supported");
113: }
114: }
|