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: ManacEC2.java 2946 2003-08-05 07:56:56Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.stests.manac;
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: * Manac Implementation (with container-managed persistence version 2)
041: * @author Philippe Durieux, Helene Joanin
042: */
043: public abstract class ManacEC2 implements EntityBean {
044:
045: static protected Logger logger = null;
046: protected static Logger history = null;
047: EntityContext ejbContext;
048:
049: // ------------------------------------------------------------------
050: // Get and Set accessor methods of the bean's abstract schema
051: // ------------------------------------------------------------------
052: public abstract String getName();
053:
054: public abstract void setName(String n);
055:
056: public abstract int getNum();
057:
058: public abstract void setNum(int n);
059:
060: public abstract int getBalance();
061:
062: public abstract void setBalance(int b);
063:
064: // ------------------------------------------------------------------
065: // EntityBean implementation
066: // ------------------------------------------------------------------
067:
068: /**
069: * Set the associated entity context. The container invokes this method
070: * on an instance after the instance has been created.
071: * This method is called in an unspecified transaction context.
072: *
073: * @param ctx - An EntityContext interface for the instance. The instance
074: * should store the reference to the context in an instance variable.
075: * @throws EJBException Thrown by the method to indicate a failure caused by a
076: * system-level error.
077: */
078: public void setEntityContext(EntityContext ctx) {
079: if (logger == null) {
080: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
081: }
082: if (history == null) {
083: history = Log
084: .getLogger("org.objectweb.jonas_tests.history");
085: }
086: logger.log(BasicLevel.DEBUG, getName());
087: ejbContext = ctx;
088: }
089:
090: /**
091: * Unset the associated entity context. The container calls this method
092: * before removing the instance.
093: * This is the last method that the container invokes on the instance.
094: * The Java garbage collector will eventually invoke the finalize() method
095: * on the instance.
096: * This method is called in an unspecified transaction context.
097: *
098: * @throws EJBException Thrown by the method to indicate a failure caused by a
099: * system-level error.
100: */
101: public void unsetEntityContext() {
102: logger.log(BasicLevel.DEBUG, getName());
103: ejbContext = null;
104: }
105:
106: /**
107: * A container invokes this method before it removes the EJB object
108: * that is currently associated with the instance. This method is
109: * invoked when a client invokes a remove operation on the enterprise Bean's
110: * home interface or the EJB object's remote interface. This method
111: * transitions the instance from the ready state to the pool of available
112: * instances.
113: *
114: * This method is called in the transaction context of the remove operation.
115: * @throws RemoveException The enterprise Bean does not allow destruction of the object.
116: * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
117: * error.
118: */
119: public void ejbRemove() throws RemoveException {
120: logger.log(BasicLevel.DEBUG, getName());
121: }
122:
123: /**
124: * A container invokes this method to instruct the instance to synchronize
125: * its state by loading it state from the underlying database.
126: * This method always executes in the proper transaction context.
127: *
128: * @throws EJBException Thrown by the method to indicate a failure caused by
129: * a system-level error.
130: */
131: public void ejbLoad() {
132: String name = getName();
133: int balance = getBalance();
134: history.log(BasicLevel.INFO, name + "\tLOAD= " + balance);
135: logger.log(BasicLevel.DEBUG, name + " balance=" + balance);
136: if (balance < 0) {
137: logger.log(BasicLevel.WARN, name + " : Bad balance loaded");
138: throw new EJBException("ejbLoad: Balance " + name
139: + " was negative =" + balance);
140: }
141: }
142:
143: /**
144: * A container invokes this method to instruct the instance to synchronize
145: * its state by storing it to the underlying database.
146: * This method always executes in the proper transaction context.
147: *
148: * @throws EJBException Thrown by the method to indicate a failure caused by
149: * a system-level error.
150: */
151: public void ejbStore() {
152: String name = getName();
153: int balance = getBalance();
154: history.log(BasicLevel.INFO, name + "\tSTORE= " + balance);
155: logger.log(BasicLevel.DEBUG, name + " balance=" + balance);
156: if (balance < 0) {
157: logger.log(BasicLevel.WARN, name + " : 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: logger.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: logger.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: logger.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: logger.log(BasicLevel.DEBUG, getName() + " balance="
210: + getBalance());
211: }
212:
213: // ------------------------------------------------------------------
214: // Manac 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: logger.log(BasicLevel.WARN, name
225: + " : tx already rollbackonly");
226: setBalance(-99000);
227: return;
228: }
229: logger.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: logger.log(BasicLevel.DEBUG, name + " old balance=" + oldval
237: + " new balance=" + getBalance());
238: history.log(BasicLevel.INFO, name + "\tOLD= " + oldval
239: + "\tNEW= " + getBalance());
240: }
241:
242: /**
243: * debit
244: */
245: public void debit(int v) {
246: String name = getName();
247: if (getBalance() < 0) {
248: if (ejbContext.getRollbackOnly() == true) {
249: logger.log(BasicLevel.WARN, name
250: + " : tx already rollbackonly");
251: setBalance(-99000);
252: return;
253: }
254: logger.log(BasicLevel.WARN, name
255: + " : Bad balance to debit");
256: throw new EJBException("debit: Balance " + name
257: + " was negative =" + getBalance());
258: }
259: int oldval = getBalance();
260: setBalance(oldval - v);
261: if (getBalance() < 0) {
262: logger.log(BasicLevel.WARN, name + " : set rollback only.");
263: ejbContext.setRollbackOnly();
264: setBalance(-90000); // put it a very bad balance to check rollback is OK
265: }
266: logger.log(BasicLevel.DEBUG, name + " old balance=" + oldval
267: + " new balance=" + getBalance());
268: history.log(BasicLevel.INFO, name + "\tOLD= " + oldval
269: + "\tNEW= " + getBalance());
270: }
271:
272: }
|