KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sampleappli > EnvBean


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 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  * Initial developer(s): ____________________________________.
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: EnvBean.java,v 1.6 2004/04/19 06:39:30 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 // EnvBean.java
30
// Stateless Session bean
31

32 package sampleappli;
33
34 import java.rmi.RemoteException;
35 import java.sql.Connection;
36 import java.sql.SQLException;
37 import java.sql.Statement;
38 import java.io.File;
39 import javax.ejb.CreateException;
40 import javax.ejb.EJBException;
41 import javax.ejb.RemoveException;
42 import javax.ejb.EJBObject;
43 import javax.ejb.SessionBean;
44 import javax.ejb.SessionContext;
45 import javax.naming.Context;
46 import javax.naming.InitialContext;
47 import javax.naming.NamingException;
48 import javax.sql.DataSource;
49
50 /**
51  * EnvBean is a stateless session bean use to create and clean a correct
52  * environement for the running of the StockClient it creates/remove the
53  * StockTable and the order file
54  * @author JOnAS team
55  */

56 public class EnvBean implements SessionBean {
57
58     SessionContext ejbContext;
59
60     DataSource dataSource = null;
61
62     Connection conn = null;
63
64     Statement stmt;
65
66     String stocktablename = null;
67
68     String orderfilename = null;
69
70     // ------------------------------------------------------------------
71
// SessionBean implementation
72
// ------------------------------------------------------------------
73

74     /**
75      * Set the associated session context. The container calls this method after
76      * the instance creation. The enterprise Bean instance should store the
77      * reference to the context object in an instance variable. This method is
78      * called with no transaction context.
79      * @param sessionContext A SessionContext interface for the instance.
80      * @throws EJBException Thrown by the method to indicate a failure caused by
81      * a system-level error.
82      */

83     public void setSessionContext(SessionContext ctx) {
84         ejbContext = ctx;
85         if (dataSource == null) {
86             // Finds DataSource from JNDI
87
Context initialContext = null;
88             try {
89                 initialContext = new InitialContext();
90                 dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/myDS");
91             } catch (Exception e) {
92                 System.err.println(" new InitialContext() : " + e);
93                 throw new EJBException("Cannot get JNDI InitialContext");
94             }
95             // Find the stocktablename
96
try {
97                 stocktablename = (String) initialContext.lookup("java:comp/env/stocktablename");
98             } catch (Exception e) {
99                 System.err.println("cannot lookup environment " + e);
100                 throw new EJBException("cannot lookup environment");
101             }
102             // Find the orderfilename
103
try {
104                 orderfilename = (String) initialContext.lookup("java:comp/env/orderfilename");
105             } catch (Exception e) {
106                 System.err.println("cannot lookup environment " + e);
107                 throw new EJBException("cannot lookup environment");
108             }
109         }
110     }
111
112     /**
113      * A container invokes this method before it ends the life of the session
114      * object. This happens as a result of a client's invoking a remove
115      * operation, or when a container decides to terminate the session object
116      * after a timeout. This method is called with no transaction context.
117      * @throws EJBException Thrown by the method to indicate a failure caused by
118      * a system-level error.
119      */

120     public void ejbRemove() {
121     }
122
123     /**
124      * The Session bean must define 1 or more ejbCreate methods.
125      * @throws CreateException Failure to create a session EJB object.
126      */

127     public void ejbCreate() throws CreateException {
128     }
129
130     /**
131      * A container invokes this method on an instance before the instance
132      * becomes disassociated with a specific EJB object.
133      */

134     public void ejbPassivate() {
135     }
136
137     /**
138      * A container invokes this method when the instance is taken out of the
139      * pool of available instances to become associated with a specific EJB
140      * object.
141      */

142     public void ejbActivate() {
143     }
144
145     // ------------------------------------------------------------------
146
// Env implementation
147
// ------------------------------------------------------------------
148

149     /**
150      * init
151      */

152     public void init() {
153         createTableStock();
154         createOrderFile();
155     }
156
157     /**
158      * clean
159      */

160     public void clean() {
161         dropTableStock();
162         removeOrderFile();
163     }
164
165     // ------------------------------------------------------------------
166
// Env internal methods
167
// ------------------------------------------------------------------
168
public void dropTableStock() {
169         // drop table
170
try {
171             conn = dataSource.getConnection();
172             stmt = conn.createStatement();
173             stmt.execute("DROP TABLE " + stocktablename);
174             stmt.close();
175             conn.close();
176         } catch (Exception e) {
177             System.err.println("Exception in dropTable : \n" + stocktablename + " " + e);
178         }
179     }
180
181     public void createTableStock() {
182         // get connection
183
try {
184             conn = dataSource.getConnection();
185
186         } catch (Exception e) {
187             System.err.println("Cannot get Connection: \n" + e);
188             throw new EJBException("Cannot get Connection");
189         }
190         try {
191             stmt = conn.createStatement();
192             stmt.execute("DROP TABLE " + stocktablename);
193             stmt.close();
194         } catch (Exception e) {
195         }
196         // create table
197
try {
198             stmt = conn.createStatement();
199             stmt.execute("create table " + stocktablename + "(ID varchar(5) not null primary key, QUANTITY integer)");
200             stmt.execute("insert into " + stocktablename + " values ('00000', 10)");
201             stmt.execute("insert into " + stocktablename + " values ('00001', 20)");
202             stmt.execute("insert into " + stocktablename + " values ('00002', 10)");
203             stmt.execute("insert into " + stocktablename + " values ('00003', 20)");
204             stmt.execute("insert into " + stocktablename + " values ('00004', 10)");
205             stmt.close();
206             conn.close();
207         } catch (SQLException e) {
208             System.err.println("Exception in createTable : " + e);
209             throw new EJBException("Exception in createTable");
210         }
211     }
212
213     public void createOrderFile() {
214         try {
215             File f = new File(System.getProperty("java.io.tmpdir") + File.separator + System.getProperty("user.name")
216                     + "_" + orderfilename);
217             if (!f.createNewFile()) {
218                 f.delete();
219                 f.createNewFile();
220             }
221         } catch (Exception ex) {
222             System.err.println("Exception on createOrderFile: " + ex);
223             throw new EJBException("Exception in createOrderFile");
224         }
225
226     }
227
228     public void removeOrderFile() {
229         try {
230             File f = new File(System.getProperty("java.io.tmpdir") + File.separator + System.getProperty("user.name")
231                     + "_" + orderfilename);
232             f.delete();
233         } catch (Exception ex) {
234             System.err.println("Exception on removeOrderFile: " + ex);
235             throw new EJBException("Exception in removeOrderFile");
236         }
237     }
238 }
Popular Tags