1 46 package org.bsf.remoteIterator.server; 47 48 import org.bsf.commons.ejb.SessionAdapterBean; 49 import org.bsf.remoteIterator.common.ColumnMetadata; 50 import org.bsf.remoteIterator.common.RemoteIteratorResultTable; 51 52 import javax.ejb.CreateException ; 53 import java.sql.*; 54 import java.util.ArrayList ; 55 import java.util.List ; 56 57 87 public class RemoteIteratorBean extends SessionAdapterBean { 88 92 private static final int RI_BUFFER_SIZE = 2000; 94 private static final int RI_DEFAULT_BLOCK_SIZE = 150; 95 96 private static final String EMPTY_STRING = ""; 97 private static final String JNDI_DATASOURCE_NAME = "DBPool"; 98 99 103 106 private int _lastReadBlockSize = 0; 107 108 112 private int _position = 0; 113 114 private String _SQLQuery; 115 private List _columnMetadatas; 116 private int _defaultBlockSize = RI_DEFAULT_BLOCK_SIZE; 117 118 private transient Statement _statement; 120 private transient ResultSet _resultSet; 121 private transient Connection _connection; 122 123 127 130 private void createMetadatas() { 131 _columnMetadatas = new ArrayList (); 132 133 try { 134 ResultSetMetaData resultSetMetaData = _resultSet.getMetaData(); 135 136 int nbColums = resultSetMetaData.getColumnCount(); 137 138 for ( int col = 1 ; col <= nbColums ; col++ ) { 139 ColumnMetadata temp = new ColumnMetadata(); 140 141 temp.setColumnClassName( resultSetMetaData.getColumnClassName( col ) ); 142 temp.setColumnName( resultSetMetaData.getColumnName( col ) ); 143 temp.setSchemaName( resultSetMetaData.getSchemaName( col ) ); 144 temp.setTableName( resultSetMetaData.getTableName( col ) ); 145 temp.setColumnType( resultSetMetaData.getColumnType( col ) ); 146 temp.setColumnTypeName( resultSetMetaData.getColumnTypeName( col ) ); 147 temp.setPrecision( resultSetMetaData.getPrecision( col ) ); 148 temp.setScale( resultSetMetaData.getScale( col ) ); 149 150 _columnMetadatas.add( temp ); 151 } 152 } catch( SQLException exception ) { 153 handleSQLException( exception ); 154 } 155 } 156 157 165 public void ejbCreate( String p_query ) throws CreateException { 166 logGraphBegin( "create" ); 167 168 if ( p_query == null || EMPTY_STRING.equals( p_query ) ) { 170 handleExceptionAsSystemException( new RuntimeException ( "The p_query shouldn't be null !!!" ) ); 171 } 172 173 _SQLQuery = p_query; 175 176 firstQueryExecution(); 178 179 createMetadatas(); 181 182 logGraphEnd( "create" ); 183 } 184 185 188 public void ejbRemove() { 189 logGraphBegin( "ejbRemove " ); 190 191 closeDataSource(); 193 194 logGraphEnd( "ejbRemove" ); 195 } 196 197 201 208 public List getColumnMetaData() { 209 return _columnMetadatas; 210 } 211 212 221 public RemoteIteratorResultTable absolute( int p_position ) { 222 return absolute( p_position, RI_DEFAULT_BLOCK_SIZE ); 223 } 224 225 234 public RemoteIteratorResultTable absolute( int p_position, int p_blockSize ) { 235 try { 236 _resultSet.absolute( p_position ); 237 } catch( SQLException sqlException ) { 238 handleSQLException( sqlException ); 240 } 241 242 return next( p_blockSize ); 244 } 245 246 257 public RemoteIteratorResultTable previous() { 258 return previous( _defaultBlockSize ); 259 } 260 261 274 public RemoteIteratorResultTable previous( int p_blockSize ) { 275 String logMessage = "previous(" + p_blockSize + ")"; 276 logGraphBegin( logMessage ); 277 278 RemoteIteratorResultTable remoteIteratorResultTable = new RemoteIteratorResultTable( 0 ); 280 281 try { 282 int positionToReach = _resultSet.getRow() - _lastReadBlockSize - p_blockSize; 284 285 if ( positionToReach <= 0 ) { 286 _resultSet.beforeFirst(); 287 } else { 288 _resultSet.absolute( positionToReach ); 289 } 290 remoteIteratorResultTable = next( p_blockSize ); 291 } catch( SQLException e ) { 292 handleSQLException( e ); 294 } 295 296 logGraphEnd( logMessage ); 297 298 return remoteIteratorResultTable; 299 } 300 301 310 public RemoteIteratorResultTable next() { 311 return next( _defaultBlockSize ); 312 } 313 314 325 public RemoteIteratorResultTable next( int p_blockSize ) { 326 String logMessage = "next(" + p_blockSize + ")"; 327 logGraphBegin( logMessage ); 328 329 int readBlockPosition = 0; 330 boolean willBeFirst = false; 331 332 RemoteIteratorResultTable remoteIteratorResultTable = new RemoteIteratorResultTable( p_blockSize ); 334 335 try { 336 if ( isBeforeFirst() ) { 337 willBeFirst = true; 339 } 340 341 if ( isAfterLast() ) { 342 remoteIteratorResultTable.setAlreadyLast( true ); 344 remoteIteratorResultTable.setLast( true ); 345 346 return remoteIteratorResultTable; 348 } 349 350 352 boolean lastNotReached = true; 355 356 while ( ( readBlockPosition < p_blockSize ) && ( lastNotReached = _resultSet.next() ) ) { 357 addCurrentResultSetRow( remoteIteratorResultTable ); 359 360 readBlockPosition++; 362 363 if ( isLast() ) { 365 logDebug( "end position found :" + getPosition() ); 366 } 367 } 368 369 _lastReadBlockSize = readBlockPosition; 371 372 if ( willBeFirst ) { 374 remoteIteratorResultTable.setFirst( ( true ) ); 375 } 376 377 if ( !lastNotReached || isAfterLast() || isLast() ) { 379 remoteIteratorResultTable.setLast( ( true ) ); 380 } 381 } catch( SQLException e ) { 382 handleSQLException( e ); 384 } 385 386 logGraphEnd( logMessage ); 387 388 return remoteIteratorResultTable; 389 } 390 391 397 public Long getRowCount() { 398 logGraphBegin( "getRowCount" ); 399 400 long totalSize = 0; 401 402 try { 403 int currentPosition = getPosition(); 405 406 _resultSet.last(); 408 409 totalSize = getPosition(); 411 412 if ( currentPosition == 0 ) { 414 _resultSet.beforeFirst(); 415 } else { 416 _resultSet.absolute( currentPosition ); 417 } 418 } catch( SQLException eSQL ) { 419 handleSQLException( eSQL ); 420 } 421 422 logGraphEnd( "getRowCount: found " + totalSize ); 423 424 return new Long ( totalSize ); 425 } 426 427 430 private boolean isLast() throws SQLException { 431 return _resultSet.isLast(); 432 } 433 434 437 private boolean isAfterLast() throws SQLException { 438 return _resultSet.isAfterLast(); 439 } 440 441 444 private boolean isBeforeFirst() throws SQLException { 445 return _resultSet.isBeforeFirst(); 446 } 447 448 452 private int getPosition() throws SQLException { 453 if ( _resultSet == null ) 454 return 0; 455 else 456 return _resultSet.getRow(); 457 } 458 459 462 private void restorePosition( int p_position, int p_lastReadBlockSize ) throws SQLException { 463 if ( p_position > 0 ) { 464 _resultSet.absolute( p_position ); 465 } 466 467 _lastReadBlockSize = p_lastReadBlockSize; 468 } 469 470 478 private void addCurrentResultSetRow( RemoteIteratorResultTable p_resultTable ) throws SQLException { 479 int columnCount = _resultSet.getMetaData().getColumnCount(); 480 ArrayList row = new ArrayList ( columnCount ); 481 482 for ( int columnIndex = 0 ; columnIndex < columnCount ; columnIndex++ ) { 483 row.add( _resultSet.getObject( columnIndex + 1 ) ); 485 } 486 487 p_resultTable.addRow( row, new Integer ( _resultSet.getRow() ) ); 489 } 490 491 495 private void prepareQuery() throws SQLException { 496 logGraphBegin( "prepareQuery" ); 497 498 _statement = _connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY ); 500 501 _statement.setFetchSize( RemoteIteratorBean.RI_BUFFER_SIZE ); 503 504 logGraphEnd( "prepareQuery" ); 505 } 506 507 512 private void executeQuery() throws SQLException { 513 logGraphBegin( "executeQuery \nQuery : " + _SQLQuery ); 514 515 try { 516 prepareQuery(); 518 519 _lastReadBlockSize = 0; 521 522 if ( _statement == null ) 523 throw new SQLException( "Statement null ! You must call prepareQuery() before any executeQuery() ! : " ); 524 525 _resultSet = _statement.executeQuery( _SQLQuery ); 527 } catch( SQLException sqlException ) { 528 logFatal( "executeQuery : error while executing query " + _SQLQuery, sqlException ); 529 530 throw sqlException; 531 } 532 } 533 534 538 private void firstQueryExecution() { 539 try { 541 openDataSource(); 542 543 executeQuery(); 544 } catch( SQLException Se ) { 545 closeDataSource(); 547 handleSQLException( Se ); 548 } 549 } 550 551 554 private void openDataSource() throws SQLException { 555 logGraphBegin( "openDataSource" ); 556 557 _connection = getConnection( JNDI_DATASOURCE_NAME ); 558 } 559 560 563 private void closeDataSource() { 564 logGraphBegin( "closeDataSource" ); 565 566 try { 567 if ( _resultSet != null ) _resultSet.close(); 568 } catch( SQLException Se ) { 569 logError( Se.getLocalizedMessage() ); 570 } 571 572 try { 573 if ( _statement != null ) _statement.close(); 574 } catch( SQLException e ) { 575 logError( e.getLocalizedMessage() ); 576 } 577 578 try { 579 if ( _connection != null ) _connection.close(); 580 } catch( SQLException e ) { 581 logError( e.getLocalizedMessage() ); 582 } 583 584 logGraphEnd( "closeDataSource" ); 585 } 586 587 592 public void setDefaultBlockSize( Integer p_defaultBlockSize ) { 593 if ( p_defaultBlockSize == null ) 594 _defaultBlockSize = RI_DEFAULT_BLOCK_SIZE; 595 else 596 _defaultBlockSize = p_defaultBlockSize.intValue(); 597 } 598 599 603 public void ejbActivate() { 604 logGraphBegin( "ejbActivate" ); 605 606 try { 607 int oldPosition = _position; 609 int oldLastReadBlockSize = _lastReadBlockSize; 610 611 firstQueryExecution(); 613 614 restorePosition( oldPosition, oldLastReadBlockSize ); 616 } catch( SQLException Se ) { 617 handleSQLException( Se ); 618 } 619 620 logDebug( " State after reactivation : lastReadBlockSize = " + _lastReadBlockSize + ", position = " + _position ); 621 622 logGraphEnd( "ejbActivate " ); 623 } 624 625 public void ejbPassivate() { 626 logGraphBegin( "ejbPassivate " ); 627 628 try { 629 if ( _resultSet != null ) { 630 _position = getPosition(); 632 } else { 633 _position = 0; 634 } 635 636 closeDataSource(); 638 } catch( SQLException e ) { 639 handleSQLException( e ); 640 } 641 642 logDebug( " State for passivation: lastReadBlockSize = " + _lastReadBlockSize + ", position = " + _position ); 643 logGraphEnd( "ejbPassivate" ); 644 } 645 } | Popular Tags |