1 17 18 package org.sape.carbon.services.sql; 19 20 21 import java.sql.CallableStatement ; 22 import java.sql.Connection ; 23 import java.sql.PreparedStatement ; 24 import java.sql.SQLException ; 25 import java.sql.Statement ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import org.sape.carbon.core.component.ComponentConfiguration; 30 import org.sape.carbon.core.component.lifecycle.Configurable; 31 import org.sape.carbon.core.config.InvalidConfigurationException; 32 import org.sape.carbon.services.sql.connection.ConnectionFactory; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 38 88 public class DefaultStatementFactoryImpl 89 implements StatementFactory, Configurable { 90 91 92 95 private Log log = LogFactory.getLog(this.getClass()); 96 97 100 protected String statementFactoryName; 101 102 105 protected StatementFactoryConfiguration config; 106 107 110 protected Map statementsMap; 111 112 116 protected static final String RESULT_SET_CONFIGURATION_ERROR = 117 "You must either configure both the ResultSet Type and it's " 118 + "Concurrency, or neither."; 119 120 124 protected static final String CONNECTION_FACTORY_CONFIGURATION_ERROR = 125 "You must configure a valid Configuration Factory reference in " 126 + "either the main StatementFactoryConfiguration or in the specific " 127 + "StatementConfiguration."; 128 129 130 140 protected PreparedStatement buildPreparedStatement( 141 StatementConfiguration statementConfig, 142 Connection connection) 143 throws SQLException { 144 145 ResultSetConcurrencyEnum concurrency = null; 146 ResultSetTypeEnum type = null; 147 148 concurrency = 149 (statementConfig.getResultSetConcurrency() != null) 150 ? statementConfig.getResultSetConcurrency() 151 : config.getResultSetConcurrency(); 152 153 type = 154 (statementConfig.getResultSetType() != null) 155 ? statementConfig.getResultSetType() 156 : config.getResultSetType(); 157 158 PreparedStatement statement = null; 159 if ((concurrency != null) && (type != null)) { 160 statement = connection.prepareStatement( 161 statementConfig.getQuery(), 162 statementConfig.getResultSetType().getOrdinal(), 163 statementConfig.getResultSetConcurrency().getOrdinal()); 164 } else if ((concurrency != null) || (type != null)) { 165 throw new InvalidConfigurationException( 166 this.getClass(), 167 this.config.getConfigurationName(), 168 "ResultSet(Type|Concurrency)", 169 RESULT_SET_CONFIGURATION_ERROR); 170 } else { 171 statement = connection.prepareStatement( 172 statementConfig.getQuery()); 173 } 174 return statement; 175 } 176 177 187 public PreparedStatement createPreparedStatement( 188 String queryName, 189 Connection connection) 190 throws StatementFactoryException { 191 192 PreparedStatement statement = null; 193 194 StatementConfiguration statementConfig = 195 retrieveConfiguration(queryName, this.getClass()); 196 try { 197 198 statement = buildPreparedStatement(statementConfig, connection); 199 200 configureStatement(statement, statementConfig); 201 202 } catch (SQLException se) { 203 throw new StatementFactoryException(this.getClass(), 204 "Failure to create PreparedStatement " 205 + buildDebugMessage(statementConfig), se); 206 } 207 208 if (log.isTraceEnabled()) { 209 log.trace("Prepared Statement created: " 210 + buildDebugMessage(statementConfig)); 211 } 212 213 return statement; 214 } 215 216 217 231 public PreparedStatement createPreparedStatement(String queryName) 232 throws StatementFactoryException { 233 234 PreparedStatement statement = null; 235 236 StatementConfiguration statementConfig = 237 retrieveConfiguration(queryName, this.getClass()); 238 239 Connection connection = null; 240 241 try { 242 ConnectionFactory connectionFactory = 243 (statementConfig.getConnectionFactory() != null) 244 ? statementConfig.getConnectionFactory() 245 : this.config.getConnectionFactory(); 246 247 if (connectionFactory == null) { 248 throw new InvalidConfigurationException( 249 this.getClass(), this.config.getConfigurationName(), 250 "ConnectionFactory", 251 CONNECTION_FACTORY_CONFIGURATION_ERROR); 252 } 253 254 connection = connectionFactory.getConnection(); 256 257 statement = buildPreparedStatement(statementConfig, connection); 260 261 configureStatement(statement, statementConfig); 263 264 } catch (SQLException se) { 265 if (connection != null) { 268 try { 269 connection.close(); 270 } catch (SQLException se2) { 271 } 273 } 274 throw new StatementFactoryException(this.getClass(), 275 "Failure to create prepared statement " 276 + buildDebugMessage(statementConfig), 277 se); 278 } 279 if (log.isTraceEnabled()) { 280 log.trace("Prepared Statement created: " 281 + buildDebugMessage(statementConfig)); 282 } 283 return statement; 284 } 285 286 296 protected CallableStatement buildCallableStatement( 297 StatementConfiguration statementConfig, 298 Connection connection) 299 throws SQLException { 300 301 ResultSetConcurrencyEnum concurrency = null; 302 ResultSetTypeEnum type = null; 303 304 concurrency = 305 (statementConfig.getResultSetConcurrency() != null) 306 ? statementConfig.getResultSetConcurrency() 307 : config.getResultSetConcurrency(); 308 309 type = 310 (statementConfig.getResultSetType() != null) 311 ? statementConfig.getResultSetType() 312 : config.getResultSetType(); 313 314 CallableStatement statement = null; 315 if ((concurrency != null) && (type != null)) { 316 statement = connection.prepareCall( 317 statementConfig.getQuery(), 318 statementConfig.getResultSetType().getOrdinal(), 319 statementConfig.getResultSetConcurrency().getOrdinal()); 320 321 } else if ((concurrency != null) || (type != null)) { 322 throw new InvalidConfigurationException( 323 this.getClass(), 324 this.config.getConfigurationName(), 325 "ResultSet(Type|Concurrency)", 326 RESULT_SET_CONFIGURATION_ERROR); 327 328 } else { 329 statement = connection.prepareCall( 330 statementConfig.getQuery()); 331 } 332 return statement; 333 } 334 335 345 public CallableStatement createCallableStatement( 346 String queryName, 347 Connection connection) 348 throws StatementFactoryException { 349 350 CallableStatement statement = null; 351 StatementConfiguration statementConfig = 352 retrieveConfiguration(queryName, this.getClass()); 353 354 try { 355 statement = buildCallableStatement(statementConfig, connection); 356 357 configureStatement(statement, statementConfig); 358 359 } catch (SQLException se) { 360 throw new StatementFactoryException(this.getClass(), 361 "Failure to create callable statement " 362 + buildDebugMessage(statementConfig), 363 se); 364 } 365 366 if (log.isTraceEnabled()) { 367 log.trace("Callable Statement created " 368 + buildDebugMessage(statementConfig)); 369 } 370 371 return statement; 372 } 373 374 388 public CallableStatement createCallableStatement(String queryName) 389 throws StatementFactoryException { 390 391 CallableStatement statement = null; 392 StatementConfiguration statementConfig = 393 retrieveConfiguration(queryName, this.getClass()); 394 395 Connection connection = null; 396 try { 397 ConnectionFactory connectionFactory = 398 (statementConfig.getConnectionFactory() != null) 399 ? statementConfig.getConnectionFactory() 400 : this.config.getConnectionFactory(); 401 402 if (connectionFactory == null) { 403 throw new InvalidConfigurationException( 404 this.getClass(), this.config.getConfigurationName(), 405 "ConnectionFactory", 406 CONNECTION_FACTORY_CONFIGURATION_ERROR); 407 } 408 409 connection = connectionFactory.getConnection(); 410 411 statement = createCallableStatement(queryName, connection); 412 413 configureStatement(statement, statementConfig); 414 415 } catch (SQLException se) { 416 if (connection != null) { 419 try { 420 connection.close(); 421 } catch (SQLException se2) { 422 } 424 } 425 throw new StatementFactoryException(this.getClass(), 426 "Failure to create callable statement " 427 + buildDebugMessage(statementConfig), 428 se); 429 } 430 431 if (log.isTraceEnabled()) { 432 log.trace("Callable Statement created " 433 + buildDebugMessage(statementConfig)); 434 } 435 return statement; 436 } 437 438 439 449 protected StatementConfiguration retrieveConfiguration( 450 String queryName, 451 Class sourceClass) 452 throws StatementNotConfiguredException { 453 454 StatementConfiguration statementConfiguration = 455 (StatementConfiguration) 456 this.statementsMap.get(queryName); 457 458 if (statementConfiguration == null) { 459 throw new StatementNotConfiguredException( 460 sourceClass, queryName, this.statementFactoryName); 461 } 462 463 return statementConfiguration; 464 } 465 466 472 public void configure(ComponentConfiguration configuration) { 473 StatementConfiguration[] statements; 474 475 this.config = (StatementFactoryConfiguration) configuration; 476 477 this.statementsMap = new HashMap (); 478 479 StatementFactoryConfiguration config = 480 (StatementFactoryConfiguration) configuration; 481 482 this.statementFactoryName = config.getConfigurationName(); 483 484 statements = config.getStatement(); 485 486 for (int i = 0; i < statements.length; i++) { 487 this.statementsMap.put(statements[i].getQueryName(), statements[i]); 488 } 489 490 if (log.isTraceEnabled()) { 491 log.trace("Configured statement factory [" 492 + this.statementFactoryName + "] with [ " 493 + statements.length + "] Statements"); 494 } 495 } 496 497 507 protected Statement configureStatement( 508 Statement statement, 509 StatementConfiguration sqlStatement) 510 throws SQLException { 511 512 Integer maxRows = 513 (sqlStatement.getMaxRows() != null) 514 ? sqlStatement.getMaxRows() 515 : this.config.getMaxRows(); 516 517 if (maxRows != null) { 518 statement.setMaxRows(maxRows.intValue()); 519 } 520 521 Integer fetchSize = 522 (sqlStatement.getFetchSize() != null) 523 ? sqlStatement.getFetchSize() 524 : this.config.getFetchSize(); 525 526 if (fetchSize != null) { 527 statement.setFetchSize(fetchSize.intValue()); 528 } 529 530 531 Integer maxFieldSize = 532 (sqlStatement.getMaxFieldSize() != null) 533 ? sqlStatement.getMaxFieldSize() 534 : this.config.getMaxFieldSize(); 535 536 if (maxFieldSize != null) { 537 statement.setMaxFieldSize(maxFieldSize.intValue()); 538 } 539 540 Integer queryTimeout = 541 (sqlStatement.getQueryTimeOut() != null) 542 ? sqlStatement.getQueryTimeOut() 543 : this.config.getQueryTimeOut(); 544 545 if (queryTimeout != null) { 546 statement.setQueryTimeout(queryTimeout.intValue()); 547 } 548 549 return statement; 550 } 551 552 553 561 protected String buildDebugMessage(StatementConfiguration statementConfig) { 562 StringBuffer buf = new StringBuffer (); 563 buf.append("StatementFactory ["); 564 buf.append(this.statementFactoryName); 565 buf.append("], Query Name ["); 566 buf.append(statementConfig.getQueryName()); 567 buf.append("] configured: {"); 568 buf.append("Fetch Size: "); 569 buf.append(statementConfig.getFetchSize()); 570 buf.append(", Max Field Size: "); 571 buf.append(statementConfig.getMaxFieldSize()); 572 buf.append(", Max Rows: "); 573 buf.append(statementConfig.getMaxRows()); 574 buf.append(", Query Timeout: "); 575 buf.append(statementConfig.getQueryTimeOut()); 576 buf.append(", Concurrency: "); 577 buf.append(statementConfig.getResultSetConcurrency()); 578 buf.append(", Result Set Type: "); 579 buf.append(statementConfig.getResultSetType()); 580 buf.append(", Sql Query: "); 581 buf.append(statementConfig.getQuery()); 582 583 return buf.toString(); 584 } 585 } | Popular Tags |