| 1 32 33 34 47 package stockonline.ejb.session.useCMP.broker; 48 49 import java.rmi.*; 50 import java.util.*; 51 import java.sql.*; 52 53 import javax.sql.*; 54 import javax.ejb.*; 55 import javax.naming.*; 56 import javax.rmi.PortableRemoteObject ; 57 59 import stockonline.util.*; import stockonline.ejb.session.interf.*; import stockonline.ejb.entity.interf.*; import stockonline.ejb.sql.SeqGenerator; 63 64 66 67 public class BrokerBean implements SessionBean 68 { 69 final static boolean verbose = true; 72 final static String BEAN_NAME = "cmpBroker"; 73 final static String RESOURCE_NAME = "java:comp/env/jdbc/StockDB"; 74 final static String accountHomeJNDI_NAME = "java:comp/env/ejb/SubAccount"; 75 final static String stockItemHomeJNDI_NAME = "java:comp/env/ejb/StockItem"; 76 final static String stockHoldingHomeJNDI_NAME = "java:comp/env/ejb/StockHolding"; 77 final static String stockTxHomeJNDI_NAME = "java:comp/env/ejb/StockTransaction"; 78 79 protected SessionContext ctx = null; 80 private DataSource ds = null; 81 private AccountHome accountHome = null; 82 private StockItemHome stockItemHome = null; 83 private StockHoldingHome stockHoldingHome= null; 84 private StockTxHome stockTxHome = null; 85 86 private boolean timing = false; 87 private stockonline.util.Timer timer = new stockonline.util.Timer(); 88 private stockonline.util.ResultLog buyLog = new stockonline.util.ResultLog(); 89 private stockonline.util.ResultLog sellLog = new stockonline.util.ResultLog(); 90 private stockonline.util.ResultLog updateLog = new stockonline.util.ResultLog(); 91 private stockonline.util.ResultLog createLog = new stockonline.util.ResultLog(); 92 private stockonline.util.ResultLog queryIDLog = new stockonline.util.ResultLog(); 93 private stockonline.util.ResultLog queryCodeLog = new stockonline.util.ResultLog(); 94 private stockonline.util.ResultLog getHoldingLog = new stockonline.util.ResultLog(); 95 96 100 106 public int newAccount(String name, String address, int credit) 107 throws Exception  108 { 109 if(verbose) print("newAccount(" + name + ", " + address + ", " + credit + ") called"); 110 111 try 112 { 113 if(timing) timer.start(); 114 115 int accountID = newAccount_Imp(name, address, credit); 116 117 if(timing) 118 { 119 timer.stop(); 120 if(verbose) timer.printTime("createTime = "); 121 createLog.addSample(timer.getTime()); 122 } 123 124 129 return accountID; 130 } 131 catch(Exception ex) 132 { 133 System.err.println(BEAN_NAME + ".newAccount(): " + ex.toString()); 134 135 try 136 { 137 if(verbose) print("To rollback"); 138 ctx.setRollbackOnly(); 139 } 140 catch(Exception e) 141 { 142 System.err.println("Fail to rollback in newAccount(), due to " + e.toString()); 143 } 144 throw ex; 145 } 146 } 147 148 152 public QueryResult queryStockValueByID(int stockID) 153 throws Exception  154 { 155 if(verbose) print("queryStockValueByID(" + stockID + ") called"); 156 157 try 158 { 159 if(timing) timer.start(); 160 161 QueryResult prices = queryStockValueByID_Imp(stockID); 162 163 if(timing) 164 { 165 timer.stop(); 166 if(verbose) timer.printTime("queryIDTime = "); 167 queryIDLog.addSample(timer.getTime()); 168 } 169 170 return prices; 171 } 172 catch(Exception ex) 173 { 174 System.err.println(BEAN_NAME + ".queryStockValueByID(): " + ex.toString()); 175 176 try 177 { 178 if(verbose) print("To rollback"); 179 ctx.setRollbackOnly(); 180 } 181 catch(Exception e) 182 { 183 System.err.println("Fail to rollback in queryStockValueByID(), due to " + e.toString()); 184 } 185 throw ex; 186 } 187 } 188 189 193 229 234 public void buyStock(int accountID, int stockID, int amount) 235 throws Exception  236 { 237 if(verbose) print("buyStock(" + accountID + ", " + stockID + ", " + amount + ") called"); 238 239 try 240 { 241 if(timing) timer.start(); 242 243 int txID = buyStock_Imp(accountID, stockID, amount); 244 if(verbose) print("buyStock()......OK: return txID = " + txID); 245 246 if(timing) 247 { 248 timer.stop(); 249 if(verbose) timer.printTime("buyTime = "); 250 buyLog.addSample(timer.getTime()); 251 } 252 253 return; 254 } 255 catch(Exception ex) 256 { 257 System.err.println(BEAN_NAME + ".buyStock(): " + ex.toString()); 258 259 try 260 { 261 if(verbose) print("To rollback"); 262 ctx.setRollbackOnly(); 263 } 264 catch(Exception e) 265 { 266 System.err.println("Fail to rollback in buyStock(), due to " + e.toString()); 267 } 268 throw ex; 269 } 270 } 271 272 277 public void sellStock(int accountID, int stockID, int amount) 278 throws Exception  279 { 280 if(verbose) print("sellStock(" + accountID + ", " + stockID + ", " + amount + ") called"); 281 282 try 283 { 284 if(timing) timer.start(); 285 286 int txID = sellStock_Imp(accountID, stockID, amount); 287 if(verbose) print("sellStock()......OK: return txID = " + txID); 288 289 if(timing) 290 { 291 timer.stop(); 292 if(verbose) timer.printTime("sellTime = "); 293 sellLog.addSample(timer.getTime()); 294 } 295 296 return; 297 } 298 catch(Exception ex) 299 { 300 System.err.println(BEAN_NAME + ".sellStock(): " + ex.toString()); 301 302 try 303 { 304 if(verbose) print("To rollback"); 305 ctx.setRollbackOnly(); 306 } 307 catch(Exception e) 308 { 309 System.err.println("Fail to rollback in sellStock(), due to " + e.toString()); 310 } 311 throw ex; 312 } 313 } 314 315 319 public void updateAccount(int accountID, int credit) 320 throws Exception  321 { 322 if(verbose) print("updataAccount(" + accountID + ", " + credit + ")"); 323 324 try 325 { 326 if(timing) timer.start(); 327 updateAccount_Imp(accountID, credit); 328 if(timing) 329 { 330 timer.stop(); 331 if(verbose) timer.printTime("updayeTime = "); 332 updateLog.addSample(timer.getTime()); 333 } 334 335 if(verbose) print("updataAccount()......OK: return void"); 336 } 337 catch(Exception ex) 338 { 339 System.err.println(BEAN_NAME + ".updateAccount(): " + ex.toString()); 340 341 try 342 { 343 if(verbose) print("To rollback"); 344 ctx.setRollbackOnly(); 345 } 346 catch(Exception e) 347 { 348 System.err.println("Fail to rollback in updateAccount(), due to " + e.toString()); 349 } 350 throw ex; 351 } 352 } 353 354 360 public Collection getHoldingStatement(int accountID, int start_stockID) 361 throws Exception  362 { 363 if(verbose) print("getHoldingStatement(" + accountID + ")"); 364 365 try 366 { 367 if(timing) timer.start(); 368 Collection list = getHoldingStatement_Imp (accountID, start_stockID); 369 if(timing) 370 { 371 timer.stop(); 372 if(verbose) timer.printTime("getHoldingTime = "); 373 getHoldingLog.addSample(timer.getTime()); 374 } 375 376 return list; 377 } 378 catch(Exception ex) 379 { 380 System.err.println(BEAN_NAME + ".getHoldingStatement(): " + ex.toString()); 381 382 try 383 { 384 if(verbose) print("To rollback"); 385 ctx.setRollbackOnly(); 386 } 387 catch(Exception e) 388 { 389 System.err.println("Fail to rollback in getHoldingStatement(), due to " + e.toString()); 390 } 391 throw ex; 392 } 393 } 394 395 399 public Collection cmdChannel(CmdMessage msg) 400 throws Exception  401 { 402 if(verbose) print("cmdChannel(" + msg.cmd + ") called"); 403 404 ArrayList list = null; 405 406 switch(msg.cmd) 407 { 408 case CmdMessage.SET_TIMING_ON: 409 if(verbose) System.out.println("SET_TIMING_ON"); 410 timing = true; 411 break; 412 case CmdMessage.SET_TIMING_OFF: 413 if(verbose) System.out.println("SET_TIMING_OFF"); 414 timing = false; 415 break; 416 case CmdMessage.GET_TIMING_LOG: 417 if(verbose) System.out.println("GET_TIMING_LOG"); 418 list = new ArrayList(); 419 420 list.add(buyLog); 421 list.add(sellLog); 422 list.add(createLog); 423 list.add(updateLog); 424 list.add(queryIDLog); 425 list.add(queryCodeLog); 426 list.add(getHoldingLog); 427 428 if(verbose) System.out.println("GET_TIMING_LOG"); 429 break; 430 case CmdMessage.RESET_DATABASE: 431 if(verbose) System.out.println("RESET_DATABASE"); 432 DBLoad.resetOracleDB(ds); 433 break; 434 default: 435 System.err.println("invalid cmd message"); 436 break; 437 } 438 439 return list; 440 } 441 442 448 public int newAccountForTestRollback(String name, String address, int credit) 449 throws Exception  450 { 451 if(verbose) print("newAccountForTestRollback(" + name + ", " + address + ", " + credit + ") called"); 452 int accountID = 0; 453 454 try 455 { 456 accountID = newAccount_Imp(name, address, credit); 457 if(verbose) System.out.println("accountID = " + accountID); 458 459 if(verbose) print("To rollback"); 460 ctx.setRollbackOnly(); 461 } 462 catch(Exception ex) 463 { 464 ex.printStackTrace(); 465 } 466 finally 467 { 468 return accountID; 469 } 470 } 471 472 476 public boolean testRollback(int accountID) 477 throws Exception  478 { 479 if(verbose) print("testRollBack() called"); 480 boolean testPass = false; 481 482 try 483 { 484 float credit = getCurrentCredit_Imp(accountID); 485 testPass = false; 486 if(verbose) System.out.println("Test failed !"); 487 } 488 catch(Exception e) 489 { 490 testPass = true; 491 if(verbose) System.out.println("Test Passed !"); 492 } 493 494 return testPass; 495 } 496 497 498 502 private int newAccount_Imp(String name, String address, int credit) 503 throws Exception  504 { 505 if(verbose) print("newAccount_Imp(" + name + ", " + address + ", " + credit + ") called"); 506 507 try 508 { 509 513 AccountPK pk = new AccountPK(); 515 pk.sub_accno = SeqGenerator.getNextAccountID(ds); 516 Account account = accountHome.create(pk.sub_accno, name, address, credit); 517 518 if(verbose) System.out.println("accountID = " + pk.sub_accno); 519 return pk.sub_accno; 520 } 521 catch(CreateException e1) 522 { 523 System.err.println(BEAN_NAME + ".newAccount_Imp(): " + e1.toString()); 524 throw new Exception (e1.toString()); 525 } 526 catch(Exception e2) 527 { 528 System.err.println(BEAN_NAME + ".newAccount_Imp(): " + e2.toString()); 529 throw new Exception (e2.toString()); 530 } 531 } 532 533 private float getCurrentCredit_Imp(int accountID) 534 throws Exception  535 { 536 if(verbose) print("getCurrentCredit_Imp(" + accountID + ") called"); 537 538 try 539 { 540 Account account = accountHome.findByPrimaryKey(new AccountPK(accountID)); 541 float credit = (float) account.getCredit(); 542 if(verbose) System.out.println("credit = $" + credit); 543 544 return credit; 545 } 546 catch(Exception ex) 547 { 548 ex.printStackTrace(); 549 throw ex; 550 } 551 } 552 553 554 private void updateAccount_Imp(int accountID, int credit) 555 throws Exception  556 { 557 if(verbose) print("updateAccount_Imp(" + accountID + ", " + credit + ") called"); 558 559 try 560 { 561 Account account = accountHome.findByPrimaryKey(new AccountPK(accountID)); 562 account.update(credit); 563 564 return; 565 } 566 catch(FinderException e1) 567 { 568 System.err.println(BEAN_NAME + ".updateAccount_Imp(): " + e1.toString()); 569 throw new Exception (e1.toString()); 570 } 571 catch(Exception e2) 572 { 573 System.err.println(BEAN_NAME + ".updateAccount_Imp(): " + e2.toString()); 574 throw new Exception (e2.toString()); 575 } 576 } 577 578 private QueryResult queryStockValueByID_Imp(int stockID) 579 throws Exception  580 { 581 if(verbose) print("queryStockValueByID_Imp(" + stockID + ") called"); 582 583 try 584 { 585 StockItemPK pk = new StockItemPK(stockID); 586 StockItem stockItem = stockItemHome.findByPrimaryKey(pk); 587 QueryResult prices = stockItem.getValues(); 588 589 if(verbose) 590 { 591 System.out.println("current_val = " + prices.current_val); 592 System.out.println(" high_val = " + prices.high_val); 593 System.out.println(" low_val = " + prices.low_val); 594 } 595 596 return prices; 597 } 598 catch(Exception ex) 599 { 600 System.err.println("Exception in BrokerBean.queryStockValueByID_Imp(): " + ex.toString()); 601 throw ex; 602 } 603 } 604 631 private int buyStock_Imp(int accountID, int stockID, int amount) 632 throws Exception  633 { 634 if(verbose) print("bugStock_Imp(" + accountID + ", " + stockID + ", " + amount + ") called"); 635 636 try 637 { 638 Account account = accountHome.findByPrimaryKey(new AccountPK(accountID)); 643 float credit = (float) account.getCredit(); 644 if(verbose) System.out.println("credit = $" + credit); 645 646 StockItem stockitem = stockItemHome.findByPrimaryKey(new StockItemPK(stockID)); 647 float current_val = stockitem.getCurrentValue(); 648 if(verbose) System.out.println("current_val = $" + current_val); 649 650 float payment = current_val * amount; 651 if(verbose) System.out.println("payment = $" + payment); 652 653 656 try 659 { 660 StockHolding stockholding = stockHoldingHome.findByPrimaryKey(new StockHoldingPK(accountID, stockID)); 661 stockholding.increase(amount); 663 } 664 catch (FinderException e) 665 { 666 stockHoldingHome.create(accountID, stockID, amount); 668 } 669 670 675 StockTxPK pk = new StockTxPK(); 678 pk.trans_id = SeqGenerator.getNextTxID(ds); 679 StockTx stockTx = stockTxHome.create(pk.trans_id, "B", accountID, stockID, amount, current_val); 680 if(verbose) System.out.println("pk.trans_id = " + pk.trans_id); 681 682 return pk.trans_id; 683 } 684 catch(Exception ex) 685 { 686 System.err.println("Exception in buyStock_Imp(): " + ex.toString()); 687 throw ex; 688 } 689 } 690 691 private int sellStock_Imp(int accountID, int stockID, int amount) 692 throws Exception  693 { 694 if(verbose) print("sellStock_Imp(" + accountID + ", " + stockID + ", " + amount + ") called"); 695 696 try 697 { 698 StockHolding stockholding = null; 703 try 704 { 705 stockholding = stockHoldingHome.findByPrimaryKey(new StockHoldingPK(accountID, stockID)); 706 } 707 catch (FinderException ex) 708 { 709 throw new Exception ("The account does not hold the stock."); 710 } 711 712 int temp = stockholding.getAmount(); 713 if (amount > temp) 714 amount = temp; 717 stockholding.decrease(amount); 719 720 StockItem stockitem = stockItemHome.findByPrimaryKey(new StockItemPK(stockID)); 723 float current_val = stockitem.getCurrentValue(); 724 if(verbose) System.out.println("current_val = $" + current_val); 725 726 731 StockTxPK pk = new StockTxPK(); 734 pk.trans_id = SeqGenerator.getNextTxID(ds); 735 StockTx stockTx = stockTxHome.create(pk.trans_id, "S", accountID, stockID, amount, current_val); 736 if(verbose) System.out.println("pk.trans_id = " + pk.trans_id); 737 738 return pk.trans_id; 739 } 740 catch(Exception ex) 741 { 742 System.err.println(BEAN_NAME + ".buyStock_Imp(): " + ex.toString()); 743 throw ex; 744 } 745 } 746 747 private Collection getHoldingStatement_Imp (int accountID, int start_stockID) 748 throws Exception  749 { 750 if(verbose) print("getHoldingStatement_Imp(" + accountID + ", " + start_stockID + ") called"); 751 752 try 753 { 754 if(verbose) print("To get holding list by calling stockHoldingHome.findByAccountID()..."); 755 Collection list1 = stockHoldingHome.findByAccountID(accountID, start_stockID); Collection list2 = new ArrayList(); 758 Iterator it = list1.iterator(); 759 int rowNum = 20; 760 761 for(int i=0; it.hasNext() && i<rowNum; i++) 762 { 763 StockHolding sh = (StockHolding) PortableRemoteObject.narrow(it.next(), StockHolding.class); 764 Holding holding = sh.getHolding(); 765 if(verbose) System.out.println("stock_id = " + holding.stock_id + " amount = " + holding.amount); 766 list2.add(holding); 767 } 768 769 return list2; 770 } 771 catch(FinderException e) 772 { 773 System.err.println(BEAN_NAME + ".getHoldingStatement_Imp(): " + e.toString()); 774 throw new Exception (e.toString()); 775 } 776 catch(Exception ex) 777 { 778 System.err.println(BEAN_NAME + ".getHoldingStatement_Imp(): " + ex.toString()); 779 throw ex; 780 } 781 } 782 783 private void print( String message ) 786 { 787 System.out.println("[" + Thread.currentThread().getName() + "] " + message); 788 } 789 790 private void lookupResources() 793 throws CreateException 794 { 795 try 796 { 797 Object obj = null; 798 799 if(verbose) System.out.println("To get a inital ctx..."); 800 InitialContext initCtx = new InitialContext(); 801 802 if(verbose) print("To get datasource: " + RESOURCE_NAME); 803 ds = (DataSource) initCtx.lookup(RESOURCE_NAME); 804 805 if(verbose) System.out.println("To get: " + accountHomeJNDI_NAME); 806 obj = (Object ) initCtx.lookup(accountHomeJNDI_NAME); 807 accountHome = (AccountHome) javax.rmi.PortableRemoteObject.narrow(obj, AccountHome.class); 808 809 if(verbose) System.out.println("To get: " + stockItemHomeJNDI_NAME); 810 obj = (Object ) initCtx.lookup(stockItemHomeJNDI_NAME); 811 stockItemHome = (StockItemHome) javax.rmi.PortableRemoteObject.narrow(obj, StockItemHome.class); 812 813 if(verbose) System.out.println("To get: " + stockHoldingHomeJNDI_NAME); 814 obj = (Object ) initCtx.lookup(stockHoldingHomeJNDI_NAME); 815 stockHoldingHome = (StockHoldingHome) javax.rmi.PortableRemoteObject.narrow(obj, StockHoldingHome.class); 816 817 if(verbose) System.out.println("To get: " + stockTxHomeJNDI_NAME); 818 obj = (Object ) initCtx.lookup(stockTxHomeJNDI_NAME); 819 stockTxHome = (StockTxHome) javax.rmi.PortableRemoteObject.narrow(obj, StockTxHome.class); 820 } 821 catch (javax.naming.NamingException ex) 822 { 823 System.err.println(BEAN_NAME + " : " + ex.toString()); 824 throw new CreateException("Failed to create " + BEAN_NAME + ", due to " + ex.toString()); 825 } 826 } 827 828 832 public void setSessionContext(SessionContext ctx) 833 { 834 this.ctx = ctx; 835 } 836 837 839 public void ejbCreate() throws CreateException 840 { 841 if(verbose) print("ejbCreate() called"); 842 843 lookupResources(); 844 } 845 846 public void ejbRemove() {} 847 public void ejbActivate() {} 848 public void ejbPassivate() {} 849 } 850 851 | Popular Tags |