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: * Initial developer(s): ____________________________________.
022: * Contributor(s): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: StockBean.java 4618 2004-04-19 06:39:30Z benoitf $
026: * --------------------------------------------------------------------------
027: */package sampleappli;
028:
029: import java.rmi.RemoteException;
030:
031: import javax.ejb.CreateException;
032: import javax.ejb.DuplicateKeyException;
033: import javax.ejb.EJBException;
034: import javax.ejb.EntityBean;
035: import javax.ejb.EntityContext;
036: import javax.ejb.FinderException;
037: import javax.ejb.ObjectNotFoundException;
038: import javax.ejb.RemoveException;
039: import javax.naming.Context;
040: import javax.naming.InitialContext;
041: import javax.naming.NamingException;
042: import javax.sql.DataSource;
043: import javax.transaction.NotSupportedException;
044:
045: /**
046: *
047: */
048: public class StockBean implements EntityBean {
049:
050: EntityContext ejbContext;
051:
052: // ------------------------------------------------------------------
053: // State of the bean.
054: // They must be public for Container Managed Persistance.
055: // ------------------------------------------------------------------
056: public String stockid;
057:
058: public int stockqty;
059:
060: // ------------------------------------------------------------------
061: // EntityBean implementation
062: // ------------------------------------------------------------------
063:
064: /**
065: * Set the associated entity context. The container invokes this method on
066: * an instance after the instance has been created. This method is called in
067: * an unspecified transaction context.
068: * @param ctx - An EntityContext interface for the instance. The instance
069: * should store the reference to the context in an instance variable.
070: * @throws EJBException Thrown by the method to indicate a failure caused by
071: * a system-level error.
072: */
073: public void setEntityContext(EntityContext ctx) {
074: ejbContext = ctx;
075: }
076:
077: /**
078: * Unset the associated entity context. The container calls this method
079: * before removing the instance. This is the last method that the container
080: * invokes on the instance. The Java garbage collector will eventually
081: * invoke the finalize() method on the instance. This method is called in an
082: * unspecified transaction context.
083: * @throws EJBException Thrown by the method to indicate a failure caused by
084: * a system-level error.
085: */
086: public void unsetEntityContext() {
087: ejbContext = null;
088: }
089:
090: /**
091: * A container invokes this method before it removes the EJB object that is
092: * currently associated with the instance. This method is invoked when a
093: * client invokes a remove operation on the enterprise Bean's home interface
094: * or the EJB object's remote interface. This method transitions the
095: * instance from the ready state to the pool of available instances. This
096: * method is called in the transaction context of the remove operation.
097: * @throws RemoveException The enterprise Bean does not allow destruction of
098: * the object.
099: * @throws EJBException - Thrown by the method to indicate a failure caused
100: * by a system-level error.
101: */
102: public void ejbRemove() throws RemoveException {
103: }
104:
105: /**
106: * A container invokes this method to instruct the instance to synchronize
107: * its state by loading it state from the underlying database. This method
108: * always executes in the proper transaction context.
109: * @throws EJBException Thrown by the method to indicate a failure caused by
110: * a system-level error.
111: */
112: public void ejbLoad() {
113: }
114:
115: /**
116: * A container invokes this method to instruct the instance to synchronize
117: * its state by storing it to the underlying database. This method always
118: * executes in the proper transaction context.
119: * @throws EJBException Thrown by the method to indicate a failure caused by
120: * a system-level error.
121: */
122: public void ejbStore() {
123: }
124:
125: /**
126: * There must be an ejbPostCreate par ejbCreate method
127: * @throws CreateException Failure to create an entity EJB object.
128: */
129: public void ejbPostCreate(String id, int qty)
130: throws CreateException {
131: }
132:
133: /**
134: * The Entity bean can define 0 or more ejbCreate methods.
135: * @throws CreateException Failure to create an entity EJB object.
136: * @throws DuplicateKeyException An object with the same key already exists.
137: */
138: public java.lang.String ejbCreate(String id, int qty)
139: throws CreateException, DuplicateKeyException {
140:
141: // Init here the bean fields
142: stockid = id;
143: stockqty = qty;
144:
145: // In CMP, should return null.
146: return null;
147: }
148:
149: /**
150: * A container invokes this method on an instance before the instance
151: * becomes disassociated with a specific EJB object.
152: */
153: public void ejbPassivate() {
154: }
155:
156: /**
157: * A container invokes this method when the instance is taken out of the
158: * pool of available instances to become associated with a specific EJB
159: * object.
160: */
161: public void ejbActivate() {
162: }
163:
164: // ------------------------------------------------------------------
165: // Stock implementation
166: // ------------------------------------------------------------------
167:
168: /**
169: * increaseQuantity
170: */
171: public void increaseQuantity(int qty) {
172: stockqty += qty;
173: }
174:
175: /**
176: * decreaseStockQuantity
177: */
178: public void decreaseQuantity(int qty) throws RemoteException {
179: stockqty = stockqty - qty;
180: if (stockqty < 0) {
181: throw new RemoteException("Negative stock");
182: }
183: }
184:
185: /**
186: * getStockQuantity
187: */
188: public int getQuantity() throws RemoteException {
189: return stockqty;
190: }
191:
192: /**
193: * setStockQuantity
194: */
195: public void setQuantity(int qty) throws RemoteException {
196: stockqty = qty;
197: }
198:
199: /**
200: * getStockQuantity
201: */
202: public String getId() throws RemoteException {
203: return stockid;
204: }
205: }
|