KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > bank > AccountEC2


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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: AccountEC2.java,v 1.3 2005/07/26 15:08:01 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.bank;
27
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.DuplicateKeyException JavaDoc;
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.EntityBean JavaDoc;
32 import javax.ejb.EntityContext JavaDoc;
33 import javax.ejb.RemoveException JavaDoc;
34
35 import org.objectweb.jonas.common.Log;
36 import org.objectweb.util.monolog.api.Logger;
37 import org.objectweb.util.monolog.api.BasicLevel;
38
39 /**
40  * Account Implementation (with container-managed persistence version 2)
41  * @author Philippe Durieux
42  */

43 public abstract class AccountEC2 implements EntityBean JavaDoc {
44
45     protected static Logger history = null;
46     EntityContext JavaDoc ejbContext;
47
48     // ------------------------------------------------------------------
49
// Get and Set accessor methods of the bean's abstract schema
50
// ------------------------------------------------------------------
51
public abstract String JavaDoc getName();
52     public abstract void setName(String JavaDoc n);
53     public abstract int getNum();
54     public abstract void setNum(int n);
55     public abstract int getBalance();
56     public abstract void setBalance(int b);
57
58     // ------------------------------------------------------------------
59
// EntityBean implementation
60
// ------------------------------------------------------------------
61

62     /**
63      * Set the associated entity context. The container invokes this method
64      * on an instance after the instance has been created.
65      * This method is called in an unspecified transaction context.
66      *
67      * @param ctx - An EntityContext interface for the instance. The instance
68      * should store the reference to the context in an instance variable.
69      * @throws EJBException Thrown by the method to indicate a failure caused by a
70      * system-level error.
71      */

72     public void setEntityContext(EntityContext JavaDoc ctx) {
73         if (history == null) {
74             history = Log.getLogger("org.objectweb.jonas_tests.history");
75         }
76         history.log(BasicLevel.DEBUG, getName());
77         ejbContext = ctx;
78     }
79
80     /**
81      * Unset the associated entity context. The container calls this method
82      * before removing the instance.
83      * This is the last method that the container invokes on the instance.
84      * The Java garbage collector will eventually invoke the finalize() method
85      * on the instance.
86      * This method is called in an unspecified transaction context.
87      *
88      * @throws EJBException Thrown by the method to indicate a failure caused by a
89      * system-level error.
90      */

91     public void unsetEntityContext() {
92         history.log(BasicLevel.DEBUG, getName());
93         ejbContext = null;
94     }
95
96     /**
97      * A container invokes this method before it removes the EJB object
98      * that is currently associated with the instance. This method is
99      * invoked when a client invokes a remove operation on the enterprise Bean's
100      * home interface or the EJB object's remote interface. This method
101      * transitions the instance from the ready state to the pool of available
102      * instances.
103      *
104      * This method is called in the transaction context of the remove operation.
105      * @throws RemoveException The enterprise Bean does not allow destruction of the object.
106      * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
107      * error.
108      */

109     public void ejbRemove() throws RemoveException JavaDoc {
110         history.log(BasicLevel.DEBUG, getName());
111     }
112
113     /**
114      * A container invokes this method to instruct the instance to synchronize
115      * its state by loading it state from the underlying database.
116      * This method always executes in the proper transaction context.
117      *
118      * @throws EJBException Thrown by the method to indicate a failure caused by
119      * a system-level error.
120      */

121     public void ejbLoad() {
122         String JavaDoc name = getName();
123         int balance = getBalance();
124         history.log(BasicLevel.DEBUG, name + "\tLOAD= " + balance);
125         if (balance < 0) {
126             history.log(BasicLevel.WARN, name + " : Bad balance loaded");
127             throw new EJBException JavaDoc("ejbLoad: Balance "+name+" was negative ="+balance);
128         }
129     }
130
131     /**
132      * A container invokes this method to instruct the instance to synchronize
133      * its state by storing it to the underlying database.
134      * This method always executes in the proper transaction context.
135      *
136      * @throws EJBException Thrown by the method to indicate a failure caused by
137      * a system-level error.
138      */

139     public void ejbStore() {
140         String JavaDoc name = getName();
141         int balance = getBalance();
142         history.log(BasicLevel.DEBUG, name + "\tSTORE= " + balance);
143         if (balance < 0) {
144             history.log(BasicLevel.WARN, name + " : Bad balance stored");
145             throw new EJBException JavaDoc("ejbStore: Balance "+name+" was negative ="+balance);
146         }
147     }
148
149     /**
150      * There must be an ejbPostCreate par ejbCreate method
151      *
152      * @throws CreateException Failure to create an entity EJB object.
153      */

154     public void ejbPostCreate(int num, int ib) throws CreateException JavaDoc {
155         history.log(BasicLevel.DEBUG, getName());
156     }
157
158     /**
159      * The Entity bean can define 0 or more ejbCreate methods.
160      *
161      * @throws CreateException Failure to create an entity EJB object.
162      * @throws DuplicateKeyException An object with the same key already exists.
163      */

164     public java.lang.String JavaDoc ejbCreate(int num, int ib) throws CreateException JavaDoc, DuplicateKeyException JavaDoc {
165
166         // Init here the bean fields
167
setNum(num);
168         setName("a_"+(new Integer JavaDoc(num)).toString());
169         setBalance(ib);
170
171         history.log(BasicLevel.DEBUG, getName());
172
173         // In CMP, should return null.
174
return null;
175     }
176
177     /**
178      * A container invokes this method on an instance before the instance
179      * becomes disassociated with a specific EJB object.
180      */

181     public void ejbPassivate() {
182         // balance may be wrong in case of rollback. Anyway, this instance is being
183
// released now, so no problem!
184
// This causes problems (in case of DB policy at least)
185
//setBalance(-80000);
186
history.log(BasicLevel.DEBUG, getName());
187     }
188
189     /**
190      * A container invokes this method when the instance is taken out of
191      * the pool of available instances to become associated with a specific
192      * EJB object.
193      */

194     public void ejbActivate() {
195         history.log(BasicLevel.DEBUG, getName() + " balance=" + getBalance());
196     }
197
198     // ------------------------------------------------------------------
199
// Bank implementation
200
// ------------------------------------------------------------------
201

202     /**
203      * credit
204      */

205     public void credit(int v) {
206         String JavaDoc name = getName();
207         if (getBalance() < 0) {
208             if (ejbContext.getRollbackOnly() == true) {
209                 history.log(BasicLevel.WARN, name + " : tx already rollbackonly");
210                 setBalance(-99000);
211                 return;
212             }
213             history.log(BasicLevel.WARN, name + " : Bad balance to credit");
214             throw new EJBException JavaDoc("credit: Balance "+name+" was negative ="+getBalance());
215         }
216         int oldval = getBalance();
217         setBalance(oldval + v);
218         history.log(BasicLevel.DEBUG, name + "\told= " + oldval + "\tnew= " + getBalance());
219     }
220
221     /**
222      * debit
223      */

224     public void debit(int v) {
225         String JavaDoc name = getName();
226         if (getBalance() < 0) {
227             if (ejbContext.getRollbackOnly() == true) {
228                 history.log(BasicLevel.WARN, name + " : tx already rollbackonly");
229                 setBalance(-99000);
230                 return;
231             }
232             history.log(BasicLevel.WARN, name + " : Bad balance to debit");
233             throw new EJBException JavaDoc("debit: Balance "+name+" was negative ="+getBalance());
234         }
235         int oldval = getBalance();
236         setBalance(oldval - v);
237         if (getBalance() < 0) {
238             history.log(BasicLevel.WARN, name + " : set rollback only.");
239             ejbContext.setRollbackOnly();
240             setBalance(-90000); // put it a very bad balance to check rollback is OK
241
}
242         history.log(BasicLevel.DEBUG, name + "\tOLD= " + oldval + "\tNEW= " + getBalance());
243     }
244
245 }
246
Popular Tags