001: package org.apache.ojb.jdo;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * 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:
018: import java.util.HashSet;
019: import java.util.Iterator;
020: import java.util.NoSuchElementException;
021:
022: import javax.jdo.Extent;
023: import javax.jdo.PersistenceManager;
024:
025: import org.apache.ojb.broker.accesslayer.OJBIterator;
026: import org.apache.ojb.broker.accesslayer.RsIterator;
027: import org.apache.ojb.broker.metadata.ClassDescriptor;
028: import org.apache.ojb.broker.query.Criteria;
029: import org.apache.ojb.broker.query.QueryByCriteria;
030: import org.apache.ojb.broker.query.QueryFactory;
031: import org.apache.ojb.otm.OTMConnection;
032:
033: /**
034: * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
035: * @author <a href="mailto:brianm@apache.org">Brian McCallister</a>
036: * @version $Id: ExtentImpl.java,v 1.9.2.1 2005/12/21 22:28:40 tomdz Exp $
037: */
038: public class ExtentImpl implements Extent {
039: private Class m_clazz;
040: private OTMConnection m_conn;
041: private PersistenceManager m_pm;
042: private HashSet m_iterators = new HashSet();
043: private Criteria m_criteria;
044:
045: /**
046: * Constructor for ExtentImpl.
047: * @param subclasses is ignored
048: */
049: public ExtentImpl(Class pClazz, OTMConnection conn,
050: PersistenceManager pm, boolean subclasses) {
051: m_clazz = pClazz;
052: m_conn = conn;
053: m_pm = pm;
054: m_criteria = new Criteria();
055: }
056:
057: Class ojbGetClass() {
058: return m_clazz;
059: }
060:
061: /**
062: * @todo is this supposed to operate outside of a user specified tx? Right now it obtains one if needed
063: */
064: public Iterator iterator() {
065: QueryByCriteria q = QueryFactory.newQuery(m_clazz, m_criteria);
066: ExtentIterator itty = new ExtentIterator((OJBIterator) m_conn
067: .getIteratorByQuery(q));
068: m_iterators.add(itty);
069: return itty;
070: }
071:
072: public boolean hasSubclasses() {
073: ClassDescriptor cld = m_conn.getDescriptorFor(m_clazz);
074: return cld.isExtent();
075: }
076:
077: public Class getCandidateClass() {
078: return m_clazz;
079: }
080:
081: public PersistenceManager getPersistenceManager() {
082: return m_pm;
083: }
084:
085: public void closeAll() {
086: for (Iterator iterator = m_iterators.iterator(); iterator
087: .hasNext();) {
088: ExtentIterator itty = (ExtentIterator) iterator.next();
089: itty.close();
090: iterator.remove();
091: }
092: }
093:
094: public void close(Iterator iterator) {
095: if (iterator instanceof ExtentIterator
096: && m_iterators.contains(iterator)) {
097: m_iterators.remove(iterator);
098: ((ExtentIterator) iterator).close();
099: }
100: }
101:
102: private class ExtentIterator implements Iterator {
103: private OJBIterator itty;
104: private boolean closed = false;
105:
106: ExtentIterator(OJBIterator itty) {
107: this .itty = itty;
108: }
109:
110: public boolean hasNext() {
111: if (closed)
112: return false;
113: return itty.hasNext();
114: }
115:
116: public Object next() {
117: if (closed)
118: throw new NoSuchElementException(
119: "Calling next() on closed JDO iterator");
120: try {
121: return itty.next();
122: } catch (RsIterator.ResourceClosedException e) {
123: throw new NoSuchElementException(
124: "Calling next() on closed JDO iterator");
125: }
126: }
127:
128: public void remove() {
129: throw new UnsupportedOperationException(
130: "Operation Not Allowd");
131: }
132:
133: private void close() {
134: itty.releaseDbResources();
135: closed = true;
136: }
137: }
138: }
|