1 package org.apache.ojb.jdo; 2 3 17 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.NoSuchElementException ; 21 22 import javax.jdo.Extent; 23 import javax.jdo.PersistenceManager; 24 25 import org.apache.ojb.broker.accesslayer.OJBIterator; 26 import org.apache.ojb.broker.accesslayer.RsIterator; 27 import org.apache.ojb.broker.metadata.ClassDescriptor; 28 import org.apache.ojb.broker.query.Criteria; 29 import org.apache.ojb.broker.query.QueryByCriteria; 30 import org.apache.ojb.broker.query.QueryFactory; 31 import org.apache.ojb.otm.OTMConnection; 32 33 38 public class ExtentImpl implements Extent 39 { 40 private Class m_clazz; 41 private OTMConnection m_conn; 42 private PersistenceManager m_pm; 43 private HashSet m_iterators = new HashSet (); 44 private Criteria m_criteria; 45 46 50 public ExtentImpl(Class pClazz, OTMConnection conn, PersistenceManager pm, boolean subclasses) 51 { 52 m_clazz = pClazz; 53 m_conn = conn; 54 m_pm = pm; 55 m_criteria = new Criteria(); 56 } 57 58 Class ojbGetClass() 59 { 60 return m_clazz; 61 } 62 63 66 public Iterator iterator() 67 { 68 QueryByCriteria q = QueryFactory.newQuery(m_clazz, m_criteria); 69 ExtentIterator itty = new ExtentIterator((OJBIterator) m_conn.getIteratorByQuery(q)); 70 m_iterators.add(itty); 71 return itty; 72 } 73 74 public boolean hasSubclasses() 75 { 76 ClassDescriptor cld = m_conn.getDescriptorFor(m_clazz); 77 return cld.isExtent(); 78 } 79 80 public Class getCandidateClass() 81 { 82 return m_clazz; 83 } 84 85 public PersistenceManager getPersistenceManager() 86 { 87 return m_pm; 88 } 89 90 public void closeAll() 91 { 92 for (Iterator iterator = m_iterators.iterator(); iterator.hasNext();) 93 { 94 ExtentIterator itty = (ExtentIterator) iterator.next(); 95 itty.close(); 96 iterator.remove(); 97 } 98 } 99 100 public void close(Iterator iterator) 101 { 102 if (iterator instanceof ExtentIterator && m_iterators.contains(iterator)) 103 { 104 m_iterators.remove(iterator); 105 ((ExtentIterator)iterator).close(); 106 } 107 } 108 109 private class ExtentIterator implements Iterator 110 { 111 private OJBIterator itty; 112 private boolean closed = false; 113 114 ExtentIterator(OJBIterator itty) 115 { 116 this.itty = itty; 117 } 118 119 public boolean hasNext() 120 { 121 if (closed) return false; 122 return itty.hasNext(); 123 } 124 125 public Object next() 126 { 127 if (closed) throw new NoSuchElementException ("Calling next() on closed JDO iterator"); 128 try 129 { 130 return itty.next(); 131 } 132 catch (RsIterator.ResourceClosedException e) 133 { 134 throw new NoSuchElementException ("Calling next() on closed JDO iterator"); 135 } 136 } 137 138 public void remove() 139 { 140 throw new UnsupportedOperationException ("Operation Not Allowd"); 141 } 142 143 private void close() 144 { 145 itty.releaseDbResources(); 146 closed = true; 147 } 148 } 149 } 150 | Popular Tags |