1 45 package org.openejb.test.entity.bmp; 46 47 import java.rmi.RemoteException ; 48 import java.sql.Connection ; 49 import java.sql.PreparedStatement ; 50 import java.sql.ResultSet ; 51 import java.util.Hashtable ; 52 import java.util.Properties ; 53 import java.util.StringTokenizer ; 54 55 import javax.ejb.EJBException ; 56 import javax.ejb.EntityContext ; 57 import javax.ejb.FinderException ; 58 import javax.ejb.RemoveException ; 59 import javax.naming.InitialContext ; 60 import javax.sql.DataSource ; 61 62 import org.openejb.test.object.OperationsPolicy; 63 64 69 public class BasicBmp2DataSourcesBean implements javax.ejb.EntityBean { 70 71 public static int primaryKey = 1; 72 public String firstName; 73 public String lastName; 74 public EntityContext ejbContext; 75 public Hashtable allowedOperationsTable = new Hashtable (); 76 77 78 82 92 public int ejbHomeSum(int x, int y) { 93 testAllowedOperations("ejbHome"); 94 return x+y; 95 } 96 97 98 104 public java.util.Collection ejbFindEmptyCollection() 105 throws javax.ejb.FinderException , java.rmi.RemoteException { 106 return new java.util.Vector (); 107 } 108 109 116 public Integer ejbFindByPrimaryKey(Integer primaryKey) 117 throws javax.ejb.FinderException { 118 boolean found = false; 119 try{ 120 InitialContext jndiContext = new InitialContext ( ); 121 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase"); 122 Connection con = ds.getConnection(); 123 124 PreparedStatement stmt = con.prepareStatement("select * from entity where id = ?"); 125 stmt.setInt(1, primaryKey.intValue()); 126 found = stmt.executeQuery().next(); 127 con.close(); 128 }catch(Exception e){ 129 throw new FinderException ("FindByPrimaryKey failed"); 130 } 131 132 if(found) return primaryKey; 133 else throw new javax.ejb.ObjectNotFoundException (); 134 } 135 136 143 public Integer ejbCreate(String name) 144 throws javax.ejb.CreateException { 145 try{ 146 StringTokenizer st = new StringTokenizer (name, " "); 147 firstName = st.nextToken(); 148 lastName = st.nextToken(); 149 150 InitialContext jndiContext = new InitialContext ( ); 151 152 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase"); 153 154 Connection con = ds.getConnection(); 155 156 PreparedStatement stmt = con.prepareStatement("insert into entity (id, first_name, last_name) values (?,?,?)"); 158 stmt.setInt(1, primaryKey++); 159 stmt.setString(2, firstName); 160 stmt.setString(3, lastName); 161 stmt.executeUpdate(); 162 163 stmt = con.prepareStatement("select id from entity where first_name = ? AND last_name = ?"); 164 stmt.setString(1, firstName); 165 stmt.setString(2, lastName); 166 ResultSet set = stmt.executeQuery(); 167 while(set.next()) primaryKey = set.getInt("id"); 168 con.close(); 169 170 ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabaseBackup"); 172 173 con = ds.getConnection(); 174 175 stmt = con.prepareStatement("insert into entityBackup (id, first_name, last_name) values (?,?,?)"); 177 stmt.setInt(1, primaryKey); 178 stmt.setString(2, firstName); 179 stmt.setString(3, lastName); 180 stmt.executeUpdate(); 181 182 con.close(); 183 184 return new Integer (primaryKey); 185 186 }catch(Exception e){ 187 e.printStackTrace(); 188 throw new javax.ejb.CreateException ("can't create"); 189 } 190 } 191 192 public void ejbPostCreate(String name) 193 throws javax.ejb.CreateException { 194 } 195 196 200 201 205 210 public String businessMethod(String text){ 211 testAllowedOperations("businessMethod"); 212 StringBuffer b = new StringBuffer (text); 213 return b.reverse().toString(); 214 } 215 216 217 225 public Properties getPermissionsReport(){ 226 227 return null; 228 } 229 230 239 public OperationsPolicy getAllowedOperationsReport(String methodName){ 240 return (OperationsPolicy) allowedOperationsTable.get(methodName); 241 } 242 243 247 248 252 257 public void ejbLoad() throws EJBException ,RemoteException { 258 try{ 259 InitialContext jndiContext = new InitialContext ( ); 260 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase"); 261 Connection con = ds.getConnection(); 262 263 PreparedStatement stmt = con.prepareStatement("select * from entity where id = ?"); 264 Integer primaryKey = (Integer )ejbContext.getPrimaryKey(); 265 stmt.setInt(1, primaryKey.intValue()); 266 ResultSet rs = stmt.executeQuery(); 267 while(rs.next()){ 268 lastName = rs.getString("last_name"); 269 firstName = rs.getString("first_name"); 270 } 271 con.close(); 272 273 }catch(Exception e){ 274 e.printStackTrace(); 275 } 276 } 277 278 282 public void setEntityContext(EntityContext ctx) throws EJBException ,RemoteException { 283 ejbContext = ctx; 284 testAllowedOperations("setEntityContext"); 285 } 286 287 291 public void unsetEntityContext() throws EJBException ,RemoteException { 292 testAllowedOperations("unsetEntityContext"); 293 } 294 295 300 public void ejbStore() throws EJBException ,RemoteException { 301 try{ 302 InitialContext jndiContext = new InitialContext ( ); 303 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase"); 304 Connection con = ds.getConnection(); 305 306 PreparedStatement stmt = con.prepareStatement("update entity set first_name = ?, last_name = ? where EmployeeID = ?"); 307 stmt.setString(1, firstName); 308 stmt.setString(2, lastName); 309 stmt.setInt(3, primaryKey); 310 stmt.execute(); 311 con.close(); 312 }catch(Exception e){ 313 e.printStackTrace(); 314 } 315 } 316 317 325 public void ejbRemove() throws RemoveException ,EJBException ,RemoteException { 326 try{ 327 InitialContext jndiContext = new InitialContext ( ); 328 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase"); 329 Connection con = ds.getConnection(); 330 331 PreparedStatement stmt = con.prepareStatement("delete from entity where id = ?"); 332 Integer primaryKey = (Integer )ejbContext.getPrimaryKey(); 333 stmt.setInt(1, primaryKey.intValue()); 334 stmt.executeUpdate(); 335 con.close(); 336 337 }catch(Exception e){ 338 e.printStackTrace(); 339 throw new javax.ejb.EJBException (e); 340 } 341 } 342 343 349 public void ejbActivate() throws EJBException ,RemoteException { 350 testAllowedOperations("ejbActivate"); 351 } 352 353 359 public void ejbPassivate() throws EJBException ,RemoteException { 360 testAllowedOperations("ejbPassivate"); 361 } 362 366 protected void testAllowedOperations(String methodName){ 367 OperationsPolicy policy = new OperationsPolicy(); 368 369 370 try{ 371 ejbContext.getEJBHome(); 372 policy.allow(policy.Context_getEJBHome); 373 }catch(IllegalStateException ise){} 374 375 376 try{ 377 ejbContext.getCallerPrincipal(); 378 policy.allow( policy.Context_getCallerPrincipal ); 379 }catch(IllegalStateException ise){} 380 381 382 try{ 383 ejbContext.isCallerInRole("ROLE"); 384 policy.allow( policy.Context_isCallerInRole ); 385 }catch(IllegalStateException ise){} 386 387 388 try{ 389 ejbContext.getRollbackOnly(); 390 policy.allow( policy.Context_getRollbackOnly ); 391 }catch(IllegalStateException ise){} 392 393 394 try{ 395 ejbContext.setRollbackOnly(); 396 policy.allow( policy.Context_setRollbackOnly ); 397 }catch(IllegalStateException ise){} 398 399 400 try{ 401 ejbContext.getUserTransaction(); 402 policy.allow( policy.Context_getUserTransaction ); 403 }catch(Exception e){} 404 405 406 try{ 407 ejbContext.getEJBObject(); 408 policy.allow( policy.Context_getEJBObject ); 409 }catch(IllegalStateException ise){} 410 411 412 try{ 413 ejbContext.getPrimaryKey(); 414 policy.allow( policy.Context_getPrimaryKey ); 415 }catch(IllegalStateException ise){} 416 417 422 allowedOperationsTable.put(methodName, policy); 423 } 424 425 } 426 | Popular Tags |