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.ApplicationException; 63 import org.openejb.test.object.OperationsPolicy; 64 65 70 public class BasicBmpBean implements javax.ejb.EntityBean { 71 72 public int primaryKey; 74 public String firstName; 75 public String lastName; 76 public EntityContext ejbContext; 77 public Hashtable allowedOperationsTable = new Hashtable (); 78 79 80 84 94 public int ejbHomeSum(int x, int y) { 95 testAllowedOperations("ejbHome"); 96 return x+y; 97 } 98 99 100 108 public java.util.Collection ejbFindEmptyCollection() 109 throws javax.ejb.FinderException , java.rmi.RemoteException { 110 return new java.util.Vector (); 111 } 112 113 119 public java.util.Enumeration ejbFindEmptyEnumeration() 120 throws javax.ejb.FinderException 121 { 122 return (new java.util.Vector ()).elements(); 123 } 124 125 133 public Integer ejbFindByPrimaryKey(Integer primaryKey) 134 throws javax.ejb.FinderException { 135 boolean found = false; 136 try { 137 InitialContext jndiContext = new InitialContext ( ); 138 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase"); 139 Connection con = ds.getConnection(); 140 141 PreparedStatement stmt = con.prepareStatement("select * from entity where id = ?"); 142 stmt.setInt(1, primaryKey.intValue()); 143 found = stmt.executeQuery().next(); 144 con.close(); 145 } catch ( Exception e ) { 146 throw new FinderException ("FindByPrimaryKey failed"); 147 } 148 149 if ( found ) return primaryKey; 150 else throw new javax.ejb.ObjectNotFoundException (); 151 } 152 153 161 public java.util.Collection ejbFindByLastName(String lastName) 162 throws javax.ejb.FinderException { 163 java.util.Vector keys = new java.util.Vector (); 164 try { 165 InitialContext jndiContext = new InitialContext ( ); 166 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase"); 167 Connection con = ds.getConnection(); 168 169 PreparedStatement stmt = con.prepareStatement("SELECT id FROM entity WHERE last_name = ?"); 170 stmt.setString(1, lastName); 171 172 ResultSet set = stmt.executeQuery(); 173 174 while ( set.next() ) keys.add( new Integer (set.getInt("id")) ); 175 con.close(); 176 } catch ( Exception e ) { 177 throw new FinderException ("FindByPrimaryKey failed"); 178 } 179 180 if ( keys.size() > 0 ) return keys; 181 else throw new javax.ejb.ObjectNotFoundException (); 182 } 183 191 public Integer ejbCreate(String name) 192 throws javax.ejb.CreateException { 193 try { 194 StringTokenizer st = new StringTokenizer (name, " "); 195 firstName = st.nextToken(); 196 lastName = st.nextToken(); 197 198 InitialContext jndiContext = new InitialContext ( ); 199 200 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase"); 201 202 Connection con = ds.getConnection(); 203 204 PreparedStatement stmt = con.prepareStatement("insert into entity (first_name, last_name) values (?,?)"); 211 stmt.setString(1, firstName); 212 stmt.setString(2, lastName); 213 stmt.executeUpdate(); 214 215 stmt = con.prepareStatement("select id from entity where first_name = ? AND last_name = ?"); 216 stmt.setString(1, firstName); 217 stmt.setString(2, lastName); 218 ResultSet set = stmt.executeQuery(); 219 while ( set.next() ) primaryKey = set.getInt("id"); 220 con.close(); 221 222 return new Integer (primaryKey); 223 224 } catch ( Exception e ) { 225 e.printStackTrace(); 226 throw new javax.ejb.CreateException ("can't create: "+e.getClass().getName()+" "+e.getMessage()); 227 } 228 } 229 230 public void ejbPostCreate(String name) 231 throws javax.ejb.CreateException { 232 } 233 234 238 239 243 249 public String businessMethod(String text) { 250 testAllowedOperations("businessMethod"); 251 StringBuffer b = new StringBuffer (text); 252 return b.reverse().toString(); 253 } 254 255 256 260 public void throwApplicationException() throws ApplicationException{ 261 throw new ApplicationException("Testing ability to throw Application Exceptions"); 262 } 263 264 271 public void throwSystemException_NullPointer() { 272 throw new NullPointerException ("Testing ability to throw System Exceptions"); 273 } 274 275 276 285 public Properties getPermissionsReport() { 286 287 return null; 288 } 289 290 300 public OperationsPolicy getAllowedOperationsReport(String methodName) { 301 return(OperationsPolicy) allowedOperationsTable.get(methodName); 302 } 303 304 308 309 313 318 public void ejbLoad() throws EJBException ,RemoteException { 319 try { 320 InitialContext jndiContext = new InitialContext ( ); 321 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase"); 322 Connection con = ds.getConnection(); 323 324 PreparedStatement stmt = con.prepareStatement("select * from entity where id = ?"); 325 Integer primaryKey = (Integer )ejbContext.getPrimaryKey(); 326 stmt.setInt(1, primaryKey.intValue()); 327 ResultSet rs = stmt.executeQuery(); 328 while ( rs.next() ) { 329 lastName = rs.getString("last_name"); 330 firstName = rs.getString("first_name"); 331 } 332 con.close(); 333 334 } catch ( Exception e ) { 335 e.printStackTrace(); 336 } 337 } 338 339 343 public void setEntityContext(EntityContext ctx) throws EJBException ,RemoteException { 344 ejbContext = ctx; 345 testAllowedOperations("setEntityContext"); 346 } 347 348 352 public void unsetEntityContext() throws EJBException ,RemoteException { 353 testAllowedOperations("unsetEntityContext"); 354 } 355 356 361 public void ejbStore() throws EJBException ,RemoteException { 362 try { 363 InitialContext jndiContext = new InitialContext ( ); 364 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase"); 365 Connection con = ds.getConnection(); 366 367 PreparedStatement stmt = con.prepareStatement("update entity set first_name = ?, last_name = ? where id = ?"); 368 stmt.setString(1, firstName); 369 stmt.setString(2, lastName); 370 stmt.setInt(3, primaryKey); 371 stmt.execute(); 372 con.close(); 373 } catch ( Exception e ) { 374 e.printStackTrace(); 375 } 376 } 377 378 386 public void ejbRemove() throws RemoveException ,EJBException ,RemoteException { 387 try { 388 InitialContext jndiContext = new InitialContext ( ); 389 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase"); 390 Connection con = ds.getConnection(); 391 392 PreparedStatement stmt = con.prepareStatement("delete from entity where id = ?"); 393 Integer primaryKey = (Integer )ejbContext.getPrimaryKey(); 394 stmt.setInt(1, primaryKey.intValue()); 395 stmt.executeUpdate(); 396 con.close(); 397 398 } catch ( Exception e ) { 399 e.printStackTrace(); 400 throw new javax.ejb.EJBException (e); 401 } 402 } 403 404 410 public void ejbActivate() throws EJBException ,RemoteException { 411 testAllowedOperations("ejbActivate"); 412 } 413 414 420 public void ejbPassivate() throws EJBException ,RemoteException { 421 testAllowedOperations("ejbPassivate"); 422 } 423 424 425 429 protected void testAllowedOperations(String methodName) { 430 OperationsPolicy policy = new OperationsPolicy(); 431 432 433 try { 434 ejbContext.getEJBHome(); 435 policy.allow(policy.Context_getEJBHome); 436 } catch ( IllegalStateException ise ) { 437 } 438 439 440 try { 441 ejbContext.getCallerPrincipal(); 442 policy.allow( policy.Context_getCallerPrincipal ); 443 } catch ( IllegalStateException ise ) { 444 } 445 446 447 try { 448 ejbContext.isCallerInRole("ROLE"); 449 policy.allow( policy.Context_isCallerInRole ); 450 } catch ( IllegalStateException ise ) { 451 } 452 453 454 try { 455 ejbContext.getRollbackOnly(); 456 policy.allow( policy.Context_getRollbackOnly ); 457 } catch ( IllegalStateException ise ) { 458 } 459 460 461 try { 462 ejbContext.setRollbackOnly(); 463 policy.allow( policy.Context_setRollbackOnly ); 464 } catch ( IllegalStateException ise ) { 465 } 466 467 468 try { 469 ejbContext.getUserTransaction(); 470 policy.allow( policy.Context_getUserTransaction ); 471 } catch ( IllegalStateException ise ) { 472 } 473 474 475 try { 476 ejbContext.getEJBObject(); 477 policy.allow( policy.Context_getEJBObject ); 478 } catch ( IllegalStateException ise ) { 479 } 480 481 485 486 487 try { 488 InitialContext jndiContext = new InitialContext (); 489 490 String actual = (String )jndiContext.lookup("java:comp/env/stateless/references/JNDI_access_to_java_comp_env"); 491 492 policy.allow( policy.JNDI_access_to_java_comp_env ); 493 } catch ( IllegalStateException ise ) { 494 } catch ( javax.naming.NamingException ne ) { 495 } 496 497 498 try { 499 InitialContext jndiContext = new InitialContext ( ); 500 501 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/stateless/references/Resource_manager_access"); 502 503 policy.allow( policy.Resource_manager_access ); 504 } catch ( IllegalStateException ise ) { 505 } catch ( javax.naming.NamingException ne ) { 506 } 507 508 509 try { 510 InitialContext jndiContext = new InitialContext ( ); 511 512 Object obj = jndiContext.lookup("java:comp/env/stateless/beanReferences/Enterprise_bean_access"); 513 514 policy.allow( policy.Enterprise_bean_access ); 515 } catch ( IllegalStateException ise ) { 516 } catch ( javax.naming.NamingException ne ) { 517 } 518 519 allowedOperationsTable.put(methodName, policy); 520 } 521 522 } 523 | Popular Tags |