KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jaasclient > beans > secusb > JAASOpBean


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: JAASOpBean.java,v 1.2 2004/04/19 06:39:30 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

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

38 public class JAASOpBean implements SessionBean, SessionSynchronization {
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 clientUser = null;
54
55     /* ========================= ejbCreate methods ============================ */
56
57     /**
58      * There must be one ejbCreate() method per create() method on the Home
59      * interface, and with the same signature.
60      * @param user the user name
61      */

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

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

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

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

146     public void setSessionContext(SessionContext sessionContext) throws EJBException, java.rmi.RemoteException {
147     }
148
149     /*
150      * ============== javax.ejb.SessionSynchronization implementation
151      * =============
152      */

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

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

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

213     public void afterCompletion(boolean committed) throws EJBException, java.rmi.RemoteException {
214
215         if (committed) {
216             total = newtotal;
217         } else {
218             newtotal = total;
219         }
220     }
221
222     /* ========================= Op implementation ============================ */
223
224     /**
225      * Business method implementation.
226      * @param s nb of shares to be bought
227      */

228     public void buy(int s) {
229         newtotal = newtotal + s;
230         return;
231     }
232
233     /**
234      * Business method implementation.
235      * @return the nb of shares bought
236      */

237     public int read() {
238         return newtotal;
239     }
240 }
Popular Tags