KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jdo > ExtentImpl


1 package org.apache.ojb.jdo;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.NoSuchElementException JavaDoc;
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 /**
34  * @author <a HREF="mailto:mattbaird@yahoo.com">Matthew Baird</a>
35  * @author <a HREF="mailto:brianm@apache.org">Brian McCallister</a>
36  * @version $Id: ExtentImpl.java,v 1.9.2.1 2005/12/21 22:28:40 tomdz Exp $
37  */

38 public class ExtentImpl implements Extent
39 {
40     private Class JavaDoc m_clazz;
41     private OTMConnection m_conn;
42     private PersistenceManager m_pm;
43     private HashSet JavaDoc m_iterators = new HashSet JavaDoc();
44     private Criteria m_criteria;
45
46     /**
47      * Constructor for ExtentImpl.
48      * @param subclasses is ignored
49      */

50     public ExtentImpl(Class JavaDoc 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 JavaDoc ojbGetClass()
59     {
60         return m_clazz;
61     }
62
63     /**
64      * @todo is this supposed to operate outside of a user specified tx? Right now it obtains one if needed
65      */

66     public Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc
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 JavaDoc next()
126         {
127             if (closed) throw new NoSuchElementException JavaDoc("Calling next() on closed JDO iterator");
128             try
129             {
130             return itty.next();
131             }
132             catch (RsIterator.ResourceClosedException e)
133             {
134                 throw new NoSuchElementException JavaDoc("Calling next() on closed JDO iterator");
135             }
136         }
137
138         public void remove()
139         {
140             throw new UnsupportedOperationException JavaDoc("Operation Not Allowd");
141         }
142
143         private void close()
144         {
145             itty.releaseDbResources();
146             closed = true;
147         }
148     }
149 }
150
Popular Tags