1 21 package oracle.toplink.essentials.descriptors; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 import java.util.*; 27 import java.lang.reflect.*; 28 import java.io.*; 29 import oracle.toplink.essentials.descriptors.DescriptorEventListener; 30 import oracle.toplink.essentials.descriptors.DescriptorEvent; 31 import oracle.toplink.essentials.internal.helper.*; 32 import oracle.toplink.essentials.exceptions.*; 33 import oracle.toplink.essentials.sessions.SessionProfiler; 34 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 35 import oracle.toplink.essentials.internal.security.PrivilegedMethodInvoker; 36 import oracle.toplink.essentials.internal.sessions.AbstractSession; 37 38 59 public class DescriptorEventManager implements Cloneable , Serializable { 60 protected ClassDescriptor descriptor; 61 protected Vector eventSelectors; 62 protected transient Vector eventMethods; 63 protected transient Vector eventListeners; 64 65 protected transient Vector defaultEventListeners; 67 protected transient Vector entityListenerEventListeners; 68 protected transient DescriptorEventListener entityEventListener; 69 70 protected transient Vector entityEventManagers; 72 protected transient Vector entityListenerEventManagers; 73 74 protected boolean excludeDefaultListeners; 76 protected boolean excludeSuperclassListeners; 77 78 79 protected boolean hasAnyEventListeners; 80 public static final int PreWriteEvent = 0; 81 public static final int PostWriteEvent = 1; 82 public static final int PreDeleteEvent = 2; 83 public static final int PostDeleteEvent = 3; 84 public static final int PreInsertEvent = 4; 85 public static final int PostInsertEvent = 5; 86 public static final int PreUpdateEvent = 6; 87 public static final int PostUpdateEvent = 7; 88 public static final int PostBuildEvent = 8; 89 public static final int PostRefreshEvent = 9; 90 public static final int PostCloneEvent = 10; 91 public static final int PostMergeEvent = 11; 92 public static final int AboutToInsertEvent = 12; 93 public static final int AboutToUpdateEvent = 13; 94 95 public static final int AboutToDeleteEvent = 14; 97 98 public static final int PrePersistEvent = 15; 100 public static final int PreRemoveEvent = 16; 101 public static final int PreUpdateWithChangesEvent = 17; 102 103 public static final int NumberOfEvents = 18; 104 105 109 public DescriptorEventManager() { 110 this.eventSelectors = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(NumberOfEvents); 111 this.eventMethods = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(NumberOfEvents); 112 this.hasAnyEventListeners = false; 113 this.excludeDefaultListeners = false; 114 this.excludeSuperclassListeners = false; 115 116 117 for (int index = 0; index < NumberOfEvents; index++) { 118 this.eventSelectors.addElement(null); 119 this.eventMethods.addElement(null); 120 } 121 } 122 123 127 public void addDefaultEventListener(DescriptorEventListener listener) { 128 getDefaultEventListeners().addElement(listener); 129 } 130 131 136 public void addEntityListenerEventListener(DescriptorEventListener listener) { 137 getEntityListenerEventListeners().addElement(listener); 138 } 139 140 145 public void addListener(DescriptorEventListener listener) { 146 getEventListeners().addElement(listener); 147 setHasAnyEventListeners(true); 148 } 149 150 154 public Object clone() { 155 DescriptorEventManager clone = null; 156 157 try { 158 clone = (DescriptorEventManager)super.clone(); 159 clone.setEventSelectors((Vector)getEventSelectors().clone()); 160 clone.setEventMethods((Vector)getEventMethods().clone()); 161 clone.setEventListeners((Vector)getEventListeners()); 162 } catch (Exception exception) { 163 ; 164 } 165 166 return clone; 167 } 168 169 174 public boolean excludeDefaultListeners() { 175 return excludeDefaultListeners; 176 } 177 178 184 public boolean excludeSuperclassListeners() { 185 return excludeSuperclassListeners; 186 } 187 188 193 public void executeEvent(DescriptorEvent event) throws DescriptorException { 194 try { 195 event.getSession().startOperationProfile(SessionProfiler.DescriptorEvent); 196 event.setDescriptor(getDescriptor()); 198 notifyListeners(event); 199 notifyEJB30Listeners(event); 200 201 if (event.getSource() instanceof DescriptorEventListener) { 202 notifyListener((DescriptorEventListener)event.getSource(), event); 204 return; 205 } 206 207 Method eventMethod = (Method)getEventMethods().elementAt(event.getEventCode()); 208 if (eventMethod == null) { 209 return; 210 } 211 212 try { 214 Object [] runtimeParameters = new Object [1]; 215 runtimeParameters[0] = event; 216 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 217 try { 218 AccessController.doPrivileged(new PrivilegedMethodInvoker(eventMethod, event.getSource(), runtimeParameters)); 219 } catch (PrivilegedActionException exception) { 220 Exception throwableException = exception.getException(); 221 if (throwableException instanceof IllegalAccessException ) { 222 throw DescriptorException.illegalAccessWhileEventExecution(eventMethod.getName(), getDescriptor(), throwableException); 223 } else { 224 throw DescriptorException.targetInvocationWhileEventExecution(eventMethod.getName(), getDescriptor(), throwableException); 225 } 226 } 227 } else { 228 PrivilegedAccessHelper.invokeMethod(eventMethod, event.getSource(), runtimeParameters); 229 } 230 } catch (IllegalAccessException exception) { 231 throw DescriptorException.illegalAccessWhileEventExecution(eventMethod.getName(), getDescriptor(), exception); 232 } catch (IllegalArgumentException exception) { 233 throw DescriptorException.illegalArgumentWhileObsoleteEventExecute(eventMethod.getName(), getDescriptor(), exception); 234 } catch (InvocationTargetException exception) { 235 throw DescriptorException.targetInvocationWhileEventExecution(eventMethod.getName(), getDescriptor(), exception); 236 } 237 } finally { 238 event.getSession().endOperationProfile(SessionProfiler.DescriptorEvent); 239 } 240 } 241 242 247 protected Method findMethod(int selector) throws DescriptorException { 248 Class [] declarationParameters = new Class [1]; 249 declarationParameters[0] = ClassConstants.DescriptorEvent_Class; 250 String methodName = (String )getEventSelectors().elementAt(selector); 251 252 try { 253 return Helper.getDeclaredMethod(getDescriptor().getJavaClass(), methodName, declarationParameters); 254 } catch (NoSuchMethodException exception) { 255 throw DescriptorException.noSuchMethodOnFindObsoleteMethod(methodName, getDescriptor(), exception); 256 } catch (SecurityException exception) { 257 throw DescriptorException.securityOnFindMethod(methodName, getDescriptor(), exception); 258 } 259 } 260 261 264 public String getAboutToInsertSelector() { 265 return (String )getEventSelectors().elementAt(AboutToInsertEvent); 266 } 267 268 271 public String getAboutToUpdateSelector() { 272 return (String )getEventSelectors().elementAt(AboutToUpdateEvent); 273 } 274 275 279 public Vector getDefaultEventListeners() { 280 if (defaultEventListeners == null) { 281 defaultEventListeners = new Vector(); 282 } 283 284 return defaultEventListeners; 285 } 286 287 290 protected ClassDescriptor getDescriptor() { 291 return descriptor; 292 } 293 294 298 public DescriptorEventListener getEntityEventListener() { 299 return entityEventListener; 300 } 301 302 306 public Vector getEntityListenerEventListeners() { 307 if (entityListenerEventListeners == null) { 308 entityListenerEventListeners = new Vector(); 309 } 310 311 return entityListenerEventListeners; 312 } 313 314 320 public Vector getEventListeners() { 321 if (eventListeners == null) { 323 eventListeners = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(1); 324 } 325 return eventListeners; 326 } 327 328 protected Vector getEventMethods() { 329 if (this.eventMethods == null) { 331 this.eventMethods = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(NumberOfEvents); 332 for (int index = 0; index < NumberOfEvents; ++index) { 333 this.eventMethods.addElement(null); 334 } 335 } 336 return eventMethods; 337 } 338 339 protected Vector getEventSelectors() { 340 if (this.eventSelectors == null) { 341 this.eventSelectors = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(NumberOfEvents); 342 for (int index = 0; index < NumberOfEvents; ++index) { 343 this.eventSelectors.addElement(null); 344 } 345 } 346 return eventSelectors; 347 } 348 349 353 public String getPostBuildSelector() { 354 return (String )getEventSelectors().elementAt(PostBuildEvent); 355 } 356 357 361 public String getPostCloneSelector() { 362 return (String )getEventSelectors().elementAt(PostCloneEvent); 363 } 364 365 369 public String getPostDeleteSelector() { 370 return (String )getEventSelectors().elementAt(PostDeleteEvent); 371 } 372 373 377 public String getPostInsertSelector() { 378 return (String )getEventSelectors().elementAt(PostInsertEvent); 379 } 380 381 385 public String getPostMergeSelector() { 386 return (String )getEventSelectors().elementAt(PostMergeEvent); 387 } 388 389 393 public String getPostRefreshSelector() { 394 return (String )getEventSelectors().elementAt(PostRefreshEvent); 395 } 396 397 401 public String getPostUpdateSelector() { 402 return (String )getEventSelectors().elementAt(PostUpdateEvent); 403 } 404 405 409 public String getPostWriteSelector() { 410 return (String )getEventSelectors().elementAt(PostWriteEvent); 411 } 412 413 418 public String getPrePersistSelector() { 419 return (String )getEventSelectors().elementAt(PrePersistEvent); 420 } 421 422 426 public String getPreDeleteSelector() { 427 return (String )getEventSelectors().elementAt(PreDeleteEvent); 428 } 429 430 434 public String getPreInsertSelector() { 435 return (String )getEventSelectors().elementAt(PreInsertEvent); 436 } 437 438 443 public String getPreRemoveSelector() { 444 return (String )getEventSelectors().elementAt(PreRemoveEvent); 445 } 446 447 451 public String getPreUpdateSelector() { 452 return (String )getEventSelectors().elementAt(PreUpdateEvent); 453 } 454 455 459 public String getPreWriteSelector() { 460 return (String )getEventSelectors().elementAt(PreWriteEvent); 461 } 462 463 468 public boolean hasAnyEventListeners() { 469 return hasAnyEventListeners || hasAnyListeners() || hasEntityEventListener() || hasEntityListenerEventListeners(); 473 } 474 475 protected boolean hasAnyListeners() { 476 return (eventListeners != null) && (!eventListeners.isEmpty()); 477 } 478 479 484 public boolean hasEntityEventListener() { 485 return entityEventListener != null; 486 } 487 488 493 public boolean hasEntityListenerEventListeners() { 494 return entityListenerEventListeners != null && entityListenerEventListeners.size() > 0; 495 } 496 497 501 public void initialize(AbstractSession session) { 502 initializeEJB30EventManagers(); 504 505 if (hasAnyListeners() || DescriptorEventListener.class.isAssignableFrom(getDescriptor().getJavaClass())) { 507 setHasAnyEventListeners(true); 508 } 509 510 for (int index = 0; index < NumberOfEvents; index++) { 511 if (getEventSelectors().elementAt(index) != null) { 512 setHasAnyEventListeners(true); 513 getEventMethods().setElementAt(findMethod(index), index); 514 } 515 } 516 517 if (getDescriptor().isChildDescriptor()) { 520 DescriptorEventManager parentEventManager = getDescriptor().getInheritancePolicy().getParentDescriptor().getEventManager(); 521 522 for (int index = 0; index < NumberOfEvents; index++) { 523 if (getEventSelectors().elementAt(index) == null) { 524 setHasAnyEventListeners(true); 525 getEventSelectors().setElementAt(parentEventManager.getEventSelectors().elementAt(index), index); 526 getEventMethods().setElementAt(parentEventManager.getEventMethods().elementAt(index), index); 527 } 528 } 529 } 530 } 531 532 538 protected void initializeEJB30EventManagers() { 539 entityEventManagers = new Vector(); 540 entityListenerEventManagers = new Vector(); 541 542 if (hasEntityEventListener()) { 543 entityEventManagers.add(this); 544 } 545 546 if (hasEntityListenerEventListeners()) { 547 entityListenerEventManagers.add(this); 548 } 549 550 ClassDescriptor currentDescriptor = getDescriptor(); 551 boolean excludeEntityListeners = excludeSuperclassListeners(); 552 553 while (currentDescriptor.isChildDescriptor()) { 554 currentDescriptor = currentDescriptor.getInheritancePolicy().getParentDescriptor(); 555 556 DescriptorEventManager eventManager = currentDescriptor.getEventManager(); 557 558 if (eventManager.hasEntityEventListener()) { 559 entityEventManagers.add(eventManager); 560 } 561 562 if (eventManager.hasEntityListenerEventListeners()) { 563 if (!excludeEntityListeners) { 564 entityListenerEventManagers.add(eventManager); 565 } 566 } 567 568 excludeEntityListeners = eventManager.excludeSuperclassListeners(); 569 } 570 } 571 572 576 protected void notifyEJB30Listeners(DescriptorEvent event) { 577 if (! excludeDefaultListeners()) { 579 for (int i = 0; i < getDefaultEventListeners().size(); i++) { 580 DescriptorEventListener listener = (DescriptorEventListener) getDefaultEventListeners().get(i); 581 notifyListener(listener, event); 582 } 583 } 584 585 for (int index = entityListenerEventManagers.size() - 1; index >= 0; index--) { 587 Vector entityListenerEventListeners = ((DescriptorEventManager) entityListenerEventManagers.get(index)).getEntityListenerEventListeners(); 588 589 for (int i = 0; i < entityListenerEventListeners.size(); i++) { 590 DescriptorEventListener listener = (DescriptorEventListener) entityListenerEventListeners.get(i); 591 notifyListener(listener, event); 592 } 593 } 594 595 for (int index = entityEventManagers.size() - 1; index >= 0; index--) { 598 DescriptorEventListener entityEventListener = ((DescriptorEventManager) entityEventManagers.get(index)).getEntityEventListener(); 599 600 if (! entityEventListener.isOverriddenEvent(event, entityEventManagers)) { 601 notifyListener(entityEventListener, event); 602 } 603 } 604 } 605 606 610 protected void notifyListener(DescriptorEventListener listener, DescriptorEvent event) throws DescriptorException { 611 switch (event.getEventCode()) { 612 case PreWriteEvent: 613 listener.preWrite(event); 614 break; 615 case PostWriteEvent: 616 listener.postWrite(event); 617 break; 618 case PreDeleteEvent: 619 listener.preDelete(event); 620 break; 621 case PostDeleteEvent: 622 listener.postDelete(event); 623 break; 624 case PreInsertEvent: 625 listener.preInsert(event); 626 break; 627 case PostInsertEvent: 628 listener.postInsert(event); 629 break; 630 case PreUpdateEvent: 631 listener.preUpdate(event); 632 break; 633 case PostUpdateEvent: 634 listener.postUpdate(event); 635 break; 636 case PostMergeEvent: 637 listener.postMerge(event); 638 break; 639 case PostCloneEvent: 640 listener.postClone(event); 641 break; 642 case PostBuildEvent: 643 listener.postBuild(event); 644 break; 645 case PostRefreshEvent: 646 listener.postRefresh(event); 647 break; 648 case AboutToInsertEvent: 649 listener.aboutToInsert(event); 650 break; 651 case AboutToUpdateEvent: 652 listener.aboutToUpdate(event); 653 break; 654 case AboutToDeleteEvent: 655 listener.aboutToDelete(event); 656 break; 657 case PrePersistEvent: 658 listener.prePersist(event); 659 break; 660 case PreRemoveEvent: 661 listener.preRemove(event); 662 break; 663 case PreUpdateWithChangesEvent: 664 listener.preUpdateWithChanges(event); 665 break; 666 default: 667 throw DescriptorException.invalidDescriptorEventCode(event, getDescriptor()); 668 } 669 } 670 671 675 public void notifyListeners(DescriptorEvent event) { 676 if (hasAnyListeners()) { 677 for (int index = 0; index < getEventListeners().size(); index++) { 678 DescriptorEventListener listener = (DescriptorEventListener)getEventListeners().get(index); 679 notifyListener(listener, event); 680 } 681 } 682 683 if (getDescriptor().isChildDescriptor()) { 685 getDescriptor().getInheritancePolicy().getParentDescriptor().getEventManager().notifyListeners(event); 686 } 687 } 688 689 693 public void remoteInitialization(AbstractSession session) { 694 this.eventMethods = new Vector(NumberOfEvents); 695 696 for (int index = 0; index < NumberOfEvents; index++) { 697 this.eventMethods.addElement(null); 698 } 699 700 initialize(session); 701 } 702 703 707 public void removeListener(DescriptorEventListener listener) { 708 getEventListeners().removeElement(listener); 709 } 710 711 719 public void setAboutToInsertSelector(String aboutToInsertSelector) { 720 getEventSelectors().setElementAt(aboutToInsertSelector, AboutToInsertEvent); 721 } 722 723 732 public void setAboutToUpdateSelector(String aboutToUpdateSelector) { 733 getEventSelectors().setElementAt(aboutToUpdateSelector, AboutToUpdateEvent); 734 } 735 736 740 public void setDescriptor(ClassDescriptor descriptor) { 741 this.descriptor = descriptor; 742 } 743 744 748 public void setEntityEventListener(DescriptorEventListener listener) { 749 this.entityEventListener = listener; 750 } 751 752 protected void setEventListeners(Vector eventListeners) { 753 this.eventListeners = eventListeners; 754 } 755 756 protected void setEventMethods(Vector eventMethods) { 757 this.eventMethods = eventMethods; 758 } 759 760 protected void setEventSelectors(Vector eventSelectors) { 761 this.eventSelectors = eventSelectors; 762 } 763 764 770 public void setExcludeDefaultListeners(boolean excludeDefaultListeners) { 771 this.excludeDefaultListeners = excludeDefaultListeners; 772 } 773 774 782 public void setExcludeSuperclassListeners(boolean excludeSuperclassListeners) { 783 this.excludeSuperclassListeners = excludeSuperclassListeners; 784 } 785 786 791 protected void setHasAnyEventListeners(boolean hasAnyEventListeners) { 792 this.hasAnyEventListeners = hasAnyEventListeners; 793 } 794 795 803 public void setPostBuildSelector(String postBuildSelector) { 804 getEventSelectors().setElementAt(postBuildSelector, PostBuildEvent); 805 } 806 807 814 public void setPostCloneSelector(String postCloneSelector) { 815 getEventSelectors().setElementAt(postCloneSelector, PostCloneEvent); 816 } 817 818 824 public void setPostDeleteSelector(String postDeleteSelector) { 825 getEventSelectors().setElementAt(postDeleteSelector, PostDeleteEvent); 826 } 827 828 835 public void setPostInsertSelector(String postInsertSelector) { 836 getEventSelectors().setElementAt(postInsertSelector, PostInsertEvent); 837 } 838 839 847 public void setPostMergeSelector(String postMergeSelector) { 848 getEventSelectors().setElementAt(postMergeSelector, PostMergeEvent); 849 } 850 851 859 public void setPostRefreshSelector(String postRefreshSelector) { 860 getEventSelectors().setElementAt(postRefreshSelector, PostRefreshEvent); 861 } 862 863 868 public void setPostUpdateSelector(String postUpdateSelector) { 869 getEventSelectors().setElementAt(postUpdateSelector, PostUpdateEvent); 870 } 871 872 882 public void setPostWriteSelector(String postWriteSelector) { 883 getEventSelectors().setElementAt(postWriteSelector, PostWriteEvent); 884 } 885 886 892 public void setPreDeleteSelector(String preDeleteSelector) { 893 getEventSelectors().setElementAt(preDeleteSelector, PreDeleteEvent); 894 } 895 896 903 public void setPreInsertSelector(String preInsertSelector) { 904 getEventSelectors().setElementAt(preInsertSelector, PreInsertEvent); 905 } 906 907 912 public void setPrePersistSelector(String prePersistSelector) { 913 getEventSelectors().setElementAt(prePersistSelector, PrePersistEvent); 914 } 915 916 921 public void setPreRemoveSelector(String preRemoveSelector) { 922 getEventSelectors().setElementAt(preRemoveSelector, PreRemoveEvent); 923 } 924 925 934 public void setPreUpdateSelector(String preUpdateSelector) { 935 getEventSelectors().setElementAt(preUpdateSelector, PreUpdateEvent); 936 } 937 938 948 public void setPreWriteSelector(String preWriteSelector) { 949 getEventSelectors().setElementAt(preWriteSelector, PreWriteEvent); 950 } 951 } 952 | Popular Tags |