1 22 package org.jboss.ejb.plugins; 23 24 import org.jboss.ejb.Container; 25 import org.jboss.ejb.EntityCache; 26 import org.jboss.ejb.EntityContainer; 27 import org.jboss.ejb.EntityEnterpriseContext; 28 import org.jboss.ejb.EntityPersistenceManager; 29 import org.jboss.ejb.AllowedOperationsAssociation; 30 import org.jboss.ejb.GenericEntityObjectFactory; 31 import org.jboss.logging.Logger; 32 33 import javax.ejb.CreateException ; 34 import javax.ejb.EJBException ; 35 import javax.ejb.EntityBean ; 36 import javax.ejb.FinderException ; 37 import javax.ejb.RemoveException ; 38 import java.lang.reflect.InvocationTargetException ; 39 import java.lang.reflect.Method ; 40 import java.rmi.RemoteException ; 41 import java.util.ArrayList ; 42 import java.util.Collection ; 43 import java.util.Enumeration ; 44 import java.util.HashMap ; 45 import java.util.Iterator ; 46 47 57 public class BMPPersistenceManager 58 implements EntityPersistenceManager 59 { 60 private final static Object [] EMPTY_OBJECT_ARRAY = new Object [0]; 62 Logger log = Logger.getLogger(BMPPersistenceManager.class); 64 65 EntityContainer con; 66 67 Method ejbLoad; 68 Method ejbStore; 69 Method ejbActivate; 70 Method ejbPassivate; 71 Method ejbRemove; 72 73 76 Method isModified; 77 78 HashMap createMethods = new HashMap (); 79 HashMap postCreateMethods = new HashMap (); 80 HashMap finderMethods = new HashMap (); 81 82 84 86 public void setContainer(Container c) 88 { 89 con = (EntityContainer)c; 90 } 91 92 public void create() 93 throws Exception 94 { 95 ejbLoad = EntityBean .class.getMethod("ejbLoad", new Class [0]); 96 ejbStore = EntityBean .class.getMethod("ejbStore", new Class [0]); 97 ejbActivate = EntityBean .class.getMethod("ejbActivate", new Class [0]); 98 ejbPassivate = EntityBean .class.getMethod("ejbPassivate", new Class [0]); 99 ejbRemove = EntityBean .class.getMethod("ejbRemove", new Class [0]); 100 101 if (con.getHomeClass() != null) 103 { 104 Method [] methods = con.getHomeClass().getMethods(); 105 createMethodCache( methods ); 106 } 107 if (con.getLocalHomeClass() != null) 108 { 109 Method [] methods = con.getLocalHomeClass().getMethods(); 110 createMethodCache( methods ); 111 } 112 113 try 114 { 115 isModified = con.getBeanClass().getMethod("isModified", new Class [0]); 116 if (!isModified.getReturnType().equals(Boolean.TYPE)) 117 isModified = null; } 119 catch (NoSuchMethodException ignored) {} 120 } 121 122 127 public Object createBeanClassInstance() throws Exception { 128 return con.getBeanClass().newInstance(); 129 } 130 131 private void createMethodCache( Method [] methods ) 132 throws NoSuchMethodException 133 { 134 for (int i = 0; i < methods.length; i++) 135 { 136 String name = methods[i].getName(); 137 if (name.startsWith("create")) 138 { 139 String nameSuffix = name.substring(0, 1).toUpperCase() + name.substring(1); 140 try 141 { 142 createMethods.put(methods[i], con.getBeanClass().getMethod("ejb" + nameSuffix, methods[i].getParameterTypes())); 143 } 144 catch (NoSuchMethodException e) 145 { 146 log.error("Home Method " + methods[i] + " not implemented in bean class " + con.getBeanClass() + " looking for method named: ejb" + nameSuffix); 147 148 throw e; 149 } 150 try 151 { 152 postCreateMethods.put(methods[i], con.getBeanClass().getMethod("ejbPost" + nameSuffix, methods[i].getParameterTypes())); 153 } 154 catch (NoSuchMethodException e) 155 { 156 log.error("Home Method " + methods[i] + " not implemented in bean class " + con.getBeanClass() + " looking for method named: ejbPost" + nameSuffix); 157 throw e; 158 } 159 } 160 } 161 162 for (int i = 0; i < methods.length; i++) 164 { 165 if (methods[i].getName().startsWith("find")) 166 { 167 try 168 { 169 finderMethods.put(methods[i], con.getBeanClass().getMethod("ejbF" + methods[i].getName().substring(1), methods[i].getParameterTypes())); 170 } 171 catch (NoSuchMethodException e) 172 { 173 log.error("Home Method " + methods[i] + " not implemented in bean class"); 174 throw e; 175 } 176 } 177 } 178 } 179 180 public void start() 181 { 182 } 183 184 public void stop() 185 { 186 } 187 188 public void destroy() 189 { 190 } 191 192 public void createEntity( 193 Method m, 194 Object [] args, 195 EntityEnterpriseContext ctx) 196 throws Exception 197 { 198 199 Object id = null; 200 try 201 { 202 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_CREATE); 204 Method createMethod = (Method )createMethods.get(m); 205 id = createMethod.invoke(ctx.getInstance(), args); 206 } catch (IllegalAccessException e) 207 { 208 throw new EJBException (e); 210 } catch (InvocationTargetException ite) 211 { 212 Throwable e = ite.getTargetException(); 213 if (e instanceof CreateException ) 214 { 215 throw (CreateException )e; 217 } 218 else if (e instanceof RemoteException ) 219 { 220 throw (RemoteException )e; 222 } 223 else if (e instanceof EJBException ) 224 { 225 throw (EJBException )e; 227 } 228 else if (e instanceof RuntimeException ) 229 { 230 throw new EJBException ((Exception )e); 232 } 233 else if(e instanceof Exception ) 234 { 235 throw (Exception )e; 236 } 237 else 238 { 239 throw (Error )e; 240 } 241 } 242 finally{ 243 AllowedOperationsAssociation.popInMethodFlag(); 244 } 245 246 ctx.setId(id); 248 249 Object cacheKey = ((EntityCache) con.getInstanceCache()).createCacheKey( id ); 251 252 ctx.setCacheKey(cacheKey); 254 } 255 256 public void postCreateEntity( 257 Method m, 258 Object [] args, 259 EntityEnterpriseContext ctx) 260 throws Exception 261 { 262 try 263 { 264 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_POST_CREATE); 265 Method postCreateMethod = (Method )postCreateMethods.get(m); 266 postCreateMethod.invoke(ctx.getInstance(), args); 267 } catch (IllegalAccessException e) 268 { 269 throw new EJBException (e); 271 } catch (InvocationTargetException ite) 272 { 273 Throwable e = ite.getTargetException(); 274 if (e instanceof CreateException ) 275 { 276 throw (CreateException )e; 278 } 279 else if (e instanceof RemoteException ) 280 { 281 throw (RemoteException )e; 283 } 284 else if (e instanceof EJBException ) 285 { 286 throw (EJBException )e; 288 } 289 else if (e instanceof RuntimeException ) 290 { 291 throw new EJBException ((Exception )e); 293 } 294 else if(e instanceof Exception ) 295 { 296 throw (Exception )e; 297 } 298 else 299 { 300 throw (Error )e; 301 } 302 } 303 finally{ 304 AllowedOperationsAssociation.popInMethodFlag(); 305 } 306 } 307 308 public Object findEntity(Method finderMethod, Object [] args, EntityEnterpriseContext ctx, GenericEntityObjectFactory factory) 309 throws Exception 310 { 311 try 312 { 313 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_FIND); 314 315 Object objectId = callFinderMethod(finderMethod, args, ctx); 317 318 final Object cacheKey = ((EntityCache)con.getInstanceCache()).createCacheKey( objectId ); 319 return factory.getEntityEJBObject(cacheKey); 320 } 321 finally 322 { 323 AllowedOperationsAssociation.popInMethodFlag(); 324 } 325 } 326 327 public Collection findEntities(Method finderMethod, Object [] args, EntityEnterpriseContext ctx, GenericEntityObjectFactory factory) 328 throws Exception 329 { 330 Object result; 332 try 333 { 334 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_FIND); 335 result = callFinderMethod(finderMethod, args, ctx); 336 } 337 finally 338 { 339 AllowedOperationsAssociation.popInMethodFlag(); 340 } 341 342 if (result == null) 343 { 344 return new ArrayList (); 348 } 349 350 if (result instanceof java.util.Enumeration ) 351 { 352 ArrayList array = new ArrayList (); 354 Enumeration e = (Enumeration ) result; 355 while (e.hasMoreElements() == true) 356 { 357 final Object cacheKey = ((EntityCache) con.getInstanceCache()).createCacheKey(e.nextElement()); 359 Object o = factory.getEntityEJBObject(cacheKey); 360 array.add(o); 361 } 362 return array; 363 } 364 else if (result instanceof java.util.Collection ) 365 { 366 367 ArrayList array = new ArrayList (((Collection ) result).size()); 368 Iterator i = ((Collection ) result).iterator(); 369 while (i.hasNext()) 370 { 371 final Object cacheKey = ((EntityCache) con.getInstanceCache()).createCacheKey(i.next()); 373 Object o = factory.getEntityEJBObject(cacheKey); 374 array.add(o); 375 } 376 return array; 377 } 378 else 379 { 380 throw new EJBException ("result of finder method is not a valid " + 383 "return type: " + result.getClass()); 384 } 385 } 386 387 public void activateEntity(EntityEnterpriseContext ctx) 388 throws RemoteException 389 { 390 Object id = ctx.getId(); 392 Object cacheKey = ((EntityCache) con.getInstanceCache()).createCacheKey( id ); 393 394 ctx.setCacheKey(cacheKey); 396 397 try 398 { 399 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_ACTIVATE); 400 ejbActivate.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY); 401 } catch (IllegalAccessException e) 402 { 403 throw new EJBException (e); 405 } catch (InvocationTargetException ite) 406 { 407 Throwable e = ite.getTargetException(); 408 if (e instanceof RemoteException ) 409 { 410 throw (RemoteException )e; 412 } 413 else if (e instanceof EJBException ) 414 { 415 throw (EJBException )e; 417 } 418 else if (e instanceof RuntimeException ) 419 { 420 throw new EJBException ((Exception )e); 422 } 423 } 424 finally{ 425 AllowedOperationsAssociation.popInMethodFlag(); 426 } 427 } 428 429 public void loadEntity(EntityEnterpriseContext ctx) 430 throws RemoteException 431 { 432 try 433 { 434 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_LOAD); 435 ejbLoad.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY); 436 } catch (IllegalAccessException e) 437 { 438 throw new EJBException (e); 440 } catch (InvocationTargetException ite) 441 { 442 Throwable e = ite.getTargetException(); 443 if (e instanceof RemoteException ) 444 { 445 throw (RemoteException )e; 447 } 448 else if (e instanceof EJBException ) 449 { 450 throw (EJBException )e; 452 } 453 else if (e instanceof RuntimeException ) 454 { 455 throw new EJBException ((Exception )e); 457 } 458 } 459 finally{ 460 AllowedOperationsAssociation.popInMethodFlag(); 461 } 462 } 463 464 public boolean isStoreRequired(EntityEnterpriseContext ctx) throws Exception 465 { 466 if(isModified == null) 467 { 468 return true; 469 } 470 471 Boolean modified = (Boolean ) isModified.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY); 472 return modified.booleanValue(); 473 } 474 475 public boolean isModified(EntityEnterpriseContext ctx) throws Exception 476 { 477 return isStoreRequired(ctx); 478 } 479 480 public void invokeEjbStore(EntityEnterpriseContext ctx) 481 throws RemoteException 482 { 483 try 484 { 485 if(!isStoreRequired(ctx)) 486 { 487 return; 488 } 489 } 490 catch(Exception e) 491 { 492 throw new EJBException ("Failed to invoke isModified().", e); 493 } 494 495 try 496 { 497 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_STORE); 498 ejbStore.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY); 499 } catch (IllegalAccessException e) 500 { 501 throw new EJBException (e); 503 } catch (InvocationTargetException ite) 504 { 505 Throwable e = ite.getTargetException(); 506 if (e instanceof RemoteException ) 507 { 508 throw (RemoteException )e; 510 } 511 else if (e instanceof EJBException ) 512 { 513 throw (EJBException )e; 515 } 516 else if (e instanceof RuntimeException ) 517 { 518 throw new EJBException ((Exception )e); 520 } 521 } 522 finally{ 523 AllowedOperationsAssociation.popInMethodFlag(); 524 } 525 } 526 527 public void storeEntity(EntityEnterpriseContext ctx) 528 throws RemoteException 529 { 530 } 531 532 public void passivateEntity(EntityEnterpriseContext ctx) 533 throws RemoteException 534 { 535 try 536 { 537 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_PASSIVATE); 538 ejbPassivate.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY); 539 } catch (IllegalAccessException e) 540 { 541 throw new EJBException (e); 543 } catch (InvocationTargetException ite) 544 { 545 Throwable e = ite.getTargetException(); 546 if (e instanceof RemoteException ) 547 { 548 throw (RemoteException )e; 550 } 551 else if (e instanceof EJBException ) 552 { 553 throw (EJBException )e; 555 } 556 else if (e instanceof RuntimeException ) 557 { 558 throw new EJBException ((Exception )e); 560 } 561 } 562 finally{ 563 AllowedOperationsAssociation.popInMethodFlag(); 564 } 565 ctx.setEJBObject(null); 566 ctx.setEJBLocalObject(null); 567 } 568 569 public void removeEntity(EntityEnterpriseContext ctx) 570 throws RemoteException , RemoveException 571 { 572 try 573 { 574 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_REMOVE); 575 ejbRemove.invoke(ctx.getInstance(), EMPTY_OBJECT_ARRAY); 576 } catch (IllegalAccessException e) 577 { 578 throw new EJBException (e); 580 } catch (InvocationTargetException ite) 581 { 582 Throwable e = ite.getTargetException(); 583 if (e instanceof RemoveException ) 584 { 585 throw (RemoveException )e; 587 } 588 else if (e instanceof RemoteException ) 589 { 590 throw (RemoteException )e; 592 } 593 else if (e instanceof EJBException ) 594 { 595 throw (EJBException )e; 597 } 598 else if (e instanceof RuntimeException ) 599 { 600 throw new EJBException ((Exception )e); 602 } 603 } 604 finally{ 605 AllowedOperationsAssociation.popInMethodFlag(); 606 } 607 } 608 609 611 613 615 private Object callFinderMethod(Method finderMethod, Object [] args, EntityEnterpriseContext ctx) 617 throws Exception 618 { 619 Method callMethod = (Method )finderMethods.get(finderMethod); 621 622 if (callMethod == null) 623 { 624 throw new EJBException ("couldn't find finder method in bean class. " + 625 finderMethod.toString()); 626 } 627 628 Object result = null; 630 try 631 { 632 result = callMethod.invoke(ctx.getInstance(), args); 633 } catch (IllegalAccessException e) 634 { 635 throw new EJBException (e); 637 } catch (InvocationTargetException ite) 638 { 639 Throwable e = ite.getTargetException(); 640 if (e instanceof FinderException ) 641 { 642 throw (FinderException )e; 644 } 645 else if (e instanceof RemoteException ) 646 { 647 throw (RemoteException )e; 649 } 650 else if (e instanceof EJBException ) 651 { 652 throw (EJBException )e; 654 } 655 else if (e instanceof RuntimeException ) 656 { 657 throw new EJBException ((Exception )e); 659 } 660 else if(e instanceof Exception ) 661 { 662 throw (Exception )e; 663 } 664 else 665 { 666 throw (Error )e; 667 } 668 } 669 670 return result; 671 } 672 673 674 } 676 677 678 679 680 | Popular Tags |