KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > stests > manac > ManacEC2


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: ManacEC2.java,v 1.5 2003/08/05 07:56:56 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.stests.manac;
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  * Manac Implementation (with container-managed persistence version 2)
41  * @author Philippe Durieux, Helene Joanin
42  */

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

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

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

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

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

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

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

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

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

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

199     public void ejbActivate() {
200         logger.log(BasicLevel.DEBUG, getName()+" balance="+getBalance());
201     }
202     
203     // ------------------------------------------------------------------
204
// Manac implementation
205
// ------------------------------------------------------------------
206

207     /**
208      * credit
209      */

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

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