KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sb > OpBean


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: OpBean.java,v 1.7 2004/04/19 06:39:29 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 package sb;
30
31 import javax.ejb.EJBException;
32 import javax.ejb.SessionBean;
33 import javax.ejb.SessionContext;
34 import javax.ejb.SessionSynchronization;
35 import javax.naming.InitialContext;
36 import javax.naming.NamingException;
37
38 /**
39  * This is an example of Session Bean, statefull, and synchronized.
40  * @author JOnAS team
41  */

42 public class OpBean implements SessionBean, SessionSynchronization {
43
44     /**
45      * Actual state of the bean
46      */

47     private int total = 0;
48
49     /**
50      * value inside Tx, not yet committed.
51      */

52     private int newtotal = 0;
53
54     /**
55      * User client
56      */

57     private String clientUser = null;
58
59
60     /* ========================= ejbCreate methods ============================ */
61
62     /**
63      * There must be one ejbCreate() method per create() method on the Home
64      * interface, and with the same signature.
65      * @param user the user name
66      */

67     public void ejbCreate(String user) {
68
69         total = 0;
70         newtotal = total; // in case we are outside transactions
71
clientUser = user;
72     }
73
74     /*
75      * ====================== javax.ejb.SessionBean implementation
76      * =================
77      */

78
79     /**
80      * The activate method is called when the instance is activated from its
81      * "passive" state. The instance should acquire any resource that it has
82      * released earlier in the ejbPassivate() method. This method is called with
83      * no transaction context.
84      * @throws EJBException Thrown by the method to indicate a failure caused by
85      * a system-level error.
86      * @throws java.rmi.RemoteException This exception is defined in the method
87      * signature to provide backward compatibility for enterprise beans
88      * written for the EJB 1.0 specification. Enterprise beans written
89      * for the EJB 1.1 specification should throw the
90      * javax.ejb.EJBException instead of this exception. Enterprise
91      * beans written for the EJB2.0 and higher specifications must throw
92      * the javax.ejb.EJBException instead of this exception.
93      */

94     public void ejbActivate() throws EJBException, java.rmi.RemoteException {
95         // Nothing to do for this simple example
96
}
97
98     /**
99      * The passivate method is called before the instance enters the "passive"
100      * state. The instance should release any resources that it can re-acquire
101      * later in the ejbActivate() method. After the passivate method completes,
102      * the instance must be in a state that allows the container to use the Java
103      * Serialization protocol to externalize and store away the instance's
104      * state. This method is called with no transaction context.
105      * @throws EJBException Thrown by the method to indicate a failure caused by
106      * a system-level error.
107      * @throws java.rmi.RemoteException This exception is defined in the method
108      * signature to provide backward compatibility for enterprise beans
109      * written for the EJB 1.0 specification. Enterprise beans written
110      * for the EJB 1.1 specification should throw the
111      * javax.ejb.EJBException instead of this exception. Enterprise
112      * beans written for the EJB2.0 and higher specifications must throw
113      * the javax.ejb.EJBException instead of this exception.
114      */

115     public void ejbPassivate() throws EJBException, java.rmi.RemoteException {
116         // Nothing to do for this simple example
117
}
118
119     /**
120      * A container invokes this method before it ends the life of the session
121      * object. This happens as a result of a client's invoking a remove
122      * operation, or when a container decides to terminate the session object
123      * after a timeout. This method is called with no transaction context.
124      * @throws EJBException Thrown by the method to indicate a failure caused by
125      * a system-level error.
126      * @throws java.rmi.RemoteException This exception is defined in the method
127      * signature to provide backward compatibility for enterprise beans
128      * written for the EJB 1.0 specification. Enterprise beans written
129      * for the EJB 1.1 specification should throw the
130      * javax.ejb.EJBException instead of this exception. Enterprise
131      * beans written for the EJB2.0 and higher specifications must throw
132      * the javax.ejb.EJBException instead of this exception.
133      */

134     public void ejbRemove() throws EJBException, java.rmi.RemoteException {
135         // Nothing to do for this simple example
136
}
137
138     /**
139      * Set the associated session context. The container calls this method after
140      * the instance creation. The enterprise Bean instance should store the
141      * reference to the context object in an instance variable. This method is
142      * called with no transaction context.
143      * @param sessionContext A SessionContext interface for the instance.
144      * @throws EJBException Thrown by the method to indicate a failure caused by
145      * a system-level error.
146      * @throws java.rmi.RemoteException This exception is defined in the method
147      * signature to provide backward compatibility for applications
148      * written for the EJB 1.0 specification. Enterprise beans written
149      * for the EJB 1.1 specification should throw the
150      * javax.ejb.EJBException instead of this exception. Enterprise
151      * beans written for the EJB2.0 and higher specifications must throw
152      * the javax.ejb.EJBException instead of this exception.
153      */

154     public void setSessionContext(SessionContext sessionContext) throws EJBException, java.rmi.RemoteException {
155
156     }
157
158     /*
159      * ============== javax.ejb.SessionSynchronization implementation
160      * =============
161      */

162
163     /**
164      * The afterBegin method notifies a session Bean instance that a new
165      * transaction has started, and that the subsequent business methods on the
166      * instance will be invoked in the context of the transaction. The instance
167      * can use this method, for example, to read data from a database and cache
168      * the data in the instance fields. This method executes in the proper
169      * transaction context.
170      * @throws EJBException Thrown by the method to indicate a failure caused by
171      * a system-level error.
172      * @throws java.rmi.RemoteException - This exception is defined in the
173      * method signature to provide backward compatibility for enterprise
174      * beans written for the EJB 1.0 specification. Enterprise beans
175      * written for the EJB 1.1 and higher specifications should throw
176      * the javax.ejb.EJBException instead of this exception. Enterprise
177      * beans written for the EJB 2.0 and higher specifications must not
178      * throw the java.rmi.RemoteException.
179      */

180     public void afterBegin() throws EJBException, java.rmi.RemoteException {
181         newtotal = total;
182     }
183
184     /**
185      * The beforeCompletion method notifies a session Bean instance that a
186      * transaction is about to be committed. The instance can use this method,
187      * for example, to write any cached data to a database. This method executes
188      * in the proper transaction context. <b>Note: </b> The instance may still
189      * cause the container to rollback the transaction by invoking the
190      * setRollbackOnly() method on the instance context, or by throwing an
191      * exception.
192      * @throws EJBException Thrown by the method to indicate a failure caused by
193      * a system-level error.
194      * @throws java.rmi.RemoteException - This exception is defined in the
195      * method signature to provide backward compatibility for enterprise
196      * beans written for the EJB 1.0 specification. Enterprise beans
197      * written for the EJB 1.1 and higher specifications should throw
198      * the javax.ejb.EJBException instead of this exception. Enterprise
199      * beans written for the EJB 2.0 and higher specifications must not
200      * throw the java.rmi.RemoteException.
201      */

202     public void beforeCompletion() throws EJBException, java.rmi.RemoteException {
203         // Nothing to do for this simple example
204

205         // We can access the bean environment everywhere in the bean,
206
// for example here!
207
try {
208             InitialContext ictx = new InitialContext();
209             String value = (String) ictx.lookup("java:comp/env/prop1");
210             // value should be the one defined in ejb-jar.xml
211
} catch (NamingException e) {
212             throw new EJBException(e);
213         }
214     }
215
216     /**
217      * The afterCompletion method notifies a session Bean instance that a
218      * transaction commit protocol has completed, and tells the instance whether
219      * the transaction has been committed or rolled back. This method executes
220      * with no transaction context.
221      * @param committed - True if the transaction has been committed, false if
222      * is has been rolled back.
223      * @throws EJBException Thrown by the method to indicate a failure caused by
224      * a system-level error.
225      * @throws java.rmi.RemoteException - This exception is defined in the
226      * method signature to provide backward compatibility for enterprise
227      * beans written for the EJB 1.0 specification. Enterprise beans
228      * written for the EJB 1.1 and higher specifications should throw
229      * the javax.ejb.EJBException instead of this exception. Enterprise
230      * beans written for the EJB 2.0 and higher specifications must not
231      * throw the java.rmi.RemoteException.
232      */

233     public void afterCompletion(boolean committed) throws EJBException, java.rmi.RemoteException {
234         if (committed) {
235             total = newtotal;
236         } else {
237             newtotal = total;
238         }
239     }
240
241     /* ========================= Op implementation ============================ */
242
243     /**
244      * Business method implementation.
245      * @param s nb of shares to be bought
246      */

247     public void buy(int s) {
248         newtotal = newtotal + s;
249         return;
250     }
251
252     /**
253      * Business method implementation.
254      * @return the nb of shares bought
255      */

256     public int read() {
257         return newtotal;
258     }
259 }
Popular Tags