1 17 18 package org.sape.carbon.services.sqldataloader.total; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import java.lang.reflect.Constructor ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.sql.Connection ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.util.Comparator ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import org.sape.carbon.core.component.ComponentConfiguration; 35 import org.sape.carbon.core.component.lifecycle.Configurable; 36 import org.sape.carbon.core.config.InvalidConfigurationException; 37 import org.sape.carbon.core.util.reflection.BeanUtil; 38 import org.sape.carbon.services.cache.CacheLoadException; 39 import org.sape.carbon.services.cache.total.ConfigurableMapTypeCacheDataLoader; 40 41 42 58 public class SqlBeanDataLoader implements ConfigurableMapTypeCacheDataLoader, 59 Configurable { 60 61 62 private Log log = LogFactory.getLog(this.getClass()); 63 64 65 protected SqlBeanDataLoaderConfiguration config = null; 66 67 68 74 public void configure(ComponentConfiguration configuration) { 75 this.config = (SqlBeanDataLoaderConfiguration) configuration; 76 } 77 78 91 protected ResultSet retrieveResult(Connection connection) 92 throws SQLException { 93 94 PreparedStatement preparedStatement = 95 connection.prepareStatement(config.getDataLoadQuery()); 96 ResultSet resultSet = preparedStatement.executeQuery(); 97 return resultSet; 98 } 99 100 113 public Map loadAllData() throws CacheLoadException { 114 Connection connection = null; 115 String beanAttributeName = null; 116 try { 117 118 connection = (config.getConnectionFactory()).getConnection(); 120 121 ResultSet resultSet = retrieveResult(connection); 123 124 Map returnMap = getMapInstance(); 126 127 Set attributeSet = (config.getBeanAttribute()).entrySet(); 129 130 while (resultSet.next()) { 131 Object key = resultSet.getObject(config.getKeyColumn()); 132 Object bean; 133 try { 134 bean = (config.getBeanClass()).newInstance(); 135 } catch (IllegalAccessException iae) { 136 throw new InvalidConfigurationException(this.getClass(), 137 this.config.getConfigurationName(), "BeanClass", 138 "Could not create instance of lookup bean", iae); 139 } catch (InstantiationException ie) { 140 throw new InvalidConfigurationException(this.getClass(), 141 this.config.getConfigurationName(), "BeanClass", 142 "Could not create instance of lookup bean", ie); 143 } 144 Iterator attributeIterator = attributeSet.iterator(); 147 148 while (attributeIterator.hasNext()) { 149 Map.Entry attribute = (Map.Entry ) attributeIterator.next(); 150 String columnName = (String ) attribute.getKey(); 151 beanAttributeName = (String ) attribute.getValue(); 152 log.info("BEAN DATA LOADER COLUMN NAME IS " + columnName); 153 log.info("BEAN DATA LOADER ATTRIBUTE NAME IS " + beanAttributeName); 154 log.info("DATA TYPE OF COLUMN IS " + resultSet.getObject(columnName).getClass()); 155 BeanUtil.setObjectAttribute(bean, 156 beanAttributeName, 157 resultSet.getObject(columnName)); 158 } 159 returnMap.put(key,bean); 160 } 161 return returnMap; 162 } catch (IllegalArgumentException iarge) { 163 throw new InvalidConfigurationException(this.getClass(), 164 this.config.getConfigurationName(), "BeanAttribute", 165 "Bean attribute type for " + beanAttributeName 166 + " and db column type do not match", iarge); 167 } catch (IllegalAccessException iae) { 168 throw new InvalidConfigurationException(this.getClass(), 169 this.config.getConfigurationName(), "BeanAttribute", 170 "Could not set attribute " + beanAttributeName 171 + " in bean class", iae); 172 } catch (NoSuchMethodException nsme) { 173 throw new InvalidConfigurationException(this.getClass(), 174 this.config.getConfigurationName(), "BeanAttribute", 175 "Could not set attribute " + beanAttributeName 176 + " in bean class", nsme); 177 } catch (InvocationTargetException ite) { 178 throw new InvalidConfigurationException(this.getClass(), 179 this.config.getConfigurationName(), "BeanAttribute", 180 "Could not set attribute " + beanAttributeName 181 + " in bean class", ite); 182 } catch (SQLException sqle) { 183 throw new CacheLoadException(getClass(), 184 "Could not load Cache from database" , sqle); 185 } finally { 186 if (connection != null) { 187 try { 188 connection.close(); 189 } catch (SQLException sqle) { 190 } 192 } 193 } 194 } 195 196 208 public Map getMapInstance() { 209 210 Map returnMap; 211 if (this.config.getComparator() != null) { 213 Class [] constructorParameters = new Class [] 214 {Comparator .class}; 215 Object [] constructorArguments; 216 try { 217 constructorArguments = new Object [] 218 {(config.getComparator()).newInstance()}; 219 } catch (IllegalAccessException iae) { 220 throw new InvalidConfigurationException(this.getClass(), 221 this.config.getConfigurationName(), "Comparator", 222 "Could not instantiate comparator instance", iae); 223 } catch (InstantiationException ie) { 224 throw new InvalidConfigurationException(this.getClass(), 225 this.config.getConfigurationName(), "Comparator", 226 "Could not instantiate comparator instance", ie); 227 } 228 try { 229 Constructor constr = (config.getMapType()).getConstructor( 230 constructorParameters); 231 returnMap = (Map ) 232 constr.newInstance(constructorArguments); 233 } catch (IllegalAccessException iae) { 234 throw new InvalidConfigurationException(this.getClass(), 235 this.config.getConfigurationName(), "MapType", 236 "Could not instantiate map instance", iae); 237 } catch (NoSuchMethodException nsme) { 238 throw new InvalidConfigurationException(this.getClass(), 239 this.config.getConfigurationName(), "MapType", 240 "Could not instantiate map instance", nsme); 241 } catch (InstantiationException ie) { 242 throw new InvalidConfigurationException(this.getClass(), 243 this.config.getConfigurationName(), "MapType", 244 "Could not instantiate map instance", ie); 245 } catch (InvocationTargetException ite) { 246 throw new InvalidConfigurationException(this.getClass(), 247 this.config.getConfigurationName(), "MapType", 248 "Could not instantiate map instance", ite); 249 } 250 } else { 251 try { 253 if (log.isTraceEnabled()) { 254 log.trace("No Comparator implementation was provided. " + 255 "Creating map instance without comparator of type " + 256 config.getMapType().getName()); 257 } 258 returnMap = (Map ) (config.getMapType()).newInstance(); 259 } catch (IllegalAccessException iae) { 260 throw new InvalidConfigurationException(this.getClass(), 261 this.config.getConfigurationName(), "MapType", 262 "Could not instantiate map instance", iae); 263 } catch (InstantiationException ie) { 264 throw new InvalidConfigurationException(this.getClass(), 265 this.config.getConfigurationName(), "MapType", 266 "Could not instantiate map instance", ie); 267 } 268 } 269 return returnMap; 270 } 271 272 } | Popular Tags |