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.SessionContext ; 53 import javax.naming.InitialContext ; 54 public class CalculatorBean implements javax.ejb.SessionBean { 55 56 public SessionContext context; 57 public InitialContext jndiContext; 58 boolean testCreate, testAdd, testSub, testSetSessionContext, testRemove; 59 60 public void ejbCreate( ){ 61 } 62 63 public int add(int a, int b){ 64 return a+b; 65 } 66 protected void doJdbcCall(){ 67 68 Connection con = null; 69 try{ 70 71 javax.sql.DataSource ds = 72 (javax.sql.DataSource )jndiContext.lookup("java:comp/env/jdbc/mydb"); 73 74 con = ds.getConnection(); 75 76 77 Statement stmt = con.createStatement(); 78 ResultSet rs = stmt.executeQuery("select * from Employees"); 79 while(rs.next()) 80 System.out.println(rs.getString(2)); 81 82 }catch(javax.naming.NamingException re){ 83 throw new RuntimeException ("Using JNDI failed"); 84 }catch(java.sql.SQLException se){ 85 throw new RuntimeException ("Getting JDBC data source failed"); 86 }finally{ 87 if(con!=null){ 88 try{ 89 con.close(); 90 }catch(SQLException se){se.printStackTrace();} 91 } 92 } 93 94 } 95 96 public int sub(int a, int b){ 97 return a-b; 98 } 99 100 public void ejbPassivate( ){ 101 } 103 public void ejbActivate(){ 104 } 106 public void ejbRemove(){ 107 if (testRemove) testAllowedOperations("ejbRemove"); 108 } 109 public void setSessionContext(javax.ejb.SessionContext cntx){ 110 context = cntx; 111 if (testSetSessionContext) testAllowedOperations("setSessionContext"); 112 113 } 114 115 private void testAllowedOperations(String methodName){ 116 System.out.println("******************************************************"); 117 System.out.println("\nTesting Allowed Operations for "+methodName+"() method\n"); 118 try{ 119 context.getEJBObject(); 120 System.out.println("SessionContext.getEJBObject() ......... Allowed"); 121 }catch(IllegalStateException ise){ 122 System.out.println("SessionContext.getEJBObject() ......... Failed"); 123 } 124 try{ 125 context.getEJBHome(); 126 System.out.println("SessionContext.getEJBHome() ........... Allowed"); 127 }catch(IllegalStateException ise){ 128 System.out.println("SessionContext.getEJBHome() ........... Failed"); 129 } 130 try{ 131 context.getCallerPrincipal(); 132 System.out.println("SessionContext.getCallerPrincipal() ... Allowed"); 133 }catch(IllegalStateException ise){ 134 System.out.println("SessionContext.getCallerPrincipal() ... Failed"); 135 } 136 try{ 137 context.isCallerInRole("ROLE"); 138 System.out.println("SessionContext.isCallerInRole() ....... Allowed"); 139 }catch(IllegalStateException ise){ 140 System.out.println("SessionContext.isCallerInRole() ....... Failed"); 141 } 142 try{ 143 context.getRollbackOnly(); 144 System.out.println("SessionContext.getRollbackOnly() ...... Allowed"); 145 }catch(IllegalStateException ise){ 146 System.out.println("SessionContext.getRollbackOnly() ...... Failed"); 147 } 148 try{ 149 context.setRollbackOnly(); 150 System.out.println("SessionContext.setRollbackOnly() ...... Allowed"); 151 }catch(IllegalStateException ise){ 152 System.out.println("SessionContext.setRollbackOnly() ...... Failed"); 153 } 154 try{ 155 context.getUserTransaction(); 156 System.out.println("SessionContext.getUserTransaction() ... Allowed"); 157 }catch(IllegalStateException ise){ 158 System.out.println("SessionContext.getUserTransaction() ... Failed"); 159 } 160 } 161 162 } 163 | Popular Tags |