1 45 package org.openejb.test.beans; 46 47 import java.sql.Connection ; 48 import java.sql.ResultSet ; 49 import java.sql.SQLException ; 50 import java.sql.Statement ; 51 52 import javax.ejb.SessionBean ; 53 import javax.ejb.SessionContext ; 54 import javax.naming.Context ; 55 import javax.naming.InitialContext ; 56 57 public class ShoppingCartBean implements SessionBean , javax.ejb.SessionSynchronization { 58 59 String name; 60 SessionContext context; 61 Context jndiContext; 62 Context envContext; 63 Boolean useJdbc = Boolean.FALSE; 64 65 public void ejbCreate(String name)throws javax.ejb.CreateException { 66 try{ 68 69 jndiContext = new InitialContext (); 70 71 String author = (String )jndiContext.lookup("java:comp/env/author"); 72 73 Double price = (Double )jndiContext.lookup("java:comp/env/price"); 74 75 }catch(javax.naming.NamingException re){ 76 throw new RuntimeException ("Using JNDI failed"); 77 } 78 79 } 80 public Calculator getCalculator(){ 81 82 try{ 83 84 boolean test = context.isCallerInRole("TheMan"); 85 86 jndiContext = new InitialContext ( ); 87 88 CalculatorHome home = (CalculatorHome)jndiContext.lookup("java:comp/env/ejb/calculator"); 89 Calculator calc = home.create(); 90 return calc; 91 92 }catch(java.rmi.RemoteException re){ 93 throw new RuntimeException ("Getting calulator bean failed"); 94 }catch(javax.naming.NamingException re){ 95 throw new RuntimeException ("Using JNDI failed"); 96 } 97 98 99 } 100 public void doJdbcCall(){ 101 102 Connection con = null; 103 try{ 104 105 javax.sql.DataSource ds = 106 (javax.sql.DataSource )jndiContext.lookup("java:comp/env/jdbc/orders"); 107 108 con = ds.getConnection(); 109 110 111 Statement stmt = con.createStatement(); 112 ResultSet rs = stmt.executeQuery("select * from Employees"); 113 while(rs.next()) 114 System.out.println(rs.getString(2)); 115 116 Calculator calc = getCalculator(); 117 calc.add(1, 1); 118 calc.sub(1,2); 119 120 int i = 1; 121 122 }catch(java.rmi.RemoteException re){ 123 throw new RuntimeException ("Accessing Calculator bean failed"); 124 }catch(javax.naming.NamingException ne){ 125 throw new RuntimeException ("Using JNDI failed"); 126 }catch(java.sql.SQLException se){ 127 throw new RuntimeException ("Getting JDBC data source failed"); 128 }finally{ 129 if(con!=null){ 130 try{ 131 con.close(); 132 }catch(SQLException se){se.printStackTrace();} 133 } 134 } 135 136 } 137 138 public String getName( ){ 139 140 return name; 141 } 142 public void setName(String name){ 143 this.name = name; 145 } 146 147 public void setSessionContext(SessionContext cntx){ 148 context = cntx; 149 } 151 152 public void ejbActivate( ){ 153 } 155 156 public void ejbPassivate( ){ 157 } 159 public void ejbRemove( ){ 160 } 162 163 public void afterBegin( ){ 164 } 166 public void beforeCompletion(){ 167 } 169 public void afterCompletion(boolean commit){ 170 } 172 private void testAllowedOperations(String methodName){ 173 System.out.println("******************************************************"); 174 System.out.println("\nTesting Allowed Operations for "+methodName+"() method\n"); 175 try{ 176 context.getEJBObject(); 177 System.out.println("SessionContext.getEJBObject() ... Allowed"); 178 }catch(IllegalStateException ise){ 179 System.out.println("SessionContext.getEJBObject() ... Failed"); 180 } 181 try{ 182 context.getEJBHome(); 183 System.out.println("SessionContext.getEJBHome() ... Allowed"); 184 }catch(IllegalStateException ise){ 185 System.out.println("SessionContext.getEJBHome() ... Failed"); 186 } 187 try{ 188 context.getCallerPrincipal(); 189 System.out.println("SessionContext.getCallerPrincipal() ... Allowed"); 190 }catch(IllegalStateException ise){ 191 System.out.println("SessionContext.getCallerPrincipal() ... Failed"); 192 } 193 try{ 194 context.isCallerInRole("ROLE"); 195 System.out.println("SessionContext.isCallerInRole() ... Allowed"); 196 }catch(IllegalStateException ise){ 197 System.out.println("SessionContext.isCallerInRole() ... Failed"); 198 } 199 try{ 200 context.getRollbackOnly(); 201 System.out.println("SessionContext.getRollbackOnly() ... Allowed"); 202 }catch(IllegalStateException ise){ 203 System.out.println("SessionContext.getRollbackOnly() ... Failed"); 204 } 205 try{ 206 context.setRollbackOnly(); 207 System.out.println("SessionContext.setRollbackOnly() ... Allowed"); 208 }catch(IllegalStateException ise){ 209 System.out.println("SessionContext.setRollbackOnly() ... Failed"); 210 } 211 try{ 212 context.getUserTransaction(); 213 System.out.println("SessionContext.getUserTransaction() ... Allowed"); 214 }catch(IllegalStateException ise){ 215 System.out.println("SessionContext.getUserTransaction() ... Failed"); 216 } 217 } 218 } 219 | Popular Tags |