1 | |
package org.apache.ojb.broker.accesslayer; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.lang.reflect.Constructor; |
19 | |
import java.sql.SQLException; |
20 | |
import java.util.Enumeration; |
21 | |
import java.util.NoSuchElementException; |
22 | |
|
23 | |
import org.apache.ojb.broker.Identity; |
24 | |
import org.apache.ojb.broker.PersistenceBroker; |
25 | |
import org.apache.ojb.broker.PersistenceBrokerException; |
26 | |
import org.apache.ojb.broker.PersistenceBrokerSQLException; |
27 | |
import org.apache.ojb.broker.metadata.ClassDescriptor; |
28 | |
import org.apache.ojb.broker.metadata.FieldDescriptor; |
29 | |
import org.apache.ojb.broker.query.Query; |
30 | |
import org.apache.ojb.broker.util.ConstructorHelper; |
31 | |
import org.apache.ojb.broker.util.logging.LoggerFactory; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class PkEnumeration implements Enumeration |
41 | |
{ |
42 | |
static final long serialVersionUID = -834955711995869884L; |
43 | |
protected boolean hasCalledCheck = false; |
44 | |
protected boolean hasNext = false; |
45 | |
|
46 | |
protected PersistenceBroker broker; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
protected ResultSetAndStatement resultSetAndStatment; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
protected ClassDescriptor classDescriptor; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
protected Constructor constructor; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public PkEnumeration(Query query, ClassDescriptor cld, Class primaryKeyClass, PersistenceBroker broker) |
71 | |
{ |
72 | |
this.resultSetAndStatment = broker.serviceJdbcAccess().executeQuery(query, cld); |
73 | |
this.classDescriptor = cld; |
74 | |
this.broker = broker; |
75 | |
|
76 | |
try |
77 | |
{ |
78 | |
Class[] argArray = {Identity.class}; |
79 | |
this.constructor = primaryKeyClass.getConstructor(argArray); |
80 | |
} |
81 | |
catch (NoSuchMethodException e) |
82 | |
{ |
83 | |
LoggerFactory.getDefaultLogger().error(primaryKeyClass.getName() |
84 | |
+ " must implement a Constructor with one argument of type org.apache.ojb.broker.Identity"); |
85 | |
throw new PersistenceBrokerException(e); |
86 | |
} |
87 | |
catch (SecurityException e) |
88 | |
{ |
89 | |
LoggerFactory.getDefaultLogger().error(e); |
90 | |
throw new PersistenceBrokerException(e); |
91 | |
} |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private Identity getIdentityFromResultSet() |
98 | |
{ |
99 | |
|
100 | |
try |
101 | |
{ |
102 | |
|
103 | |
Constructor con = classDescriptor.getZeroArgumentConstructor(); |
104 | |
Object obj = ConstructorHelper.instantiate(con); |
105 | |
|
106 | |
|
107 | |
Object colValue; |
108 | |
FieldDescriptor fld; |
109 | |
FieldDescriptor[] pkfields = classDescriptor.getPkFields(); |
110 | |
for (int i = 0; i < pkfields.length; i++) |
111 | |
{ |
112 | |
fld = pkfields[i]; |
113 | |
colValue = fld.getJdbcType().getObjectFromColumn(resultSetAndStatment.m_rs, fld.getColumnName()); |
114 | |
fld.getPersistentField().set(obj, colValue); |
115 | |
} |
116 | |
|
117 | |
return broker.serviceIdentity().buildIdentity(classDescriptor, obj); |
118 | |
} |
119 | |
catch (SQLException e) |
120 | |
{ |
121 | |
throw new PersistenceBrokerSQLException("Error reading object from column", e); |
122 | |
} |
123 | |
catch (Exception e) |
124 | |
{ |
125 | |
throw new PersistenceBrokerException("Error reading Identity from result set", e); |
126 | |
} |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public boolean hasMoreElements() |
136 | |
{ |
137 | |
try |
138 | |
{ |
139 | |
if (!hasCalledCheck) |
140 | |
{ |
141 | |
hasCalledCheck = true; |
142 | |
hasNext = resultSetAndStatment.m_rs.next(); |
143 | |
} |
144 | |
} |
145 | |
catch (SQLException e) |
146 | |
{ |
147 | |
LoggerFactory.getDefaultLogger().error(e); |
148 | |
|
149 | |
hasNext = false; |
150 | |
} |
151 | |
finally |
152 | |
{ |
153 | |
if(!hasNext) |
154 | |
{ |
155 | |
releaseDbResources(); |
156 | |
} |
157 | |
} |
158 | |
return hasNext; |
159 | |
} |
160 | |
|
161 | |
private void releaseDbResources() |
162 | |
{ |
163 | |
resultSetAndStatment.close(); |
164 | |
resultSetAndStatment = null; |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public Object nextElement() |
174 | |
{ |
175 | |
try |
176 | |
{ |
177 | |
if (!hasCalledCheck) |
178 | |
{ |
179 | |
hasMoreElements(); |
180 | |
} |
181 | |
hasCalledCheck = false; |
182 | |
if (hasNext) |
183 | |
{ |
184 | |
Identity oid = getIdentityFromResultSet(); |
185 | |
Identity[] args = {oid}; |
186 | |
return this.constructor.newInstance(args); |
187 | |
} |
188 | |
else |
189 | |
throw new NoSuchElementException(); |
190 | |
} |
191 | |
catch (Exception ex) |
192 | |
{ |
193 | |
LoggerFactory.getDefaultLogger().error(ex); |
194 | |
throw new NoSuchElementException(); |
195 | |
} |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
protected void finalize() |
202 | |
{ |
203 | |
if(resultSetAndStatment != null) |
204 | |
{ |
205 | |
LoggerFactory.getDefaultLogger().error("["+PkEnumeration.class.getName()+"] Found unclosed resources while finalize"); |
206 | |
releaseDbResources(); |
207 | |
} |
208 | |
} |
209 | |
} |