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 3992 2004-01-07 07:43:17Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.stests.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: history.log(BasicLevel.DEBUG, name + "\tLOAD= ");
130: int balance = getBalance();
131: history.log(BasicLevel.DEBUG, name + "\tLOAD= " + balance);
132: if (balance < 0) {
133: history
134: .log(BasicLevel.WARN, name
135: + " : Bad balance loaded");
136: throw new EJBException("ejbLoad: Balance " + name
137: + " was negative =" + balance);
138: }
139: }
140:
141: /**
142: * A container invokes this method to instruct the instance to synchronize
143: * its state by storing it to the underlying database.
144: * This method always executes in the proper transaction context.
145: *
146: * @throws EJBException Thrown by the method to indicate a failure caused by
147: * a system-level error.
148: */
149: public void ejbStore() {
150: String name = getName();
151: history.log(BasicLevel.DEBUG, name + "\tSTORE= ");
152: int balance = getBalance();
153: history.log(BasicLevel.DEBUG, name + "\tSTORE= " + balance);
154: if (balance < 0) {
155: history
156: .log(BasicLevel.WARN, name
157: + " : Bad balance stored");
158: throw new EJBException("ejbStore: Balance " + name
159: + " was negative =" + balance);
160: }
161: }
162:
163: /**
164: * There must be an ejbPostCreate par ejbCreate method
165: *
166: * @throws CreateException Failure to create an entity EJB object.
167: */
168: public void ejbPostCreate(int num, int ib) throws CreateException {
169: history.log(BasicLevel.DEBUG, getName());
170: }
171:
172: /**
173: * The Entity bean can define 0 or more ejbCreate methods.
174: *
175: * @throws CreateException Failure to create an entity EJB object.
176: * @throws DuplicateKeyException An object with the same key already exists.
177: */
178: public java.lang.String ejbCreate(int num, int ib)
179: throws CreateException, DuplicateKeyException {
180:
181: // Init here the bean fields
182: setNum(num);
183: setName("a_" + (new Integer(num)).toString());
184: setBalance(ib);
185:
186: history.log(BasicLevel.DEBUG, getName());
187:
188: // In CMP, should return null.
189: return null;
190: }
191:
192: /**
193: * A container invokes this method on an instance before the instance
194: * becomes disassociated with a specific EJB object.
195: */
196: public void ejbPassivate() {
197: // balance may be wrong in case of rollback. Anyway, this instance is being
198: // released now, so no problem!
199: setBalance(-80000);
200: history.log(BasicLevel.DEBUG, getName());
201: }
202:
203: /**
204: * A container invokes this method when the instance is taken out of
205: * the pool of available instances to become associated with a specific
206: * EJB object.
207: */
208: public void ejbActivate() {
209: history.log(BasicLevel.DEBUG, getName() + " balance="
210: + getBalance());
211: }
212:
213: // ------------------------------------------------------------------
214: // Bank implementation
215: // ------------------------------------------------------------------
216:
217: /**
218: * credit
219: */
220: public void credit(int v) {
221: String name = getName();
222: if (getBalance() < 0) {
223: if (ejbContext.getRollbackOnly() == true) {
224: history.log(BasicLevel.WARN, name
225: + " : tx already rollbackonly");
226: setBalance(-99000);
227: return;
228: }
229: history.log(BasicLevel.WARN, name
230: + " : Bad balance to credit");
231: throw new EJBException("credit: Balance " + name
232: + " was negative =" + getBalance());
233: }
234: int oldval = getBalance();
235: setBalance(oldval + v);
236: history.log(BasicLevel.DEBUG, name + "\told= " + oldval
237: + "\tnew= " + getBalance());
238: }
239:
240: /**
241: * debit
242: */
243: public void debit(int v) {
244: String name = getName();
245: if (getBalance() < 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");
254: throw new EJBException("debit: Balance " + name
255: + " was negative =" + getBalance());
256: }
257: int oldval = getBalance();
258: setBalance(oldval - v);
259: if (getBalance() < 0) {
260: history
261: .log(BasicLevel.WARN, name
262: + " : set rollback only.");
263: ejbContext.setRollbackOnly();
264: setBalance(-90000); // put it a very bad balance to check rollback is OK
265: }
266: history.log(BasicLevel.DEBUG, name + "\tOLD= " + oldval
267: + "\tNEW= " + getBalance());
268: }
269:
270: }
|