KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > eb > AccountImplBean


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AccountImplBean.java,v 1.8 2004/04/19 06:39:29 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package eb;
27
28 /**
29  * Accountbean is an entity bean with "container managed persistence". The state
30  * of an instance is stored into a relational database. The following table
31  * should exist: create table ACCOUNT (ACCNO integer primary key, CUSTOMER
32  * varchar(30), BALANCE number(15,4));
33  * @author JOnAS team
34  */

35
36
37 import javax.ejb.CreateException;
38 import javax.ejb.EntityBean;
39 import javax.ejb.EntityContext;
40 import javax.ejb.RemoveException;
41
42 public class AccountImplBean implements EntityBean {
43
44     // Keep the reference on the EntityContext
45
protected EntityContext entityContext;
46
47     // Object state
48
public Integer accno;
49
50     public String customer;
51
52     public double balance;
53
54     /* ========================= ejbCreate methods ============================ */
55
56     /**
57      * There must be one ejbCreate() method per create() method on the Home
58      * interface, and with the same signature.
59      * @param accno account number
60      * @param customer customer name
61      * @param balance initial balance
62      * @return pk primary key set to null
63      */

64     public Integer ejbCreate(int val_accno, String val_customer, double val_balance) throws CreateException {
65
66         // Init object state
67
accno = new Integer(val_accno);
68         customer = val_customer;
69         balance = val_balance;
70         return null;
71     }
72
73     /**
74      * Each ejbCreate method should have a matching ejbPostCreate method
75      */

76     public void ejbPostCreate(int val_accno, String val_customer, double val_balance) {
77         // Nothing to be done for this simple example.
78
}
79
80     /*
81      * ====================== javax.ejb.EntityBean implementation
82      * =================
83      */

84
85     /**
86      * A container invokes this method when the instance is taken out of the
87      * pool of available instances to become associated with a specific EJB
88      * object. This method transitions the instance to the ready state. This
89      * method executes in an unspecified transaction context.
90      * @exception EJBException Thrown by the method to indicate a failure caused
91      * by a system-level error.
92      */

93     public void ejbActivate() {
94         // Nothing to be done for this simple example.
95
}
96
97     /**
98      * A container invokes this method to instruct the instance to synchronize
99      * its state by loading it state from the underlying database. This method
100      * always executes in the proper transaction context.
101      * @exception EJBException Thrown by the method to indicate a failure caused
102      * by a system-level error.
103      * @exception RemoteException - This exception is defined in the method
104      * signature to provide backward compatibility for enterprise
105      * beans written for the EJB 1.0 specification. Enterprise beans
106      * written for the EJB 1.1 and higher specification should throw
107      * the javax.ejb.EJBException instead of this exception.
108      */

109     public void ejbLoad() {
110         // Nothing to be done for this simple example, in implicit persistance.
111
}
112
113     /**
114      * A container invokes this method on an instance before the instance
115      * becomes disassociated with a specific EJB object. After this method
116      * completes, the container will place the instance into the pool of
117      * available instances. This method executes in an unspecified transaction
118      * context.
119      * @exception EJBException Thrown by the method to indicate a failure caused
120      * by a system-level error.
121      */

122     public void ejbPassivate() {
123         // Nothing to be done for this simple example.
124
}
125
126     /**
127      * A container invokes this method before it removes the EJB object that is
128      * currently associated with the instance. This method is invoked when a
129      * client invokes a remove operation on the enterprise Bean's home interface
130      * or the EJB object's remote interface. This method transitions the
131      * instance from the ready state to the pool of available instances. This
132      * method is called in the transaction context of the remove operation.
133      * @exception EJBException Thrown by the method to indicate a failure caused
134      * by a system-level error.
135      * @exception RemoteException - This exception is defined in the method
136      * signature to provide backward compatibility for enterprise
137      * beans written for the EJB 1.0 specification. Enterprise beans
138      * written for the EJB 1.1 and higher specification should throw
139      * the javax.ejb.EJBException instead of this exception.
140      * @exception RemoveException The enterprise Bean does not allow destruction
141      * of the object.
142      */

143     public void ejbRemove() throws RemoveException {
144         // Nothing to be done for this simple example, in implicit persistance.
145
}
146
147     /**
148      * A container invokes this method to instruct the instance to synchronize
149      * its state by storing it to the underlying database. This method always
150      * executes in the proper transaction context.
151      * @exception EJBException Thrown by the method to indicate a failure caused
152      * by a system-level error.
153      * @exception RemoteException - This exception is defined in the method
154      * signature to provide backward compatibility for enterprise
155      * beans written for the EJB 1.0 specification. Enterprise beans
156      * written for the EJB 1.1 and higher specification should throw
157      * the javax.ejb.EJBException instead of this exception.
158      */

159     public void ejbStore() {
160         // Nothing to be done for this simple example, in implicit persistance.
161
}
162
163     /**
164      * Sets the associated entity context. The container invokes this method on
165      * an instance after the instance has been created. This method is called in
166      * an unspecified transaction context.
167      * @param ctx - An EntityContext interface for the instance. The instance
168      * should store the reference to the context in an instance variable.
169      * @exception EJBException Thrown by the method to indicate a failure caused
170      * by a system-level error.
171      * @exception RemoteException - This exception is defined in the method
172      * signature to provide backward compatibility for enterprise
173      * beans written for the EJB 1.0 specification. Enterprise beans
174      * written for the EJB 1.1 and higher specification should throw
175      * the javax.ejb.EJBException instead of this exception.
176      */

177     public void setEntityContext(EntityContext ctx) {
178
179         // Keep the entity context in object
180
entityContext = ctx;
181     }
182
183     /**
184      * Unsets the associated entity context. The container calls this method
185      * before removing the instance. This is the last method that the container
186      * invokes on the instance. The Java garbage collector will eventually
187      * invoke the finalize() method on the instance. This method is called in an
188      * unspecified transaction context.
189      * @exception EJBException Thrown by the method to indicate a failure caused
190      * by a system-level error.
191      * @exception RemoteException - This exception is defined in the method
192      * signature to provide backward compatibility for enterprise
193      * beans written for the EJB 1.0 specification. Enterprise beans
194      * written for the EJB 1.1 and higher specification should throw
195      * the javax.ejb.EJBException instead of this exception.
196      */

197     public void unsetEntityContext() {
198         entityContext = null;
199     }
200
201     /*
202      * ========================= Account implementation
203      * ============================
204      */

205
206     /**
207      * Business method for returning the balance.
208      * @return balance
209      */

210     public double getBalance() {
211
212         return balance;
213     }
214
215     /**
216      * Business method for updating the balance.
217      * @param d balance to update
218      */

219     public void setBalance(double d) {
220
221         balance = balance + d;
222     }
223
224     /**
225      * Business method for returning the customer.
226      * @return customer
227      */

228     public String getCustomer() {
229
230         return customer;
231     }
232
233     /**
234      * Business method for changing the customer name.
235      * @param c customer to update
236      */

237     public void setCustomer(String c) {
238
239         customer = c;
240     }
241
242     /**
243      * Business method to get the Account number
244      */

245     public int getNumber() {
246         return accno.intValue();
247     }
248 }
Popular Tags