KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > earsample > beans > secusb > 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  * --------------------------------------------------------------------------
23  * $Id: OpBean.java,v 1.2 2004/04/13 14:11:22 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.earsample.beans.secusb;
28
29 import javax.ejb.EJBException JavaDoc;
30 import javax.ejb.SessionBean JavaDoc;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.ejb.SessionSynchronization JavaDoc;
33
34 /**
35  * This is an example of Session Bean, statefull, and synchronized.
36  * @author JOnAS team
37  */

38 public class OpBean implements SessionBean JavaDoc, SessionSynchronization JavaDoc {
39
40     /**
41      * Actual state of the bean
42      */

43     private int total = 0;
44
45     /**
46      * value inside Tx, not yet committed.
47      */

48     private int newtotal = 0;
49
50     /**
51      * User client
52      */

53     private String JavaDoc clientUser = null;
54
55     /**
56      * Session context that the container provides for a session enterprise Bean
57      * instance.
58      */

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

68     public void ejbCreate(String JavaDoc user) {
69         total = 0;
70         // in case we are outside transactions
71
newtotal = total;
72         clientUser = user;
73     }
74
75     /* =============== javax.ejb.SessionBean 2.0 implementation ============== */
76
77     /**
78      * The activate method is called when the instance is activated from its
79      * "passive" state. The instance should acquire any resource that it has
80      * released earlier in the ejbPassivate() method. This method is called with
81      * no transaction context.
82      * @throws EJBException Thrown by the method to indicate a failure caused by
83      * a system-level error.
84      * @throws java.rmi.RemoteException This exception is defined in the method
85      * signature to provide backward compatibility for enterprise beans
86      * written for the EJB 1.0 specification. Enterprise beans written
87      * for the EJB 1.1 specification should throw the
88      * javax.ejb.EJBException instead of this exception. Enterprise
89      * beans written for the EJB2.0 and higher specifications must throw
90      * the javax.ejb.EJBException instead of this exception.
91      */

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

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

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

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

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

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

200     public void beforeCompletion() throws EJBException JavaDoc, java.rmi.RemoteException JavaDoc {
201     }
202
203     /**
204      * The afterCompletion method notifies a session Bean instance that a
205      * transaction commit protocol has completed, and tells the instance whether
206      * the transaction has been committed or rolled back. This method executes
207      * with no transaction context.
208      * @param committed - True if the transaction has been committed, false if
209      * is has been rolled back.
210      * @throws EJBException Thrown by the method to indicate a failure caused by
211      * a system-level error.
212      * @throws java.rmi.RemoteException - This exception is defined in the
213      * method signature to provide backward compatibility for enterprise
214      * beans written for the EJB 1.0 specification. Enterprise beans
215      * written for the EJB 1.1 and higher specifications should throw
216      * the javax.ejb.EJBException instead of this exception. Enterprise
217      * beans written for the EJB 2.0 and higher specifications must not
218      * throw the java.rmi.RemoteException.
219      */

220     public void afterCompletion(boolean committed) throws EJBException JavaDoc, java.rmi.RemoteException JavaDoc {
221
222         if (committed) {
223             total = newtotal;
224         } else {
225             newtotal = total;
226         }
227     }
228
229     /* ========================= Op implementation ============================ */
230
231     /**
232      * Business method implementation.
233      * @param s nb of shares to be bought
234      */

235     public void buy(int s) {
236         newtotal = newtotal + s;
237         return;
238     }
239
240     /**
241      * Business method implementation.
242      * @return the nb of shares bought
243      */

244     public int read() {
245         return newtotal;
246     }
247 }
Popular Tags