1 28 29 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 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 74 83 public void setSessionContext(SessionContext ctx) { 84 ejbContext = ctx; 85 if (dataSource == null) { 86 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 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 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 120 public void ejbRemove() { 121 } 122 123 127 public void ejbCreate() throws CreateException { 128 } 129 130 134 public void ejbPassivate() { 135 } 136 137 142 public void ejbActivate() { 143 } 144 145 149 152 public void init() { 153 createTableStock(); 154 createOrderFile(); 155 } 156 157 160 public void clean() { 161 dropTableStock(); 162 removeOrderFile(); 163 } 164 165 public void dropTableStock() { 169 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 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 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 |