1 22 package org.jboss.varia.stats; 23 24 import org.jboss.system.ServiceMBeanSupport; 25 import org.jboss.system.ServiceMBean; 26 import org.jboss.naming.NonSerializableFactory; 27 28 import javax.sql.DataSource ; 29 import javax.management.ObjectName ; 30 import javax.naming.NamingException ; 31 import javax.naming.Name ; 32 import javax.naming.InitialContext ; 33 import java.sql.SQLException ; 34 import java.sql.Connection ; 35 import java.sql.DatabaseMetaData ; 36 import java.sql.SQLWarning ; 37 import java.sql.Statement ; 38 import java.sql.CallableStatement ; 39 import java.sql.PreparedStatement ; 40 import java.sql.ResultSet ; 41 import java.sql.Array ; 42 import java.sql.Blob ; 43 import java.sql.Clob ; 44 import java.sql.Date ; 45 import java.sql.Ref ; 46 import java.sql.ResultSetMetaData ; 47 import java.sql.Time ; 48 import java.sql.Timestamp ; 49 50 import java.sql.Savepoint ; 51 import java.sql.ParameterMetaData ; 52 53 import java.io.PrintWriter ; 54 import java.io.InputStream ; 55 import java.io.Reader ; 56 import java.util.Map ; 57 import java.util.Calendar ; 58 import java.math.BigDecimal ; 59 import java.net.URL ; 60 61 67 public class DataSourceInterceptor 68 extends ServiceMBeanSupport 69 implements DataSource , DataSourceInterceptorMBean 70 { 71 74 private String bindName; 75 78 private String targetName; 79 82 private DataSource target; 83 84 private ObjectName statsCollector; 85 86 88 91 public ObjectName getStatsCollector() 92 { 93 return statsCollector; 94 } 95 96 99 public void setStatsCollector(ObjectName statsCollector) 100 { 101 this.statsCollector = statsCollector; 102 } 103 104 107 public String getBindName() 108 { 109 return bindName; 110 } 111 112 115 public void setBindName(String bindName) throws NamingException 116 { 117 this.bindName = bindName; 118 if(getState() == ServiceMBean.STARTED) 119 { 120 bind(); 121 } 122 } 123 124 127 public String getTargetName() 128 { 129 return targetName; 130 } 131 132 135 public void setTargetName(String targetName) throws NamingException 136 { 137 this.targetName = targetName; 138 if(getState() == ServiceMBean.STARTED) 139 { 140 updateTarget(); 141 } 142 } 143 144 public void startService() 145 throws Exception 146 { 147 updateTarget(); 148 bind(); 149 } 150 151 public void stopService() 152 throws Exception 153 { 154 unbind(); 155 } 156 157 159 public int getLoginTimeout() throws SQLException 160 { 161 return target.getLoginTimeout(); 162 } 163 164 public void setLoginTimeout(int seconds) throws SQLException 165 { 166 target.setLoginTimeout(seconds); 167 } 168 169 public PrintWriter getLogWriter() throws SQLException 170 { 171 return target.getLogWriter(); 172 } 173 174 public void setLogWriter(PrintWriter out) throws SQLException 175 { 176 target.setLogWriter(out); 177 } 178 179 public Connection getConnection() throws SQLException 180 { 181 return new ConnectionInterceptor(target.getConnection()); 182 } 183 184 public Connection getConnection(String username, String password) throws SQLException 185 { 186 return new ConnectionInterceptor(target.getConnection()); 187 } 188 189 191 public class ConnectionInterceptor 192 implements Connection 193 { 194 private final Connection target; 195 196 public ConnectionInterceptor(Connection target) 197 { 198 this.target = target; 199 } 200 201 202 public int getHoldability() throws SQLException 203 { 204 return target.getHoldability(); 205 } 206 207 208 public int getTransactionIsolation() throws SQLException 209 { 210 return target.getTransactionIsolation(); 211 } 212 213 public void clearWarnings() throws SQLException 214 { 215 target.clearWarnings(); 216 } 217 218 public void close() throws SQLException 219 { 220 target.close(); 221 } 222 223 public void commit() throws SQLException 224 { 225 target.commit(); 226 } 227 228 public void rollback() throws SQLException 229 { 230 target.rollback(); 231 } 232 233 public boolean getAutoCommit() throws SQLException 234 { 235 return target.getAutoCommit(); 236 } 237 238 public boolean isClosed() throws SQLException 239 { 240 return target.isClosed(); 241 } 242 243 public boolean isReadOnly() throws SQLException 244 { 245 return target.isReadOnly(); 246 } 247 248 249 public void setHoldability(int holdability) throws SQLException 250 { 251 target.setHoldability(holdability); 252 } 253 254 255 public void setTransactionIsolation(int level) throws SQLException 256 { 257 target.setTransactionIsolation(level); 258 } 259 260 public void setAutoCommit(boolean autoCommit) throws SQLException 261 { 262 target.setAutoCommit(autoCommit); 263 } 264 265 public void setReadOnly(boolean readOnly) throws SQLException 266 { 267 target.setReadOnly(readOnly); 268 } 269 270 public String getCatalog() throws SQLException 271 { 272 return target.getCatalog(); 273 } 274 275 public void setCatalog(String catalog) throws SQLException 276 { 277 target.setCatalog(catalog); 278 } 279 280 public DatabaseMetaData getMetaData() throws SQLException 281 { 282 return target.getMetaData(); 283 } 284 285 public SQLWarning getWarnings() throws SQLException 286 { 287 return target.getWarnings(); 288 } 289 290 291 public Savepoint setSavepoint() throws SQLException 292 { 293 return target.setSavepoint(); 294 } 295 296 297 298 public void releaseSavepoint(Savepoint savepoint) throws SQLException 299 { 300 target.releaseSavepoint(savepoint); 301 } 302 303 304 305 public void rollback(Savepoint savepoint) throws SQLException 306 { 307 target.rollback(savepoint); 308 } 309 310 311 public Statement createStatement() throws SQLException 312 { 313 return new StatementInterceptor(this, target.createStatement()); 314 } 315 316 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException 317 { 318 return new StatementInterceptor(this, target.createStatement(resultSetType, resultSetConcurrency)); 319 } 320 321 322 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) 323 throws SQLException 324 { 325 return new StatementInterceptor(this, 326 target.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability)); 327 } 328 329 330 public Map getTypeMap() throws SQLException 331 { 332 return target.getTypeMap(); 333 } 334 335 public void setTypeMap(Map map) throws SQLException 336 { 337 target.setTypeMap(map); 338 } 339 340 public String nativeSQL(String sql) throws SQLException 341 { 342 return target.nativeSQL(sql); 343 } 344 345 public CallableStatement prepareCall(String sql) throws SQLException 346 { 347 return target.prepareCall(sql); 348 } 349 350 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) 351 throws SQLException 352 { 353 return target.prepareCall(sql, resultSetType, resultSetConcurrency); 354 } 355 356 357 public CallableStatement prepareCall(String sql, 358 int resultSetType, 359 int resultSetConcurrency, 360 int resultSetHoldability) throws SQLException 361 { 362 return target.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); 363 } 364 365 366 public PreparedStatement prepareStatement(String sql) throws SQLException 367 { 368 logSql(sql); 369 return new PreparedStatementInterceptor(this, target.prepareStatement(sql)); 370 } 371 372 373 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException 374 { 375 logSql(sql); 376 return new PreparedStatementInterceptor(this, target.prepareStatement(sql, autoGeneratedKeys)); 377 } 378 379 380 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) 381 throws SQLException 382 { 383 logSql(sql); 384 return new PreparedStatementInterceptor(this, 385 target.prepareStatement(sql, resultSetType, resultSetConcurrency)); 386 } 387 388 389 public PreparedStatement prepareStatement(String sql, 390 int resultSetType, 391 int resultSetConcurrency, 392 int resultSetHoldability) throws SQLException 393 { 394 logSql(sql); 395 return new PreparedStatementInterceptor(this, 396 target.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); 397 } 398 399 400 401 public PreparedStatement prepareStatement(String sql, int columnIndexes[]) throws SQLException 402 { 403 logSql(sql); 404 return new PreparedStatementInterceptor(this, target.prepareStatement(sql, columnIndexes)); 405 } 406 407 408 409 public Savepoint setSavepoint(String name) throws SQLException 410 { 411 return target.setSavepoint(name); 412 } 413 414 415 416 public PreparedStatement prepareStatement(String sql, String columnNames[]) throws SQLException 417 { 418 logSql(sql); 419 return new PreparedStatementInterceptor(this, target.prepareStatement(sql, columnNames)); 420 } 421 422 } 423 424 public class StatementInterceptor 425 implements Statement 426 { 427 private final Connection con; 428 private final Statement target; 429 430 public StatementInterceptor(Connection con, Statement target) 431 { 432 this.con = con; 433 this.target = target; 434 } 435 436 public int getFetchDirection() throws SQLException 437 { 438 return target.getFetchDirection(); 439 } 440 441 public int getFetchSize() throws SQLException 442 { 443 return target.getFetchSize(); 444 } 445 446 public int getMaxFieldSize() throws SQLException 447 { 448 return target.getMaxFieldSize(); 449 } 450 451 public int getMaxRows() throws SQLException 452 { 453 return target.getMaxRows(); 454 } 455 456 public int getQueryTimeout() throws SQLException 457 { 458 return target.getQueryTimeout(); 459 } 460 461 public int getResultSetConcurrency() throws SQLException 462 { 463 return target.getResultSetConcurrency(); 464 } 465 466 467 public int getResultSetHoldability() throws SQLException 468 { 469 return target.getResultSetHoldability(); 470 } 471 472 473 public int getResultSetType() throws SQLException 474 { 475 return target.getResultSetType(); 476 } 477 478 public int getUpdateCount() throws SQLException 479 { 480 return target.getUpdateCount(); 481 } 482 483 public void cancel() throws SQLException 484 { 485 target.cancel(); 486 } 487 488 public void clearBatch() throws SQLException 489 { 490 target.clearBatch(); 491 } 492 493 public void clearWarnings() throws SQLException 494 { 495 target.clearWarnings(); 496 } 497 498 public void close() throws SQLException 499 { 500 target.close(); 501 } 502 503 public boolean getMoreResults() throws SQLException 504 { 505 return target.getMoreResults(); 506 } 507 508 public int[] executeBatch() throws SQLException 509 { 510 return target.executeBatch(); 511 } 512 513 public void setFetchDirection(int direction) throws SQLException 514 { 515 target.setFetchDirection(direction); 516 } 517 518 public void setFetchSize(int rows) throws SQLException 519 { 520 target.setFetchSize(rows); 521 } 522 523 public void setMaxFieldSize(int max) throws SQLException 524 { 525 target.setMaxFieldSize(max); 526 } 527 528 public void setMaxRows(int max) throws SQLException 529 { 530 target.setMaxRows(max); 531 } 532 533 public void setQueryTimeout(int seconds) throws SQLException 534 { 535 target.setQueryTimeout(seconds); 536 } 537 538 539 public boolean getMoreResults(int current) throws SQLException 540 { 541 return target.getMoreResults(current); 542 } 543 544 545 public void setEscapeProcessing(boolean enable) throws SQLException 546 { 547 target.setEscapeProcessing(enable); 548 } 549 550 public int executeUpdate(String sql) throws SQLException 551 { 552 logSql(sql); 553 return target.executeUpdate(sql); 554 } 555 556 public void addBatch(String sql) throws SQLException 557 { 558 logSql(sql); 559 target.addBatch(sql); 560 } 561 562 public void setCursorName(String name) throws SQLException 563 { 564 target.setCursorName(name); 565 } 566 567 public boolean execute(String sql) throws SQLException 568 { 569 logSql(sql); 570 return target.execute(sql); 571 } 572 573 574 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException 575 { 576 logSql(sql); 577 return target.executeUpdate(sql, autoGeneratedKeys); 578 } 579 580 581 582 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException 583 { 584 logSql(sql); 585 return target.execute(sql, autoGeneratedKeys); 586 } 587 588 589 590 public int executeUpdate(String sql, int columnIndexes[]) throws SQLException 591 { 592 logSql(sql); 593 return target.executeUpdate(sql, columnIndexes); 594 } 595 596 597 598 public boolean execute(String sql, int columnIndexes[]) throws SQLException 599 { 600 logSql(sql); 601 return target.execute(sql, columnIndexes); 602 } 603 604 605 public Connection getConnection() throws SQLException 606 { 607 return con; 608 } 609 610 611 public ResultSet getGeneratedKeys() throws SQLException 612 { 613 return target.getGeneratedKeys(); 614 } 615 616 617 public ResultSet getResultSet() throws SQLException 618 { 619 return target.getResultSet(); 620 } 621 622 public SQLWarning getWarnings() throws SQLException 623 { 624 return target.getWarnings(); 625 } 626 627 628 public int executeUpdate(String sql, String columnNames[]) throws SQLException 629 { 630 logSql(sql); 631 return target.executeUpdate(sql, columnNames); 632 } 633 634 635 636 public boolean execute(String sql, String columnNames[]) throws SQLException 637 { 638 logSql(sql); 639 return target.execute(sql, columnNames); 640 } 641 642 643 public ResultSet executeQuery(String sql) throws SQLException 644 { 645 logSql(sql); 646 return target.executeQuery(sql); 647 } 648 } 649 650 public class PreparedStatementInterceptor 651 extends StatementInterceptor 652 implements PreparedStatement 653 { 654 private final PreparedStatement target; 655 656 public PreparedStatementInterceptor(Connection con, PreparedStatement target) 657 { 658 super(con, target); 659 this.target = target; 660 } 661 662 public int executeUpdate() throws SQLException 663 { 664 return target.executeUpdate(); 665 } 666 667 public void addBatch() throws SQLException 668 { 669 target.addBatch(); 670 } 671 672 public void clearParameters() throws SQLException 673 { 674 target.clearParameters(); 675 } 676 677 public boolean execute() throws SQLException 678 { 679 return target.execute(); 680 } 681 682 public void setByte(int parameterIndex, byte x) throws SQLException 683 { 684 target.setByte(parameterIndex, x); 685 } 686 687 public void setDouble(int parameterIndex, double x) throws SQLException 688 { 689 target.setDouble(parameterIndex, x); 690 } 691 692 public void setFloat(int parameterIndex, float x) throws SQLException 693 { 694 target.setFloat(parameterIndex, x); 695 } 696 697 public void setInt(int parameterIndex, int x) throws SQLException 698 { 699 target.setInt(parameterIndex, x); 700 } 701 702 public void setNull(int parameterIndex, int sqlType) throws SQLException 703 { 704 target.setNull(parameterIndex, sqlType); 705 } 706 707 public void setLong(int parameterIndex, long x) throws SQLException 708 { 709 target.setLong(parameterIndex, x); 710 } 711 712 public void setShort(int parameterIndex, short x) throws SQLException 713 { 714 target.setShort(parameterIndex, x); 715 } 716 717 public void setBoolean(int parameterIndex, boolean x) throws SQLException 718 { 719 target.setBoolean(parameterIndex, x); 720 } 721 722 public void setBytes(int parameterIndex, byte x[]) throws SQLException 723 { 724 target.setBytes(parameterIndex, x); 725 } 726 727 public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException 728 { 729 target.setAsciiStream(parameterIndex, x, length); 730 } 731 732 public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException 733 { 734 target.setBinaryStream(parameterIndex, x, length); 735 } 736 737 public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException 738 { 739 target.setUnicodeStream(parameterIndex, x, length); 740 } 741 742 public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException 743 { 744 target.setCharacterStream(parameterIndex, reader, length); 745 } 746 747 public void setObject(int parameterIndex, Object x) throws SQLException 748 { 749 target.setObject(parameterIndex, x); 750 } 751 752 public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException 753 { 754 target.setObject(parameterIndex, x, targetSqlType); 755 } 756 757 public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException 758 { 759 target.setObject(parameterIndex, x, targetSqlType, scale); 760 } 761 762 public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException 763 { 764 target.setNull(paramIndex, sqlType, typeName); 765 } 766 767 public void setString(int parameterIndex, String x) throws SQLException 768 { 769 target.setString(parameterIndex, x); 770 } 771 772 public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException 773 { 774 target.setBigDecimal(parameterIndex, x); 775 } 776 777 778 public void setURL(int parameterIndex, URL x) throws SQLException 779 { 780 target.setURL(parameterIndex, x); 781 } 782 783 784 public void setArray(int i, Array x) throws SQLException 785 { 786 target.setArray(i, x); 787 } 788 789 public void setBlob(int i, Blob x) throws SQLException 790 { 791 target.setBlob(i, x); 792 } 793 794 public void setClob(int i, Clob x) throws SQLException 795 { 796 target.setClob(i, x); 797 } 798 799 public void setDate(int parameterIndex, Date x) throws SQLException 800 { 801 target.setDate(parameterIndex, x); 802 } 803 804 805 public ParameterMetaData getParameterMetaData() throws SQLException 806 { 807 return target.getParameterMetaData(); 808 } 809 810 811 public void setRef(int i, Ref x) throws SQLException 812 { 813 target.setRef(i, x); 814 } 815 816 public ResultSet executeQuery() throws SQLException 817 { 818 return target.executeQuery(); 819 } 820 821 public ResultSetMetaData getMetaData() throws SQLException 822 { 823 return target.getMetaData(); 824 } 825 826 public void setTime(int parameterIndex, Time x) throws SQLException 827 { 828 target.setTime(parameterIndex, x); 829 } 830 831 public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException 832 { 833 target.setTimestamp(parameterIndex, x); 834 } 835 836 public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException 837 { 838 target.setDate(parameterIndex, x, cal); 839 } 840 841 public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException 842 { 843 target.setTime(parameterIndex, x, cal); 844 } 845 846 public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException 847 { 848 target.setTimestamp(parameterIndex, x, cal); 849 } 850 } 851 852 854 private void logSql(String sql) 855 { 856 try 857 { 858 StatisticalItem item = new TxReport.SqlStats(sql); 859 server.invoke(statsCollector, "addStatisticalItem", 860 new Object []{item}, 861 new String []{StatisticalItem.class.getName()}); 862 } 863 catch(Exception e) 864 { 865 log.error("Failed to add invocation.", e); 866 } 867 } 868 869 private void bind() 870 throws NamingException 871 { 872 InitialContext ic = null; 873 try 874 { 875 ic = new InitialContext (); 876 Name name = ic.getNameParser("").parse(bindName); 877 NonSerializableFactory.rebind(name, this, true); 878 log.debug("bound to JNDI name " + bindName); 879 } 880 finally 881 { 882 if(ic != null) 883 { 884 ic.close(); 885 } 886 } 887 } 888 889 private void unbind() 890 throws NamingException 891 { 892 InitialContext ic = null; 893 try 894 { 895 ic = new InitialContext (); 896 ic.unbind(bindName); 897 NonSerializableFactory.unbind(bindName); 898 } 899 finally 900 { 901 if(ic != null) 902 { 903 ic.close(); 904 } 905 } 906 } 907 908 private void updateTarget() 909 throws NamingException 910 { 911 InitialContext ic = null; 912 try 913 { 914 ic = new InitialContext (); 915 target = (DataSource ) ic.lookup(targetName); 916 log.debug("target updated to " + targetName); 917 } 918 finally 919 { 920 if(ic != null) 921 { 922 ic.close(); 923 } 924 } 925 } 926 } 927 | Popular Tags |