001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: AccountEC2.java 7394 2005-09-20 07:24:06Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.bank;
027:
028: import javax.ejb.CreateException;
029: import javax.ejb.DuplicateKeyException;
030: import javax.ejb.EJBException;
031: import javax.ejb.EntityBean;
032: import javax.ejb.EntityContext;
033: import javax.ejb.RemoveException;
034:
035: import org.objectweb.jonas.common.Log;
036: import org.objectweb.util.monolog.api.Logger;
037: import org.objectweb.util.monolog.api.BasicLevel;
038:
039: /**
040: * Account Implementation (with container-managed persistence version 2)
041: * @author Philippe Durieux
042: */
043: public abstract class AccountEC2 implements EntityBean {
044:
045: protected static Logger history = null;
046: EntityContext ejbContext;
047:
048: // ------------------------------------------------------------------
049: // Get and Set accessor methods of the bean's abstract schema
050: // ------------------------------------------------------------------
051: public abstract String getName();
052:
053: public abstract void setName(String n);
054:
055: public abstract int getNum();
056:
057: public abstract void setNum(int n);
058:
059: public abstract int getBalance();
060:
061: public abstract void setBalance(int b);
062:
063: // ------------------------------------------------------------------
064: // EntityBean implementation
065: // ------------------------------------------------------------------
066:
067: /**
068: * Set the associated entity context. The container invokes this method
069: * on an instance after the instance has been created.
070: * This method is called in an unspecified transaction context.
071: *
072: * @param ctx - An EntityContext interface for the instance. The instance
073: * should store the reference to the context in an instance variable.
074: * @throws EJBException Thrown by the method to indicate a failure caused by a
075: * system-level error.
076: */
077: public void setEntityContext(EntityContext ctx) {
078: if (history == null) {
079: history = Log
080: .getLogger("org.objectweb.jonas_tests.history");
081: }
082: history.log(BasicLevel.DEBUG, getName());
083: ejbContext = ctx;
084: }
085:
086: /**
087: * Unset the associated entity context. The container calls this method
088: * before removing the instance.
089: * This is the last method that the container invokes on the instance.
090: * The Java garbage collector will eventually invoke the finalize() method
091: * on the instance.
092: * This method is called in an unspecified transaction context.
093: *
094: * @throws EJBException Thrown by the method to indicate a failure caused by a
095: * system-level error.
096: */
097: public void unsetEntityContext() {
098: history.log(BasicLevel.DEBUG, getName());
099: ejbContext = null;
100: }
101:
102: /**
103: * A container invokes this method before it removes the EJB object
104: * that is currently associated with the instance. This method is
105: * invoked when a client invokes a remove operation on the enterprise Bean's
106: * home interface or the EJB object's remote interface. This method
107: * transitions the instance from the ready state to the pool of available
108: * instances.
109: *
110: * This method is called in the transaction context of the remove operation.
111: * @throws RemoveException The enterprise Bean does not allow destruction of the object.
112: * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
113: * error.
114: */
115: public void ejbRemove() throws RemoveException {
116: history.log(BasicLevel.DEBUG, getName());
117: }
118:
119: /**
120: * A container invokes this method to instruct the instance to synchronize
121: * its state by loading it state from the underlying database.
122: * This method always executes in the proper transaction context.
123: *
124: * @throws EJBException Thrown by the method to indicate a failure caused by
125: * a system-level error.
126: */
127: public void ejbLoad() {
128: String name = getName();
129: int balance = getBalance();
130: history.log(BasicLevel.DEBUG, name + "\tLOAD= " + balance);
131: if (balance < 0) {
132: history
133: .log(BasicLevel.WARN, name
134: + " : Bad balance loaded");
135: throw new EJBException("ejbLoad: Balance " + name
136: + " was negative =" + balance);
137: }
138: }
139:
140: /**
141: * A container invokes this method to instruct the instance to synchronize
142: * its state by storing it to the underlying database.
143: * This method always executes in the proper transaction context.
144: *
145: * @throws EJBException Thrown by the method to indicate a failure caused by
146: * a system-level error.
147: */
148: public void ejbStore() {
149: String name = getName();
150: int balance = getBalance();
151: history.log(BasicLevel.DEBUG, name + "\tSTORE= " + balance);
152: if (balance < 0) {
153: history
154: .log(BasicLevel.WARN, name
155: + " : Bad balance stored");
156: throw new EJBException("ejbStore: Balance " + name
157: + " was negative =" + balance);
158: }
159: }
160:
161: /**
162: * There must be an ejbPostCreate par ejbCreate method
163: *
164: * @throws CreateException Failure to create an entity EJB object.
165: */
166: public void ejbPostCreate(int num, int ib) throws CreateException {
167: history.log(BasicLevel.DEBUG, getName());
168: }
169:
170: /**
171: * The Entity bean can define 0 or more ejbCreate methods.
172: *
173: * @throws CreateException Failure to create an entity EJB object.
174: * @throws DuplicateKeyException An object with the same key already exists.
175: */
176: public java.lang.String ejbCreate(int num, int ib)
177: throws CreateException, DuplicateKeyException {
178:
179: // Init here the bean fields
180: setNum(num);
181: setName("a_" + (new Integer(num)).toString());
182: setBalance(ib);
183:
184: history.log(BasicLevel.DEBUG, getName());
185:
186: // In CMP, should return null.
187: return null;
188: }
189:
190: /**
191: * A container invokes this method on an instance before the instance
192: * becomes disassociated with a specific EJB object.
193: */
194: public void ejbPassivate() {
195: // balance may be wrong in case of rollback. Anyway, this instance is being
196: // released now, so no problem!
197: // This causes problems (in case of DB policy at least)
198: //setBalance(-80000);
199: history.log(BasicLevel.DEBUG, getName());
200: }
201:
202: /**
203: * A container invokes this method when the instance is taken out of
204: * the pool of available instances to become associated with a specific
205: * EJB object.
206: */
207: public void ejbActivate() {
208: history.log(BasicLevel.DEBUG, getName() + " balance="
209: + getBalance());
210: }
211:
212: // ------------------------------------------------------------------
213: // Bank implementation
214: // ------------------------------------------------------------------
215:
216: /**
217: * credit
218: */
219: public void credit(int v) {
220: String name = getName();
221: if (getBalance() < 0) {
222: if (ejbContext.getRollbackOnly() == true) {
223: history.log(BasicLevel.WARN, name
224: + " : tx already rollbackonly");
225: setBalance(-99000);
226: return;
227: }
228: history.log(BasicLevel.WARN, name
229: + " : Bad balance to credit =" + getBalance());
230: throw new EJBException("credit: Balance " + name
231: + " was negative =" + getBalance());
232: }
233: int oldval = getBalance();
234: setBalance(oldval + v);
235: history.log(BasicLevel.DEBUG, name + "\told= " + oldval
236: + "\tnew= " + getBalance());
237: }
238:
239: /**
240: * debit
241: */
242: public void debit(int v) {
243: String name = getName();
244: int oldval = getBalance();
245: if (oldval < 0) {
246: if (ejbContext.getRollbackOnly() == true) {
247: history.log(BasicLevel.WARN, name
248: + " : tx already rollbackonly");
249: setBalance(-99000);
250: return;
251: }
252: history.log(BasicLevel.WARN, name
253: + " : Bad balance to debit=" + oldval);
254: throw new EJBException("debit: Balance " + name
255: + " was negative =" + oldval);
256: }
257: setBalance(oldval - v);
258: if (getBalance() < 0) {
259: history.log(BasicLevel.WARN, name
260: + " : set rollback only. NEW BAL = -90000");
261: ejbContext.setRollbackOnly();
262: setBalance(-90000); // put it a very bad balance to check rollback is OK
263: }
264: history.log(BasicLevel.DEBUG, name + "\tOLD= " + oldval
265: + "\tNEW= " + getBalance());
266: }
267:
268: }
|