KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > lb > ManacEC


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: ManacEC.java,v 1.2 2004/04/19 06:39:29 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package lb;
27
28 import javax.ejb.CreateException;
29 import javax.ejb.DuplicateKeyException;
30 import javax.ejb.EJBException;
31 import javax.ejb.EntityBean;
32 import javax.ejb.EntityContext;
33 import javax.ejb.RemoveException;
34
35 /**
36  * Manac Implementation
37  * @author Philippe Durieux
38  */

39 public class ManacEC implements EntityBean {
40
41     EntityContext ejbContext;
42
43     // ------------------------------------------------------------------
44
// State of the bean.
45
// They must be public for Container Managed Persistance.
46
// ------------------------------------------------------------------
47
public String name;
48
49     public int num;
50
51     public int balance;
52
53     // ------------------------------------------------------------------
54
// EntityBean implementation
55
// ------------------------------------------------------------------
56

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

66     public void setEntityContext(EntityContext ctx) {
67         ejbContext = ctx;
68     }
69
70     /**
71      * Unset the associated entity context. The container calls this method
72      * before removing the instance. This is the last method that the container
73      * invokes on the instance. The Java garbage collector will eventually
74      * invoke the finalize() method on the instance. This method is called in an
75      * unspecified transaction context.
76      * @throws EJBException Thrown by the method to indicate a failure caused by
77      * a system-level error.
78      */

79     public void unsetEntityContext() {
80         ejbContext = null;
81     }
82
83     /**
84      * A container invokes this method before it removes the EJB object that is
85      * currently associated with the instance. This method is invoked when a
86      * client invokes a remove operation on the enterprise Bean's home interface
87      * or the EJB object's remote interface. This method transitions the
88      * instance from the ready state to the pool of available instances. This
89      * method is called in the transaction context of the remove operation.
90      * @throws RemoveException The enterprise Bean does not allow destruction of
91      * the object.
92      * @throws EJBException - Thrown by the method to indicate a failure caused
93      * by a system-level error.
94      */

95     public void ejbRemove() throws RemoveException {
96     }
97
98     /**
99      * A container invokes this method to instruct the instance to synchronize
100      * its state by loading it state from the underlying database. This method
101      * always executes in the proper transaction context.
102      * @throws EJBException Thrown by the method to indicate a failure caused by
103      * a system-level error.
104      */

105     public void ejbLoad() {
106         if (balance < 0) {
107             System.out.println(name + " : Bad balance loaded");
108             throw new EJBException("ejbLoad: Balance " + name + " was negative =" + balance);
109         }
110     }
111
112     /**
113      * A container invokes this method to instruct the instance to synchronize
114      * its state by storing it to the underlying database. This method always
115      * executes in the proper transaction context.
116      * @throws EJBException Thrown by the method to indicate a failure caused by
117      * a system-level error.
118      */

119     public void ejbStore() {
120         if (balance < 0) {
121             System.out.println(name + " : Bad balance stored");
122             throw new EJBException("ejbStore: Balance " + name + " was negative =" + balance);
123         }
124     }
125
126     /**
127      * The Entity bean can define 0 or more ejbCreate methods.
128      * @throws CreateException Failure to create an entity EJB object.
129      * @throws DuplicateKeyException An object with the same key already exists.
130      */

131     public java.lang.String ejbCreate(int num, String name, int ib) throws CreateException, DuplicateKeyException {
132
133         // Init here the bean fields
134
this.num = num;
135         this.name = new String(name);
136         this.balance = ib;
137
138         // In CMP, should return null.
139
return null;
140     }
141
142     /**
143      * There must be an ejbPostCreate per ejbCreate method
144      * @throws CreateException Failure to create an entity EJB object.
145      */

146     public void ejbPostCreate(int num, String name, int ib) throws CreateException {
147     }
148
149     /**
150      * The Entity bean can define 0 or more ejbCreate methods.
151      * @throws CreateException Failure to create an entity EJB object.
152      * @throws DuplicateKeyException An object with the same key already exists.
153      */

154     public java.lang.String ejbCreateWithDefaultName(int num, int ib) throws CreateException, DuplicateKeyException {
155
156         // Init here the bean fields
157
this.num = num;
158         this.name = "M" + (new Integer(num)).toString();
159         this.balance = ib;
160
161         // In CMP, should return null.
162
return null;
163     }
164
165     /**
166      * There must be an ejbPostCreate per ejbCreate method
167      * @throws CreateException Failure to create an entity EJB object.
168      */

169     public void ejbPostCreateWithDefaultName(int num, int ib) throws CreateException {
170     }
171
172     /**
173      * A container invokes this method on an instance before the instance
174      * becomes disassociated with a specific EJB object.
175      */

176     public void ejbPassivate() {
177         // balance may be wrong in case of rollback.
178
// Anyway, this instance is being released now, so no problem!
179
balance = -80000;
180     }
181
182     /**
183      * A container invokes this method when the instance is taken out of the
184      * pool of available instances to become associated with a specific EJB
185      * object.
186      */

187     public void ejbActivate() {
188     }
189
190     // ------------------------------------------------------------------
191
// Manac implementation
192
// ------------------------------------------------------------------
193

194     /**
195      * getBalance
196      */

197     public int getBalance() {
198         return balance;
199     }
200
201     /**
202      * credit
203      */

204     public void credit(int v) {
205         balance += v;
206     }
207
208     /**
209      * debit
210      */

211     public void debit(int v) {
212         balance -= v;
213         if (balance < 0) {
214             System.out.println(name + " : set rollback only.");
215             ejbContext.setRollbackOnly();
216             balance = -90000; // put it a very bad balance to check rollback is
217
// OK
218
}
219     }
220
221     public String getName() {
222         return name;
223     }
224 }
Popular Tags