KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sampleappli > StockBean


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): ____________________________________.
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: StockBean.java,v 1.4 2004/04/19 06:39:30 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 // StockBean.java
30
// Entity Bean
31
package sampleappli;
32
33 import java.rmi.RemoteException;
34
35 import javax.ejb.CreateException;
36 import javax.ejb.DuplicateKeyException;
37 import javax.ejb.EJBException;
38 import javax.ejb.EntityBean;
39 import javax.ejb.EntityContext;
40 import javax.ejb.FinderException;
41 import javax.ejb.ObjectNotFoundException;
42 import javax.ejb.RemoveException;
43 import javax.naming.Context;
44 import javax.naming.InitialContext;
45 import javax.naming.NamingException;
46 import javax.sql.DataSource;
47 import javax.transaction.NotSupportedException;
48
49 /**
50  *
51  */

52 public class StockBean implements EntityBean {
53
54     EntityContext ejbContext;
55
56     // ------------------------------------------------------------------
57
// State of the bean.
58
// They must be public for Container Managed Persistance.
59
// ------------------------------------------------------------------
60
public String stockid;
61
62     public int stockqty;
63
64     // ------------------------------------------------------------------
65
// EntityBean implementation
66
// ------------------------------------------------------------------
67

68     /**
69      * Set the associated entity context. The container invokes this method on
70      * an instance after the instance has been created. This method is called in
71      * an unspecified transaction context.
72      * @param ctx - An EntityContext interface for the instance. The instance
73      * should store the reference to the context in an instance variable.
74      * @throws EJBException Thrown by the method to indicate a failure caused by
75      * a system-level error.
76      */

77     public void setEntityContext(EntityContext ctx) {
78         ejbContext = ctx;
79     }
80
81     /**
82      * Unset the associated entity context. The container calls this method
83      * before removing the instance. This is the last method that the container
84      * invokes on the instance. The Java garbage collector will eventually
85      * invoke the finalize() method on the instance. This method is called in an
86      * unspecified transaction context.
87      * @throws EJBException Thrown by the method to indicate a failure caused by
88      * a system-level error.
89      */

90     public void unsetEntityContext() {
91         ejbContext = null;
92     }
93
94     /**
95      * A container invokes this method before it removes the EJB object that is
96      * currently associated with the instance. This method is invoked when a
97      * client invokes a remove operation on the enterprise Bean's home interface
98      * or the EJB object's remote interface. This method transitions the
99      * instance from the ready state to the pool of available instances. This
100      * method is called in the transaction context of the remove operation.
101      * @throws RemoveException The enterprise Bean does not allow destruction of
102      * the object.
103      * @throws EJBException - Thrown by the method to indicate a failure caused
104      * by a system-level error.
105      */

106     public void ejbRemove() throws RemoveException {
107     }
108
109     /**
110      * A container invokes this method to instruct the instance to synchronize
111      * its state by loading it state from the underlying database. This method
112      * always executes in the proper transaction context.
113      * @throws EJBException Thrown by the method to indicate a failure caused by
114      * a system-level error.
115      */

116     public void ejbLoad() {
117     }
118
119     /**
120      * A container invokes this method to instruct the instance to synchronize
121      * its state by storing it to the underlying database. This method always
122      * executes in the proper transaction context.
123      * @throws EJBException Thrown by the method to indicate a failure caused by
124      * a system-level error.
125      */

126     public void ejbStore() {
127     }
128
129     /**
130      * There must be an ejbPostCreate par ejbCreate method
131      * @throws CreateException Failure to create an entity EJB object.
132      */

133     public void ejbPostCreate(String id, int qty) throws CreateException {
134     }
135
136     /**
137      * The Entity bean can define 0 or more ejbCreate methods.
138      * @throws CreateException Failure to create an entity EJB object.
139      * @throws DuplicateKeyException An object with the same key already exists.
140      */

141     public java.lang.String ejbCreate(String id, int qty) throws CreateException, DuplicateKeyException {
142
143         // Init here the bean fields
144
stockid = id;
145         stockqty = qty;
146
147         // In CMP, should return null.
148
return null;
149     }
150
151     /**
152      * A container invokes this method on an instance before the instance
153      * becomes disassociated with a specific EJB object.
154      */

155     public void ejbPassivate() {
156     }
157
158     /**
159      * A container invokes this method when the instance is taken out of the
160      * pool of available instances to become associated with a specific EJB
161      * object.
162      */

163     public void ejbActivate() {
164     }
165
166     // ------------------------------------------------------------------
167
// Stock implementation
168
// ------------------------------------------------------------------
169

170     /**
171      * increaseQuantity
172      */

173     public void increaseQuantity(int qty) {
174         stockqty += qty;
175     }
176
177     /**
178      * decreaseStockQuantity
179      */

180     public void decreaseQuantity(int qty) throws RemoteException {
181         stockqty = stockqty - qty;
182         if (stockqty < 0) {
183             throw new RemoteException("Negative stock");
184         }
185     }
186
187     /**
188      * getStockQuantity
189      */

190     public int getQuantity() throws RemoteException {
191         return stockqty;
192     }
193
194     /**
195      * setStockQuantity
196      */

197     public void setQuantity(int qty) throws RemoteException {
198         stockqty = qty;
199     }
200
201     /**
202      * getStockQuantity
203      */

204     public String getId() throws RemoteException {
205         return stockid;
206     }
207 }
Popular Tags