1 32 33 34 45 package stockonline.ejb.session.stateless.broker; 46 47 import java.rmi.*; 48 import java.util.*; 49 import java.sql.*; 50 51 import javax.sql.*; 52 import javax.ejb.*; 53 import javax.naming.*; 54 import javax.rmi.PortableRemoteObject ; 55 56 import stockonline.util.*; import stockonline.ejb.session.interf.*; import stockonline.ejb.sql.*; 60 61 63 64 public class BrokerBean implements SessionBean 65 { 66 final static boolean verbose = false; 69 final static String BEAN_NAME = "statelessBroker"; 70 final static String RESOURCE_NAME = "java:comp/env/jdbc/StockDB"; 71 72 protected SessionContext ctx = null; 73 private DataSource ds = null; 74 private Account account = new Account(); 75 private StockItem stock = new StockItem(); 76 private StockHolding holding = new StockHolding(); 77 private StockTx transaction = new StockTx(); 78 79 private boolean timing = false; 80 private stockonline.util.Timer timer = new stockonline.util.Timer(); 81 private stockonline.util.ResultLog buyLog = new stockonline.util.ResultLog(); 82 private stockonline.util.ResultLog sellLog = new stockonline.util.ResultLog(); 83 private stockonline.util.ResultLog updateLog = new stockonline.util.ResultLog(); 84 private stockonline.util.ResultLog createLog = new stockonline.util.ResultLog(); 85 private stockonline.util.ResultLog queryIDLog = new stockonline.util.ResultLog(); 86 private stockonline.util.ResultLog queryCodeLog = new stockonline.util.ResultLog(); 87 private stockonline.util.ResultLog getHoldingLog = new stockonline.util.ResultLog(); 88 89 93 99 public int newAccount(String name, String address, int credit) 100 throws Exception 101 { 102 if(verbose) print("newAccount(" + name + ", " + address + ", " + credit + ") called"); 103 104 try 105 { 106 if(timing) timer.start(); 107 108 int accountID = newAccount_Imp(name, address, credit); 109 110 if(timing) 111 { 112 timer.stop(); 113 if(verbose) timer.printTime("createTime = "); 114 createLog.addSample(timer.getTime()); 115 } 116 117 122 return accountID; 123 } 124 catch(Exception ex) 125 { 126 System.err.println("Exception in newAccount(): " + ex.toString()); 127 128 try 129 { 130 if(verbose) print("To rollback"); 131 ctx.setRollbackOnly(); 132 } 133 catch(Exception e) 134 { 135 System.err.println("Fail to rollback in newAccount(), due to " + e.toString()); 136 } 137 throw ex; 138 } 139 } 140 141 145 public QueryResult queryStockValueByID(int stockID) 146 throws Exception 147 { 148 if(verbose) print("queryStockValueByID(" + stockID + ") called"); 149 150 try 151 { 152 if(timing) timer.start(); 153 154 QueryResult prices = queryStockValueByID_Imp(stockID); 155 156 if(timing) 157 { 158 timer.stop(); 159 if(verbose) timer.printTime("queryIDTime = "); 160 queryIDLog.addSample(timer.getTime()); 161 } 162 163 return prices; 164 } 165 catch(Exception ex) 166 { 167 System.err.println("Exception in queryStockValueByID(): " + ex.toString()); 168 169 try 170 { 171 if(verbose) print("To rollback"); 172 ctx.setRollbackOnly(); 173 } 174 catch(Exception e) 175 { 176 System.err.println("Fail to rollback in queryStockValueByID(), due to " + e.toString()); 177 } 178 throw ex; 179 } 180 } 181 182 186 222 227 public void buyStock(int accountID, int stockID, int amount) 228 throws Exception 229 { 230 if(verbose) print("buyStock(" + accountID + ", " + stockID + ", " + amount + ") called"); 231 232 try 233 { 234 if(timing) timer.start(); 235 int txID = buyStock_Imp(accountID, stockID, amount); 236 if(timing) 237 { 238 timer.stop(); 239 if(verbose) timer.printTime("buyTime = "); 240 buyLog.addSample(timer.getTime()); 241 } 242 243 return; 244 } 245 catch(Exception ex) 246 { 247 System.err.println("Exception in buyStock(): " + ex.toString()); 248 249 try 250 { 251 if(verbose) print("To rollback"); 252 ctx.setRollbackOnly(); 253 } 254 catch(Exception e) 255 { 256 System.err.println("Fail to rollback in buyStock(), due to " + e.toString()); 257 } 258 throw ex; 259 } 260 } 261 262 267 public void sellStock(int accountID, int stockID, int amount) 268 throws Exception 269 { 270 if(verbose) print("sellStock(" + accountID + ", " + stockID + ", " + amount + ") called"); 271 272 try 273 { 274 if(timing) timer.start(); 275 int txID = sellStock_Imp(accountID, stockID, amount); 276 if(verbose) print("buyStock()......OK: return txID = " + txID); 277 if(timing) 278 { 279 timer.stop(); 280 if(verbose) timer.printTime("sellTime = "); 281 sellLog.addSample(timer.getTime()); 282 } 283 284 return; 285 } 286 catch(Exception ex) 287 { 288 System.err.println("Exception in sellStock(): " + ex.toString()); 289 290 try 291 { 292 if(verbose) print("To rollback"); 293 ctx.setRollbackOnly(); 294 } 295 catch(Exception e) 296 { 297 System.err.println("Fail to rollback in sellStock(), due to " + e.toString()); 298 } 299 throw ex; 300 } 301 } 302 303 307 public void updateAccount(int accountID, int credit) 308 throws Exception 309 { 310 if(verbose) print("updataAccount(" + accountID + ", " + credit + ")"); 311 312 try 313 { 314 if(timing) timer.start(); 315 updateAccount_Imp(accountID, credit); 316 if(timing) 317 { 318 timer.stop(); 319 if(verbose) timer.printTime("updayeTime = "); 320 updateLog.addSample(timer.getTime()); 321 } 322 323 if(verbose) print("updataAccount()......OK: return void"); 324 } 325 catch(Exception ex) 326 { 327 System.err.println("Exception in updateAccount(): " + ex.toString()); 328 329 try 330 { 331 if(verbose) print("To rollback"); 332 ctx.setRollbackOnly(); 333 } 334 catch(Exception e) 335 { 336 System.err.println("Fail to rollback in updateAccount(), due to " + e.toString()); 337 } 338 throw ex; 339 } 340 } 341 342 348 public Collection getHoldingStatement(int accountID, int start_stockID) 349 throws Exception 350 { 351 if(verbose) print("getHoldingStatement(" + accountID + ")"); 352 353 try 354 { 355 if(timing) timer.start(); 356 Collection list = getHoldingStatement_Imp (accountID, start_stockID); 357 if(timing) 358 { 359 timer.stop(); 360 if(verbose) timer.printTime("getHoldingTime = "); 361 getHoldingLog.addSample(timer.getTime()); 362 } 363 364 return list; 365 } 366 catch(Exception ex) 367 { 368 System.err.println("Exception in updateAccount(): " + ex.toString()); 369 370 try 371 { 372 if(verbose) print("To rollback"); 373 ctx.setRollbackOnly(); 374 } 375 catch(Exception e) 376 { 377 System.err.println("Fail to rollback in updateAccount(), due to " + e.toString()); 378 } 379 throw ex; 380 } 381 } 382 383 387 public Collection cmdChannel(CmdMessage msg) 388 throws Exception 389 { 390 if(verbose) print("cmdChannel(" + msg.cmd + ") called"); 391 392 ArrayList list = new ArrayList(); 393 394 switch(msg.cmd) 395 { 396 case CmdMessage.SET_TIMING_ON: 397 if(verbose) System.out.println("SET_TIMING_ON"); 398 timing = true; 399 break; 400 case CmdMessage.SET_TIMING_OFF: 401 if(verbose) System.out.println("SET_TIMING_OFF"); 402 timing = false; 403 break; 404 case CmdMessage.GET_TIMING_LOG: 405 if(verbose) System.out.println("GET_TIMING_LOG"); 406 407 list.add(buyLog); 408 list.add(sellLog); 409 list.add(createLog); 410 list.add(updateLog); 411 list.add(queryIDLog); 412 list.add(queryCodeLog); 413 list.add(getHoldingLog); 414 415 if(verbose) System.out.println("GET_TIMING_LOG"); 416 break; 417 case CmdMessage.RESET_DATABASE: 418 if(verbose) System.out.println("RESET_DATABASE"); 419 DBLoad.resetOracleDB(ds); 420 break; 421 default: 422 System.err.println("invalid cmd message"); 423 break; 424 } 425 426 return list; 427 } 428 429 435 public int newAccountForTestRollback(String name, String address, int credit) 436 throws Exception 437 { 438 if(verbose) print("newAccountForTestRollback(" + name + ", " + address + ", " + credit + ") called"); 439 int accountID = 0; 440 441 try 442 { 443 accountID = newAccount_Imp(name, address, credit); 444 if(verbose) print("accountID = " + accountID); 445 446 if(verbose) System.out.println("To rollback"); 447 ctx.setRollbackOnly(); 448 } 449 catch(Exception ex) 450 { 451 System.err.println(BEAN_NAME + " Exception: " + ex.getMessage()); 452 } 453 finally 454 { 455 return accountID; 456 } 457 } 458 459 463 public boolean testRollback(int accountID) 464 throws Exception 465 { 466 if(verbose) print("testRollBack() called"); 467 boolean testPass = false; 468 469 try 470 { 471 float credit = getCurrentCredit_Imp(accountID); 472 testPass = false; 473 if(verbose) System.out.println("Test failed !"); 474 } 475 catch(Exception e) 476 { 477 testPass = true; 478 if(verbose) System.out.println("Test Passed !"); 479 } 480 481 return testPass; 482 } 483 484 488 private int newAccount_Imp(String name, String address, int credit) 489 throws Exception 490 { 491 if(verbose) print("newAccount_Imp(" + name + ", " + address + ", " + credit + ") called"); 492 493 Connection con = ds.getConnection(); 494 495 try 496 { 497 int accountID = account.createAccount(con, name, address, credit); 498 return accountID; 499 } 500 catch(Exception ex) 501 { 502 System.err.println(BEAN_NAME + " Exception: " + ex.getMessage()); 503 throw ex; 504 } 505 finally 506 { 507 con.close(); 508 } 509 } 510 511 private float getCurrentCredit_Imp(int accountID) 512 throws Exception 513 { 514 if(verbose) print("getCurrentCredit_Imp(" + accountID + ") called"); 515 516 Connection con = ds.getConnection(); 517 518 try 519 { 520 float credit = account.getCredit(con, accountID); 521 return credit; 522 } 523 catch(Exception ex) 524 { 525 System.err.println(BEAN_NAME + " Exception: " + ex.getMessage()); 526 throw ex; 527 } 528 finally 529 { 530 con.close(); 531 } 532 } 533 534 private void updateAccount_Imp(int accountID, int credit) 535 throws Exception 536 { 537 if(verbose) print("nupdateAccount_Imp(" + accountID + ", " + credit + ") called"); 538 539 Connection con = ds.getConnection(); 540 541 try 542 { 543 account.updateCredit(con, accountID, credit); 544 } 545 catch(Exception ex) 546 { 547 System.err.println(BEAN_NAME + " Exception: " + ex.getMessage()); 548 throw ex; 549 } 550 finally 551 { 552 con.close(); 553 } 554 } 555 556 private QueryResult queryStockValueByID_Imp(int stockID) 557 throws Exception 558 { 559 if(verbose) print("queryStockValueByID_Imp(" + stockID + ") called"); 560 561 Connection con = ds.getConnection(); 562 563 try 564 { 565 QueryResult prices = stock.queryByID(con, stockID); 566 return prices; 567 } 568 catch(Exception ex) 569 { 570 System.err.println(BEAN_NAME + " Exception: " + ex.getMessage()); 571 throw ex; 572 } 573 finally 574 { 575 con.close(); 576 } 577 } 578 579 private QueryResult queryStockValueByCode_Imp(String stockCode) 580 throws Exception 581 { 582 if(verbose) print("queryStockValueByCode_Imp(" + stockCode + ") called"); 583 584 Connection con = ds.getConnection(); 585 586 try 587 { 588 QueryResult prices = stock.queryByCode(con, stockCode); 589 return prices; 590 } 591 catch(Exception ex) 592 { 593 System.err.println(BEAN_NAME + " Exception: " + ex.getMessage()); 594 throw ex; 595 } 596 finally 597 { 598 con.close(); 599 } 600 } 601 602 private int buyStock_Imp(int accountID, int stockID, int amount) 603 throws Exception 604 { 605 if(verbose) print("bugStock_Imp(" + accountID + ", " + stockID + ", " + amount + ") called"); 606 607 Connection con = ds.getConnection(); 609 610 try 611 { 612 float credit = account.getCredit(con, accountID); 617 if(verbose) System.out.println("credit = $" + credit); 618 619 float current_val = stock.getCurrentPrice(con, stockID); 620 if(verbose) System.out.println("current_val = $" + current_val); 621 622 float payment = current_val * amount; 623 if(verbose) System.out.println("payment = $" + payment); 624 625 628 holding.updateForBuy(con, accountID, stockID, amount); 631 632 637 int txID = transaction.create(con, "B", accountID, stockID, amount, current_val); 640 641 return txID; 642 } 643 catch (Exception ex) 644 { 645 System.err.println("Exception in BrokerBean.buyStock_Imp(): " + ex.getMessage()); 646 throw ex; 647 } 648 finally 649 { 650 con.close(); 651 } 652 } 653 654 private int sellStock_Imp(int accountID, int stockID, int amount) 655 throws Exception 656 { 657 if(verbose) print("sellStock_Imp(" + accountID + ", " + stockID + ", " + amount + ") called"); 658 659 Connection con = ds.getConnection(); 661 662 try 663 { 664 float currentPrice = stock.getCurrentPrice(con, stockID); 667 if(verbose) System.out.println("currentPrice = " + currentPrice); 668 669 holding.updateForSell(con, accountID, stockID, amount); 674 675 681 int txID = transaction.create(con, "S", accountID, stockID, amount, currentPrice); 684 685 return txID; 686 } 687 catch (Exception ex) 688 { 689 System.err.println("Exception in BrokerBean.sellStock_Imp(): " + ex.getMessage()); 690 throw ex; 691 } 692 finally 693 { 694 con.close(); 695 } 696 } 697 698 private Collection getHoldingStatement_Imp (int accountID, int start_stockID) 699 throws Exception 700 { 701 if(verbose) print("getHoldingStatement_Imp(" + accountID + ", " + start_stockID + ") called"); 702 703 Connection con = ds.getConnection(); 704 705 try 706 { 707 Collection list = holding.getHoldingList(con, accountID, start_stockID); 708 return list; 709 } 710 catch(Exception ex) 711 { 712 System.err.println(BEAN_NAME + " Exception: " + ex.getMessage()); 713 throw ex; 714 } 715 finally 716 { 717 con.close(); 718 } 719 } 720 721 private void print( String message ) 724 { 725 System.out.println("[" + Thread.currentThread().getName() + "] " + message); 726 } 727 728 732 public void setSessionContext(SessionContext ctx) 733 { 734 this.ctx = ctx; 735 } 736 737 739 public void ejbCreate() 740 throws CreateException 741 { 742 if(verbose) print("ejbCreate() called"); 743 744 try 745 { 746 if(verbose) print("To get datasource: " + RESOURCE_NAME); 747 ds = (DataSource)(new InitialContext()).lookup(RESOURCE_NAME); 748 } 749 catch (javax.naming.NamingException ex) 750 { 751 System.err.println(BEAN_NAME + ".ejbCreate(): " + ex.toString()); 752 } 754 } 755 756 public void ejbRemove() {} 757 public void ejbActivate() {} 758 public void ejbPassivate() {} 759 } 760 761 | Popular Tags |