1 21 22 package org.apache.derby.impl.tools.ij; 23 24 import org.apache.derby.iapi.tools.i18n.LocalizedResource; 25 import java.sql.Connection ; 26 import java.sql.SQLException ; 27 import java.util.Locale ; 28 import java.util.Vector ; 29 30 import javax.transaction.xa.Xid ; 31 import javax.transaction.xa.XAResource ; 32 import javax.transaction.xa.XAException ; 33 import javax.sql.PooledConnection ; 34 import javax.sql.XAConnection ; 35 import javax.sql.XADataSource ; 36 import javax.sql.DataSource ; 37 import javax.sql.ConnectionPoolDataSource ; 38 import org.apache.derby.iapi.services.info.JVMInfo; 39 40 44 class xaHelper implements xaAbstractHelper 45 { 46 47 private XADataSource currentXADataSource; 48 private XAConnection currentXAConnection; 49 50 private String databaseName; 51 52 private DataSource currentDataSource; 54 private ConnectionPoolDataSource currentCPDataSource; 55 private PooledConnection currentPooledConnection; 56 57 private boolean isJCC; 58 private boolean isNetClient; 59 private String framework; 60 61 xaHelper() 62 { 63 } 64 65 66 public void setFramework(String fm) 67 { 68 if (fm == null) { 69 return; 70 } 71 framework = fm.toUpperCase(Locale.ENGLISH); 72 if (framework.endsWith("NET") || 73 framework.equals("DB2JCC")) 74 isJCC = true; 75 else if (framework.equals("DERBYNETCLIENT")) 76 isNetClient = true; 77 78 } 79 80 private Xid makeXid(int xid) 81 { 82 return new ijXid(xid, databaseName.getBytes()); 83 } 84 85 public void XADataSourceStatement(ij parser, Token dbname, Token shutdown, 86 String create) 87 throws SQLException 88 { 89 try 90 { 91 currentXADataSource = (XADataSource ) getXADataSource(); 92 93 databaseName = parser.stringValue(dbname.image); 94 95 if (isJCC || isNetClient) 96 { 97 String hostName = System.getProperty("hostName"); 98 if ((hostName != null ) && (!hostName.equals("localhost"))) 99 { 100 xaHelper.setDataSourceProperty(currentXADataSource, 101 "ServerName", hostName); 102 } 103 else 104 { 105 xaHelper.setDataSourceProperty(currentXADataSource, 106 "ServerName", "localhost"); 107 } 108 xaHelper.setDataSourceProperty(currentXADataSource, 109 "portNumber", 1527); 110 111 String user; 112 String password; 113 user = "APP"; 114 password = "APP"; 115 xaHelper.setDataSourceProperty(currentXADataSource, 116 "user", user); 117 xaHelper.setDataSourceProperty(currentXADataSource, 118 "password", password); 119 } 122 if (isJCC) 123 { 124 xaHelper.setDataSourceProperty(currentXADataSource, 125 "driverType", 4); 126 127 xaHelper.setDataSourceProperty(currentXADataSource, 128 "retrieveMessagesFromServerOnGetMessage", true); 129 } 130 xaHelper.setDataSourceProperty(currentXADataSource, "databaseName", databaseName); 131 132 if (shutdown != null && shutdown.toString().toLowerCase(Locale.ENGLISH).equals("shutdown")) 133 { 134 if (isJCC || isNetClient) 135 xaHelper.setDataSourceProperty(currentXADataSource,"databaseName", databaseName + ";shutdown=true"); 136 else 137 xaHelper.setDataSourceProperty(currentXADataSource, "shutdownDatabase", "shutdown"); 138 139 currentXADataSource.getXAConnection().getConnection(); 141 currentXADataSource = null; 142 currentXAConnection = null; 143 } 144 else if (create != null && create.toLowerCase(java.util.Locale.ENGLISH).equals("create")) 145 { 146 if (isJCC || isNetClient) 147 xaHelper.setDataSourceProperty(currentXADataSource,"databaseName", databaseName + ";create=true"); 148 else 149 xaHelper.setDataSourceProperty(currentXADataSource, 150 "createDatabase", "create"); 151 152 153 XAConnection conn = currentXADataSource.getXAConnection(); 154 conn.close(); 155 156 xaHelper.setDataSourceProperty(currentXADataSource, "createDatabase", null); 157 } 158 } 159 catch (Throwable t) 160 { 161 handleException(t); 162 } 163 } 164 165 166 public void XAConnectStatement(ij parser, Token user, Token pass, String id) 167 throws SQLException 168 { 169 try 170 { 171 if (currentXAConnection != null) 172 { 173 try { 174 currentXAConnection.close(); 175 } catch (SQLException sqle) { 176 } 177 178 currentXAConnection = null; 179 } 180 181 String username = null; 182 String password = ""; 183 184 if (pass != null) 185 password = parser.stringValue(pass.image); 186 187 if (user != null) 188 { 189 username = parser.stringValue(user.image); 190 191 currentXAConnection = 192 currentXADataSource.getXAConnection(username, password); 193 } 194 else 195 { 196 197 currentXAConnection = currentXADataSource.getXAConnection(); 198 } 199 200 } 201 catch (Throwable t) 202 { 203 handleException(t); 204 } 205 } 206 207 public void XADisconnectStatement(ij parser, String n) throws SQLException 208 { 209 if (currentXAConnection == null) 210 throw ijException.noSuchConnection("XAConnection"); 211 currentXAConnection.close(); 212 currentXAConnection = null; 213 } 214 215 public Connection XAGetConnectionStatement(ij parser, String n) throws SQLException 216 { 217 try 218 { 219 return currentXAConnection.getConnection(); 220 } 221 catch(Throwable t) 222 { 223 handleException(t); 224 } 225 return null; 226 } 227 228 public void CommitStatement(ij parser, Token onePhase, Token twoPhase, 229 int xid) 230 throws SQLException 231 { 232 try 233 { 234 currentXAConnection.getXAResource().commit(makeXid(xid), (onePhase != null)); 235 } 236 catch(Throwable t) 237 { 238 handleException(t); 239 } 240 } 241 242 public void EndStatement(ij parser, int flag, int xid) throws SQLException 243 { 244 try 245 { 246 currentXAConnection.getXAResource().end(makeXid(xid), flag); 247 } 248 catch(Throwable t) 249 { 250 handleException(t); 251 } 252 } 253 254 public void ForgetStatement(ij parser, int xid) throws SQLException 255 { 256 try 257 { 258 currentXAConnection.getXAResource().forget(makeXid(xid)); 259 } 260 catch(Throwable t) 261 { 262 handleException(t); 263 } 264 } 265 266 public void PrepareStatement(ij parser, int xid) throws SQLException 267 { 268 try 269 { 270 currentXAConnection.getXAResource().prepare(makeXid(xid)); 271 } 272 catch(Throwable t) 273 { 274 handleException(t); 275 } 276 } 277 278 public ijResult RecoverStatement(ij parser, int flag) throws SQLException 279 { 280 Object [] val = null; 281 282 try 283 { 284 val = currentXAConnection.getXAResource().recover(flag); 285 } 286 catch(Throwable t) 287 { 288 handleException(t); 289 } 290 291 Vector v = new Vector (); 292 v.addElement(""); 293 v.addElement(LocalizedResource.getMessage("IJ_Reco0InDoubT", LocalizedResource.getNumber(val.length))); 294 v.addElement(""); 295 for (int i = 0; i < val.length; i++) 296 v.addElement(LocalizedResource.getMessage("IJ_Tran01", LocalizedResource.getNumber(i+1), val[i].toString())); 297 298 return new ijVectorResult(v,null); 299 300 } 301 302 public void RollbackStatement(ij parser, int xid) throws SQLException 303 { 304 try 305 { 306 currentXAConnection.getXAResource().rollback(makeXid(xid)); 307 } 308 catch(Throwable t) 309 { 310 handleException(t); 311 } 312 313 } 314 315 public void StartStatement(ij parser, int flag, int xid) throws SQLException 316 { 317 try 318 { 319 currentXAConnection.getXAResource().start(makeXid(xid), flag); 320 } 321 catch(Throwable t) 322 { 323 handleException(t); 324 } 325 } 326 327 private void handleException(Throwable t) throws SQLException 328 { 329 if (t instanceof SQLException ) 330 { 331 throw (SQLException )t; 333 } 334 if (t instanceof XAException ) 335 { 336 int errorCode = ((XAException )t).errorCode; 337 String error = LocalizedResource.getMessage("IJ_IlleValu"); 338 339 367 switch(errorCode) 368 { 369 case XAException.XA_HEURCOM : error = "XA_HEURCOM "; break; 370 case XAException.XA_HEURHAZ : error = "XA_HEURHAZ"; break; 371 case XAException.XA_HEURMIX : error = "XA_HEURMIX"; break; 372 case XAException.XA_HEURRB : error = "XA_HEURRB "; break; 373 case XAException.XA_NOMIGRATE : error = "XA_NOMIGRATE "; break; 374 case XAException.XA_RBCOMMFAIL : error = "XA_RBCOMMFAIL "; break; 376 case XAException.XA_RBDEADLOCK : error = "XA_RBDEADLOCK "; break; 377 case XAException.XA_RBINTEGRITY : error = "XA_RBINTEGRITY "; break; 379 case XAException.XA_RBOTHER : error = "XA_RBOTHER "; break; 380 case XAException.XA_RBPROTO : error = "XA_RBPROTO "; break; 381 case XAException.XA_RBROLLBACK : error = "XA_RBROLLBACK "; break; 382 case XAException.XA_RBTIMEOUT : error = "XA_RBTIMEOUT "; break; 383 case XAException.XA_RBTRANSIENT : error = "XA_RBTRANSIENT "; break; 384 case XAException.XA_RDONLY : error = "XA_RDONLY "; break; 385 case XAException.XA_RETRY : error = "XA_RETRY "; break; 386 case XAException.XAER_ASYNC : error = "XAER_ASYNC "; break; 387 case XAException.XAER_DUPID : error = "XAER_DUPID "; break; 388 case XAException.XAER_INVAL : error = "XAER_INVAL "; break; 389 case XAException.XAER_NOTA : error = "XAER_NOTA "; break; 390 case XAException.XAER_OUTSIDE : error = "XAER_OUTSIDE "; break; 391 case XAException.XAER_PROTO : error = "XAER_PROTO "; break; 392 case XAException.XAER_RMERR : error = "XAER_RMERR "; break; 393 case XAException.XAER_RMFAIL : error = "XAER_RMFAIL "; break; 394 } 395 throw new ijException(error); 397 398 } 399 else { 401 String info = LocalizedResource.getMessage("IJ_01SeeLog", t.toString(), t.getMessage()); 402 throw new ijException(info); 404 } 405 } 406 407 408 public Connection DataSourceStatement(ij parser, Token dbname, Token protocol, 410 Token userT, Token passT, String id) 411 throws SQLException 412 { 413 414 try { 415 currentDataSource = (DataSource ) (Class.forName("org.apache.derby.jdbc.EmbeddedDataSource").newInstance()); 416 } catch (Exception e) { 417 throw new SQLException (e.toString()); 418 } 419 databaseName = parser.stringValue(dbname.image); 420 xaHelper.setDataSourceProperty(currentDataSource, "databaseName", databaseName); 421 xaHelper.setDataSourceProperty(currentXADataSource, "dataSourceName", databaseName); 422 Connection c = null; 424 String username = null; 425 String password = ""; 426 427 if (passT != null) 428 password = parser.stringValue(passT.image); 429 430 if (userT != null) 431 { 432 username = parser.stringValue(userT.image); 433 c = currentDataSource.getConnection(username, password); 434 } 435 else 436 { 437 c = currentDataSource.getConnection(); 438 } 439 440 return c; 441 442 } 443 444 public void CPDataSourceStatement(ij parser, Token dbname, Token protocol) 445 throws SQLException 446 { 447 try { 448 currentCPDataSource = (ConnectionPoolDataSource ) (Class.forName("org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource").newInstance()); 449 } catch (Exception e) { 450 throw new SQLException (e.toString()); 451 } 452 databaseName = parser.stringValue(dbname.image); 453 xaHelper.setDataSourceProperty(currentCPDataSource, "databaseName", databaseName); 454 } 455 456 public void CPConnectStatement(ij parser, Token userT, Token passT, String n) 457 throws SQLException 458 { 459 String username = null; 460 String password = ""; 461 462 if (passT != null) 463 password = parser.stringValue(passT.image); 464 465 if (userT != null) 466 { 467 username = parser.stringValue(userT.image); 468 currentPooledConnection = 469 currentCPDataSource.getPooledConnection(username, password); 470 } 471 else 472 { 473 currentPooledConnection = 474 currentCPDataSource.getPooledConnection(); 475 } 476 } 477 478 public Connection CPGetConnectionStatement(ij parser, String n) 479 throws SQLException 480 { 481 return currentPooledConnection.getConnection(); 482 } 483 484 public void CPDisconnectStatement(ij parser, String n) throws SQLException 485 { 486 if (currentPooledConnection == null) 487 throw ijException.noSuchConnection(LocalizedResource.getMessage("IJ_Pool")); 488 currentPooledConnection.close(); 489 currentPooledConnection = null; 490 } 491 492 499 private XADataSource getXADataSource() throws Exception 500 { 501 try 504 { 505 if (isJCC) 506 return (XADataSource ) 507 (Class.forName("com.ibm.db2.jcc.DB2XADataSource").newInstance()); 508 else if (isNetClient){ 509 if (JVMInfo.JDK_ID >= JVMInfo.J2SE_16) { 510 try { 513 return (XADataSource )(Class.forName( 514 "org.apache.derby.jdbc." + 515 "ClientXADataSource40").newInstance()); 516 } 517 catch (ClassNotFoundException e) { 518 } 521 } 522 return (XADataSource ) (Class.forName( 523 "org.apache.derby.jdbc.ClientXADataSource" 524 ).newInstance()); 525 } 526 else { 527 if (JVMInfo.JDK_ID >= JVMInfo.J2SE_16) { 528 try { 531 return (XADataSource )(Class.forName( 532 "org.apache.derby.jdbc." + 533 "EmbeddedXADataSource40").newInstance()); 534 } 535 catch (ClassNotFoundException e) { 536 } 539 } 540 return (XADataSource )(Class.forName("org.apache.derby.jdbc.EmbeddedXADataSource").newInstance()); 541 } 542 } 543 catch(ClassNotFoundException cnfe) { 544 throw new ijException(LocalizedResource.getMessage("IJ_XAClass")); 545 } 546 catch (InstantiationException e) { } 547 catch (IllegalAccessException e) { } 548 549 throw new ijException(LocalizedResource.getMessage("IJ_XANoI")); 550 } 551 private static final Class [] STRING_P = { "".getClass() }; 552 private static final Class [] INT_P = { Integer.TYPE }; 553 private static final Class [] BOOLEAN_P = {Boolean.TYPE }; 554 555 private static void setDataSourceProperty(Object ds, String property, int 556 value) throws SQLException 557 { 558 String methodName = 559 "set" + Character.toUpperCase(property.charAt(0)) + property.substring(1); 560 try { 561 java.lang.reflect.Method m = ds.getClass().getMethod(methodName, INT_P); 562 m.invoke(ds, new Object [] {new Integer (value)}); 563 } 564 catch (Exception e) 565 { 566 throw new SQLException (property + " ???" + e.getMessage()); 567 } 568 569 } 570 571 private static void setDataSourceProperty(Object ds, String property, String value) throws SQLException { 572 573 String methodName = 574 "set" + Character.toUpperCase(property.charAt(0)) + property.substring(1); 575 576 try { 577 java.lang.reflect.Method m = ds.getClass().getMethod(methodName, STRING_P); 578 m.invoke(ds, new Object [] {value}); 579 return; 580 } catch (Exception nsme) { 581 throw new SQLException (property + " ???"); 582 } 585 } 586 587 private static void setDataSourceProperty(Object ds, String property, boolean value) throws SQLException { 588 589 String methodName = 590 "set" + Character.toUpperCase(property.charAt(0)) + property.substring(1); 591 592 try { 593 java.lang.reflect.Method m = ds.getClass().getMethod(methodName, BOOLEAN_P); 594 m.invoke(ds, new Object [] {new Boolean (value)}); 595 return; 596 } catch (Exception nsme) { 597 throw new SQLException (property + " ???"); 598 } 599 } 600 } 601 602 603 604 class ijXid implements Xid , java.io.Serializable 605 { 606 private static final long serialVersionUID = 64467452100036L; 607 608 private final int format_id; 609 private final byte[] global_id; 610 private final byte[] branch_id; 611 612 613 ijXid(int xid, byte[] id) 614 { 615 format_id = xid; 616 global_id = id; 617 branch_id = id; 618 619 } 620 626 public int getFormatId() 627 { 628 return(format_id); 629 } 630 631 638 public byte[] getGlobalTransactionId() 639 { 640 return(global_id); 641 } 642 643 649 public byte[] getBranchQualifier() 650 { 651 return(branch_id); 652 } 653 } 654 655 656 | Popular Tags |