KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > stests > 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 2004/01/07 07:43:17 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

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

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

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

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

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

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

203     /**
204      * credit
205      */

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

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