001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 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: * --------------------------------------------------------------------------
022: * $Id: FolderSY.java 5996 2004-12-17 15:09:45Z joaninh $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.ffolder;
027:
028: import java.rmi.RemoteException;
029: import java.sql.Connection;
030: import java.sql.SQLException;
031: import javax.ejb.CreateException;
032: import javax.ejb.EJBException;
033: import javax.ejb.FinderException;
034: import javax.ejb.SessionBean;
035: import javax.ejb.SessionSynchronization;
036: import javax.ejb.SessionContext;
037: import javax.naming.Context;
038: import javax.naming.InitialContext;
039: import javax.naming.NamingException;
040: import javax.rmi.PortableRemoteObject;
041: import javax.sql.DataSource;
042:
043: /**
044: * FolderSY implementation
045: * This bean is a stateful session bean that implements SessionSynchronization
046: * @author Philippe Durieux, Philippe Coq
047: */
048: public class FolderSY implements SessionBean, SessionSynchronization {
049:
050: // protected static Logger logger = null;
051: SessionContext ejbContext;
052: InitialContext ictx;
053: Context myEnv;
054: FileHome filehome;
055:
056: /**
057: * Check environment variables
058: */
059: void checkEnv(String method) {
060:
061: // Check directly in my context
062: //logger.log(BasicLevel.DEBUG, "Check directly in my context");
063: try {
064: String value = (String) myEnv.lookup("myname");
065: if (!value.equals("mysession")) {
066: // logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: myname=" + value);
067: throw new EJBException("FolderSY 1: " + method);
068: }
069: } catch (NamingException e) {
070: //logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e);
071: throw new EJBException("FolderSY 2: " + method);
072: }
073: // Idem with compound name
074: //logger.log(BasicLevel.DEBUG, "Idem with compound name");
075: try {
076: String value = (String) myEnv.lookup("dir1/dir2/name");
077: if (!value.equals("sessionvalue")) {
078: // logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: dir1/dir2/name=" + value);
079: throw new EJBException("FolderSY 3: " + method);
080: }
081: } catch (NamingException e) {
082: //logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e);
083: throw new EJBException("FolderSY 4: " + method);
084: }
085: // Check from initial Context
086: //logger.log(BasicLevel.DEBUG, "Check from initial Context");
087: try {
088: String value = (String) ictx.lookup("java:comp/env/myname");
089: if (!value.equals("mysession")) {
090: //logger.log(BasicLevel.ERROR, ": ictx.lookup failed: myname=" + value);
091: throw new EJBException("FolderSY 6: " + method);
092: }
093: } catch (NamingException e) {
094: // logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
095: throw new EJBException("FolderSY 7: " + method);
096: }
097:
098: // Check datasource directly
099: //logger.log(BasicLevel.DEBUG, "Check datasource directly");
100: DataSource ds1 = null;
101: try {
102: ds1 = (DataSource) PortableRemoteObject.narrow(ictx
103: .lookup("jdbc_1"), DataSource.class);
104: } catch (NamingException e) {
105: //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
106: throw new EJBException("FolderSY 8: " + method);
107: }
108: Connection con = null;
109: if (!method.equals("afterCompletion")) {
110: try {
111: con = (Connection) ds1.getConnection();
112: if (con.isClosed()) {
113: //logger.log(BasicLevel.ERROR, ": connection is closed");
114: throw new EJBException("FolderSY 8a: " + method);
115: }
116: con.close();
117: } catch (SQLException e) {
118: // logger.log(BasicLevel.ERROR, ": getConnection:\n" + e);
119: throw new EJBException("FolderSY 8b: " + method);
120: }
121: }
122:
123: // Check DataSource from resource ref in bean environment
124: //logger.log(BasicLevel.DEBUG, "Check DataSource from resource ref");
125: DataSource ds2 = null;
126: try {
127: // The name is the one defined in FolderSY.xml
128: ds2 = (DataSource) myEnv.lookup("jdbc/mydb");
129: } catch (NamingException e) {
130: //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
131: throw new EJBException("FolderSY 9: " + method);
132: }
133: if (!method.equals("afterCompletion")) {
134: try {
135: con = (Connection) ds2.getConnection();
136: if (con.isClosed()) {
137: //logger.log(BasicLevel.ERROR, ": connection is closed");
138: throw new EJBException("FolderSY 9a: " + method);
139: }
140: con.close();
141: } catch (SQLException e) {
142: // logger.log(BasicLevel.ERROR, ": getConnection:\n" + e);
143: throw new EJBException("FolderSY 9b: " + method);
144: }
145: }
146:
147: // Check boolean values
148: //logger.log(BasicLevel.DEBUG, "Check boolean values");
149: try {
150: Boolean value = (Boolean) ictx
151: .lookup("java:comp/env/bVrai");
152: if (!value.booleanValue()) {
153: //logger.log(BasicLevel.ERROR, ": ictx.lookup failed: bVrai=" + value);
154: throw new EJBException("FolderSY 10a: " + method);
155: }
156: } catch (NamingException e) {
157: //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
158: throw new EJBException("FolderSY10a: " + method);
159: } catch (ClassCastException e) {
160: //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
161: throw new EJBException("FolderSY10a1: " + method);
162: }
163: try {
164: Boolean value = (Boolean) ictx
165: .lookup("java:comp/env/bFaux");
166: if (value.booleanValue()) {
167: // logger.log(BasicLevel.ERROR, ": ictx.lookup failed: bFaux=" + value);
168: throw new EJBException("FolderSY 10b: " + method);
169: }
170: } catch (NamingException e) {
171: //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
172: throw new EJBException("FolderSY10b: " + method);
173: } catch (ClassCastException e) {
174: //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
175: throw new EJBException("FolderSY10b1: " + method);
176: }
177:
178: //logger.log(BasicLevel.DEBUG, ": checkEnv OK");
179: }
180:
181: // ------------------------------------------------------------------
182: // SessionBean implementation
183: // ------------------------------------------------------------------
184:
185: public void setSessionContext(SessionContext ctx) {
186: // if (logger == null) {
187: // logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
188: //}
189: //logger.log(BasicLevel.DEBUG, "");
190: ejbContext = ctx;
191: try {
192: // Get initial Context
193: ictx = new InitialContext();
194: myEnv = (Context) ictx.lookup("java:comp/env");
195: // lookup filehome in JNDI
196: filehome = (FileHome) ictx.lookup("java:comp/env/ejb/file");
197: } catch (NamingException e) {
198: throw new EJBException("FolderSY: Cannot get filehome:" + e);
199: }
200: checkEnv("setSessionContext");
201: }
202:
203: public void ejbRemove() {
204: //logger.log(BasicLevel.DEBUG, "");
205: checkEnv("ejbRemove");
206: }
207:
208: public void ejbCreate() throws CreateException {
209: //logger.log(BasicLevel.DEBUG, "");
210: checkEnv("ejbCreate");
211: }
212:
213: public void ejbPassivate() {
214: //logger.log(BasicLevel.DEBUG, "");
215: checkEnv("ejbPassivate");
216: }
217:
218: public void ejbActivate() {
219: //logger.log(BasicLevel.DEBUG, "");
220: checkEnv("ejbActivate");
221: }
222:
223: // ------------------------------------------------------------------
224: // SessionSynchronization implementation
225: // ------------------------------------------------------------------
226:
227: public void afterBegin() {
228: //logger.log(BasicLevel.DEBUG, "");
229: checkEnv("afterBegin");
230: }
231:
232: public void beforeCompletion() {
233: //logger.log(BasicLevel.DEBUG, "");
234: checkEnv("beforeCompletion");
235: }
236:
237: public void afterCompletion(boolean committed) {
238: //logger.log(BasicLevel.DEBUG, "");
239: checkEnv("afterCompletion");
240: }
241:
242: // ------------------------------------------------------------------
243: // Folder implementation
244: // ------------------------------------------------------------------
245:
246: public File newFile(String fname) throws RemoteException,
247: CreateException {
248: //logger.log(BasicLevel.DEBUG, "");
249: checkEnv("newFile");
250: File ret = filehome.create(fname);
251: return ret;
252: }
253:
254: public File getFile(String fname) throws RemoteException,
255: FinderException {
256: //logger.log(BasicLevel.DEBUG, "");
257: checkEnv("getFile");
258: File ret = filehome.findByPrimaryKey(fname);
259: return ret;
260: }
261:
262: public void sendRef(Folder f) throws RemoteException {
263: }
264:
265: public void sendInt(int i) throws RemoteException {
266: }
267:
268: public void sendRefTS(Folder f) throws RemoteException {
269: }
270:
271: public void sendIntTS(int i) throws RemoteException {
272: }
273:
274: public Folder getRef(Folder f) throws RemoteException {
275: return f;
276: }
277:
278: public int getInt(int i) throws RemoteException {
279: return i;
280: }
281:
282: public Folder getRefTS(Folder f) throws RemoteException {
283: return f;
284: }
285:
286: public int getIntTS(int i) throws RemoteException {
287: return i;
288: }
289: }
|