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: * --------------------------------------------------------------------------
023: * $Id: OpBean.java 4591 2004-04-13 14:11:22Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.earsample.beans.secusb;
026:
027: import javax.ejb.EJBException;
028: import javax.ejb.SessionBean;
029: import javax.ejb.SessionContext;
030: import javax.ejb.SessionSynchronization;
031:
032: /**
033: * This is an example of Session Bean, statefull, and synchronized.
034: * @author JOnAS team
035: */
036: public class OpBean implements SessionBean, SessionSynchronization {
037:
038: /**
039: * Actual state of the bean
040: */
041: private int total = 0;
042:
043: /**
044: * value inside Tx, not yet committed.
045: */
046: private int newtotal = 0;
047:
048: /**
049: * User client
050: */
051: private String clientUser = null;
052:
053: /**
054: * Session context that the container provides for a session enterprise Bean
055: * instance.
056: */
057: private SessionContext sessionContext = null;
058:
059: /* ========================= ejbCreate methods ============================ */
060:
061: /**
062: * There must be one ejbCreate() method per create() method on the Home
063: * interface, and with the same signature.
064: * @param user the user name
065: */
066: public void ejbCreate(String user) {
067: total = 0;
068: // in case we are outside transactions
069: newtotal = total;
070: clientUser = user;
071: }
072:
073: /* =============== javax.ejb.SessionBean 2.0 implementation ============== */
074:
075: /**
076: * The activate method is called when the instance is activated from its
077: * "passive" state. The instance should acquire any resource that it has
078: * released earlier in the ejbPassivate() method. This method is called with
079: * no transaction context.
080: * @throws EJBException Thrown by the method to indicate a failure caused by
081: * a system-level error.
082: * @throws java.rmi.RemoteException This exception is defined in the method
083: * signature to provide backward compatibility for enterprise beans
084: * written for the EJB 1.0 specification. Enterprise beans written
085: * for the EJB 1.1 specification should throw the
086: * javax.ejb.EJBException instead of this exception. Enterprise
087: * beans written for the EJB2.0 and higher specifications must throw
088: * the javax.ejb.EJBException instead of this exception.
089: */
090: public void ejbActivate() throws EJBException,
091: java.rmi.RemoteException {
092: // Nothing to do for this simple example
093: }
094:
095: /**
096: * The passivate method is called before the instance enters the "passive"
097: * state. The instance should release any resources that it can re-acquire
098: * later in the ejbActivate() method. After the passivate method completes,
099: * the instance must be in a state that allows the container to use the Java
100: * Serialization protocol to externalize and store away the instance's
101: * state. This method is called with no transaction context.
102: * @throws EJBException Thrown by the method to indicate a failure caused by
103: * a system-level error.
104: * @throws java.rmi.RemoteException This exception is defined in the method
105: * signature to provide backward compatibility for enterprise beans
106: * written for the EJB 1.0 specification. Enterprise beans written
107: * for the EJB 1.1 specification should throw the
108: * javax.ejb.EJBException instead of this exception. Enterprise
109: * beans written for the EJB2.0 and higher specifications must throw
110: * the javax.ejb.EJBException instead of this exception.
111: */
112: public void ejbPassivate() throws EJBException,
113: java.rmi.RemoteException {
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,
133: java.rmi.RemoteException {
134: // Nothing to do for this simple example
135: }
136:
137: /**
138: * Set the associated session context. The container calls this method after
139: * the instance creation. The enterprise Bean instance should store the
140: * reference to the context object in an instance variable. This method is
141: * called with no transaction context.
142: * @param sessionContext A SessionContext interface for the instance.
143: * @throws EJBException Thrown by the method to indicate a failure caused by
144: * a system-level error.
145: * @throws java.rmi.RemoteException This exception is defined in the method
146: * signature to provide backward compatibility for applications
147: * written for the EJB 1.0 specification. Enterprise beans written
148: * for the EJB 1.1 specification should throw the
149: * javax.ejb.EJBException instead of this exception. Enterprise
150: * beans written for the EJB2.0 and higher specifications must throw
151: * the javax.ejb.EJBException instead of this exception.
152: */
153: public void setSessionContext(SessionContext sessionContext)
154: throws EJBException, java.rmi.RemoteException {
155: this .sessionContext = sessionContext;
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,
181: java.rmi.RemoteException {
182: newtotal = total;
183: }
184:
185: /**
186: * The beforeCompletion method notifies a session Bean instance that a
187: * transaction is about to be committed. The instance can use this method,
188: * for example, to write any cached data to a database. This method executes
189: * in the proper transaction context. <b>Note: </b> The instance may still
190: * cause the container to rollback the transaction by invoking the
191: * setRollbackOnly() method on the instance context, or by throwing an
192: * exception.
193: * @throws EJBException Thrown by the method to indicate a failure caused by
194: * a system-level error.
195: * @throws java.rmi.RemoteException - This exception is defined in the
196: * method signature to provide backward compatibility for enterprise
197: * beans written for the EJB 1.0 specification. Enterprise beans
198: * written for the EJB 1.1 and higher specifications should throw
199: * the javax.ejb.EJBException instead of this exception. Enterprise
200: * beans written for the EJB 2.0 and higher specifications must not
201: * throw the java.rmi.RemoteException.
202: */
203: public void beforeCompletion() throws EJBException,
204: java.rmi.RemoteException {
205: }
206:
207: /**
208: * The afterCompletion method notifies a session Bean instance that a
209: * transaction commit protocol has completed, and tells the instance whether
210: * the transaction has been committed or rolled back. This method executes
211: * with no transaction context.
212: * @param committed - True if the transaction has been committed, false if
213: * is has been rolled back.
214: * @throws EJBException Thrown by the method to indicate a failure caused by
215: * a system-level error.
216: * @throws java.rmi.RemoteException - This exception is defined in the
217: * method signature to provide backward compatibility for enterprise
218: * beans written for the EJB 1.0 specification. Enterprise beans
219: * written for the EJB 1.1 and higher specifications should throw
220: * the javax.ejb.EJBException instead of this exception. Enterprise
221: * beans written for the EJB 2.0 and higher specifications must not
222: * throw the java.rmi.RemoteException.
223: */
224: public void afterCompletion(boolean committed) throws EJBException,
225: java.rmi.RemoteException {
226:
227: if (committed) {
228: total = newtotal;
229: } else {
230: newtotal = total;
231: }
232: }
233:
234: /* ========================= Op implementation ============================ */
235:
236: /**
237: * Business method implementation.
238: * @param s nb of shares to be bought
239: */
240: public void buy(int s) {
241: newtotal = newtotal + s;
242: return;
243: }
244:
245: /**
246: * Business method implementation.
247: * @return the nb of shares bought
248: */
249: public int read() {
250: return newtotal;
251: }
252: }
|