KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stockonline > ejb > entity > cmp > account > AccountBean


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
//
40
//
41

42
43 package stockonline.ejb.entity.cmp.account;
44
45 import java.sql.*;
46 import java.util.*;
47 import java.rmi.*;
48
49 import javax.ejb.*;
50 import javax.naming.*;
51 import javax.sql.*;
52 import javax.rmi.*;
53
54 import stockonline.ejb.sql.SeqGenerator;
55 import stockonline.ejb.entity.interf.*;
56
57 /**
58  * Container-Managed Persistent Entity Bean.
59  * This Entity Bean represents a subscriber Account.
60  */

61 public class AccountBean implements EntityBean
62 {
63     final static boolean verbose = false;
64     final static String JavaDoc BEAN_NAME = "cmpAccount";
65     final static String JavaDoc RESOURCE_NAME = "java:comp/env/jdbc/StockDB";
66
67     public int sub_accno;
68     public String JavaDoc sub_name;
69     public String JavaDoc sub_address;
70     public int sub_credit;
71
72     protected EntityContext ctx;
73     private transient boolean isDirty; // "dirty" flag for WebLogic
74

75     public AccountBean()
76     {
77         if (verbose) System.out.println(BEAN_NAME + " instantiated");
78     }
79
80     //----------------------------------------------------------------
81
// Begin business methods
82
//----------------------------------------------------------------
83

84     /**
85      * Updates the credit of account.
86      */

87     public void update(int amount)
88     {
89         if (verbose)
90             System.out.println(BEAN_NAME + ".update(" + amount + ") called.");
91
92         sub_credit = amount;
93         setModified(true);
94     }
95
96     /**
97      * Deposits amount into account.
98      */

99     public void deposit(int amount)
100     {
101         if (verbose)
102             System.out.println(BEAN_NAME + ".deposit(" + amount + ") called.");
103
104         sub_credit += amount;
105         setModified(true);
106     }
107
108     /**
109      * Withdraws amount from account.
110      * @throw AccountException thrown in amount < available credit
111      */

112     public int withdraw(int amount) throws Exception JavaDoc
113     {
114         if (verbose)
115             System.out.println(BEAN_NAME + ".withdraw(" + amount + ") called.");
116
117         if (amount > sub_credit) {
118             throw new Exception JavaDoc("Your credit is " + sub_credit + "! You cannot withdraw " + amount + "!");
119         }
120
121         sub_credit -= amount;
122         setModified(true);
123
124         return sub_credit;
125     }
126
127     // Getter/setter methods on Entity Bean fields
128

129     public int getCredit()
130     {
131         if (verbose)
132             System.out.println(BEAN_NAME + ".getCredit() called.");
133
134         return sub_credit;
135     }
136
137     //----------------------------------------------------------------
138
// End public business methods
139
//----------------------------------------------------------------
140

141     //----------------------------------------------------------------
142
// Begin EJB-Required methods. The methods below are called
143
// by the Container, and never called by client code.
144
//----------------------------------------------------------------
145

146     /**
147      * Associates this Bean instance with a particular context.
148      * We can query the Bean properties which customize the Bean here.
149      */

150     public void setEntityContext(EntityContext ctx)
151     {
152         if (verbose)
153             System.out.println(BEAN_NAME + ".setEntityContext called");
154
155         this.ctx = ctx;
156     }
157
158     /**
159      * Disassociates this Bean instance with a
160      * particular context environment.
161      */

162     public void unsetEntityContext()
163     {
164         if (verbose)
165             System.out.println(BEAN_NAME + ".unsetEntityContext called");
166
167         this.ctx = null;
168     }
169
170     /**
171      * Implementation can acquire needed resources.
172      */

173     public void ejbActivate()
174     {
175         if (verbose)
176             System.out.println(BEAN_NAME + ".ejbActivate() called.");
177
178         setModified(true);
179     }
180
181     /**
182      * Releases held resources for passivation.
183      */

184     public void ejbPassivate()
185     {
186         if (verbose)
187             System.out.println(BEAN_NAME + ".ejbPassivate () called.");
188     }
189
190     /**
191      * This is the initialization method that corresponds to the
192      * create() method in the Home Interface.
193      *
194      * @return The primary key for this account
195      */

196     public AccountPK ejbCreate(String JavaDoc sub_name, String JavaDoc sub_address, int sub_credit)
197         throws CreateException
198     {
199         if (verbose)
200             System.out.println(BEAN_NAME + ".ejbCreate() called.");
201
202         try
203         {
204             this.sub_accno = SeqGenerator.getNextAccountID(RESOURCE_NAME);
205             this.sub_name = sub_name;
206             this.sub_address = sub_address;
207             this.sub_credit = sub_credit;
208
209             return null;
210         }
211         catch(Exception JavaDoc ex)
212         {
213             String JavaDoc msg = BEAN_NAME + ".ejbCreate(): Failed to get a sequence number, due to: " + ex.toString();
214
215             System.err.println(msg);
216             throw new CreateException(msg);
217         }
218     }
219
220     /**
221      * Called after ejbCreate(). Now, the Bean can retrieve its EJBObject
222      * from its context, and pass it as a 'this' argument.
223      */

224     public void ejbPostCreate(String JavaDoc sub_name, String JavaDoc sub_address, int sub_credit)
225     {
226         if (verbose)
227             System.out.println(BEAN_NAME + ".ejbPostCreate() called.");
228
229         setModified(false);
230     }
231
232     /**
233      * This is the initialization method that corresponds to the
234      * create() method in the Home Interface.
235      *
236      * @return The primary key for this account
237      */

238     public AccountPK ejbCreate(int sub_accno, String JavaDoc sub_name, String JavaDoc sub_address, int sub_credit)
239         throws CreateException
240     {
241         if (verbose)
242             System.out.println(BEAN_NAME + ".ejbCreate() called.");
243
244         try
245         {
246             this.sub_accno = sub_accno;
247             this.sub_name = sub_name;
248             this.sub_address = sub_address;
249             this.sub_credit = sub_credit;
250
251             return null;
252         }
253         catch(Exception JavaDoc ex)
254         {
255             String JavaDoc msg = BEAN_NAME + ".ejbCreate(): Failed to get a sequence number, due to: " + ex.toString();
256
257             System.err.println(msg);
258             throw new CreateException(msg);
259         }
260     }
261
262     /**
263      * Called after ejbCreate(). Now, the Bean can retrieve its EJBObject
264      * from its context, and pass it as a 'this' argument.
265      */

266     public void ejbPostCreate(int sub_accno, String JavaDoc sub_name, String JavaDoc sub_address, int sub_credit)
267     {
268         if (verbose)
269             System.out.println(BEAN_NAME + ".ejbPostCreate() called.");
270
271         setModified(false);
272     }
273
274
275     /**
276      * Removes the Entity Bean from the database.
277      * Corresponds to when client calls home.remove().
278      */

279     public void ejbRemove() throws RemoteException
280     {
281         if (verbose)
282             System.out.println(BEAN_NAME + ".ejbRemove() called.");
283     }
284
285     /**
286      * Updates the in-memory Entity Bean object
287      * to reflect the current value stored in the database.
288      */

289     public void ejbLoad() throws RemoteException
290     {
291         if (verbose)
292             System.out.println(BEAN_NAME + ".ejbLoad() called.");
293
294         if (!isModified()) return;
295         setModified(false);
296     }
297
298     /**
299      * Updates the database to reflect the
300      * current values of this in-memory Entity Bean object representation.
301      */

302     public void ejbStore() throws RemoteException
303     {
304         if (verbose)
305             System.out.println(BEAN_NAME + ".ejbStore() called.");
306         if (!isModified()) {
307             return;
308         }
309
310         setModified(false);
311     }
312
313     /**
314      * Returns whether the EJBean has been modified or not.
315      *
316      * @return boolean isDirty
317      */

318     public boolean isModified()
319     {
320         if (verbose)
321             System.out.println (BEAN_NAME + ".isModified(): isDirty = " + (isDirty ? "true" : "false"));
322
323         return isDirty;
324     }
325
326     /**
327      * Sets the EJBean as modified.
328      *
329      * @param flag boolean Flag
330      */

331     public void setModified(boolean flag)
332     {
333         isDirty = flag;
334     }
335
336     //----------------------------------------------------------------
337
// End EJB-Required methods.
338
//----------------------------------------------------------------
339
}
340
Popular Tags