1 package org.apache.ojb.broker.accesslayer; 2 3 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 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 51 protected ResultSetAndStatement resultSetAndStatment; 52 53 56 protected ClassDescriptor classDescriptor; 57 58 61 protected Constructor constructor; 62 63 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 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 97 private Identity getIdentityFromResultSet() 98 { 99 100 try 101 { 102 Constructor con = classDescriptor.getZeroArgumentConstructor(); 104 Object obj = ConstructorHelper.instantiate(con); 105 106 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 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 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 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 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 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 } 210 | Popular Tags |