001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 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: ManacEC.java 4618 2004-04-19 06:39:30Z benoitf $
023: * --------------------------------------------------------------------------
024: */package lb;
025:
026: import javax.ejb.CreateException;
027: import javax.ejb.DuplicateKeyException;
028: import javax.ejb.EJBException;
029: import javax.ejb.EntityBean;
030: import javax.ejb.EntityContext;
031: import javax.ejb.RemoveException;
032:
033: /**
034: * Manac Implementation
035: * @author Philippe Durieux
036: */
037: public class ManacEC implements EntityBean {
038:
039: EntityContext ejbContext;
040:
041: // ------------------------------------------------------------------
042: // State of the bean.
043: // They must be public for Container Managed Persistance.
044: // ------------------------------------------------------------------
045: public String name;
046:
047: public int num;
048:
049: public int balance;
050:
051: // ------------------------------------------------------------------
052: // EntityBean implementation
053: // ------------------------------------------------------------------
054:
055: /**
056: * Set the associated entity context. The container invokes this method on
057: * an instance after the instance has been created. This method is called in
058: * an unspecified transaction context.
059: * @param ctx - An EntityContext interface for the instance. The instance
060: * should store the reference to the context in an instance variable.
061: * @throws EJBException Thrown by the method to indicate a failure caused by
062: * a system-level error.
063: */
064: public void setEntityContext(EntityContext ctx) {
065: ejbContext = ctx;
066: }
067:
068: /**
069: * Unset the associated entity context. The container calls this method
070: * before removing the instance. This is the last method that the container
071: * invokes on the instance. The Java garbage collector will eventually
072: * invoke the finalize() method on the instance. This method is called in an
073: * unspecified transaction context.
074: * @throws EJBException Thrown by the method to indicate a failure caused by
075: * a system-level error.
076: */
077: public void unsetEntityContext() {
078: ejbContext = null;
079: }
080:
081: /**
082: * A container invokes this method before it removes the EJB object that is
083: * currently associated with the instance. This method is invoked when a
084: * client invokes a remove operation on the enterprise Bean's home interface
085: * or the EJB object's remote interface. This method transitions the
086: * instance from the ready state to the pool of available instances. This
087: * method is called in the transaction context of the remove operation.
088: * @throws RemoveException The enterprise Bean does not allow destruction of
089: * the object.
090: * @throws EJBException - Thrown by the method to indicate a failure caused
091: * by a system-level error.
092: */
093: public void ejbRemove() throws RemoveException {
094: }
095:
096: /**
097: * A container invokes this method to instruct the instance to synchronize
098: * its state by loading it state from the underlying database. This method
099: * always executes in the proper transaction context.
100: * @throws EJBException Thrown by the method to indicate a failure caused by
101: * a system-level error.
102: */
103: public void ejbLoad() {
104: if (balance < 0) {
105: System.out.println(name + " : Bad balance loaded");
106: throw new EJBException("ejbLoad: Balance " + name
107: + " was negative =" + balance);
108: }
109: }
110:
111: /**
112: * A container invokes this method to instruct the instance to synchronize
113: * its state by storing it to the underlying database. This method always
114: * executes in the proper transaction context.
115: * @throws EJBException Thrown by the method to indicate a failure caused by
116: * a system-level error.
117: */
118: public void ejbStore() {
119: if (balance < 0) {
120: System.out.println(name + " : Bad balance stored");
121: throw new EJBException("ejbStore: Balance " + name
122: + " 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)
132: throws CreateException, DuplicateKeyException {
133:
134: // Init here the bean fields
135: this .num = num;
136: this .name = new String(name);
137: this .balance = ib;
138:
139: // In CMP, should return null.
140: return null;
141: }
142:
143: /**
144: * There must be an ejbPostCreate per ejbCreate method
145: * @throws CreateException Failure to create an entity EJB object.
146: */
147: public void ejbPostCreate(int num, String name, int ib)
148: throws CreateException {
149: }
150:
151: /**
152: * The Entity bean can define 0 or more ejbCreate methods.
153: * @throws CreateException Failure to create an entity EJB object.
154: * @throws DuplicateKeyException An object with the same key already exists.
155: */
156: public java.lang.String ejbCreateWithDefaultName(int num, int ib)
157: throws CreateException, DuplicateKeyException {
158:
159: // Init here the bean fields
160: this .num = num;
161: this .name = "M" + (new Integer(num)).toString();
162: this .balance = ib;
163:
164: // In CMP, should return null.
165: return null;
166: }
167:
168: /**
169: * There must be an ejbPostCreate per ejbCreate method
170: * @throws CreateException Failure to create an entity EJB object.
171: */
172: public void ejbPostCreateWithDefaultName(int num, int ib)
173: throws CreateException {
174: }
175:
176: /**
177: * A container invokes this method on an instance before the instance
178: * becomes disassociated with a specific EJB object.
179: */
180: public void ejbPassivate() {
181: // balance may be wrong in case of rollback.
182: // Anyway, this instance is being released now, so no problem!
183: balance = -80000;
184: }
185:
186: /**
187: * A container invokes this method when the instance is taken out of the
188: * pool of available instances to become associated with a specific EJB
189: * object.
190: */
191: public void ejbActivate() {
192: }
193:
194: // ------------------------------------------------------------------
195: // Manac implementation
196: // ------------------------------------------------------------------
197:
198: /**
199: * getBalance
200: */
201: public int getBalance() {
202: return balance;
203: }
204:
205: /**
206: * credit
207: */
208: public void credit(int v) {
209: balance += v;
210: }
211:
212: /**
213: * debit
214: */
215: public void debit(int v) {
216: balance -= v;
217: if (balance < 0) {
218: System.out.println(name + " : set rollback only.");
219: ejbContext.setRollbackOnly();
220: balance = -90000; // put it a very bad balance to check rollback is
221: // OK
222: }
223: }
224:
225: public String getName() {
226: return name;
227: }
228: }
|