KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > descriptors > DescriptorEventManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.descriptors;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedActionException JavaDoc;
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 /**
39  * <p><b>Purpose</b>: The event manager allows for a descriptor to specify that
40  * an object should be notified when a TopLink event occurs. It also determines
41  * how the object will be notified. To specify an event a method name can be
42  * registered to be called on the object when the event occurs. Events can be
43  * used to extend the TopLink reading and writing behavior.
44  * <p>
45  * These events include:
46  * <ul>
47  * <li> pre/postWrite - occurs when an object is written (occurs even if no changes to the object).
48  * <li> pre/postInsert - occurs when an object is inserted.
49  * <li> pre/postUpdate - occurs when an object is updated (occurs even if no changes to the object).
50  * <li> pre/postDeleted - occurs when an object is deleted.
51  * <li> postBuild/postRefresh - occurs after a object has been built/refreshed from its database row.
52  * <li> aboutTo/Insert/Update - occurs when an object is about to be inserted/update allows for row modification.
53  * <li> postClone - occurs when an object is registered/cloned in a unit of work.
54  * <li> postMerge - occurs when an object is merged with its original in a unit of work.
55  * </ul>
56  *
57  * @see ClassDescriptor
58  */

59 public class DescriptorEventManager implements Cloneable JavaDoc, Serializable {
60     protected ClassDescriptor descriptor;
61     protected Vector eventSelectors;
62     protected transient Vector eventMethods;
63     protected transient Vector eventListeners;
64     
65     // EJB 3.0 support for event listeners.
66
protected transient Vector defaultEventListeners;
67     protected transient Vector entityListenerEventListeners;
68     protected transient DescriptorEventListener entityEventListener;
69     
70     // EJB 3.0 support - cache our parent event managers.
71
protected transient Vector entityEventManagers;
72     protected transient Vector entityListenerEventManagers;
73     
74     // EJB 3.0 support for event listener configuration flags.
75
protected boolean excludeDefaultListeners;
76     protected boolean excludeSuperclassListeners;
77
78     /** PERF: Cache if any events listener exist. */
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     // CR#2660080 was missing aboutToDelete
96
public static final int AboutToDeleteEvent = 14;
97     
98     // EJB 3.0 events
99
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     /**
106      * INTERNAL:
107      * Returns a new DescriptorEventManager for the specified Descriptor.
108      */

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     /**
124      * PUBLIC:
125      * EJB 3.0 support for default listeners.
126      */

127     public void addDefaultEventListener(DescriptorEventListener listener) {
128         getDefaultEventListeners().addElement(listener);
129     }
130     
131     /**
132      * PUBLIC:
133      * EJB 3.0 support for lifecycle callback events defined on an entity
134      * listener class.
135      */

136     public void addEntityListenerEventListener(DescriptorEventListener listener) {
137         getEntityListenerEventListeners().addElement(listener);
138     }
139      
140     /**
141      * PUBLIC:
142      * Listener objects can be registered with the event manager to be notified
143      * when an event occurs on any instance of the descriptor's class.
144      */

145     public void addListener(DescriptorEventListener listener) {
146         getEventListeners().addElement(listener);
147         setHasAnyEventListeners(true);
148     }
149     
150     /**
151      * INTERNAL:
152      * Clone the manager and its private parts.
153      */

154     public Object JavaDoc 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 JavaDoc exception) {
163             ;
164         }
165
166         return clone;
167     }
168     
169     /**
170      * INTERNAL:
171      * EJB 3.0 support. Returns true if this event manager should exclude the
172      * invocation of the default listeners for this descriptor.
173      */

174     public boolean excludeDefaultListeners() {
175         return excludeDefaultListeners;
176     }
177
178     /**
179      * INTERNAL:
180      * EJB 3.0 support. Returns true is this event manager should exclude the
181      * invocation of the listeners defined by the entity listener classes for
182      * the superclasses of this descriptor.
183      */

184     public boolean excludeSuperclassListeners() {
185         return excludeSuperclassListeners;
186     }
187     
188     /**
189      * INTERNAL:
190      * Execute the given selector with the event as argument.
191      * @exception DescriptorException - the method cannot be found or executed
192      */

193     public void executeEvent(DescriptorEvent event) throws DescriptorException {
194         try {
195             event.getSession().startOperationProfile(SessionProfiler.DescriptorEvent);
196             // CR#3467758, ensure the descriptor is set on the event.
197
event.setDescriptor(getDescriptor());
198             notifyListeners(event);
199             notifyEJB30Listeners(event);
200
201             if (event.getSource() instanceof DescriptorEventListener) {
202                 // Allow the object itself to implement the interface.
203
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             // Now that I have the method, I need to invoke it
213
try {
214                 Object JavaDoc[] runtimeParameters = new Object JavaDoc[1];
215                 runtimeParameters[0] = event;
216                 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
217                     try {
218                         AccessController.doPrivileged(new PrivilegedMethodInvoker(eventMethod, event.getSource(), runtimeParameters));
219                     } catch (PrivilegedActionException JavaDoc exception) {
220                         Exception JavaDoc throwableException = exception.getException();
221                         if (throwableException instanceof IllegalAccessException JavaDoc) {
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 JavaDoc exception) {
231                 throw DescriptorException.illegalAccessWhileEventExecution(eventMethod.getName(), getDescriptor(), exception);
232             } catch (IllegalArgumentException JavaDoc 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     /**
243      * Find the method corresponding to the event selector. The method MUST take
244      * DescriptorEvent as argument, Session is also supported as argument for
245      * backward compatibility.
246      */

247     protected Method findMethod(int selector) throws DescriptorException {
248         Class JavaDoc[] declarationParameters = new Class JavaDoc[1];
249         declarationParameters[0] = ClassConstants.DescriptorEvent_Class;
250         String JavaDoc methodName = (String JavaDoc)getEventSelectors().elementAt(selector);
251
252         try {
253             return Helper.getDeclaredMethod(getDescriptor().getJavaClass(), methodName, declarationParameters);
254         } catch (NoSuchMethodException JavaDoc exception) {
255             throw DescriptorException.noSuchMethodOnFindObsoleteMethod(methodName, getDescriptor(), exception);
256         } catch (SecurityException JavaDoc exception) {
257             throw DescriptorException.securityOnFindMethod(methodName, getDescriptor(), exception);
258         }
259     }
260     
261     /**
262      * INTERNAL:
263      */

264     public String JavaDoc getAboutToInsertSelector() {
265         return (String JavaDoc)getEventSelectors().elementAt(AboutToInsertEvent);
266     }
267
268     /**
269      * INTERNAL:
270      */

271     public String JavaDoc getAboutToUpdateSelector() {
272         return (String JavaDoc)getEventSelectors().elementAt(AboutToUpdateEvent);
273     }
274
275     /**
276      * INTERNAL:
277      * EJB 3.0 support. Returns the default listeners.
278      */

279     public Vector getDefaultEventListeners() {
280         if (defaultEventListeners == null) {
281             defaultEventListeners = new Vector();
282         }
283         
284         return defaultEventListeners;
285     }
286     
287     /**
288      * INTERNAL:
289      */

290     protected ClassDescriptor getDescriptor() {
291         return descriptor;
292     }
293
294     /**
295      * INTERNAL:
296      * EJB 3.0 support. Returns the entity event listener.
297      */

298     public DescriptorEventListener getEntityEventListener() {
299         return entityEventListener;
300     }
301     
302     /**
303      * INTERNAL:
304      * EJB 3.0 support. Returns the entity listener event listeners.
305      */

306     public Vector getEntityListenerEventListeners() {
307         if (entityListenerEventListeners == null) {
308             entityListenerEventListeners = new Vector();
309         }
310         
311         return entityListenerEventListeners;
312     }
313     
314     /**
315      * PUBLIC:
316      * Returns the Listener objects that have been added.
317      *
318      * @see #addListener(DescriptorEventListener)
319      */

320     public Vector getEventListeners() {
321         // Lazy initialize to avoid unessisary enumerations.
322
if (eventListeners == null) {
323             eventListeners = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(1);
324         }
325         return eventListeners;
326     }
327
328     protected Vector getEventMethods() {
329         //Lazy Initialized to prevent Null Pointer exception after serialization
330
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     /**
350      * PUBLIC:
351      * The name of the method called after an object is built
352      */

353     public String JavaDoc getPostBuildSelector() {
354         return (String JavaDoc)getEventSelectors().elementAt(PostBuildEvent);
355     }
356
357     /**
358      * PUBLIC:
359      * The name of the method called after an object is cloned
360      */

361     public String JavaDoc getPostCloneSelector() {
362         return (String JavaDoc)getEventSelectors().elementAt(PostCloneEvent);
363     }
364
365     /**
366      * PUBLIC:
367      * The name of the method called after an object is deleted
368      */

369     public String JavaDoc getPostDeleteSelector() {
370         return (String JavaDoc)getEventSelectors().elementAt(PostDeleteEvent);
371     }
372
373     /**
374      * PUBLIC:
375      * The name of the method called after an object is inserted
376      */

377     public String JavaDoc getPostInsertSelector() {
378         return (String JavaDoc)getEventSelectors().elementAt(PostInsertEvent);
379     }
380
381     /**
382      * PUBLIC:
383      * The name of the method called after an object is merged
384      */

385     public String JavaDoc getPostMergeSelector() {
386         return (String JavaDoc)getEventSelectors().elementAt(PostMergeEvent);
387     }
388
389     /**
390      * PUBLIC:
391      * The name of the method called after an object is refreshed
392      */

393     public String JavaDoc getPostRefreshSelector() {
394         return (String JavaDoc)getEventSelectors().elementAt(PostRefreshEvent);
395     }
396
397     /**
398      * PUBLIC:
399      * The name of the method called after an object is updated
400      */

401     public String JavaDoc getPostUpdateSelector() {
402         return (String JavaDoc)getEventSelectors().elementAt(PostUpdateEvent);
403     }
404
405     /**
406      * PUBLIC:
407      * The name of the method called after an object is written
408      */

409     public String JavaDoc getPostWriteSelector() {
410         return (String JavaDoc)getEventSelectors().elementAt(PostWriteEvent);
411     }
412
413     /**
414      * PUBLIC:
415      * The name of the method called before the create operation is applied to
416      * an object
417      */

418     public String JavaDoc getPrePersistSelector() {
419         return (String JavaDoc)getEventSelectors().elementAt(PrePersistEvent);
420     }
421
422   /**
423      * PUBLIC:
424      * The name of the method called before an object is deleted
425      */

426     public String JavaDoc getPreDeleteSelector() {
427         return (String JavaDoc)getEventSelectors().elementAt(PreDeleteEvent);
428     }
429
430     /**
431      * PUBLIC:
432      * The name of the method called before an object is inserted
433      */

434     public String JavaDoc getPreInsertSelector() {
435         return (String JavaDoc)getEventSelectors().elementAt(PreInsertEvent);
436     }
437
438     /**
439      * PUBLIC:
440      * The name of the method called before the remove operation is applied to
441      * an object
442      */

443     public String JavaDoc getPreRemoveSelector() {
444         return (String JavaDoc)getEventSelectors().elementAt(PreRemoveEvent);
445     }
446
447   /**
448      * PUBLIC:
449      * The name of the method called before an object is updated
450      */

451     public String JavaDoc getPreUpdateSelector() {
452         return (String JavaDoc)getEventSelectors().elementAt(PreUpdateEvent);
453     }
454
455     /**
456      * PUBLIC:
457      * The name of the method called before an object is written
458      */

459     public String JavaDoc getPreWriteSelector() {
460         return (String JavaDoc)getEventSelectors().elementAt(PreWriteEvent);
461     }
462
463     /**
464      * INTERNAL:
465      * Return if the event manager has any event listeners, or event methods.
466      * If nothing is listening to event they can be avoided.
467      */

468     public boolean hasAnyEventListeners() {
469         // Check listeners in case of collection added to directly as occurs
470
// for aggregates that have a clone of the event manager but not the
471
// listeners.
472
return hasAnyEventListeners || hasAnyListeners() || hasEntityEventListener() || hasEntityListenerEventListeners();
473     }
474     
475     protected boolean hasAnyListeners() {
476         return (eventListeners != null) && (!eventListeners.isEmpty());
477     }
478     
479     /**
480      * INTERNAL:
481      * EJB 3.0 support. Return true if this event manager has any entity event
482      * listeners.
483      */

484     public boolean hasEntityEventListener() {
485         return entityEventListener != null;
486     }
487     
488     /**
489      * INTERNAL:
490      * EJB 3.0 support. Return true if this event manager has any entity
491      * listener event listeners.
492      */

493     public boolean hasEntityListenerEventListeners() {
494         return entityListenerEventListeners != null && entityListenerEventListeners.size() > 0;
495     }
496
497     /**
498      * INTERNAL:
499      * Configure inherited selectors.
500      */

501     public void initialize(AbstractSession session) {
502         // Initialize the EJB 3.0 supported listeners.
503
initializeEJB30EventManagers();
504         
505         // Initialize if events are required at all.
506
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         // Inherit all parent defined event method
518
// Do NOT inherit the listener as the events are broadcast to the parent.
519
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     /**
533      * INTERNAL:
534      * EJB 3.0 support. Builds our chains of descriptor event managers that will
535      * need to be notified. The chains are cache so we only need to build them
536      * once.
537      */

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     /**
573      * INTERNAL:
574      * Notify the EJB 3.0 event listeners.
575      */

576     protected void notifyEJB30Listeners(DescriptorEvent event) {
577         // Step 1 - notify our default listeners.
578
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         // Step 2 - Notify the Entity Listener's first, top -> down.
586
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         // Step 3 - Notify the Entity event listeners. top -> down, unless
596
// they are overriden in a subclass.
597
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     /**
607      * INTERNAL:
608      * Big ugly case statement to notify listeners.
609      */

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     /**
672      * INTERNAL:
673      * Notify the event listeners.
674      */

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         // Also must notify any inherited listeners.
684
if (getDescriptor().isChildDescriptor()) {
685             getDescriptor().getInheritancePolicy().getParentDescriptor().getEventManager().notifyListeners(event);
686         }
687     }
688     
689     /**
690      * INTERNAL:
691      * Used to initialize a remote DescriptorEventManager.
692      */

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     /**
704      * PUBLIC:
705      * Remove a event listener.
706      */

707     public void removeListener(DescriptorEventListener listener) {
708         getEventListeners().removeElement(listener);
709     }
710
711     /**
712      * PUBLIC:
713      * A method can be registered to be called when an object's row it about to
714      * be inserted. This uses the optional event argument of the DatabaseRow.
715      * This is different from pre/postInsert because it occurs after the row has
716      * already been built. This event can be used to modify the row before
717      * insert, such as adding a user inserted by.
718      */

719     public void setAboutToInsertSelector(String JavaDoc aboutToInsertSelector) {
720         getEventSelectors().setElementAt(aboutToInsertSelector, AboutToInsertEvent);
721     }
722
723     /**
724      * PUBLIC:
725      * A method can be registered to be called when an object's row it about to
726      * be updated. This uses the optional event argument of the DatabaseRow.
727      * This is different from pre/postUpdate because it occurs after the row has
728      * already been built, and it ONLY called if the update is required (changed
729      * within a unit of work), as the other occur ALWAYS. This event can be used
730      * to modify the row before insert, such as adding a user inserted by.
731      */

732     public void setAboutToUpdateSelector(String JavaDoc aboutToUpdateSelector) {
733         getEventSelectors().setElementAt(aboutToUpdateSelector, AboutToUpdateEvent);
734     }
735
736     /**
737      * INTERNAL:
738      * Set the descriptor.
739      */

740     public void setDescriptor(ClassDescriptor descriptor) {
741         this.descriptor = descriptor;
742     }
743     
744     /**
745      * PUBLIC:
746      * EJB 3.0 support for lifecycle callback events defined on an entity class.
747      */

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     /**
765      * INTERNAL:
766      * EJB 3.0 support. Default listeners apply to all entities in a persistence
767      * unit. Set this flag to true to exclude the invocation of the default
768      * listeners for this descriptor.
769      */

770     public void setExcludeDefaultListeners(boolean excludeDefaultListeners) {
771         this.excludeDefaultListeners = excludeDefaultListeners;
772     }
773
774     /**
775      * INTERNAL:
776      * EJB 3.0 support. If multiple entity classes in an inheritance hierarchy
777      * define entity listeners, the listeners defined for a superclass are
778      * invoked before the listeners defined for its subclasses. Set this flag
779      * to true to exclude the invocation of the listeners defined by the entity
780      * listener classes for the superclasses of this descriptor.
781      */

782     public void setExcludeSuperclassListeners(boolean excludeSuperclassListeners) {
783         this.excludeSuperclassListeners = excludeSuperclassListeners;
784     }
785     
786     /**
787      * INTERNAL:
788      * Set if the event manager has any event listeners, or event methods.
789      * If nothing is listening to event they can be avoided.
790      */

791     protected void setHasAnyEventListeners(boolean hasAnyEventListeners) {
792         this.hasAnyEventListeners = hasAnyEventListeners;
793     }
794
795     /**
796      * PUBLIC:
797      * A method can be registered to be called on a object that has just been
798      * built from the database. This uses the optional event argument of the
799      * DatabaseRow. This event can be used to correctly initialize an object's
800      * non-persistent attributes or to perform complex optimizations or
801      * mappings. This event is called whenever an object is built.
802      */

803     public void setPostBuildSelector(String JavaDoc postBuildSelector) {
804         getEventSelectors().setElementAt(postBuildSelector, PostBuildEvent);
805     }
806
807     /**
808      * PUBLIC:
809      * A method can be registered to be called on a object that has just been
810      * cloned into a unit of work. This uses the optional event argument of the
811      * orignial object (the source object it the clone). This event can be used
812      * to correctly initialize an object's non-persistent attributes.
813      */

814     public void setPostCloneSelector(String JavaDoc postCloneSelector) {
815         getEventSelectors().setElementAt(postCloneSelector, PostCloneEvent);
816     }
817
818     /**
819      * PUBLIC:
820      * A method can be registered to be called on a object that has just been
821      * deleted from the database. This event can notify/remove any dependents
822      * on the object.
823      */

824     public void setPostDeleteSelector(String JavaDoc postDeleteSelector) {
825         getEventSelectors().setElementAt(postDeleteSelector, PostDeleteEvent);
826     }
827
828     /**
829      * PUBLIC:
830      * A method can be registered to be called on a object that has just been
831      * inserted into the database. This event can be used to notify any
832      * dependent on the object, or to update information not accessible until
833      * the object has been inserted.
834      */

835     public void setPostInsertSelector(String JavaDoc postInsertSelector) {
836         getEventSelectors().setElementAt(postInsertSelector, PostInsertEvent);
837     }
838
839     /**
840      * PUBLIC:
841      * A method can be registered to be called on a object that has just been
842      * merge from a unit of work. This uses the optional event argument of the
843      * orignial object which is the object being merged from, the source object
844      * is the object being merged into. This event can be used to correctly
845      * initialize an object's non-persistent attributes.
846      */

847     public void setPostMergeSelector(String JavaDoc postMergeSelector) {
848         getEventSelectors().setElementAt(postMergeSelector, PostMergeEvent);
849     }
850
851     /**
852      * PUBLIC:
853      * A method can be registered to be called on a object that has just been
854      * refreshed from the database. This uses the optional event argument of
855      * the DatabaseRow. This event can be used to correctly initialize an
856      * object's non-persistent attributes or to perform complex optimizations or
857      * mappings. This event is only called on refreshes of existing objects.
858      */

859     public void setPostRefreshSelector(String JavaDoc postRefreshSelector) {
860         getEventSelectors().setElementAt(postRefreshSelector, PostRefreshEvent);
861     }
862
863     /**
864      * PUBLIC:
865      * A method can be registered to be called on a object that has just been
866      * updated into the database.
867      */

868     public void setPostUpdateSelector(String JavaDoc postUpdateSelector) {
869         getEventSelectors().setElementAt(postUpdateSelector, PostUpdateEvent);
870     }
871
872     /**
873      * PUBLIC:
874      * A method can be registered to be called on a object that has just been
875      * written to the database. This event is raised on any registered object
876      * in a unit of work, even if it has not changed, refer to the
877      * "aboutToUpdate" selector if it is required for the event to be raised
878      * only when the object has been changed. This will be called on all inserts
879      * and updates, after the "postInsert/Update" event has been raised. This
880      * event can be used to notify any dependent on the object.
881      */

882     public void setPostWriteSelector(String JavaDoc postWriteSelector) {
883         getEventSelectors().setElementAt(postWriteSelector, PostWriteEvent);
884     }
885
886     /**
887      * PUBLIC:
888      * A method can be registered to be called on a object that is going to be
889      * deleted from the database. This event can notify/remove any dependents
890      * on the object.
891      */

892     public void setPreDeleteSelector(String JavaDoc preDeleteSelector) {
893         getEventSelectors().setElementAt(preDeleteSelector, PreDeleteEvent);
894     }
895
896     /**
897      * PUBLIC:
898      * A method can be registered to be called on a object that is going to be
899      * inserted into the database. This event can be used to notify any
900      * dependent on the object or acquire the object's id through a custom
901      * mechanism.
902      */

903     public void setPreInsertSelector(String JavaDoc preInsertSelector) {
904         getEventSelectors().setElementAt(preInsertSelector, PreInsertEvent);
905     }
906     
907     /**
908      * PUBLIC:
909      * A method can be registered to be called on a object when that object has
910      * the create operation applied to it.
911      */

912     public void setPrePersistSelector(String JavaDoc prePersistSelector) {
913         getEventSelectors().setElementAt(prePersistSelector, PrePersistEvent);
914     }
915
916     /**
917      * PUBLIC:
918      * A method can be registered to be called on a object when that object has
919      * the remove operation applied to it.
920      */

921     public void setPreRemoveSelector(String JavaDoc preRemoveSelector) {
922         getEventSelectors().setElementAt(preRemoveSelector, PreRemoveEvent);
923     }
924
925     /**
926      * PUBLIC:
927      * A method can be registered to be called on a object that is going to be
928      * updated into the database. This event is raised on any registered object
929      * in a unit of work, even if it has not changed, refer to the
930      * "aboutToUpdate" selector if it is required for the event to be raised
931      * only when the object has been changed. This event can be used to notify
932      * any dependent on the object.
933      */

934     public void setPreUpdateSelector(String JavaDoc preUpdateSelector) {
935         getEventSelectors().setElementAt(preUpdateSelector, PreUpdateEvent);
936     }
937
938     /**
939      * PUBLIC:
940      * A method can be registered to be called on a object that is going to be
941      * written to the database. This event is raised on any registered object
942      * in a unit of work, even if it has not changed, refer to the
943      * "aboutToUpdate" selector if it is required for the event to be raised
944      * only when the object has been changed. This will be called on all inserts
945      * and updates, before the "preInsert/Update" event has been raised. This
946      * event can be used to notify any dependent on the object.
947      */

948     public void setPreWriteSelector(String JavaDoc preWriteSelector) {
949         getEventSelectors().setElementAt(preWriteSelector, PreWriteEvent);
950     }
951 }
952
Popular Tags