1 22 package org.jboss.test.cts.ejb; 23 24 import java.util.Collection ; 25 26 import javax.sql.DataSource ; 27 import java.sql.Connection ; 28 import java.sql.Statement ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.ResultSetMetaData ; 32 import java.sql.Types ; 33 import java.sql.SQLException ; 34 35 import javax.naming.InitialContext ; 36 import javax.naming.Context ; 37 import javax.naming.NamingException ; 38 39 import javax.jms.JMSException ; 40 import javax.ejb.EntityBean ; 41 import javax.ejb.EntityContext ; 42 import javax.ejb.CreateException ; 43 import javax.ejb.DuplicateKeyException ; 44 import javax.ejb.FinderException ; 45 import javax.ejb.ObjectNotFoundException ; 46 import javax.ejb.NoSuchEntityException ; 47 import javax.ejb.EJBException ; 48 49 import org.jboss.test.cts.keys.AccountPK; 50 51 import org.jboss.test.cts.jms.ContainerMBox; 52 import org.jboss.test.cts.jms.MsgSender; 53 54 63 64 public class CtsBmpBean 65 implements EntityBean 66 { 67 org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass()); 68 69 private static final String TABLE_NAME = "BMP_BEAN_TBL"; 70 EntityContext ctx = null; 71 DataSource ds = null; 72 73 private MsgSender ms = null; 74 75 String accountNumber; 77 String personsName; 78 79 87 public AccountPK ejbCreate(AccountPK pk, String personsName) 88 throws CreateException , DuplicateKeyException 89 { 90 log.debug("entry ejbCreate(\"" + pk.getKey() + "\", " + 91 "\"" + personsName + "\")"); 92 93 sendMsg(ContainerMBox.EJB_CREATE_MSG); 94 95 try 96 { 97 Connection con = ds.getConnection(); 98 try 99 { 100 PreparedStatement ps; 101 102 ps = con.prepareStatement("SELECT accountNumber " + 104 "FROM " + TABLE_NAME + " " + 105 "WHERE accountNumber=?"); 106 try 107 { 108 ps.setString(1, pk.getKey()); 109 110 ResultSet rs = ps.executeQuery(); 111 112 if (rs.next()) 113 throw new DuplicateKeyException ("Bean with accountNumber=" + 114 pk.getKey() + 115 " already exists."); 116 } 117 finally 118 { 119 ps.close(); 120 } 121 122 ps = con.prepareStatement("INSERT INTO " + TABLE_NAME + 124 " VALUES (?,?)"); 125 try 126 { 127 ps.setString(1, pk.getKey()); 128 ps.setString(2, personsName); 129 130 ps.execute(); 131 } 132 finally 133 { 134 ps.close(); 135 } 136 } 137 finally 138 { 139 con.close(); 140 } 141 } 142 catch (SQLException e) 143 { 144 log.debug("failed", e); 145 146 throw new CreateException ("Entity bean creation failure: " + 147 e.getMessage()); 148 } 149 150 this.accountNumber = pk.getKey(); 151 this.personsName = personsName; 152 153 log.debug("Created \"" + accountNumber + "\"."); 154 155 return pk; 156 } 157 158 161 public void ejbPostCreate(AccountPK pk, String personsName) 162 { 163 log.debug("ejbPostCreate(AccountPK, String) called"); 164 165 sendMsg(ContainerMBox.EJB_POST_CREATE_MSG); 166 } 167 168 175 public AccountPK ejbFindByPrimaryKey(AccountPK pk) 176 throws FinderException 177 { 178 log.debug("entry ejbFindByPrimaryKey"); 179 180 try 181 { 182 Connection con = ds.getConnection(); 183 try 184 { 185 PreparedStatement ps; 186 187 ps = con.prepareStatement("SELECT accountNumber " + 188 "FROM " + TABLE_NAME + " " + 189 "WHERE accountNumber=?"); 190 try 191 { 192 ps.setString(1, pk.getKey()); 193 194 ResultSet rs = ps.executeQuery(); 195 196 if (!rs.next()) 197 throw new ObjectNotFoundException ("No bean with " + 198 "accountNumber=" + 199 pk.getKey() + " found."); 200 201 return pk; 202 } 203 finally 204 { 205 ps.close(); 206 } 207 } 208 finally 209 { 210 con.close(); 211 } 212 } 213 catch (SQLException e) 214 { 215 log.debug("failed", e); 216 217 throw new FinderException ("Could not find: " + e.getMessage()); 218 } 219 } 220 221 226 public Collection ejbFindAll() 227 throws FinderException 228 { 229 log.debug("entry ejbFindAll"); 230 231 ensureTableExists(); 232 233 Collection result = new java.util.LinkedList (); 234 try 235 { 236 Connection con = ds.getConnection(); 237 try 238 { 239 PreparedStatement ps; 240 241 ps = con.prepareStatement("SELECT accountNumber " + 242 "FROM " + TABLE_NAME); 243 try 244 { 245 ResultSet rs = ps.executeQuery(); 246 247 while (rs.next()) 248 result.add(new AccountPK(rs.getString(1))); 249 250 return result; 251 } 252 finally 253 { 254 ps.close(); 255 } 256 } 257 finally 258 { 259 con.close(); 260 } 261 } 262 catch (SQLException e) 263 { 264 log.debug("failed", e); 265 266 throw new FinderException ("Could not find: " + e.getMessage()); 267 } 268 } 269 270 276 public Collection ejbFindByPersonsName(String guysName) 277 throws FinderException 278 { 279 log.debug("entry ejbFindByPersonsName(\"" + guysName + "\")."); 280 281 Collection result = new java.util.LinkedList (); 282 try 283 { 284 Connection con = ds.getConnection(); 285 try 286 { 287 PreparedStatement ps; 288 289 ps = con.prepareStatement("SELECT accountNumber " + 290 "FROM " + TABLE_NAME + " " + 291 "WHERE name=?"); 292 try 293 { 294 ps.setString(1, guysName); 295 296 ResultSet rs = ps.executeQuery(); 297 298 while (rs.next()) 299 result.add(new AccountPK(rs.getString(1))); 300 301 return result; 302 } 303 finally 304 { 305 ps.close(); 306 } 307 } 308 finally 309 { 310 con.close(); 311 } 312 } 313 catch (SQLException e) 314 { 315 log.debug("failed", e); 316 317 throw new FinderException ("Could not find: " + e.getMessage()); 318 } 319 } 320 321 324 public void ejbLoad() 325 { 326 log.debug("ejbLoad(\"" + 327 ((AccountPK) ctx.getPrimaryKey()).getKey() + 328 "\") called"); 329 330 sendMsg(ContainerMBox.EJB_LOAD_MSG); 331 332 try 333 { 334 Connection con = ds.getConnection(); 335 try 336 { 337 PreparedStatement ps; 338 339 ps = con.prepareStatement("SELECT accountNumber,name " + 340 "FROM " + TABLE_NAME + " " + 341 "WHERE accountNumber=?"); 342 try 343 { 344 AccountPK pk = (AccountPK) ctx.getPrimaryKey(); 345 346 ps.setString(1, pk.getKey()); 347 ResultSet rs = ps.executeQuery(); 348 349 if (rs.next() == false) 350 throw new NoSuchEntityException ("Instance " + pk.getKey() + 351 " not found in database."); 352 353 accountNumber = rs.getString(1); 354 personsName = rs.getString(2); 355 } 356 finally 357 { 358 ps.close(); 359 } 360 } 361 finally 362 { 363 con.close(); 364 } 365 } 366 catch (SQLException e) 367 { 368 log.debug("failed", e); 369 370 throw new EJBException (e); 371 } 372 373 log.debug("ejbLoad(\"" + 374 ((AccountPK) ctx.getPrimaryKey()).getKey() + 375 "\") returning"); 376 377 } 378 379 382 public void ejbStore() 383 { 384 log.debug("ejbStore(\"" + accountNumber + "\") called"); 385 387 sendMsg(ContainerMBox.EJB_STORE_MSG); 388 389 try 390 { 391 Connection con = ds.getConnection(); 392 try 393 { 394 PreparedStatement ps; 395 396 ps = con.prepareStatement("UPDATE " + TABLE_NAME + " " + 397 "SET name=? " + 398 "WHERE accountNumber=?"); 399 try 400 { 401 ps.setString(1, personsName); 402 ps.setString(2, accountNumber); 403 404 ps.executeUpdate(); 405 } 406 finally 407 { 408 ps.close(); 409 } 410 } 411 finally 412 { 413 con.close(); 414 } 415 } 416 catch (SQLException e) 417 { 418 log.debug("failed", e); 419 420 throw new EJBException (e); 421 } 422 423 log.debug("ejbStore(\"" + accountNumber + "\") returning"); 424 } 425 426 429 public void ejbRemove() 430 { 431 log.debug("ejbRemove(\"" + accountNumber + "\") called"); 432 433 sendMsg(ContainerMBox.EJB_REMOVE_MSG); 434 435 try 436 { 437 Connection con = ds.getConnection(); 438 try 439 { 440 PreparedStatement ps; 441 442 ps = con.prepareStatement("DELETE FROM " + TABLE_NAME + " " + 443 "WHERE accountNumber=?"); 444 try 445 { 446 ps.setString(1, accountNumber); 447 448 ps.executeUpdate(); 449 } 450 finally 451 { 452 ps.close(); 453 } 454 } 455 finally 456 { 457 con.close(); 458 } 459 } 460 catch (SQLException e) 461 { 462 log.debug("failed", e); 463 464 throw new EJBException (e); 465 } 466 467 log.debug("Removed \"" + accountNumber + "\"."); 468 } 469 470 473 public void ejbActivate() 474 { 475 log.debug("ejbActivate() called"); 476 477 sendMsg(ContainerMBox.EJB_ACTIVATE_MSG); 478 } 479 480 483 public void ejbPassivate() 484 { 485 log.debug("ejbPassivate() called"); 486 487 sendMsg(ContainerMBox.EJB_PASSIVATE_MSG); 488 489 if (ms != null) 491 { 492 try 493 { 494 ms.close(); 495 } 496 catch (JMSException e) 497 { 498 log.debug("failed", e); 499 } 501 ms = null; 502 } 503 } 504 505 508 public void setEntityContext(EntityContext ctx) 509 { 510 log.debug("setEntityContext() called"); 511 512 sendMsg(ContainerMBox.SET_ENTITY_CONTEXT_MSG); 513 514 this.ctx = ctx; 515 516 try 518 { 519 Context context = new InitialContext (); 520 521 ds = (DataSource ) context.lookup("java:comp/env/datasource"); 522 } 523 catch (NamingException nex) 524 { 525 log.debug("failed", nex); 526 527 throw new EJBException ("Datasource not found: " + nex.getMessage()); 528 } 529 } 530 531 534 public void unsetEntityContext() 535 { 536 log.debug("unsetEntityContext() called"); 537 538 sendMsg(ContainerMBox.UNSET_ENTITY_CONTEXT_MSG); 539 540 ctx = null; 541 ds = null; 542 } 543 544 545 549 552 private void ensureTableExists() 553 { 554 boolean exists = true; 555 556 try 557 { 558 Connection con = ds.getConnection(); 559 try 560 { 561 Statement s = con.createStatement(); 562 try 563 { 564 ResultSet rs = s.executeQuery("SELECT * FROM " + TABLE_NAME); 565 ResultSetMetaData md = rs.getMetaData(); 566 567 if (md.getColumnCount() != 2) 568 throw new SQLException ("Not two columns"); 569 570 if (!"ACCOUNTNUMBER".equals(md.getColumnName(1).toUpperCase())) 571 throw new SQLException ("First column name not \"accountNumber\""); 572 if (!"NAME".equals(md.getColumnName(2).toUpperCase())) 573 throw new SQLException ("Second column name not \"name\""); 574 575 if (md.getColumnType(1) != Types.VARCHAR) 576 throw new SQLException ("First column type not VARCHAR"); 577 if (md.getColumnType(2) != Types.VARCHAR) 578 throw new SQLException ("Second column type not VARCHAR"); 579 } 580 finally 581 { 582 s.close(); 583 } 584 } 585 finally 586 { 587 con.close(); 588 } 589 } 590 catch (SQLException e) 591 { 592 exists = false; 593 } 594 595 if (!exists) 596 initializeDatabaseTable(); 597 } 598 599 602 private void initializeDatabaseTable() 603 { 604 log.debug("Initializing DATABASE tables for BMP test..."); 605 606 try 607 { 608 Connection con = ds.getConnection(); 609 try 610 { 611 Statement s; 612 613 s = con.createStatement(); 614 try 615 { 616 s.executeUpdate("DROP TABLE " + TABLE_NAME); 617 log.debug("Dropped old table."); 618 } 619 catch (SQLException e) 620 { 621 } 623 finally 624 { 625 s.close(); 626 } 627 628 s = con.createStatement(); 629 try 630 { 631 s.executeUpdate("CREATE TABLE " + TABLE_NAME + " " + 632 "(accountNumber VARCHAR(25)," + 633 " name VARCHAR(200))"); 634 log.debug("Created new table."); 635 } 636 finally 637 { 638 s.close(); 639 } 640 } 641 finally 642 { 643 con.close(); 644 } 645 } 646 catch (SQLException e) 647 { 648 log.debug("failed", e); 649 650 throw new EJBException (e); 651 } 652 653 log.debug("Initialized DATABASE tables for BMP test."); 654 } 655 656 659 private void sendMsg(String msg) 660 { 661 if (ms == null) 662 ms = new MsgSender(); 663 664 ms.sendMsg(msg); 665 } 666 667 671 674 public void setPersonsName(String personsName) 675 { 676 this.personsName = personsName; 677 } 678 679 682 public String getPersonsName() 683 { 684 return this.personsName; 685 } 686 } 687 688 | Popular Tags |