1 23 24 package com.sun.gjc.spi; 25 26 import javax.resource.ResourceException ; 27 import javax.resource.spi.ConnectionRequestInfo ; 28 import com.sun.gjc.spi.ConnectionManager; 29 import com.sun.gjc.util.SecurityUtils; 30 import javax.resource.spi.security.PasswordCredential ; 31 import java.sql.DriverManager ; 32 import com.sun.gjc.common.DataSourceSpec; 33 import com.sun.gjc.common.DataSourceObjectBuilder; 34 import java.sql.SQLException ; 35 import java.sql.Connection ; 36 import javax.sql.PooledConnection ; 37 38 import com.sun.logging.*; 39 import java.util.logging.Logger ; 40 import java.util.logging.Level ; 41 42 import java.util.Set ; 43 import java.util.HashSet ; 44 import java.util.Iterator ; 45 46 54 55 public abstract class ManagedConnectionFactory implements javax.resource.spi.ManagedConnectionFactory , 56 javax.resource.spi.ValidatingManagedConnectionFactory , java.io.Serializable { 57 58 protected DataSourceSpec spec = new DataSourceSpec(); 59 protected transient DataSourceObjectBuilder dsObjBuilder; 60 61 protected java.io.PrintWriter logWriter = null; 62 protected javax.resource.spi.ResourceAdapter ra = null; 63 64 private static Logger _logger; 65 static { 66 _logger = LogDomains.getLogger( LogDomains.RSR_LOGGER ); 67 } 68 protected javax.resource.spi.LazyEnlistableConnectionManager cm_; 69 protected boolean isLazyCm_; 70 71 77 public Object createConnectionFactory() { 78 if(logWriter != null) { 79 logWriter.println("In createConnectionFactory()"); 80 } 81 com.sun.gjc.spi.DataSource cf = new com.sun.gjc.spi.DataSource( 82 (javax.resource.spi.ManagedConnectionFactory )this, null); 83 84 return cf; 85 } 86 87 94 public Object createConnectionFactory(javax.resource.spi.ConnectionManager cxManager) { 95 if(logWriter != null) { 96 logWriter.println("In createConnectionFactory(javax.resource.spi.ConnectionManager cxManager)"); 97 } 98 com.sun.gjc.spi.DataSource cf = new com.sun.gjc.spi.DataSource( 99 (javax.resource.spi.ManagedConnectionFactory )this, cxManager); 100 101 if ( cxManager instanceof javax.resource.spi.LazyEnlistableConnectionManager ) { 102 cm_ = (javax.resource.spi.LazyEnlistableConnectionManager )cxManager; 103 isLazyCm_ = true; 104 } 105 return cf; 106 } 107 108 125 public abstract javax.resource.spi.ManagedConnection createManagedConnection(javax.security.auth.Subject subject, 126 ConnectionRequestInfo cxRequestInfo) throws ResourceException ; 127 128 137 public abstract boolean equals(Object other); 138 139 145 public java.io.PrintWriter getLogWriter() { 146 return logWriter; 147 } 148 149 155 public javax.resource.spi.ResourceAdapter getResourceAdapter() { 156 if(logWriter != null) { 157 logWriter.println("In getResourceAdapter"); 158 } 159 return ra; 160 } 161 162 167 public int hashCode(){ 168 if(logWriter != null) { 169 logWriter.println("In hashCode"); 170 } 171 return spec.hashCode(); 172 } 173 174 189 public javax.resource.spi.ManagedConnection matchManagedConnections(java.util.Set connectionSet, 190 javax.security.auth.Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException { 191 if(logWriter != null) { 192 logWriter.println("In matchManagedConnections"); 193 } 194 195 if(connectionSet == null) { 196 return null; 197 } 198 199 PasswordCredential pc = SecurityUtils.getPasswordCredential(this, subject, cxRequestInfo); 200 201 java.util.Iterator iter = connectionSet.iterator(); 202 com.sun.gjc.spi.ManagedConnection mc = null; 203 while(iter.hasNext()) { 204 try { 205 mc = (com.sun.gjc.spi.ManagedConnection) iter.next(); 206 } catch(java.util.NoSuchElementException nsee) { 207 _logger.log(Level.SEVERE, "jdbc.exc_iter"); 208 throw new ResourceException (nsee.getMessage()); 209 } 210 if(pc == null && this.equals(mc.getManagedConnectionFactory())) { 211 return mc; 212 } else if(SecurityUtils.isPasswordCredentialEqual(pc, mc.getPasswordCredential()) == true) { 213 return mc; 214 } 215 } 216 return null; 217 } 218 219 231 public Set getInvalidConnections(Set connectionSet) throws ResourceException { 232 Iterator iter = connectionSet.iterator(); 233 Set invalid = new HashSet (); 234 while( iter.hasNext() ) { 235 ManagedConnection mc = (ManagedConnection) iter.next(); 236 try { 237 isValid( mc ); 238 } catch( ResourceException re ) { 239 invalid.add( mc ); 240 mc.connectionErrorOccurred( re, null ); 241 242 } 243 } 244 return invalid; 245 } 246 247 256 void isValid(com.sun.gjc.spi.ManagedConnection mc) throws ResourceException { 257 258 if(mc.isTransactionInProgress()) { 259 return; 260 } 261 262 String conVal = spec.getDetail( DataSourceSpec.CONNECTIONVALIDATIONREQUIRED ); 263 264 boolean connectionValidationRequired = 265 ( conVal == null ) ? false : (new Boolean (conVal.toLowerCase())).booleanValue(); 266 if( connectionValidationRequired == false || mc == null) { 267 return; 268 } 269 270 271 String validationMethod = spec.getDetail(DataSourceSpec.VALIDATIONMETHOD).toLowerCase(); 272 273 mc.checkIfValid(); 274 278 java.sql.Connection con = mc.getActualConnection(); 279 280 if(validationMethod.equals("auto-commit") == true) { 281 isValidByAutoCommit(con); 282 } else if(validationMethod.equalsIgnoreCase("meta-data") == true) { 283 isValidByMetaData(con); 284 } else if(validationMethod.equalsIgnoreCase("table") == true) { 285 isValidByTableQuery(con, spec.getDetail(DataSourceSpec.VALIDATIONTABLENAME)); 286 } else { 287 throw new ResourceException ("The validation method is not proper"); 288 } 289 } 290 291 298 protected void isValidByAutoCommit(java.sql.Connection con) throws ResourceException { 299 if(con == null) { 300 throw new ResourceException ("The connection is not valid as " 301 + "the connection is null"); 302 } 303 304 try { 305 315 boolean ac = con.getAutoCommit(); 316 if (ac) { 317 con.setAutoCommit(false); 318 } else { 319 con.rollback(); con.setAutoCommit(true); 321 } 322 323 con.setAutoCommit(ac); 324 325 } catch(Exception sqle) { 326 _logger.log(Level.SEVERE, "jdbc.exc_autocommit"); 327 throw new ResourceException (sqle.getMessage()); 328 } 329 } 330 331 338 protected void isValidByMetaData(java.sql.Connection con) throws ResourceException { 339 if(con == null) { 340 throw new ResourceException ("The connection is not valid as " 341 + "the connection is null"); 342 } 343 344 try { 345 java.sql.DatabaseMetaData dmd = con.getMetaData(); 346 } catch(Exception sqle) { 347 _logger.log(Level.SEVERE, "jdbc.exc_md"); 348 throw new ResourceException ("The connection is not valid as " 349 + "getting the meta data failed: " + sqle.getMessage()); 350 } 351 } 352 353 361 protected void isValidByTableQuery(java.sql.Connection con, 362 String tableName) throws ResourceException { 363 if(con == null) { 364 throw new ResourceException ("The connection is not valid as " 365 + "the connection is null"); 366 } 367 368 java.sql.Statement stmt = null; 369 java.sql.ResultSet rs = null; 370 try { 371 stmt = con.createStatement(); 372 rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName); 373 } catch(Exception sqle) { 374 _logger.log(Level.SEVERE, "jdbc.exc_execute"); 375 throw new ResourceException ("The connection is not valid as " 376 + "querying the table " + tableName + " failed: " + sqle.getMessage()); 377 } finally { 378 try { 379 if (rs != null ) { 380 rs.close(); 381 } 382 } catch (Exception e1) {} 383 384 try { 385 if (stmt != null ) { 386 stmt.close(); 387 } 388 } catch (Exception e2) {} 389 } 390 } 391 392 400 protected void setIsolation(com.sun.gjc.spi.ManagedConnection mc) throws ResourceException { 401 402 java.sql.Connection con = mc.getActualConnection(); 403 if(con == null) { 404 return; 405 } 406 407 String tranIsolation = spec.getDetail(DataSourceSpec.TRANSACTIONISOLATION); 408 if(tranIsolation != null && tranIsolation.equals("") == false) { 409 int tranIsolationInt = getTransactionIsolationInt(tranIsolation); 410 try { 411 con.setTransactionIsolation(tranIsolationInt); 412 } catch(java.sql.SQLException sqle) { 413 _logger.log(Level.SEVERE, "jdbc.exc_tx_level"); 414 throw new ResourceException ("The transaction isolation could " 415 + "not be set: " + sqle.getMessage()); 416 } 417 } 418 } 419 420 433 void resetIsolation(com.sun.gjc.spi.ManagedConnection mc, int tranIsol) throws ResourceException { 434 435 java.sql.Connection con = mc.getActualConnection(); 436 if(con == null) { 437 return; 438 } 439 440 String tranIsolation = spec.getDetail(DataSourceSpec.TRANSACTIONISOLATION); 441 if(tranIsolation != null && tranIsolation.equals("") == false) { 442 String guaranteeIsolationLevel = spec.getDetail(DataSourceSpec.GUARANTEEISOLATIONLEVEL); 443 444 if(guaranteeIsolationLevel != null && guaranteeIsolationLevel.equals("") == false) { 445 boolean guarantee = (new Boolean (guaranteeIsolationLevel.toLowerCase())).booleanValue(); 446 447 if(guarantee) { 448 int tranIsolationInt = getTransactionIsolationInt(tranIsolation); 449 try { 450 if(tranIsolationInt != con.getTransactionIsolation()) { 451 con.setTransactionIsolation(tranIsolationInt); 452 } 453 } catch(java.sql.SQLException sqle) { 454 _logger.log(Level.SEVERE, "jdbc.exc_tx_level"); 455 throw new ResourceException ("The isolation level could not be set: " 456 + sqle.getMessage()); 457 } 458 } else { 459 try { 460 if(tranIsol != con.getTransactionIsolation()) { 461 con.setTransactionIsolation(tranIsol); 462 } 463 } catch(java.sql.SQLException sqle) { 464 _logger.log(Level.SEVERE, "jdbc.exc_tx_level"); 465 throw new ResourceException ("The isolation level could not be set: " 466 + sqle.getMessage()); 467 } 468 } 469 } 470 } 471 } 472 473 481 private int getTransactionIsolationInt(String tranIsolation) throws ResourceException { 482 if(tranIsolation.equalsIgnoreCase("read-uncommitted")) { 483 return java.sql.Connection.TRANSACTION_READ_UNCOMMITTED; 484 } else if(tranIsolation.equalsIgnoreCase("read-committed")) { 485 return java.sql.Connection.TRANSACTION_READ_COMMITTED; 486 } else if(tranIsolation.equalsIgnoreCase("repeatable-read")) { 487 return java.sql.Connection.TRANSACTION_REPEATABLE_READ; 488 } else if(tranIsolation.equalsIgnoreCase("serializable")) { 489 return java.sql.Connection.TRANSACTION_SERIALIZABLE; 490 } else { 491 throw new ResourceException ("Invalid transaction isolation; the transaction " 492 + "isolation level can be empty or any of the following: " 493 + "read-uncommitted, read-committed, repeatable-read, serializable"); 494 } 495 } 496 497 500 protected void validateAndSetIsolation( ManagedConnection mc ) 501 throws ResourceException 502 { 503 isValid( mc ); 504 setIsolation( mc ); 505 } 506 507 513 public void setLogWriter(java.io.PrintWriter out) { 514 logWriter = out; 515 } 516 517 524 public void setResourceAdapter(javax.resource.spi.ResourceAdapter ra) { 525 this.ra = ra; 526 } 527 528 533 public void setUser(String user) { 534 spec.setDetail(DataSourceSpec.USERNAME, user); 535 } 536 537 542 public String getUser() { 543 return spec.getDetail(DataSourceSpec.USERNAME); 544 } 545 546 551 public void setuser(String user) { 552 spec.setDetail(DataSourceSpec.USERNAME, user); 553 } 554 555 560 public String getuser() { 561 return spec.getDetail(DataSourceSpec.USERNAME); 562 } 563 564 569 public void setPassword(String passwd) { 570 spec.setDetail(DataSourceSpec.PASSWORD, passwd); 571 } 572 573 578 public String getPassword() { 579 return spec.getDetail(DataSourceSpec.PASSWORD); 580 } 581 582 587 public void setpassword(String passwd) { 588 spec.setDetail(DataSourceSpec.PASSWORD, passwd); 589 } 590 591 596 public String getpassword() { 597 return spec.getDetail(DataSourceSpec.PASSWORD); 598 } 599 600 605 public void setClassName(String className) { 606 spec.setDetail(DataSourceSpec.CLASSNAME, className); 607 } 608 609 614 public String getClassName() { 615 return spec.getDetail(DataSourceSpec.CLASSNAME); 616 } 617 618 623 public void setclassName(String className) { 624 spec.setDetail(DataSourceSpec.CLASSNAME, className); 625 } 626 627 632 public String getclassName() { 633 return spec.getDetail(DataSourceSpec.CLASSNAME); 634 } 635 636 641 public void setConnectionValidationRequired(String conVldReq) { 642 spec.setDetail(DataSourceSpec.CONNECTIONVALIDATIONREQUIRED, conVldReq); 643 } 644 645 650 public String getConnectionValidationRequired() { 651 return spec.getDetail(DataSourceSpec.CONNECTIONVALIDATIONREQUIRED); 652 } 653 654 659 public void setconnectionValidationRequired(String conVldReq) { 660 spec.setDetail(DataSourceSpec.CONNECTIONVALIDATIONREQUIRED, conVldReq); 661 } 662 663 668 public String getconnectionValidationRequired() { 669 return spec.getDetail(DataSourceSpec.CONNECTIONVALIDATIONREQUIRED); 670 } 671 672 677 public void setValidationMethod(String validationMethod) { 678 spec.setDetail(DataSourceSpec.VALIDATIONMETHOD, validationMethod); 679 } 680 681 686 public String getValidationMethod() { 687 return spec.getDetail(DataSourceSpec.VALIDATIONMETHOD); 688 } 689 690 695 public void setvalidationMethod(String validationMethod) { 696 spec.setDetail(DataSourceSpec.VALIDATIONMETHOD, validationMethod); 697 } 698 699 704 public String getvalidationMethod() { 705 return spec.getDetail(DataSourceSpec.VALIDATIONMETHOD); 706 } 707 708 713 public void setValidationTableName(String table) { 714 spec.setDetail(DataSourceSpec.VALIDATIONTABLENAME, table); 715 } 716 717 722 public String getValidationTableName() { 723 return spec.getDetail(DataSourceSpec.VALIDATIONTABLENAME); 724 } 725 726 731 public void setvalidationTableName(String table) { 732 spec.setDetail(DataSourceSpec.VALIDATIONTABLENAME, table); 733 } 734 735 740 public String getvalidationTableName() { 741 return spec.getDetail(DataSourceSpec.VALIDATIONTABLENAME); 742 } 743 744 749 public void setTransactionIsolation(String trnIsolation) { 750 spec.setDetail(DataSourceSpec.TRANSACTIONISOLATION, trnIsolation); 751 } 752 753 758 public String getTransactionIsolation() { 759 return spec.getDetail(DataSourceSpec.TRANSACTIONISOLATION); 760 } 761 762 767 public void settransactionIsolation(String trnIsolation) { 768 spec.setDetail(DataSourceSpec.TRANSACTIONISOLATION, trnIsolation); 769 } 770 771 776 public String gettransactionIsolation() { 777 return spec.getDetail(DataSourceSpec.TRANSACTIONISOLATION); 778 } 779 780 785 public void setGuaranteeIsolationLevel(String guaranteeIsolation) { 786 spec.setDetail(DataSourceSpec.GUARANTEEISOLATIONLEVEL, guaranteeIsolation); 787 } 788 789 794 public String getGuaranteeIsolationLevel() { 795 return spec.getDetail(DataSourceSpec.GUARANTEEISOLATIONLEVEL); 796 } 797 798 803 public void setguaranteeIsolationLevel(String guaranteeIsolation) { 804 spec.setDetail(DataSourceSpec.GUARANTEEISOLATIONLEVEL, guaranteeIsolation); 805 } 806 807 812 public String getguaranteeIsolationLevel() { 813 return spec.getDetail(DataSourceSpec.GUARANTEEISOLATIONLEVEL); 814 } 815 816 protected boolean isEqual( PasswordCredential pc, String user, 817 String password) { 818 819 822 String thisUser = (pc == null) ? null : pc.getUserName(); 823 char[] passwordArray = (pc == null) ? null : pc.getPassword(); 824 String thisPassword = (passwordArray == null) ? null : new String (passwordArray); 825 826 return (isStringEqual(thisUser, user) && isStringEqual(thisPassword, password)); 827 828 } 829 830 private boolean isStringEqual(String str1, String str2) { 831 return str1 == null ? str2 == null : str1.equals( str2 ); 832 } 833 834 835 841 public void setserverName(String serverName) { 842 spec.setDetail(DataSourceSpec.SERVERNAME, serverName); 843 } 844 845 851 public String getserverName() { 852 return spec.getDetail(DataSourceSpec.SERVERNAME); 853 } 854 855 861 public void setServerName(String serverName) { 862 spec.setDetail(DataSourceSpec.SERVERNAME, serverName); 863 } 864 865 871 public String getServerName() { 872 return spec.getDetail(DataSourceSpec.SERVERNAME); 873 } 874 875 881 public void setportNumber(String portNumber) { 882 spec.setDetail(DataSourceSpec.PORTNUMBER, portNumber); 883 } 884 885 891 public String getportNumber() { 892 return spec.getDetail(DataSourceSpec.PORTNUMBER); 893 } 894 895 901 public void setPortNumber(String portNumber) { 902 spec.setDetail(DataSourceSpec.PORTNUMBER, portNumber); 903 } 904 905 911 public String getPortNumber() { 912 return spec.getDetail(DataSourceSpec.PORTNUMBER); 913 } 914 915 921 public void setdatabaseName(String databaseName) { 922 spec.setDetail(DataSourceSpec.DATABASENAME, databaseName); 923 } 924 925 931 public String getdatabaseName() { 932 return spec.getDetail(DataSourceSpec.DATABASENAME); 933 } 934 935 941 public void setDatabaseName(String databaseName) { 942 spec.setDetail(DataSourceSpec.DATABASENAME, databaseName); 943 } 944 945 951 public String getDatabaseName() { 952 return spec.getDetail(DataSourceSpec.DATABASENAME); 953 } 954 955 961 public void setdataSourceName(String dsn) { 962 spec.setDetail(DataSourceSpec.DATASOURCENAME, dsn); 963 } 964 965 971 public String getdataSourceName() { 972 return spec.getDetail(DataSourceSpec.DATASOURCENAME); 973 } 974 975 981 public void setDataSourceName(String dsn) { 982 spec.setDetail(DataSourceSpec.DATASOURCENAME, dsn); 983 } 984 985 991 public String getDataSourceName() { 992 return spec.getDetail(DataSourceSpec.DATASOURCENAME); 993 } 994 995 1001 public void setdescription(String desc) { 1002 spec.setDetail(DataSourceSpec.DESCRIPTION, desc); 1003 } 1004 1005 1011 public String getdescription() { 1012 return spec.getDetail(DataSourceSpec.DESCRIPTION); 1013 } 1014 1015 1021 public void setDescription(String desc) { 1022 spec.setDetail(DataSourceSpec.DESCRIPTION, desc); 1023 } 1024 1025 1031 public String getDescription() { 1032 return spec.getDetail(DataSourceSpec.DESCRIPTION); 1033 } 1034 1035 1041 public void setnetworkProtocol(String nwProtocol) { 1042 spec.setDetail(DataSourceSpec.NETWORKPROTOCOL, nwProtocol); 1043 } 1044 1045 1051 public String getnetworkProtocol() { 1052 return spec.getDetail(DataSourceSpec.NETWORKPROTOCOL); 1053 } 1054 1055 1061 public void setNetworkProtocol(String nwProtocol) { 1062 spec.setDetail(DataSourceSpec.NETWORKPROTOCOL, nwProtocol); 1063 } 1064 1065 1071 public String getNetworkProtocol() { 1072 return spec.getDetail(DataSourceSpec.NETWORKPROTOCOL); 1073 } 1074 1075 1081 public void setroleName(String roleName) { 1082 spec.setDetail(DataSourceSpec.ROLENAME, roleName); 1083 } 1084 1085 1091 public String getroleName() { 1092 return spec.getDetail(DataSourceSpec.ROLENAME); 1093 } 1094 1095 1101 public void setRoleName(String roleName) { 1102 spec.setDetail(DataSourceSpec.ROLENAME, roleName); 1103 } 1104 1105 1111 public String getRoleName() { 1112 return spec.getDetail(DataSourceSpec.ROLENAME); 1113 } 1114 1115 1116 1122 public void setloginTimeOut(String loginTimeOut) { 1123 spec.setDetail(DataSourceSpec.LOGINTIMEOUT, loginTimeOut); 1124 } 1125 1126 1132 public String getloginTimeOut() { 1133 return spec.getDetail(DataSourceSpec.LOGINTIMEOUT); 1134 } 1135 1136 1142 public void setLoginTimeOut(String loginTimeOut) { 1143 spec.setDetail(DataSourceSpec.LOGINTIMEOUT, loginTimeOut); 1144 } 1145 1146 1152 public String getLoginTimeOut() { 1153 return spec.getDetail(DataSourceSpec.LOGINTIMEOUT); 1154 } 1155 1156 1162 public void setdelimiter(String delim) { 1163 spec.setDetail(DataSourceSpec.DELIMITER, delim); 1164 } 1165 1166 1172 public String getdelimiter() { 1173 return spec.getDetail(DataSourceSpec.DELIMITER); 1174 } 1175 1176 1182 public void setDelimiter(String delim) { 1183 spec.setDetail(DataSourceSpec.DELIMITER, delim); 1184 } 1185 1186 1192 public String getDelimiter() { 1193 return spec.getDetail(DataSourceSpec.DELIMITER); 1194 } 1195 1196 1202 public void setdriverProperties(String driverProps) { 1203 spec.setDetail(DataSourceSpec.DRIVERPROPERTIES, driverProps); 1204 } 1205 1206 1212 public String getdriverProperties() { 1213 return spec.getDetail(DataSourceSpec.DRIVERPROPERTIES); 1214 } 1215 1216 1222 public void setDriverProperties(String driverProps) { 1223 spec.setDetail(DataSourceSpec.DRIVERPROPERTIES, driverProps); 1224 } 1225 1226 1232 public String getDriverProperties() { 1233 return spec.getDetail(DataSourceSpec.DRIVERPROPERTIES); 1234 } 1235 1236 protected ManagedConnection constructManagedConnection(PooledConnection pc, 1237 Connection sqlCon, PasswordCredential passCred, 1238 ManagedConnectionFactory mcf ) throws ResourceException 1239 { 1240 1241 com.sun.gjc.spi.ManagedConnection mc = 1242 new com.sun.gjc.spi.ManagedConnection( pc, sqlCon, passCred, mcf ); 1243 1244 return mc; 1245 1246 } 1247 1248} 1249 | Popular Tags |