KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > event > SessionEventListenerConfig


1 //$Id: SessionEventListenerConfig.java,v 1.16 2005/04/01 21:25:29 steveebersole Exp $
2
package org.hibernate.event;
3
4 import java.io.Serializable JavaDoc;
5 import java.beans.Introspector JavaDoc;
6 import java.beans.BeanInfo JavaDoc;
7 import java.beans.PropertyDescriptor JavaDoc;
8 import java.beans.IntrospectionException JavaDoc;
9 import java.security.AccessController JavaDoc;
10 import java.security.PrivilegedAction JavaDoc;
11
12 import org.hibernate.event.def.DefaultAutoFlushEventListener;
13 import org.hibernate.event.def.DefaultPersistEventListener;
14 import org.hibernate.event.def.DefaultDeleteEventListener;
15 import org.hibernate.event.def.DefaultDirtyCheckEventListener;
16 import org.hibernate.event.def.DefaultEvictEventListener;
17 import org.hibernate.event.def.DefaultFlushEntityEventListener;
18 import org.hibernate.event.def.DefaultFlushEventListener;
19 import org.hibernate.event.def.DefaultInitializeCollectionEventListener;
20 import org.hibernate.event.def.DefaultLoadEventListener;
21 import org.hibernate.event.def.DefaultLockEventListener;
22 import org.hibernate.event.def.DefaultMergeEventListener;
23 import org.hibernate.event.def.DefaultPostDeleteEventListener;
24 import org.hibernate.event.def.DefaultPostInsertEventListener;
25 import org.hibernate.event.def.DefaultPostLoadEventListener;
26 import org.hibernate.event.def.DefaultPostUpdateEventListener;
27 import org.hibernate.event.def.DefaultPreDeleteEventListener;
28 import org.hibernate.event.def.DefaultPreInsertEventListener;
29 import org.hibernate.event.def.DefaultPreLoadEventListener;
30 import org.hibernate.event.def.DefaultPreUpdateEventListener;
31 import org.hibernate.event.def.DefaultRefreshEventListener;
32 import org.hibernate.event.def.DefaultReplicateEventListener;
33 import org.hibernate.event.def.DefaultSaveEventListener;
34 import org.hibernate.event.def.DefaultSaveOrUpdateEventListener;
35 import org.hibernate.event.def.DefaultUpdateEventListener;
36 import org.hibernate.HibernateException;
37
38 /**
39  * A convience holder for all defined session event listeners.
40  *
41  * @author Steve Ebersole
42  */

43 public class SessionEventListenerConfig implements Serializable JavaDoc {
44
45     private static final Object JavaDoc[] READER_METHOD_ARGS = new Object JavaDoc[0];
46
47     private LoadEventListener loadEventListener = new DefaultLoadEventListener();
48     private SaveOrUpdateEventListener saveOrUpdateEventListener = new DefaultSaveOrUpdateEventListener();
49     private MergeEventListener mergeEventListener = new DefaultMergeEventListener();
50     private PersistEventListener createEventListener = new DefaultPersistEventListener();
51     private ReplicateEventListener replicateEventListener = new DefaultReplicateEventListener();
52     private DeleteEventListener deleteEventListener = new DefaultDeleteEventListener();
53     private AutoFlushEventListener autoFlushEventListener = new DefaultAutoFlushEventListener();
54     private DirtyCheckEventListener dirtyCheckEventListener = new DefaultDirtyCheckEventListener();
55     private FlushEventListener flushEventListener = new DefaultFlushEventListener();
56     private EvictEventListener evictEventListener = new DefaultEvictEventListener();
57     private LockEventListener lockEventListener = new DefaultLockEventListener();
58     private RefreshEventListener refreshEventListener = new DefaultRefreshEventListener();
59     private FlushEntityEventListener flushEntityEventListener = new DefaultFlushEntityEventListener();
60     private InitializeCollectionEventListener initializeCollectionEventListener = new DefaultInitializeCollectionEventListener();
61
62     private PostLoadEventListener postLoadEventListener = new DefaultPostLoadEventListener();
63     private PreLoadEventListener preLoadEventListener = new DefaultPreLoadEventListener();
64
65     private PostDeleteEventListener postDeleteEventListener = new DefaultPostDeleteEventListener();
66     private PostUpdateEventListener postUpdateEventListener = new DefaultPostUpdateEventListener();
67     private PostInsertEventListener postInsertEventListener = new DefaultPostInsertEventListener();
68     private PreDeleteEventListener preDeleteEventListener = new DefaultPreDeleteEventListener();
69     private PreUpdateEventListener preUpdateEventListener = new DefaultPreUpdateEventListener();
70     private PreInsertEventListener preInsertEventListener = new DefaultPreInsertEventListener();
71
72     //for deprecated saveOrUpdateCopy()
73
private MergeEventListener saveOrUpdateCopyEventListener = new DefaultMergeEventListener(true);
74
75     private SaveOrUpdateEventListener saveEventListener = new DefaultSaveEventListener();
76     private SaveOrUpdateEventListener updateEventListener = new DefaultUpdateEventListener();
77
78
79     public LoadEventListener getLoadEventListener() {
80         return loadEventListener;
81     }
82
83     public void setLoadEventListener(LoadEventListener loadEventListener) {
84         this.loadEventListener = loadEventListener;
85     }
86
87     public ReplicateEventListener getReplicateEventListener() {
88         return replicateEventListener;
89     }
90
91     public void setReplicateEventListener(ReplicateEventListener replicateEventListener) {
92         this.replicateEventListener = replicateEventListener;
93     }
94
95     public DeleteEventListener getDeleteEventListener() {
96         return deleteEventListener;
97     }
98
99     public void setDeleteEventListener(DeleteEventListener deleteEventListener) {
100         this.deleteEventListener = deleteEventListener;
101     }
102
103     public AutoFlushEventListener getAutoFlushEventListener() {
104         return autoFlushEventListener;
105     }
106
107     public void setAutoFlushEventListener(AutoFlushEventListener autoFlushEventListener) {
108         this.autoFlushEventListener = autoFlushEventListener;
109     }
110
111     public DirtyCheckEventListener getDirtyCheckEventListener() {
112         return dirtyCheckEventListener;
113     }
114
115     public void setDirtyCheckEventListener(DirtyCheckEventListener dirtyCheckEventListener) {
116         this.dirtyCheckEventListener = dirtyCheckEventListener;
117     }
118
119     public FlushEventListener getFlushEventListener() {
120         return flushEventListener;
121     }
122
123     public void setFlushEventListener(FlushEventListener flushEventListener) {
124         this.flushEventListener = flushEventListener;
125     }
126
127     public EvictEventListener getEvictEventListener() {
128         return evictEventListener;
129     }
130
131     public void setEvictEventListener(EvictEventListener evictEventListener) {
132         this.evictEventListener = evictEventListener;
133     }
134
135     public LockEventListener getLockEventListener() {
136         return lockEventListener;
137     }
138
139     public void setLockEventListener(LockEventListener lockEventListener) {
140         this.lockEventListener = lockEventListener;
141     }
142
143     public RefreshEventListener getRefreshEventListener() {
144         return refreshEventListener;
145     }
146
147     public void setRefreshEventListener(RefreshEventListener refreshEventListener) {
148         this.refreshEventListener = refreshEventListener;
149     }
150
151     public InitializeCollectionEventListener getInitializeCollectionEventListener() {
152         return initializeCollectionEventListener;
153     }
154
155     public void setInitializeCollectionEventListener(InitializeCollectionEventListener initializeCollectionEventListener) {
156         this.initializeCollectionEventListener = initializeCollectionEventListener;
157     }
158     
159     public FlushEntityEventListener getFlushEntityEventListener() {
160         return flushEntityEventListener;
161     }
162     
163     public void setFlushEntityEventListener(FlushEntityEventListener flushEntityEventListener) {
164         this.flushEntityEventListener = flushEntityEventListener;
165     }
166     
167     public SaveOrUpdateEventListener getSaveOrUpdateEventListener() {
168         return saveOrUpdateEventListener;
169     }
170     
171     public void setSaveOrUpdateEventListener(SaveOrUpdateEventListener saveOrUpdateEventListener) {
172         this.saveOrUpdateEventListener = saveOrUpdateEventListener;
173     }
174     
175     public MergeEventListener getMergeEventListener() {
176         return mergeEventListener;
177     }
178     
179     public void setMergeEventListener(MergeEventListener mergeEventListener) {
180         this.mergeEventListener = mergeEventListener;
181     }
182     
183     public PersistEventListener getCreateEventListener() {
184         return createEventListener;
185     }
186     
187     public void setCreateEventListener(PersistEventListener createEventListener) {
188         this.createEventListener = createEventListener;
189     }
190     
191     public MergeEventListener getSaveOrUpdateCopyEventListener() {
192         return saveOrUpdateCopyEventListener;
193     }
194     
195     public void setSaveOrUpdateCopyEventListener(MergeEventListener saveOrUpdateCopyEventListener) {
196         this.saveOrUpdateCopyEventListener = saveOrUpdateCopyEventListener;
197     }
198     
199     public SaveOrUpdateEventListener getSaveEventListener() {
200         return saveEventListener;
201     }
202     
203     public void setSaveEventListener(SaveOrUpdateEventListener saveEventListener) {
204         this.saveEventListener = saveEventListener;
205     }
206     
207     public SaveOrUpdateEventListener getUpdateEventListener() {
208         return updateEventListener;
209     }
210     
211     public void setUpdateEventListener(SaveOrUpdateEventListener updateEventListener) {
212         this.updateEventListener = updateEventListener;
213     }
214
215     public PostLoadEventListener getPostLoadEventListener() {
216         return postLoadEventListener;
217     }
218
219     public void setPostLoadEventListener(PostLoadEventListener postLoadEventListener) {
220         this.postLoadEventListener = postLoadEventListener;
221     }
222
223     public PreLoadEventListener getPreLoadEventListener() {
224         return preLoadEventListener;
225     }
226
227     public void setPreLoadEventListener(PreLoadEventListener preLoadEventListener) {
228         this.preLoadEventListener = preLoadEventListener;
229     }
230
231     public PostDeleteEventListener getPostDeleteEventListener() {
232         return postDeleteEventListener;
233     }
234     public PostInsertEventListener getPostInsertEventListener() {
235         return postInsertEventListener;
236     }
237     public PostUpdateEventListener getPostUpdateEventListener() {
238         return postUpdateEventListener;
239     }
240     
241     public void setPostDeleteEventListener(PostDeleteEventListener postDeleteEventListener) {
242         this.postDeleteEventListener = postDeleteEventListener;
243     }
244     public void setPostInsertEventListener(PostInsertEventListener postInsertEventListener) {
245         this.postInsertEventListener = postInsertEventListener;
246     }
247     public void setPostUpdateEventListener(PostUpdateEventListener postUpdateEventListener) {
248         this.postUpdateEventListener = postUpdateEventListener;
249     }
250     
251     public PreDeleteEventListener getPreDeleteEventListener() {
252         return preDeleteEventListener;
253     }
254     public void setPreDeleteEventListener(
255             PreDeleteEventListener preDeleteEventListener) {
256         this.preDeleteEventListener = preDeleteEventListener;
257     }
258     public PreInsertEventListener getPreInsertEventListener() {
259         return preInsertEventListener;
260     }
261     
262     public void setPreInsertEventListener(PreInsertEventListener preInsertEventListener) {
263         this.preInsertEventListener = preInsertEventListener;
264     }
265     public PreUpdateEventListener getPreUpdateEventListener() {
266         return preUpdateEventListener;
267     }
268     public void setPreUpdateEventListener(PreUpdateEventListener preUpdateEventListener) {
269         this.preUpdateEventListener = preUpdateEventListener;
270     }
271
272     /**
273      * Essentially performs a shallow copy of this SessionEventListenerConfig
274      * instance; meaning the SessionEventListenerConfig itself is cloned, but
275      * the individual listeners are <b>not</b> cloned.
276      *
277      * @return The SessionEventListenerConfig shallow copy.
278      */

279     public SessionEventListenerConfig shallowCopy() {
280         return ( SessionEventListenerConfig ) AccessController.doPrivileged(
281                 new PrivilegedAction JavaDoc() {
282                     public Object JavaDoc run() {
283                         return copyListeners();
284                     }
285                 }
286         );
287     }
288
289     /**
290      * Checks to ensure the SessionEventListenerConfig is fully
291      * configured (basically, that none of the listeners is null).
292      *
293      * @throws HibernateException If the SessionEventListenerConfig
294      * is not fully configured.
295      */

296     public void validate() throws HibernateException {
297         AccessController.doPrivileged(
298                 new PrivilegedAction JavaDoc() {
299                     public Object JavaDoc run() {
300                         checkListeners();
301                         return null;
302                     }
303                 }
304         );
305
306     }
307
308     private SessionEventListenerConfig copyListeners() {
309         SessionEventListenerConfig copy = null;
310         BeanInfo JavaDoc beanInfo = null;
311         try {
312             beanInfo = Introspector.getBeanInfo( SessionEventListenerConfig.class, Object JavaDoc.class );
313             internalCheckListeners( beanInfo );
314             copy = new SessionEventListenerConfig();
315             PropertyDescriptor JavaDoc[] pds = beanInfo.getPropertyDescriptors();
316             for ( int i = 0, max = pds.length; i < max; i++ ) {
317                 try {
318                     pds[i].getWriteMethod().invoke(
319                             copy,
320                             new Object JavaDoc[] {
321                                 pds[i].getReadMethod().invoke( this, READER_METHOD_ARGS )
322                             }
323                     );
324                 }
325                 catch( Throwable JavaDoc t ) {
326                     throw new HibernateException( "Unable copy copy listener [" + pds[i].getName() + "]" );
327                 }
328             }
329         }
330         catch( IntrospectionException JavaDoc t ) {
331             throw new HibernateException( "Unable to copy listeners", t );
332         }
333         finally {
334             if ( beanInfo != null ) {
335                 // release the jdk internal caches everytime to ensure this
336
// plays nicely with destroyable class-loaders
337
Introspector.flushFromCaches( SessionEventListenerConfig.class );
338             }
339         }
340
341 // copy.loadEventListener = this.loadEventListener;
342
// copy.saveOrUpdateEventListener = this.saveOrUpdateEventListener;
343
// copy.mergeEventListener = this.mergeEventListener;
344
// copy.createEventListener = this.createEventListener;
345
// copy.replicateEventListener = this.replicateEventListener;
346
// copy.deleteEventListener = this.deleteEventListener;
347
// copy.autoFlushEventListener = this.autoFlushEventListener;
348
// copy.dirtyCheckEventListener = this.dirtyCheckEventListener;
349
// copy.flushEventListener = this.flushEventListener;
350
// copy.evictEventListener = this.evictEventListener;
351
// copy.lockEventListener = this.lockEventListener;
352
// copy.refreshEventListener = this.refreshEventListener;
353
// copy.flushEntityEventListener = this.flushEntityEventListener;
354
// copy.initializeCollectionEventListener = this.initializeCollectionEventListener;
355
// copy.postLoadEventListener = this.postLoadEventListener;
356
// copy.preLoadEventListener = this.preLoadEventListener;
357
// copy.postDeleteEventListener = this.postDeleteEventListener;
358
// copy.postUpdateEventListener = this.postUpdateEventListener;
359
// copy.postInsertEventListener = this.postInsertEventListener;
360
// copy.preDeleteEventListener = this.preDeleteEventListener;
361
// copy.preUpdateEventListener = this.preUpdateEventListener;
362
// copy.preInsertEventListener = this.preInsertEventListener;
363
// copy.saveOrUpdateCopyEventListener = this.saveOrUpdateCopyEventListener;
364
// copy.saveEventListener = this.saveEventListener;
365
// copy.updateEventListener = this.updateEventListener;
366

367         return copy;
368     }
369
370     private void checkListeners() {
371         BeanInfo JavaDoc beanInfo = null;
372         try {
373             beanInfo = Introspector.getBeanInfo( SessionEventListenerConfig.class, Object JavaDoc.class );
374             internalCheckListeners( beanInfo );
375         }
376         catch( IntrospectionException JavaDoc t ) {
377             throw new HibernateException( "Unable to validate listener config", t );
378         }
379         finally {
380             if ( beanInfo != null ) {
381                 // release the jdk internal caches everytime to ensure this
382
// plays nicely with destroyable class-loaders
383
Introspector.flushFromCaches( SessionEventListenerConfig.class );
384             }
385         }
386     }
387
388     private void internalCheckListeners(BeanInfo JavaDoc beanInfo) {
389         PropertyDescriptor JavaDoc[] pds = beanInfo.getPropertyDescriptors();
390         try {
391             for ( int i = 0, max = pds.length; i < max; i++ ) {
392                 final Object JavaDoc listener = pds[i].getReadMethod().invoke( this, READER_METHOD_ARGS );
393                 if ( listener == null ) {
394                     throw new HibernateException( "Listener [" + pds[i].getName() + "] was null" );
395                 }
396             }
397         }
398         catch( HibernateException e ) {
399             throw e;
400         }
401         catch( Throwable JavaDoc t ) {
402             throw new HibernateException( "Unable to validate listener config" );
403         }
404     }
405 }
406
Popular Tags