1 25 26 package lb; 27 28 import java.rmi.RemoteException; 29 import java.sql.Connection; 30 import java.sql.SQLException; 31 import java.sql.Statement; 32 import java.util.Collection; 33 import java.util.Iterator; 34 35 import javax.ejb.CreateException; 36 import javax.ejb.EJBException; 37 import javax.ejb.FinderException; 38 import javax.ejb.SessionBean; 39 import javax.ejb.SessionContext; 40 import javax.ejb.TransactionRolledbackLocalException; 41 import javax.naming.Context; 42 import javax.naming.InitialContext; 43 import javax.naming.NamingException; 44 import javax.sql.DataSource; 45 46 50 public class ManagerSF implements SessionBean { 51 52 SessionContext ejbContext; 53 54 ManacLocalHome manacLocalHome = null; 55 56 int c1 = 0; 57 58 int d1 = 0; 59 60 ManacLocal cred1, deb1; 61 62 int initialValue = 1000; 63 64 int value = 10; 65 66 private void initDB() { 70 71 DataSource ds = null; 73 InitialContext ictx = null; 74 try { 75 ictx = new InitialContext(); 76 } catch (Exception e) { 77 System.out.println("new InitialContext() : " + e); 78 throw new EJBException("Cannot get JNDI InitialContext"); 79 } 80 try { 81 ds = (DataSource) ictx.lookup("java:comp/env/jdbc/mydb"); 82 } catch (Exception e) { 83 System.out.println("cannot lookup datasource " + e); 84 throw new EJBException("cannot lookup datasource"); 85 } 86 87 Connection conn = null; 89 Statement stmt = null; 90 String myTable = "manacsample"; 92 try { 93 conn = ds.getConnection(); 94 stmt = conn.createStatement(); 95 stmt.execute("drop table " + myTable); 96 stmt.close(); 97 } catch (SQLException e) { 98 } 100 101 try { 103 stmt = conn.createStatement(); 104 stmt.execute("create table " + myTable 105 + "(c_name varchar(30) not null primary key, c_num integer, c_balance integer)"); 106 stmt.close(); 107 conn.close(); 108 } catch (SQLException e) { 109 System.out.println("Exception in createTable : " + e); 110 throw new EJBException("Exception in createTable"); 111 } 112 } 113 114 118 127 public void setSessionContext(SessionContext ctx) { 128 ejbContext = ctx; 129 } 130 131 139 public void ejbRemove() { 140 } 141 142 146 public void ejbCreate(int ival) throws CreateException { 147 148 try { 150 Context ictx = new InitialContext(); 151 manacLocalHome = (ManacLocalHome) ictx.lookup("java:comp/env/ejb/manac"); 152 } catch (NamingException e) { 153 System.out.println("ManagerSF : Cannot get ManacLocalHome:" + e); 154 throw new CreateException("Cannot get ManacLocalHome"); 155 } 156 157 initialValue = ival; 158 } 159 160 164 public void ejbPassivate() { 165 } 166 167 172 public void ejbActivate() { 173 } 174 175 179 public void createAll(int nb) throws RemoteException { 180 181 initDB(); 183 184 for (int i = 0; i < nb; i++) { 186 try { 187 manacLocalHome.createWithDefaultName(i, initialValue); 188 } catch (CreateException e) { 189 System.out.println("createAll:\n" + e); 190 throw new RemoteException("Cannot create Manac"); 191 } 192 } 193 } 194 195 public void setAccounts(int d1, int c1) throws RemoteException { 196 this.d1 = d1; 197 this.c1 = c1; 198 try { 199 deb1 = manacLocalHome.findByNum(d1); 200 cred1 = manacLocalHome.findByNum(c1); 201 } catch (FinderException e) { 202 System.out.println("Cannot find manac bean:" + e); 203 throw new RemoteException("Cannot find manac bean"); 204 } 205 } 206 207 public void setValue(int v) throws RemoteException { 208 this.value = v; 209 } 210 211 public void movement() throws RemoteException { 212 213 try { 216 cred1.credit(value); 217 } catch (EJBException e) { 218 System.out.println("ManagerSF: Cannot credit account:" + e); 219 throw new RemoteException("ManagerSF: Cannot credit account"); 220 } 221 222 try { 224 deb1.debit(value); 225 } catch (TransactionRolledbackLocalException e) { 226 System.out.println("ManagerSF: Rollback transaction"); 227 } catch (EJBException e) { 228 System.out.println("ManagerSF debit:" + e); 229 } 230 } 231 232 public boolean checkAccount(int a) throws RemoteException { 233 234 boolean ret = false; 235 ManacLocal m = null; 236 237 try { 238 m = manacLocalHome.findByNum(a); 239 int b = m.getBalance(); 240 if (b >= 0) { 241 ret = true; 242 } else { 243 System.out.println("ManagerSF checkAccount: bad balance=" + b); 244 } 245 return ret; 246 } catch (Exception e) { 247 System.out.println("ManagerSF checkAccount: cannot check account: " + e); 248 return false; 249 } 250 } 251 252 public boolean checkAll() throws RemoteException { 253 254 int count = 0; 255 int total = 0; 256 try { 257 Collection accCol = manacLocalHome.findAll(); 258 for (Iterator accIter = accCol.iterator(); accIter.hasNext();) { 259 count++; 260 ManacLocal a = (ManacLocal) accIter.next(); 261 int balance = a.getBalance(); 262 if (balance < 0) { 263 System.out.println("checkAllAccounts: bad balance: " + balance); 264 return false; 265 } 266 String name = a.getName(); 267 total += balance; 268 } 269 } catch (Exception e) { 270 System.out.println("checkAllAccounts:" + e); 271 return false; 272 } 273 int exp = initialValue * count; 274 if (total != exp) { 275 System.out.println("checkAllAccounts: bad total: " + total + " (expected: " + exp + ")"); 276 return false; 277 } 278 return true; 279 } 280 281 } | Popular Tags |