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: * Contributor(s): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: OpBean.java 4618 2004-04-19 06:39:30Z benoitf $
026: * --------------------------------------------------------------------------
027: */package sb;
028:
029: import javax.ejb.EJBException;
030: import javax.ejb.SessionBean;
031: import javax.ejb.SessionContext;
032: import javax.ejb.SessionSynchronization;
033: import javax.naming.InitialContext;
034: import javax.naming.NamingException;
035:
036: /**
037: * This is an example of Session Bean, statefull, and synchronized.
038: * @author JOnAS team
039: */
040: public class OpBean implements SessionBean, SessionSynchronization {
041:
042: /**
043: * Actual state of the bean
044: */
045: private int total = 0;
046:
047: /**
048: * value inside Tx, not yet committed.
049: */
050: private int newtotal = 0;
051:
052: /**
053: * User client
054: */
055: private String clientUser = null;
056:
057: /* ========================= ejbCreate methods ============================ */
058:
059: /**
060: * There must be one ejbCreate() method per create() method on the Home
061: * interface, and with the same signature.
062: * @param user the user name
063: */
064: public void ejbCreate(String user) {
065:
066: total = 0;
067: newtotal = total; // in case we are outside transactions
068: clientUser = user;
069: }
070:
071: /*
072: * ====================== javax.ejb.SessionBean implementation
073: * =================
074: */
075:
076: /**
077: * The activate method is called when the instance is activated from its
078: * "passive" state. The instance should acquire any resource that it has
079: * released earlier in the ejbPassivate() method. This method is called with
080: * no transaction context.
081: * @throws EJBException Thrown by the method to indicate a failure caused by
082: * a system-level error.
083: * @throws java.rmi.RemoteException This exception is defined in the method
084: * signature to provide backward compatibility for enterprise beans
085: * written for the EJB 1.0 specification. Enterprise beans written
086: * for the EJB 1.1 specification should throw the
087: * javax.ejb.EJBException instead of this exception. Enterprise
088: * beans written for the EJB2.0 and higher specifications must throw
089: * the javax.ejb.EJBException instead of this exception.
090: */
091: public void ejbActivate() throws EJBException,
092: java.rmi.RemoteException {
093: // Nothing to do for this simple example
094: }
095:
096: /**
097: * The passivate method is called before the instance enters the "passive"
098: * state. The instance should release any resources that it can re-acquire
099: * 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,
114: java.rmi.RemoteException {
115: // Nothing to do for this simple example
116: }
117:
118: /**
119: * A container invokes this method before it ends the life of the session
120: * object. This happens as a result of a client's invoking a remove
121: * operation, or when a container decides to terminate the session object
122: * after a timeout. This method is called with no transaction context.
123: * @throws EJBException Thrown by the method to indicate a failure caused by
124: * a system-level error.
125: * @throws java.rmi.RemoteException This exception is defined in the method
126: * signature to provide backward compatibility for enterprise beans
127: * written for the EJB 1.0 specification. Enterprise beans written
128: * for the EJB 1.1 specification should throw the
129: * javax.ejb.EJBException instead of this exception. Enterprise
130: * beans written for the EJB2.0 and higher specifications must throw
131: * the javax.ejb.EJBException instead of this exception.
132: */
133: public void ejbRemove() throws EJBException,
134: 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)
155: throws EJBException, java.rmi.RemoteException {
156:
157: }
158:
159: /*
160: * ============== javax.ejb.SessionSynchronization implementation
161: * =============
162: */
163:
164: /**
165: * The afterBegin method notifies a session Bean instance that a new
166: * transaction has started, and that the subsequent business methods on the
167: * instance will be invoked in the context of the transaction. The instance
168: * can use this method, for example, to read data from a database and cache
169: * the data in the instance fields. This method executes in the proper
170: * transaction context.
171: * @throws EJBException Thrown by the method to indicate a failure caused by
172: * a system-level error.
173: * @throws java.rmi.RemoteException - This exception is defined in the
174: * method signature to provide backward compatibility for enterprise
175: * beans written for the EJB 1.0 specification. Enterprise beans
176: * written for the EJB 1.1 and higher specifications should throw
177: * the javax.ejb.EJBException instead of this exception. Enterprise
178: * beans written for the EJB 2.0 and higher specifications must not
179: * throw the java.rmi.RemoteException.
180: */
181: public void afterBegin() throws EJBException,
182: java.rmi.RemoteException {
183: newtotal = total;
184: }
185:
186: /**
187: * The beforeCompletion method notifies a session Bean instance that a
188: * transaction is about to be committed. The instance can use this method,
189: * for example, to write any cached data to a database. This method executes
190: * in the proper transaction context. <b>Note: </b> The instance may still
191: * cause the container to rollback the transaction by invoking the
192: * setRollbackOnly() method on the instance context, or by throwing an
193: * exception.
194: * @throws EJBException Thrown by the method to indicate a failure caused by
195: * a system-level error.
196: * @throws java.rmi.RemoteException - This exception is defined in the
197: * method signature to provide backward compatibility for enterprise
198: * beans written for the EJB 1.0 specification. Enterprise beans
199: * written for the EJB 1.1 and higher specifications should throw
200: * the javax.ejb.EJBException instead of this exception. Enterprise
201: * beans written for the EJB 2.0 and higher specifications must not
202: * throw the java.rmi.RemoteException.
203: */
204: public void beforeCompletion() throws EJBException,
205: java.rmi.RemoteException {
206: // Nothing to do for this simple example
207:
208: // We can access the bean environment everywhere in the bean,
209: // for example here!
210: try {
211: InitialContext ictx = new InitialContext();
212: String value = (String) ictx.lookup("java:comp/env/prop1");
213: // value should be the one defined in ejb-jar.xml
214: } catch (NamingException e) {
215: throw new EJBException(e);
216: }
217: }
218:
219: /**
220: * The afterCompletion method notifies a session Bean instance that a
221: * transaction commit protocol has completed, and tells the instance whether
222: * the transaction has been committed or rolled back. This method executes
223: * with no transaction context.
224: * @param committed - True if the transaction has been committed, false if
225: * is has been rolled back.
226: * @throws EJBException Thrown by the method to indicate a failure caused by
227: * a system-level error.
228: * @throws java.rmi.RemoteException - This exception is defined in the
229: * method signature to provide backward compatibility for enterprise
230: * beans written for the EJB 1.0 specification. Enterprise beans
231: * written for the EJB 1.1 and higher specifications should throw
232: * the javax.ejb.EJBException instead of this exception. Enterprise
233: * beans written for the EJB 2.0 and higher specifications must not
234: * throw the java.rmi.RemoteException.
235: */
236: public void afterCompletion(boolean committed) throws EJBException,
237: java.rmi.RemoteException {
238: if (committed) {
239: total = newtotal;
240: } else {
241: newtotal = total;
242: }
243: }
244:
245: /* ========================= Op implementation ============================ */
246:
247: /**
248: * Business method implementation.
249: * @param s nb of shares to be bought
250: */
251: public void buy(int s) {
252: newtotal = newtotal + s;
253: return;
254: }
255:
256: /**
257: * Business method implementation.
258: * @return the nb of shares bought
259: */
260: public int read() {
261: return newtotal;
262: }
263: }
|