KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > examples > statefulbean > StatefulBean


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@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  * --------------------------------------------------------------------------
22  * $Id: StatefulBean.java 9 2006-02-19 18:53:32Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.examples.statefulbean;
27
28 import java.rmi.RemoteException JavaDoc;
29
30 import javax.ejb.Remote JavaDoc;
31 import javax.ejb.SessionSynchronization JavaDoc;
32 import javax.ejb.Stateful JavaDoc;
33
34 /**
35  * Simple stateful bean.
36  * @author Florent Benoit
37  */

38 @Stateful JavaDoc
39 @Remote JavaDoc(StatefulRemote.class)
40 public class StatefulBean implements SessionSynchronization JavaDoc, StatefulRemote {
41
42     /**
43      * Actual state of the bean.
44      */

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

50     private int newtotal = 0;
51
52     /**
53      * Initialize the amounts for with and without Tx.
54      */

55     public StatefulBean() {
56         total = 0;
57         newtotal = total; // in case we are outside transactions
58
}
59
60     /**
61      * The afterBegin method notifies a session Bean instance that a new
62      * transaction has started, and that the subsequent business methods on the
63      * instance will be invoked in the context of the transaction. The instance
64      * can use this method, for example, to read data from a database and cache
65      * the data in the instance fields. This method executes in the proper
66      * transaction context.
67      * @throws java.rmi.RemoteException - This exception is defined in the
68      * method signature to provide backward compatibility for enterprise
69      * beans written for the EJB 1.0 specification. Enterprise beans
70      * written for the EJB 1.1 and higher specifications should throw
71      * the javax.ejb.EJBException instead of this exception. Enterprise
72      * beans written for the EJB 2.0 and higher specifications must not
73      * throw the java.rmi.RemoteException.
74      */

75     public void afterBegin() throws RemoteException JavaDoc {
76         newtotal = total;
77     }
78
79     /**
80      * The beforeCompletion method notifies a session Bean instance that a
81      * transaction is about to be committed. The instance can use this method,
82      * for example, to write any cached data to a database. This method executes
83      * in the proper transaction context. <b>Note: </b> The instance may still
84      * cause the container to rollback the transaction by invoking the
85      * setRollbackOnly() method on the instance context, or by throwing an
86      * exception.
87      * @throws java.rmi.RemoteException - This exception is defined in the
88      * method signature to provide backward compatibility for enterprise
89      * beans written for the EJB 1.0 specification. Enterprise beans
90      * written for the EJB 1.1 and higher specifications should throw
91      * the javax.ejb.EJBException instead of this exception. Enterprise
92      * beans written for the EJB 2.0 and higher specifications must not
93      * throw the java.rmi.RemoteException.
94      */

95     public void beforeCompletion() throws RemoteException JavaDoc {
96     }
97
98     /**
99      * The afterCompletion method notifies a session Bean instance that a
100      * transaction commit protocol has completed, and tells the instance whether
101      * the transaction has been committed or rolled back. This method executes
102      * with no transaction context.
103      * @param committed - True if the transaction has been committed, false if
104      * is has been rolled back.
105      * @throws java.rmi.RemoteException - This exception is defined in the
106      * method signature to provide backward compatibility for enterprise
107      * beans written for the EJB 1.0 specification. Enterprise beans
108      * written for the EJB 1.1 and higher specifications should throw
109      * the javax.ejb.EJBException instead of this exception. Enterprise
110      * beans written for the EJB 2.0 and higher specifications must not
111      * throw the java.rmi.RemoteException.
112      */

113     public void afterCompletion(final boolean committed) throws RemoteException JavaDoc {
114         if (committed) {
115             total = newtotal;
116         } else {
117             newtotal = total;
118         }
119     }
120
121     /**
122      * Business method implementation.
123      * @param s nb of shares to be bought
124      */

125     public void buy(final int s) {
126         newtotal = newtotal + s;
127         return;
128     }
129
130     /**
131      * Business method implementation.
132      * @return the nb of shares bought
133      */

134     public int read() {
135         return newtotal;
136     }
137 }
138
Popular Tags