001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 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: * Initial developer(s): ____________________________________.
022: * Contributor(s): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: EnvBean.java 4618 2004-04-19 06:39:30Z benoitf $
026: * --------------------------------------------------------------------------
027: */package sampleappli;
028:
029: import java.rmi.RemoteException;
030: import java.sql.Connection;
031: import java.sql.SQLException;
032: import java.sql.Statement;
033: import java.io.File;
034: import javax.ejb.CreateException;
035: import javax.ejb.EJBException;
036: import javax.ejb.RemoveException;
037: import javax.ejb.EJBObject;
038: import javax.ejb.SessionBean;
039: import javax.ejb.SessionContext;
040: import javax.naming.Context;
041: import javax.naming.InitialContext;
042: import javax.naming.NamingException;
043: import javax.sql.DataSource;
044:
045: /**
046: * EnvBean is a stateless session bean use to create and clean a correct
047: * environement for the running of the StockClient it creates/remove the
048: * StockTable and the order file
049: * @author JOnAS team
050: */
051: public class EnvBean implements SessionBean {
052:
053: SessionContext ejbContext;
054:
055: DataSource dataSource = null;
056:
057: Connection conn = null;
058:
059: Statement stmt;
060:
061: String stocktablename = null;
062:
063: String orderfilename = null;
064:
065: // ------------------------------------------------------------------
066: // SessionBean implementation
067: // ------------------------------------------------------------------
068:
069: /**
070: * Set the associated session context. The container calls this method after
071: * the instance creation. The enterprise Bean instance should store the
072: * reference to the context object in an instance variable. This method is
073: * called with no transaction context.
074: * @param sessionContext A SessionContext interface for the instance.
075: * @throws EJBException Thrown by the method to indicate a failure caused by
076: * a system-level error.
077: */
078: public void setSessionContext(SessionContext ctx) {
079: ejbContext = ctx;
080: if (dataSource == null) {
081: // Finds DataSource from JNDI
082: Context initialContext = null;
083: try {
084: initialContext = new InitialContext();
085: dataSource = (DataSource) initialContext
086: .lookup("java:comp/env/jdbc/myDS");
087: } catch (Exception e) {
088: System.err.println(" new InitialContext() : " + e);
089: throw new EJBException("Cannot get JNDI InitialContext");
090: }
091: // Find the stocktablename
092: try {
093: stocktablename = (String) initialContext
094: .lookup("java:comp/env/stocktablename");
095: } catch (Exception e) {
096: System.err.println("cannot lookup environment " + e);
097: throw new EJBException("cannot lookup environment");
098: }
099: // Find the orderfilename
100: try {
101: orderfilename = (String) initialContext
102: .lookup("java:comp/env/orderfilename");
103: } catch (Exception e) {
104: System.err.println("cannot lookup environment " + e);
105: throw new EJBException("cannot lookup environment");
106: }
107: }
108: }
109:
110: /**
111: * A container invokes this method before it ends the life of the session
112: * object. This happens as a result of a client's invoking a remove
113: * operation, or when a container decides to terminate the session object
114: * after a timeout. This method is called with no transaction context.
115: * @throws EJBException Thrown by the method to indicate a failure caused by
116: * a system-level error.
117: */
118: public void ejbRemove() {
119: }
120:
121: /**
122: * The Session bean must define 1 or more ejbCreate methods.
123: * @throws CreateException Failure to create a session EJB object.
124: */
125: public void ejbCreate() throws CreateException {
126: }
127:
128: /**
129: * A container invokes this method on an instance before the instance
130: * becomes disassociated with a specific EJB object.
131: */
132: public void ejbPassivate() {
133: }
134:
135: /**
136: * A container invokes this method when the instance is taken out of the
137: * pool of available instances to become associated with a specific EJB
138: * object.
139: */
140: public void ejbActivate() {
141: }
142:
143: // ------------------------------------------------------------------
144: // Env implementation
145: // ------------------------------------------------------------------
146:
147: /**
148: * init
149: */
150: public void init() {
151: createTableStock();
152: createOrderFile();
153: }
154:
155: /**
156: * clean
157: */
158: public void clean() {
159: dropTableStock();
160: removeOrderFile();
161: }
162:
163: // ------------------------------------------------------------------
164: // Env internal methods
165: // ------------------------------------------------------------------
166: public void dropTableStock() {
167: // drop table
168: try {
169: conn = dataSource.getConnection();
170: stmt = conn.createStatement();
171: stmt.execute("DROP TABLE " + stocktablename);
172: stmt.close();
173: conn.close();
174: } catch (Exception e) {
175: System.err.println("Exception in dropTable : \n"
176: + stocktablename + " " + e);
177: }
178: }
179:
180: public void createTableStock() {
181: // get connection
182: try {
183: conn = dataSource.getConnection();
184:
185: } catch (Exception e) {
186: System.err.println("Cannot get Connection: \n" + e);
187: throw new EJBException("Cannot get Connection");
188: }
189: try {
190: stmt = conn.createStatement();
191: stmt.execute("DROP TABLE " + stocktablename);
192: stmt.close();
193: } catch (Exception e) {
194: }
195: // create table
196: try {
197: stmt = conn.createStatement();
198: stmt
199: .execute("create table "
200: + stocktablename
201: + "(ID varchar(5) not null primary key, QUANTITY integer)");
202: stmt.execute("insert into " + stocktablename
203: + " values ('00000', 10)");
204: stmt.execute("insert into " + stocktablename
205: + " values ('00001', 20)");
206: stmt.execute("insert into " + stocktablename
207: + " values ('00002', 10)");
208: stmt.execute("insert into " + stocktablename
209: + " values ('00003', 20)");
210: stmt.execute("insert into " + stocktablename
211: + " values ('00004', 10)");
212: stmt.close();
213: conn.close();
214: } catch (SQLException e) {
215: System.err.println("Exception in createTable : " + e);
216: throw new EJBException("Exception in createTable");
217: }
218: }
219:
220: public void createOrderFile() {
221: try {
222: File f = new File(System.getProperty("java.io.tmpdir")
223: + File.separator + System.getProperty("user.name")
224: + "_" + orderfilename);
225: if (!f.createNewFile()) {
226: f.delete();
227: f.createNewFile();
228: }
229: } catch (Exception ex) {
230: System.err.println("Exception on createOrderFile: " + ex);
231: throw new EJBException("Exception in createOrderFile");
232: }
233:
234: }
235:
236: public void removeOrderFile() {
237: try {
238: File f = new File(System.getProperty("java.io.tmpdir")
239: + File.separator + System.getProperty("user.name")
240: + "_" + orderfilename);
241: f.delete();
242: } catch (Exception ex) {
243: System.err.println("Exception on removeOrderFile: " + ex);
244: throw new EJBException("Exception in removeOrderFile");
245: }
246: }
247: }
|