1 17 18 package org.sape.carbon.services.sqldataloader.mru; 19 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.sql.Connection ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.util.Comparator ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import org.sape.carbon.core.component.ComponentConfiguration; 36 import org.sape.carbon.core.component.lifecycle.Configurable; 37 import org.sape.carbon.core.config.InvalidConfigurationException; 38 import org.sape.carbon.core.util.reflection.BeanUtil; 39 import org.sape.carbon.services.cache.CacheLoadException; 40 import org.sape.carbon.services.cache.mru.MRUCacheDataLoader; 41 42 59 public class SqlBeanMapMRUDataLoader 60 implements MRUCacheDataLoader, Configurable { 61 62 63 private Log log = LogFactory.getLog(this.getClass()); 64 65 66 protected SqlBeanMapMRUDataLoaderConfiguration config = null; 67 68 69 75 public void configure(ComponentConfiguration configuration) { 76 this.config = (SqlBeanMapMRUDataLoaderConfiguration) configuration; 77 } 78 79 93 protected ResultSet retrieveResult(Connection connection, Object key) 94 throws SQLException { 95 96 PreparedStatement preparedStatement = 97 connection.prepareStatement(config.getDataLoadQuery()); 98 preparedStatement.setObject(1,key); 99 ResultSet resultSet = preparedStatement.executeQuery(); 100 return resultSet; 101 } 102 103 118 public Object loadDatum(Object key) throws CacheLoadException { 119 Connection connection = null; 120 String beanAttributeName = null; 121 try { 122 123 connection = (config.getConnectionFactory()).getConnection(); 125 126 ResultSet resultSet = retrieveResult(connection, key); 128 129 Map returnMap = getMapInstance(); 131 132 Set attributeSet = (config.getBeanAttribute()).entrySet(); 134 135 while (resultSet.next()) { 136 Object keyOfMap = resultSet.getObject(config.getKeyColumn()); 137 Object bean; 138 try { 139 bean = (config.getBeanClass()).newInstance(); 140 } catch (IllegalAccessException iae) { 141 throw new InvalidConfigurationException(this.getClass(), 142 this.config.getConfigurationName(), "BeanClass", 143 "Could not create instance of lookup bean", iae); 144 } catch (InstantiationException ie) { 145 throw new InvalidConfigurationException(this.getClass(), 146 this.config.getConfigurationName(), "BeanClass", 147 "Could not create instance of lookup bean", ie); 148 } 149 150 Iterator attributeIterator = attributeSet.iterator(); 153 154 while (attributeIterator.hasNext()) { 155 Map.Entry attribute = (Map.Entry ) attributeIterator.next(); 156 String columnName = (String ) attribute.getKey(); 157 beanAttributeName = (String ) attribute.getValue(); 158 BeanUtil.setObjectAttribute(bean, 159 beanAttributeName, 160 resultSet.getObject(columnName)); 161 } 162 returnMap.put(keyOfMap,bean); 163 } 164 return returnMap; 165 } catch (IllegalArgumentException iarge) { 166 throw new InvalidConfigurationException(this.getClass(), 167 this.config.getConfigurationName(), "BeanAttribute", 168 "Bean attribute type for " + beanAttributeName 169 + " and db column type do not match", iarge); 170 } catch (IllegalAccessException iae) { 171 throw new InvalidConfigurationException(this.getClass(), 172 this.config.getConfigurationName(), "BeanAttribute", 173 "Could not set attribute " + beanAttributeName 174 + " in bean class", iae); 175 } catch (NoSuchMethodException nsme) { 176 throw new InvalidConfigurationException(this.getClass(), 177 this.config.getConfigurationName(), "BeanAttribute", 178 "Could not set attribute " + beanAttributeName 179 + " in bean class", nsme); 180 } catch (InvocationTargetException ite) { 181 throw new InvalidConfigurationException(this.getClass(), 182 this.config.getConfigurationName(), "BeanAttribute", 183 "Could not set attribute " + beanAttributeName 184 + " in bean class", ite); 185 } catch (SQLException sqle) { 186 throw new CacheLoadException(getClass(), 187 "Could not load Cache from database" , sqle); 188 } finally { 189 if (connection != null) { 190 try { 191 connection.close(); 192 } catch (SQLException sqle) { 193 } 195 } 196 } 197 } 198 199 208 protected 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.getValueMapType()).getConstructor( 230 constructorParameters); 231 returnMap = (Map ) 232 constr.newInstance(constructorArguments); 233 } catch (IllegalAccessException iae) { 234 throw new InvalidConfigurationException(this.getClass(), 235 this.config.getConfigurationName(), "ValueMapType", 236 "Could not instantiate map instance", iae); 237 } catch (NoSuchMethodException nsme) { 238 throw new InvalidConfigurationException(this.getClass(), 239 this.config.getConfigurationName(), "ValueMapType", 240 "Could not instantiate map instance", nsme); 241 } catch (InstantiationException ie) { 242 throw new InvalidConfigurationException(this.getClass(), 243 this.config.getConfigurationName(), "ValueMapType", 244 "Could not instantiate map instance", ie); 245 } catch (InvocationTargetException ite) { 246 throw new InvalidConfigurationException(this.getClass(), 247 this.config.getConfigurationName(), "ValueMapType", 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 value map instance without comparator of type " + 256 config.getValueMapType().getName()); 257 } 258 returnMap = (Map ) (config.getValueMapType()).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 |