KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stockonline > ejb > session > useCMP > broker > BrokerBean


1 /*
2  * StockOnline: EJB 1.1 Benchmark.
3  *
4  * Copyright Commonwealth Scientific and Industrial Research Organisation (CSIRO - www.csiro.au), Australia 2001, 2002, 2003.
5  *
6  * Contact: Paul.Brebner@csiro.au
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or any
11  * later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * Originally developed for the CSIRO Middleware Technology Evaluation (MTE) Project, by
23  * the Software Architectures and Component Technologies Group, CSIRO Mathematical and Information Sciences
24  * Canberra and Sydney, Australia
25  *
26  * www.cmis.csiro.au/sact/
27  * www.cmis.csiro.au/adsat/mte.htm
28  *
29  * Initial developer(s): Shiping Chen, Paul Brebner, Lei Hu, Shuping Ran, Ian Gorton, Anna Liu.
30  * Contributor(s): ______________________.
31  */

32
33
34 //
35
//
36
// History:
37
// 10/08/2001 Shiping Initial coding based on the existing code
38
//
39
// 15/11/2001 Shiping Change all entity beans' names to their table names.
40
//
41
// 26/11/2001 Shiping Two changes to avoide application excpetions
42
// (Note: These should not occur if database has been correctly initialised):
43
// 1. If an account's credit is not enough, keep going
44
// 2. If an account's holding is not enough, keep going
45
//
46

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 JavaDoc;
57 // import javax.transaction.UserTransaction;
58

59 import stockonline.util.*; // common stuff shared between clients and EJB servers
60
import stockonline.ejb.session.interf.*; // stockonline session beans interfaces
61
import stockonline.ejb.entity.interf.*; // stockonline entity beans interfaces
62
import stockonline.ejb.sql.SeqGenerator;
63
64 /** This class implements the business logic for the Stockonline application.
65 */

66
67 public class BrokerBean implements SessionBean
68 {
69     // Internal constances
70
//
71
final static boolean verbose = true;
72     final static String JavaDoc BEAN_NAME = "cmpBroker";
73     final static String JavaDoc RESOURCE_NAME = "java:comp/env/jdbc/StockDB";
74     final static String JavaDoc accountHomeJNDI_NAME = "java:comp/env/ejb/SubAccount";
75     final static String JavaDoc stockItemHomeJNDI_NAME = "java:comp/env/ejb/StockItem";
76     final static String JavaDoc stockHoldingHomeJNDI_NAME = "java:comp/env/ejb/StockHolding";
77     final static String JavaDoc 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     // ----------------
97
// Business methods
98
//-----------------
99

100     /** To create a new account
101     * @param name The name of the account owner
102     * @param address The address of the account owner
103     * @param credit The initial credit in the account
104     * @return The iditifier of the new account
105     */

106     public int newAccount(String JavaDoc name, String JavaDoc address, int credit)
107         throws Exception JavaDoc
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             // To test transaction rollback:
125
// only the accounts with even account numbers should be created.
126
//
127
// if(accountID%2==1) throw new Exception("To test rollback of a transaction");
128

129             return accountID;
130         }
131         catch(Exception JavaDoc 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 JavaDoc e)
141             {
142                 System.err.println("Fail to rollback in newAccount(), due to " + e.toString());
143             }
144             throw ex;
145         }
146     }
147
148     /** To query a stock's prices by its ID
149     * @param stockID The iditifer of the stock (1-3000)
150     * @return An object which wraps the stock prices
151     */

152     public QueryResult queryStockValueByID(int stockID)
153         throws Exception JavaDoc
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 JavaDoc 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 JavaDoc e)
182             {
183                 System.err.println("Fail to rollback in queryStockValueByID(), due to " + e.toString());
184             }
185             throw ex;
186         }
187     }
188
189     /** To query a stock's prices by its Code
190     * @param stockCode The code of the stock (C1-C3000)
191     * @return An object which wraps the stock prices
192     */

193 /*
194     public QueryResult queryStockValueByCode(String stockCode)
195         throws Exception
196     {
197         if(verbose) print("queryStockValueByCode(" + stockCode + ") called");
198     
199         try
200         {
201             if(timing) timer.start();
202             QueryResult prices = queryStockValueByCode_Imp(stockCode);
203             if(timing)
204             {
205                 timer.stop();
206                 if(verbose) timer.printTime("queryCodeTime = ");
207                 queryCodeLog.addSample(timer.getTime());
208             }
209
210             return prices;
211         }
212         catch(Exception ex)
213         {
214             System.err.println(BEAN_NAME + ".queryStockValueByCode(): " + ex.toString());
215
216             try
217             {
218                 if(verbose) print("To rollback");
219                 ctx.setRollbackOnly();
220             }
221             catch(Exception e)
222             {
223                 System.err.println("Fail to rollback in queryStockValueByCode(), due to " + e.toString());
224             }
225             throw ex;
226         }
227     }
228 */

229     /** To buy amount of a stock
230     * @param accountID The iditifier of the acccount
231     * @param stockID The iditifier of the stock
232     * @param amount The amount of the stock to buy
233     */

234     public void buyStock(int accountID, int stockID, int amount)
235         throws Exception JavaDoc
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 JavaDoc 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 JavaDoc e)
265             {
266                 System.err.println("Fail to rollback in buyStock(), due to " + e.toString());
267             }
268             throw ex;
269         }
270     }
271
272     /** To buy amount of a stock
273     * @param accountID The iditifier of the acccount
274     * @param stockID The iditifier of the stock
275     * @param amount The amount of the stock to sell
276     */

277     public void sellStock(int accountID, int stockID, int amount)
278         throws Exception JavaDoc
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 JavaDoc 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 JavaDoc e)
308             {
309                 System.err.println("Fail to rollback in sellStock(), due to " + e.toString());
310             }
311             throw ex;
312         }
313     }
314
315     /** To update the credit of an account
316     * @param accountID The iditifier of the acccount
317     * @param credit The credit to be increased or decreased (-)
318     */

319     public void updateAccount(int accountID, int credit)
320         throws Exception JavaDoc
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 JavaDoc 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 JavaDoc e)
347             {
348                 System.err.println("Fail to rollback in updateAccount(), due to " + e.toString());
349             }
350             throw ex;
351         }
352     }
353
354     /** To get current stock holding statement for an account
355     * @param accountID The iditifier of the acccount
356     * @param start_stockID The stockID from which the list starts, default is 0
357     * @return A set of objects of Holding class, which wraps the holding information.
358     * @see stockonline.util.Holding
359     */

360     public Collection getHoldingStatement(int accountID, int start_stockID)
361         throws Exception JavaDoc
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 JavaDoc 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 JavaDoc e)
388             {
389                 System.err.println("Fail to rollback in getHoldingStatement(), due to " + e.toString());
390             }
391             throw ex;
392         }
393     }
394
395     /** This method provides an interface for clients and servers to communicate each other
396     * @param msg The command
397     * @return The expected message in a string
398     */

399     public Collection cmdChannel(CmdMessage msg)
400         throws Exception JavaDoc
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     /** To create a new account for testing rollback
443     * @param name The name of the account owner
444     * @param address The address of the account owner
445     * @param credit The initial credit in the account
446     * @return The iditifier of the new account
447     */

448     public int newAccountForTestRollback(String JavaDoc name, String JavaDoc address, int credit)
449         throws Exception JavaDoc
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 JavaDoc ex)
463         {
464             ex.printStackTrace();
465         }
466         finally
467         {
468             return accountID;
469         }
470     }
471
472     /** To test rollback
473     * @param accountID The iditifier of account
474     * @return true = rollback sucessful; false = rollback unsucessful
475     */

476     public boolean testRollback(int accountID)
477         throws Exception JavaDoc
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 JavaDoc e)
489         {
490             testPass = true;
491             if(verbose) System.out.println("Test Passed !");
492         }
493         
494         return testPass;
495     }
496
497
498     //-----------------------------------------------
499
// Implemenetation using the entity beans
500
//-----------------------------------------------
501

502     private int newAccount_Imp(String JavaDoc name, String JavaDoc address, int credit)
503         throws Exception JavaDoc
504     {
505         if(verbose) print("newAccount_Imp(" + name + ", " + address + ", " + credit + ") called");
506
507         try
508         {
509             // get key within the entity bean
510
// Account account = accountHome.create(name, address, credit);
511
// AccountPK pk = (AccountPK) account.getPrimaryKey();
512

513             // get key outside the entity bean
514
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 JavaDoc(e1.toString());
525         }
526         catch(Exception JavaDoc e2)
527         {
528             System.err.println(BEAN_NAME + ".newAccount_Imp(): " + e2.toString());
529             throw new Exception JavaDoc(e2.toString());
530         }
531     }
532
533     private float getCurrentCredit_Imp(int accountID)
534         throws Exception JavaDoc
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 JavaDoc ex)
547         {
548             ex.printStackTrace();
549             throw ex;
550         }
551     }
552
553
554     private void updateAccount_Imp(int accountID, int credit)
555         throws Exception JavaDoc
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 JavaDoc(e1.toString());
570         }
571         catch(Exception JavaDoc e2)
572         {
573             System.err.println(BEAN_NAME + ".updateAccount_Imp(): " + e2.toString());
574             throw new Exception JavaDoc(e2.toString());
575         }
576     }
577
578     private QueryResult queryStockValueByID_Imp(int stockID)
579             throws Exception JavaDoc
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 JavaDoc ex)
599         {
600             System.err.println("Exception in BrokerBean.queryStockValueByID_Imp(): " + ex.toString());
601             throw ex;
602         }
603     }
604 /*
605     private QueryResult queryStockValueByCode_Imp(String stockCode)
606             throws SQLException, Exception
607     {
608         if(verbose) print("queryStockValueByCode_Imp(" + stockCode + ") called");
609
610         try
611         {
612             StockItem stockItem = stockItemHome.findByCode(stockCode);
613             QueryResult prices = stockItem.getValues();
614
615             if(verbose)
616             {
617                 System.out.println("current_val = " + prices.current_val);
618                 System.out.println(" high_val = " + prices.high_val);
619                 System.out.println(" low_val = " + prices.low_val);
620             }
621
622             return prices;
623         }
624         catch(Exception ex)
625         {
626             System.err.println("Exception in BrokerBean.queryStockValueByID_Imp(): " + ex.toString());
627             throw ex;
628         }
629     }
630 */

631     private int buyStock_Imp(int accountID, int stockID, int amount)
632         throws Exception JavaDoc
633     {
634         if(verbose) print("bugStock_Imp(" + accountID + ", " + stockID + ", " + amount + ") called");
635
636         try
637         {
638             // Step 1. To check credit
639
// If the account does not have enough credit to buy,
640
// an exception will be thrown.
641
//
642
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             // if (credit < payment) // if the account cannot afford the buy
654
// throw new Exception("No enough credit for the buy transaction: accountID = " + accountID);
655

656             // Step 2. To update stockholding associated with this account
657
//
658
try
659             {
660                 StockHolding stockholding = stockHoldingHome.findByPrimaryKey(new StockHoldingPK(accountID, stockID));
661                 // if(verbose) System.out.println("This account holds this stock, so update it");
662
stockholding.increase(amount);
663             }
664             catch (FinderException e)
665             {
666                 // if(verbose) System.out.println("This account does not hold the stock, thus create a new record");
667
stockHoldingHome.create(accountID, stockID, amount);
668             }
669
670             // Step 3. To update the account credit for the transaction.
671
// (Should do, but we do NOT do that currently)
672
//
673
// account.withdrow( (int) payment);
674

675             // Step 4. To create the transaction record
676
//
677
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 JavaDoc 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 JavaDoc
693     {
694         if(verbose) print("sellStock_Imp(" + accountID + ", " + stockID + ", " + amount + ") called");
695
696         try
697         {
698             // Step 1. To check and update the stock holding
699
// If the account does not hold or does not hold enough holding for sell,
700
// an exception will be thrown.
701
//
702
StockHolding stockholding = null;
703             try
704             {
705                 stockholding = stockHoldingHome.findByPrimaryKey(new StockHoldingPK(accountID, stockID));
706             }
707             catch (FinderException ex)
708             {
709                 throw new Exception JavaDoc("The account does not hold the stock.");
710             }
711
712             int temp = stockholding.getAmount();
713             if (amount > temp)
714                 // throw new Exception("The account doest not hold enough amount of the stock to sell.");
715
amount = temp; // To sell what you have.
716

717             // To update the stock holding
718
stockholding.decrease(amount);
719
720             // Step 2. To get the stock price
721
//
722
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             // Step 3. To update the account credit for the transaction.
727
// (Should do, but we do NOT do that currently)
728
//
729
// account.deposit( (int) current_val*amount );
730

731             // Step 4. To create the transaction record
732
//
733
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 JavaDoc 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 JavaDoc
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); // list for entity beans
756
Collection list2 = new ArrayList(); // list for holdings
757

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 JavaDoc(e.toString());
775         }
776         catch(Exception JavaDoc ex)
777         {
778             System.err.println(BEAN_NAME + ".getHoldingStatement_Imp(): " + ex.toString());
779             throw ex;
780         }
781     }
782
783     // Print out a message with the current thread ID
784
//
785
private void print( String JavaDoc message )
786     {
787         System.out.println("[" + Thread.currentThread().getName() + "] " + message);
788     }
789
790     // To look up all resources required in the bean, eg. datasources and entity bean home.
791
//
792
private void lookupResources()
793         throws CreateException
794     {
795         try
796         {
797             Object JavaDoc 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 JavaDoc) 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 JavaDoc) 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 JavaDoc) 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 JavaDoc) initCtx.lookup(stockTxHomeJNDI_NAME);
819             stockTxHome = (StockTxHome) javax.rmi.PortableRemoteObject.narrow(obj, StockTxHome.class);
820         }
821         catch (javax.naming.NamingException JavaDoc 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     //-----------------------------------------------
829
// EJB-required methods.
830
//-----------------------------------------------
831

832     public void setSessionContext(SessionContext ctx)
833     {
834         this.ctx = ctx;
835     }
836
837     /** In this method, the datasource reference is obtained via JNDI.
838     */

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