KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > transacted > SimpleSY


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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  * --------------------------------------------------------------------------
22  * $Id: SimpleSY.java,v 1.8 2004/12/17 15:09:45 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.transacted;
27
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.EJBException JavaDoc;
30 import javax.ejb.RemoveException JavaDoc;
31 import javax.ejb.SessionSynchronization JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.rmi.PortableRemoteObject JavaDoc;
36
37 import org.objectweb.util.monolog.api.BasicLevel;
38
39 /*
40  * Stateful session bean
41  */

42 public class SimpleSY extends SimpleSL implements SessionSynchronization JavaDoc {
43
44     private boolean rollbackonly = false;
45     private Context JavaDoc ictx = null;
46     private SimpleELocalHome lhome = null;
47     
48
49     // ------------------------------------------------------------------
50
// SynchroSimple implementation
51
// ------------------------------------------------------------------
52

53     /**
54      * Ask bean to set transaction rollback only
55      */

56     public void setRollbackOnly() {
57         logger.log(BasicLevel.DEBUG, "");
58         rollbackonly = true;
59     }
60
61     /**
62      * REQUIRED -> should return true.
63      */

64     public boolean call_requires_new_local() {
65         logger.log(BasicLevel.DEBUG, "");
66         SimpleLocal loc = getLocalBean();
67         boolean tx = loc.opwith_requires_new();
68         if (!tx) {
69             logger.log(BasicLevel.ERROR, "opwith_requires_new was outside tx");
70             return false; // error
71
}
72         try {
73             loc.remove(); // cleaning
74
} catch (RemoveException JavaDoc e) {
75             logger.log(BasicLevel.ERROR, "cannot remove local bean");
76         }
77         return isAssociated();
78     }
79
80     /**
81      * REQUIRED -> should return true.
82      */

83     public boolean call_notsupported_local() {
84         logger.log(BasicLevel.DEBUG, "");
85         SimpleLocal loc = getLocalBean();
86         boolean tx = loc.opwith_notsupported();
87         if (tx) {
88             logger.log(BasicLevel.ERROR, "opwith_notsupported was in tx");
89             return false; // error
90
}
91         try {
92             loc.remove(); // cleaning
93
} catch (RemoveException JavaDoc e) {
94             logger.log(BasicLevel.ERROR, "cannot remove local bean");
95         }
96         return isAssociated();
97     }
98
99     // ------------------------------------------------------------------
100
// SessionSynchronization implementation
101
// ------------------------------------------------------------------
102

103     /**
104      * The afterBegin method notifies a session Bean instance that a new
105      * transaction has started, and that the subsequent business methods on
106      * the instance will be invoked in the context of the transaction.
107      * This method executes in the proper transaction context.
108      *
109      * @throws EJBException Thrown if the instance could not perform
110      * the function requested by the container because of a system-level error.
111      */

112     public void afterBegin() {
113         logger.log(BasicLevel.DEBUG, "");
114     }
115
116
117     /**
118      * The beforeCompletion method notifies a session Bean instance that a
119      * transaction is about to be committed.
120      * This method executes in the proper transaction context.
121      * Note: The instance may still cause the container to rollback the transaction
122      * by invoking the setRollbackOnly() method on the instance context, or by throwing
123      * an exception.
124      *
125      * @throws EJBException Thrown by the method to indicate a failure caused by a
126      * system-level error.
127      */

128     public void beforeCompletion() {
129         logger.log(BasicLevel.DEBUG, "");
130         if (rollbackonly) {
131             sessionContext.setRollbackOnly();
132         }
133     }
134
135
136     /**
137      * The afterCompletion method notifies a session Bean instance that a
138      * transaction commit protocol has completed, and tells the instance whether
139      * the transaction has been committed or rolled back.
140      * This method executes with no transaction context.
141      *
142      * @param : committed - True if the transaction has been committed, false if
143      * it has been rolled back.
144      *
145      * @throws EJBException Thrown by the method to indicate a failure caused by a
146      * system-level error.
147      */

148     public void afterCompletion(boolean committed) {
149         logger.log(BasicLevel.DEBUG, "");
150     }
151
152     // ------------------------------------------------------------------
153
// private methods
154
// ------------------------------------------------------------------
155

156     /**
157      * init non persistent bean data.
158      * This should be called when instance is created or activated.
159      */

160     private void stateUpdate() throws NamingException JavaDoc {
161         // Get initial Context
162
if (ictx == null) {
163             ictx = new InitialContext JavaDoc();
164         }
165         // lookup paperhome in JNDI
166
if (lhome == null) {
167             lhome = (SimpleELocalHome) ictx.lookup("java:comp/env/ejb/SimpleEC");
168         }
169     }
170
171     /**
172      * get local bean
173      */

174     private SimpleLocal getLocalBean() {
175         SimpleLocal ret = null;
176         try {
177             stateUpdate();
178         } catch (NamingException JavaDoc e) {
179             logger.log(BasicLevel.ERROR, "getLocalBean raised exception "+e);
180             throw new EJBException JavaDoc("Error in getLocalBean:"+e);
181         }
182         try {
183             ret = lhome.create(123);
184         } catch (CreateException JavaDoc e) {
185             logger.log(BasicLevel.ERROR, "Cannot create local entity bean "+e);
186             throw new EJBException JavaDoc("Cannot create local entity bean:"+e);
187         }
188         return ret;
189     }
190 }
191
Popular Tags