KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > ffolder > FolderSY


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: FolderSY.java,v 1.3 2004/12/17 15:09:45 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.ffolder;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import javax.ejb.CreateException JavaDoc;
32 import javax.ejb.EJBException JavaDoc;
33 import javax.ejb.FinderException JavaDoc;
34 import javax.ejb.SessionBean JavaDoc;
35 import javax.ejb.SessionSynchronization JavaDoc;
36 import javax.ejb.SessionContext JavaDoc;
37 import javax.naming.Context JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40 import javax.rmi.PortableRemoteObject JavaDoc;
41 import javax.sql.DataSource JavaDoc;
42
43 /**
44  * FolderSY implementation
45  * This bean is a stateful session bean that implements SessionSynchronization
46  * @author Philippe Durieux, Philippe Coq
47  */

48 public class FolderSY implements SessionBean JavaDoc, SessionSynchronization JavaDoc {
49
50     // protected static Logger logger = null;
51
SessionContext JavaDoc ejbContext;
52     InitialContext JavaDoc ictx;
53     Context JavaDoc myEnv;
54     FileHome filehome;
55
56     /**
57      * Check environment variables
58      */

59     void checkEnv(String JavaDoc method) {
60
61         // Check directly in my context
62
//logger.log(BasicLevel.DEBUG, "Check directly in my context");
63
try {
64             String JavaDoc value = (String JavaDoc) myEnv.lookup("myname");
65             if (!value.equals("mysession")) {
66                 // logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: myname=" + value);
67
throw new EJBException JavaDoc("FolderSY 1: " + method);
68             }
69         } catch (NamingException JavaDoc e) {
70             //logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e);
71
throw new EJBException JavaDoc("FolderSY 2: " + method);
72         }
73         // Idem with compound name
74
//logger.log(BasicLevel.DEBUG, "Idem with compound name");
75
try {
76             String JavaDoc value = (String JavaDoc) myEnv.lookup("dir1/dir2/name");
77             if (!value.equals("sessionvalue")) {
78                 // logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: dir1/dir2/name=" + value);
79
throw new EJBException JavaDoc("FolderSY 3: " + method);
80             }
81         } catch (NamingException JavaDoc e) {
82             //logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e);
83
throw new EJBException JavaDoc("FolderSY 4: " + method);
84         }
85         // Check from initial Context
86
//logger.log(BasicLevel.DEBUG, "Check from initial Context");
87
try {
88             String JavaDoc value = (String JavaDoc) ictx.lookup("java:comp/env/myname");
89             if (!value.equals("mysession")) {
90                 //logger.log(BasicLevel.ERROR, ": ictx.lookup failed: myname=" + value);
91
throw new EJBException JavaDoc("FolderSY 6: " + method);
92             }
93         } catch (NamingException JavaDoc e) {
94             // logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
95
throw new EJBException JavaDoc("FolderSY 7: " + method);
96         }
97
98         // Check datasource directly
99
//logger.log(BasicLevel.DEBUG, "Check datasource directly");
100
DataSource JavaDoc ds1 = null;
101         try {
102             ds1 = (DataSource JavaDoc) PortableRemoteObject.narrow(ictx.lookup("jdbc_1"), DataSource JavaDoc.class);
103         } catch (NamingException JavaDoc e) {
104             //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
105
throw new EJBException JavaDoc("FolderSY 8: " + method);
106         }
107         Connection JavaDoc con = null;
108         if (!method.equals("afterCompletion")) {
109             try {
110                 con = (Connection JavaDoc) ds1.getConnection();
111                 if (con.isClosed()) {
112                     //logger.log(BasicLevel.ERROR, ": connection is closed");
113
throw new EJBException JavaDoc("FolderSY 8a: " + method);
114                 }
115                 con.close();
116             } catch (SQLException JavaDoc e) {
117                 // logger.log(BasicLevel.ERROR, ": getConnection:\n" + e);
118
throw new EJBException JavaDoc("FolderSY 8b: " + method);
119             }
120         }
121
122         // Check DataSource from resource ref in bean environment
123
//logger.log(BasicLevel.DEBUG, "Check DataSource from resource ref");
124
DataSource JavaDoc ds2 = null;
125         try {
126             // The name is the one defined in FolderSY.xml
127
ds2 = (DataSource JavaDoc) myEnv.lookup("jdbc/mydb");
128         } catch (NamingException JavaDoc e) {
129             //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
130
throw new EJBException JavaDoc("FolderSY 9: " + method);
131         }
132         if (!method.equals("afterCompletion")) {
133             try {
134                 con = (Connection JavaDoc) ds2.getConnection();
135                 if (con.isClosed()) {
136                     //logger.log(BasicLevel.ERROR, ": connection is closed");
137
throw new EJBException JavaDoc("FolderSY 9a: " + method);
138                 }
139                 con.close();
140             } catch (SQLException JavaDoc e) {
141                 // logger.log(BasicLevel.ERROR, ": getConnection:\n" + e);
142
throw new EJBException JavaDoc("FolderSY 9b: " + method);
143             }
144         }
145
146         // Check boolean values
147
//logger.log(BasicLevel.DEBUG, "Check boolean values");
148
try {
149             Boolean JavaDoc value = (Boolean JavaDoc) ictx.lookup("java:comp/env/bVrai");
150             if (!value.booleanValue()) {
151                 //logger.log(BasicLevel.ERROR, ": ictx.lookup failed: bVrai=" + value);
152
throw new EJBException JavaDoc("FolderSY 10a: " + method);
153             }
154         } catch (NamingException JavaDoc e) {
155             //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
156
throw new EJBException JavaDoc("FolderSY10a: " + method);
157         } catch (ClassCastException JavaDoc e) {
158             //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
159
throw new EJBException JavaDoc("FolderSY10a1: " + method);
160         }
161         try {
162             Boolean JavaDoc value = (Boolean JavaDoc) ictx.lookup("java:comp/env/bFaux");
163             if (value.booleanValue()) {
164                 // logger.log(BasicLevel.ERROR, ": ictx.lookup failed: bFaux=" + value);
165
throw new EJBException JavaDoc("FolderSY 10b: " + method);
166             }
167         } catch (NamingException JavaDoc e) {
168             //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
169
throw new EJBException JavaDoc("FolderSY10b: " + method);
170         } catch (ClassCastException JavaDoc e) {
171             //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
172
throw new EJBException JavaDoc("FolderSY10b1: " + method);
173         }
174
175         //logger.log(BasicLevel.DEBUG, ": checkEnv OK");
176
}
177
178     // ------------------------------------------------------------------
179
// SessionBean implementation
180
// ------------------------------------------------------------------
181

182     public void setSessionContext(SessionContext JavaDoc ctx) {
183         // if (logger == null) {
184
// logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
185
//}
186
//logger.log(BasicLevel.DEBUG, "");
187
ejbContext = ctx;
188         try {
189             // Get initial Context
190
ictx = new InitialContext JavaDoc();
191             myEnv = (Context JavaDoc) ictx.lookup("java:comp/env");
192             // lookup filehome in JNDI
193
filehome = (FileHome) ictx.lookup("java:comp/env/ejb/file");
194         } catch (NamingException JavaDoc e) {
195             throw new EJBException JavaDoc("FolderSY: Cannot get filehome:" + e);
196         }
197         checkEnv("setSessionContext");
198     }
199
200     public void ejbRemove() {
201         //logger.log(BasicLevel.DEBUG, "");
202
checkEnv("ejbRemove");
203     }
204     public void ejbCreate() throws CreateException JavaDoc {
205         //logger.log(BasicLevel.DEBUG, "");
206
checkEnv("ejbCreate");
207     }
208     public void ejbPassivate() {
209         //logger.log(BasicLevel.DEBUG, "");
210
checkEnv("ejbPassivate");
211     }
212     public void ejbActivate() {
213         //logger.log(BasicLevel.DEBUG, "");
214
checkEnv("ejbActivate");
215     }
216
217     // ------------------------------------------------------------------
218
// SessionSynchronization implementation
219
// ------------------------------------------------------------------
220

221     public void afterBegin() {
222         //logger.log(BasicLevel.DEBUG, "");
223
checkEnv("afterBegin");
224     }
225     public void beforeCompletion() {
226         //logger.log(BasicLevel.DEBUG, "");
227
checkEnv("beforeCompletion");
228     }
229     public void afterCompletion(boolean committed) {
230         //logger.log(BasicLevel.DEBUG, "");
231
checkEnv("afterCompletion");
232     }
233
234     // ------------------------------------------------------------------
235
// Folder implementation
236
// ------------------------------------------------------------------
237

238     public File newFile(String JavaDoc fname) throws RemoteException JavaDoc, CreateException JavaDoc {
239         //logger.log(BasicLevel.DEBUG, "");
240
checkEnv("newFile");
241         File ret = filehome.create(fname);
242         return ret;
243     }
244
245     public File getFile(String JavaDoc fname) throws RemoteException JavaDoc, FinderException JavaDoc {
246         //logger.log(BasicLevel.DEBUG, "");
247
checkEnv("getFile");
248         File ret = filehome.findByPrimaryKey(fname);
249         return ret;
250     }
251
252     public void sendRef(Folder f) throws RemoteException JavaDoc {
253     }
254
255     public void sendInt(int i) throws RemoteException JavaDoc {
256     }
257
258     public void sendRefTS(Folder f) throws RemoteException JavaDoc {
259     }
260
261     public void sendIntTS(int i) throws RemoteException JavaDoc {
262     }
263
264     public Folder getRef(Folder f) throws RemoteException JavaDoc {
265         return f;
266     }
267
268     public int getInt(int i) throws RemoteException JavaDoc {
269         return i;
270     }
271
272     public Folder getRefTS(Folder f) throws RemoteException JavaDoc {
273         return f;
274     }
275
276     public int getIntTS(int i) throws RemoteException JavaDoc {
277         return i;
278     }
279 }
280
Popular Tags