1 16 17 package org.springframework.jca.cci.core; 18 19 import java.sql.SQLException ; 20 21 import javax.resource.NotSupportedException ; 22 import javax.resource.ResourceException ; 23 import javax.resource.cci.Connection ; 24 import javax.resource.cci.ConnectionFactory ; 25 import javax.resource.cci.ConnectionSpec ; 26 import javax.resource.cci.IndexedRecord ; 27 import javax.resource.cci.Interaction ; 28 import javax.resource.cci.InteractionSpec ; 29 import javax.resource.cci.MappedRecord ; 30 import javax.resource.cci.Record ; 31 import javax.resource.cci.RecordFactory ; 32 import javax.resource.cci.ResultSet ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 import org.springframework.dao.DataAccessException; 38 import org.springframework.dao.DataAccessResourceFailureException; 39 import org.springframework.jca.cci.CannotCreateRecordException; 40 import org.springframework.jca.cci.CciOperationNotSupportedException; 41 import org.springframework.jca.cci.InvalidResultSetAccessException; 42 import org.springframework.jca.cci.RecordTypeNotSupportedException; 43 import org.springframework.jca.cci.connection.ConnectionFactoryUtils; 44 import org.springframework.jca.cci.connection.NotSupportedRecordFactory; 45 import org.springframework.util.Assert; 46 47 74 public class CciTemplate implements CciOperations { 75 76 private final Log logger = LogFactory.getLog(getClass()); 77 78 private ConnectionFactory connectionFactory; 79 80 private ConnectionSpec connectionSpec; 81 82 private RecordCreator outputRecordCreator; 83 84 85 90 public CciTemplate() { 91 } 92 93 98 public CciTemplate(ConnectionFactory connectionFactory) { 99 setConnectionFactory(connectionFactory); 100 afterPropertiesSet(); 101 } 102 103 110 public CciTemplate(ConnectionFactory connectionFactory, ConnectionSpec connectionSpec) { 111 setConnectionFactory(connectionFactory); 112 setConnectionSpec(connectionSpec); 113 afterPropertiesSet(); 114 } 115 116 117 120 public void setConnectionFactory(ConnectionFactory connectionFactory) { 121 this.connectionFactory = connectionFactory; 122 } 123 124 127 public ConnectionFactory getConnectionFactory() { 128 return this.connectionFactory; 129 } 130 131 135 public void setConnectionSpec(ConnectionSpec connectionSpec) { 136 this.connectionSpec = connectionSpec; 137 } 138 139 142 public ConnectionSpec getConnectionSpec() { 143 return this.connectionSpec; 144 } 145 146 158 public void setOutputRecordCreator(RecordCreator creator) { 159 outputRecordCreator = creator; 160 } 161 162 165 public RecordCreator getOutputRecordCreator() { 166 return this.outputRecordCreator; 167 } 168 169 public void afterPropertiesSet() { 170 if (getConnectionFactory() == null) { 171 throw new IllegalArgumentException ("Property 'connectionFactory' is required"); 172 } 173 } 174 175 176 185 public CciTemplate getDerivedTemplate(ConnectionSpec connectionSpec) { 186 CciTemplate derived = new CciTemplate(); 187 derived.setConnectionFactory(getConnectionFactory()); 188 derived.setConnectionSpec(connectionSpec); 189 derived.setOutputRecordCreator(getOutputRecordCreator()); 190 return derived; 191 } 192 193 194 public Object execute(ConnectionCallback action) throws DataAccessException { 195 Assert.notNull(action, "Callback object must not be null"); 196 197 Connection con = ConnectionFactoryUtils.getConnection(getConnectionFactory(), getConnectionSpec()); 198 try { 199 return action.doInConnection(con, getConnectionFactory()); 200 } 201 catch (NotSupportedException ex) { 202 throw new CciOperationNotSupportedException("CCI operation not supported by connector", ex); 203 } 204 catch (ResourceException ex) { 205 throw new DataAccessResourceFailureException("CCI operation failed", ex); 206 } 207 catch (SQLException ex) { 208 throw new InvalidResultSetAccessException("Parsing of CCI ResultSet failed", ex); 209 } 210 finally { 211 ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory()); 212 } 213 } 214 215 public Object execute(final InteractionCallback action) throws DataAccessException { 216 Assert.notNull(action, "Callback object must not be null"); 217 218 return execute(new ConnectionCallback() { 219 public Object doInConnection(Connection connection, ConnectionFactory connectionFactory) 220 throws ResourceException , SQLException , DataAccessException { 221 222 Interaction interaction = connection.createInteraction(); 223 try { 224 return action.doInInteraction(interaction, connectionFactory); 225 } 226 finally { 227 closeInteraction(interaction); 228 } 229 } 230 }); 231 } 232 233 public Record execute(InteractionSpec spec, Record inputRecord) throws DataAccessException { 234 return (Record ) doExecute(spec, inputRecord, null, null); 235 } 236 237 public void execute(InteractionSpec spec, Record inputRecord, Record outputRecord) throws DataAccessException { 238 doExecute(spec, inputRecord, outputRecord, null); 239 } 240 241 public Record execute(InteractionSpec spec, RecordCreator inputCreator) throws DataAccessException { 242 return (Record ) doExecute(spec, createRecord(inputCreator), null, null); 243 } 244 245 public Object execute(InteractionSpec spec, Record inputRecord, RecordExtractor outputExtractor) 246 throws DataAccessException { 247 248 return doExecute(spec, inputRecord, null, outputExtractor); 249 } 250 251 public Object execute(InteractionSpec spec, RecordCreator inputCreator, RecordExtractor outputExtractor) 252 throws DataAccessException { 253 254 return doExecute(spec, createRecord(inputCreator), null, outputExtractor); 255 } 256 257 268 protected Object doExecute( 269 final InteractionSpec spec, final Record inputRecord, final Record outputRecord, 270 final RecordExtractor outputExtractor) throws DataAccessException { 271 272 return execute(new InteractionCallback() { 273 public Object doInInteraction(Interaction interaction, ConnectionFactory connectionFactory) 274 throws ResourceException , SQLException , DataAccessException { 275 276 Record outputRecordToUse = outputRecord; 277 try { 278 if (outputRecord != null || getOutputRecordCreator() != null) { 279 if (outputRecord == null) { 281 RecordFactory recordFactory = getRecordFactory(connectionFactory); 282 outputRecordToUse = getOutputRecordCreator().createRecord(recordFactory); 283 } 284 interaction.execute(spec, inputRecord, outputRecordToUse); 285 } 286 else { 287 outputRecordToUse = interaction.execute(spec, inputRecord); 288 } 289 if (outputExtractor != null) { 290 return outputExtractor.extractData(outputRecordToUse); 291 } 292 else { 293 return outputRecordToUse; 294 } 295 } 296 finally { 297 if (outputRecordToUse instanceof ResultSet ) { 298 closeResultSet((ResultSet ) outputRecordToUse); 299 } 300 } 301 } 302 }); 303 } 304 305 306 314 public IndexedRecord createIndexedRecord(String name) throws DataAccessException { 315 try { 316 RecordFactory recordFactory = getRecordFactory(getConnectionFactory()); 317 return recordFactory.createIndexedRecord(name); 318 } 319 catch (NotSupportedException ex) { 320 throw new RecordTypeNotSupportedException("Creation of indexed Record not supported by connector", ex); 321 } 322 catch (ResourceException ex) { 323 throw new CannotCreateRecordException("Creation of indexed Record failed", ex); 324 } 325 } 326 327 335 public MappedRecord createMappedRecord(String name) throws DataAccessException { 336 try { 337 RecordFactory recordFactory = getRecordFactory(getConnectionFactory()); 338 return recordFactory.createMappedRecord(name); 339 } 340 catch (NotSupportedException ex) { 341 throw new RecordTypeNotSupportedException("Creation of mapped Record not supported by connector", ex); 342 } 343 catch (ResourceException ex) { 344 throw new CannotCreateRecordException("Creation of mapped Record failed", ex); 345 } 346 } 347 348 357 protected Record createRecord(RecordCreator recordCreator) throws DataAccessException { 358 try { 359 RecordFactory recordFactory = getRecordFactory(getConnectionFactory()); 360 return recordCreator.createRecord(recordFactory); 361 } 362 catch (NotSupportedException ex) { 363 throw new RecordTypeNotSupportedException( 364 "Creation of the desired Record type not supported by connector", ex); 365 } 366 catch (ResourceException ex) { 367 throw new CannotCreateRecordException("Creation of the desired Record failed", ex); 368 } 369 } 370 371 382 protected RecordFactory getRecordFactory(ConnectionFactory connectionFactory) throws ResourceException { 383 try { 384 return getConnectionFactory().getRecordFactory(); 385 } 386 catch (NotSupportedException ex) { 387 return new NotSupportedRecordFactory(); 388 } 389 } 390 391 392 398 private void closeInteraction(Interaction interaction) { 399 if (interaction != null) { 400 try { 401 interaction.close(); 402 } 403 catch (ResourceException ex) { 404 logger.debug("Could not close CCI Interaction", ex); 405 } 406 catch (Throwable ex) { 407 logger.debug("Unexpected exception on closing CCI Interaction", ex); 409 } 410 } 411 } 412 413 419 private void closeResultSet(ResultSet resultSet) { 420 if (resultSet != null) { 421 try { 422 resultSet.close(); 423 } 424 catch (SQLException ex) { 425 logger.debug("Could not close CCI ResultSet", ex); 426 } 427 catch (Throwable ex) { 428 logger.debug("Unexpected exception on closing CCI ResultSet", ex); 430 } 431 } 432 } 433 434 } 435 | Popular Tags |