001    /*******************************************************************************
002     * Copyright (c) PicoContainer Organization. All rights reserved.
003     * ---------------------------------------------------------------------------
004     * The software in this package is published under the terms of the BSD style
005     * license a copy of which has been included with this distribution in the
006     * license.html file.
007     ******************************************************************************/
008    
009    package org.picocontainer.persistence.hibernate;
010    
011    import java.io.Serializable;
012    import java.sql.Connection;
013    
014    import org.hibernate.CacheMode;
015    import org.hibernate.Criteria;
016    import org.hibernate.EntityMode;
017    import org.hibernate.Filter;
018    import org.hibernate.FlushMode;
019    import org.hibernate.HibernateException;
020    import org.hibernate.LockMode;
021    import org.hibernate.Query;
022    import org.hibernate.ReplicationMode;
023    import org.hibernate.SQLQuery;
024    import org.hibernate.Session;
025    import org.hibernate.SessionFactory;
026    import org.hibernate.Transaction;
027    import org.hibernate.stat.SessionStatistics;
028    
029    /**
030     * Abstract session decorator
031     */
032    public abstract class AbstractSessionDecorator implements Session {
033    
034        /**
035         * Returns the Session delegate
036         * 
037         * @return The Session
038         */
039        public abstract Session getDelegate();
040    
041        /**
042         * Perform actions to dispose "burned" session properly.
043         */
044        public abstract void invalidateDelegate();
045    
046        /**
047         * Invalidates the session calling {@link #invalidateDelegate()} and just
048         * return the <code>cause</code> back.
049         * 
050         * @return
051         * @param cause
052         */
053        protected RuntimeException handleException(final RuntimeException cause) {
054            try {
055                invalidateDelegate();
056            } catch (RuntimeException e) {
057                return e;
058            }
059            return cause;
060        }
061    
062        /** {@inheritDoc} * */
063        public Transaction beginTransaction() {
064            try {
065                return getDelegate().beginTransaction();
066            } catch (HibernateException ex) {
067                throw handleException(ex);
068            }
069        }
070    
071        /** {@inheritDoc} * */
072        public void cancelQuery() {
073            try {
074                getDelegate().cancelQuery();
075            } catch (HibernateException ex) {
076                throw handleException(ex);
077            }
078        }
079    
080        /** {@inheritDoc} * */
081        public void clear() {
082            try {
083                getDelegate().clear();
084            } catch (HibernateException ex) {
085                throw handleException(ex);
086            }
087        }
088    
089        /** {@inheritDoc} * */
090        public Connection close() {
091            try {
092                return getDelegate().close();
093            } catch (HibernateException ex) {
094                throw handleException(ex);
095            }
096        }
097    
098        /** {@inheritDoc} * */
099        @Deprecated
100        public Connection connection() {
101            try {
102                return getDelegate().connection();
103            } catch (HibernateException ex) {
104                throw handleException(ex);
105            }
106        }
107    
108        /** {@inheritDoc} * */
109        public boolean contains(final Object object) {
110            try {
111                return getDelegate().contains(object);
112            } catch (HibernateException ex) {
113                throw handleException(ex);
114            }
115        }
116    
117        /** {@inheritDoc} * */
118        @SuppressWarnings("unchecked")
119        public Criteria createCriteria(final Class persistentClass) {
120            try {
121                return getDelegate().createCriteria(persistentClass);
122            } catch (HibernateException ex) {
123                throw handleException(ex);
124            }
125        }
126    
127        /** {@inheritDoc} * */
128        @SuppressWarnings("unchecked")
129        public Criteria createCriteria(final Class persistentClass, final String alias) {
130            try {
131                return getDelegate().createCriteria(persistentClass, alias);
132            } catch (HibernateException ex) {
133                throw handleException(ex);
134            }
135        }
136    
137        /** {@inheritDoc} * */
138        public Criteria createCriteria(final String entityName) {
139            try {
140                return getDelegate().createCriteria(entityName);
141            } catch (HibernateException ex) {
142                throw handleException(ex);
143            }
144        }
145    
146        /** {@inheritDoc} * */
147        public Criteria createCriteria(final String entityName, final String alias) {
148            try {
149                return getDelegate().createCriteria(entityName, alias);
150            } catch (HibernateException ex) {
151                throw handleException(ex);
152            }
153        }
154    
155        /** {@inheritDoc} * */
156        public Query createFilter(final Object collection, final String queryString) {
157            try {
158                return getDelegate().createFilter(collection, queryString);
159            } catch (HibernateException ex) {
160                throw handleException(ex);
161            }
162        }
163    
164        /** {@inheritDoc} * */
165        public Query createQuery(final String queryString) {
166            try {
167                return getDelegate().createQuery(queryString);
168            } catch (HibernateException ex) {
169                throw handleException(ex);
170            }
171        }
172    
173        /** {@inheritDoc} * */
174        public SQLQuery createSQLQuery(final String queryString) {
175            try {
176                return getDelegate().createSQLQuery(queryString);
177            } catch (HibernateException ex) {
178                throw handleException(ex);
179            }
180        }
181    
182        /** {@inheritDoc} * */
183        public void delete(final Object object) {
184            try {
185                getDelegate().delete(object);
186            } catch (HibernateException ex) {
187                throw handleException(ex);
188            }
189        }
190    
191        /** {@inheritDoc} * */
192        public void delete(final String entityName, final Object object) {
193            try {
194                getDelegate().delete(entityName, object);
195            } catch (HibernateException ex) {
196                throw handleException(ex);
197            }
198        }
199    
200        /** {@inheritDoc} * */
201        public void disableFilter(final String filterName) {
202            try {
203                getDelegate().disableFilter(filterName);
204            } catch (HibernateException ex) {
205                throw handleException(ex);
206            }
207        }
208    
209        /** {@inheritDoc} * */
210        public Connection disconnect() {
211            try {
212                return getDelegate().disconnect();
213            } catch (HibernateException ex) {
214                throw handleException(ex);
215            }
216        }
217    
218        /** {@inheritDoc} * */
219        public Filter enableFilter(final String filterName) {
220            try {
221                return getDelegate().enableFilter(filterName);
222            } catch (HibernateException ex) {
223                throw handleException(ex);
224            }
225        }
226    
227        /** {@inheritDoc} * */
228        public void evict(final Object object) {
229            try {
230                getDelegate().evict(object);
231            } catch (HibernateException ex) {
232                throw handleException(ex);
233            }
234        }
235    
236        /** {@inheritDoc} * */
237        public void flush() {
238            try {
239                getDelegate().flush();
240            } catch (HibernateException ex) {
241                throw handleException(ex);
242            }
243        }
244    
245        /** {@inheritDoc} * */
246        @SuppressWarnings("unchecked")
247        public Object get(final Class clazz, final Serializable id) {
248            try {
249                return getDelegate().get(clazz, id);
250            } catch (HibernateException ex) {
251                throw handleException(ex);
252            }
253        }
254    
255        /** {@inheritDoc} * */
256        @SuppressWarnings("unchecked")
257        public Object get(final Class clazz, final Serializable id, final LockMode lockMode) {
258            try {
259                return getDelegate().get(clazz, id, lockMode);
260            } catch (HibernateException ex) {
261                throw handleException(ex);
262            }
263        }
264    
265        /** {@inheritDoc} * */
266        public Object get(final String entityName, final Serializable id) {
267            try {
268                return getDelegate().get(entityName, id);
269            } catch (HibernateException ex) {
270                throw handleException(ex);
271            }
272        }
273    
274        /** {@inheritDoc} * */
275        public Object get(final String entityName, final Serializable id, final LockMode lockMode) {
276            try {
277                return getDelegate().get(entityName, id, lockMode);
278            } catch (HibernateException ex) {
279                throw handleException(ex);
280            }
281        }
282    
283        /** {@inheritDoc} * */
284        public CacheMode getCacheMode() {
285            try {
286                return getDelegate().getCacheMode();
287            } catch (HibernateException ex) {
288                throw handleException(ex);
289            }
290        }
291    
292        /** {@inheritDoc} * */
293        public LockMode getCurrentLockMode(final Object object) {
294            try {
295                return getDelegate().getCurrentLockMode(object);
296            } catch (HibernateException ex) {
297                throw handleException(ex);
298            }
299        }
300    
301        /** {@inheritDoc} * */
302        public Filter getEnabledFilter(final String filterName) {
303            try {
304                return getDelegate().getEnabledFilter(filterName);
305            } catch (HibernateException ex) {
306                throw handleException(ex);
307            }
308        }
309    
310        /** {@inheritDoc} * */
311        public EntityMode getEntityMode() {
312            try {
313                return getDelegate().getEntityMode();
314            } catch (HibernateException ex) {
315                throw handleException(ex);
316            }
317        }
318    
319        /** {@inheritDoc} * */
320        public String getEntityName(final Object object) {
321            try {
322                return getDelegate().getEntityName(object);
323            } catch (HibernateException ex) {
324                throw handleException(ex);
325            }
326        }
327    
328        /** {@inheritDoc} * */
329        public FlushMode getFlushMode() {
330            try {
331                return getDelegate().getFlushMode();
332            } catch (HibernateException ex) {
333                throw handleException(ex);
334            }
335        }
336    
337        /** {@inheritDoc} * */
338        public Serializable getIdentifier(final Object object) {
339            try {
340                return getDelegate().getIdentifier(object);
341            } catch (HibernateException ex) {
342                throw handleException(ex);
343            }
344        }
345    
346        /** {@inheritDoc} * */
347        public Query getNamedQuery(final String queryName) {
348            try {
349                return getDelegate().getNamedQuery(queryName);
350            } catch (HibernateException ex) {
351                throw handleException(ex);
352            }
353        }
354    
355        /** {@inheritDoc} * */
356        public Session getSession(final EntityMode entityMode) {
357            try {
358                return getDelegate().getSession(entityMode);
359            } catch (HibernateException ex) {
360                throw handleException(ex);
361            }
362        }
363    
364        /** {@inheritDoc} * */
365        public SessionFactory getSessionFactory() {
366            try {
367                return getDelegate().getSessionFactory();
368            } catch (HibernateException ex) {
369                throw handleException(ex);
370            }
371        }
372    
373        /** {@inheritDoc} * */
374        public SessionStatistics getStatistics() {
375            try {
376                return getDelegate().getStatistics();
377            } catch (HibernateException ex) {
378                throw handleException(ex);
379            }
380        }
381    
382        /** {@inheritDoc} * */
383        public Transaction getTransaction() {
384            try {
385                return getDelegate().getTransaction();
386            } catch (HibernateException ex) {
387                throw handleException(ex);
388            }
389        }
390    
391        /** {@inheritDoc} * */
392        public boolean isConnected() {
393            try {
394                return getDelegate().isConnected();
395            } catch (HibernateException ex) {
396                throw handleException(ex);
397            }
398        }
399    
400        /** {@inheritDoc} * */
401        public boolean isDirty() {
402            try {
403                return getDelegate().isDirty();
404            } catch (HibernateException ex) {
405                throw handleException(ex);
406            }
407        }
408    
409        /** {@inheritDoc} * */
410        public boolean isOpen() {
411            try {
412                return getDelegate().isOpen();
413            } catch (HibernateException ex) {
414                throw handleException(ex);
415            }
416        }
417    
418        /** {@inheritDoc} * */
419        @SuppressWarnings("unchecked")
420        public Object load(final Class theClass, final Serializable id) {
421            try {
422                return getDelegate().load(theClass, id);
423            } catch (HibernateException ex) {
424                throw handleException(ex);
425            }
426        }
427    
428        /** {@inheritDoc} * */
429        @SuppressWarnings("unchecked")
430        public Object load(final Class theClass, final Serializable id, final LockMode lockMode) {
431            try {
432                return getDelegate().load(theClass, id, lockMode);
433            } catch (HibernateException ex) {
434                throw handleException(ex);
435            }
436        }
437    
438        /** {@inheritDoc} * */
439        public void load(final Object object, final Serializable id) {
440            try {
441                getDelegate().load(object, id);
442            } catch (HibernateException ex) {
443                throw handleException(ex);
444            }
445        }
446    
447        /** {@inheritDoc} * */
448        public Object load(final String entityName, final Serializable id) {
449            try {
450                return getDelegate().load(entityName, id);
451            } catch (HibernateException ex) {
452                throw handleException(ex);
453            }
454        }
455    
456        /** {@inheritDoc} * */
457        public Object load(final String entityName, final Serializable id, final LockMode lockMode) {
458            try {
459                return getDelegate().load(entityName, id, lockMode);
460            } catch (HibernateException ex) {
461                throw handleException(ex);
462            }
463        }
464    
465        /** {@inheritDoc} * */
466        public void lock(final Object object, final LockMode lockMode) {
467            try {
468                getDelegate().lock(object, lockMode);
469            } catch (HibernateException ex) {
470                throw handleException(ex);
471            }
472        }
473    
474        /** {@inheritDoc} * */
475        public void lock(final String entityEntity, final Object object, final LockMode lockMode) {
476            try {
477                getDelegate().lock(entityEntity, object, lockMode);
478            } catch (HibernateException ex) {
479                throw handleException(ex);
480            }
481        }
482    
483        /** {@inheritDoc} * */
484        public Object merge(final Object object) {
485            try {
486                return getDelegate().merge(object);
487            } catch (HibernateException ex) {
488                throw handleException(ex);
489            }
490        }
491    
492        /** {@inheritDoc} * */
493        public Object merge(final String entityName, final Object object) {
494            try {
495                return getDelegate().merge(entityName, object);
496            } catch (HibernateException ex) {
497                throw handleException(ex);
498            }
499        }
500    
501        /** {@inheritDoc} * */
502        public void persist(final Object object) {
503            try {
504                getDelegate().persist(object);
505            } catch (HibernateException ex) {
506                throw handleException(ex);
507            }
508        }
509    
510        /** {@inheritDoc} * */
511        public void persist(final String entityName, final Object object) {
512            try {
513                getDelegate().persist(entityName, object);
514            } catch (HibernateException ex) {
515                throw handleException(ex);
516            }
517        }
518    
519        /** {@inheritDoc} * */
520        @Deprecated
521        public void reconnect() {
522            try {
523                getDelegate().reconnect();
524            } catch (HibernateException ex) {
525                throw handleException(ex);
526            }
527        }
528    
529        /** {@inheritDoc} * */
530        public void reconnect(final Connection conn) {
531            try {
532                getDelegate().reconnect(conn);
533            } catch (HibernateException ex) {
534                throw handleException(ex);
535            }
536        }
537    
538        /** {@inheritDoc} * */
539        public void refresh(final Object object) {
540            try {
541                getDelegate().refresh(object);
542            } catch (HibernateException ex) {
543                throw handleException(ex);
544            }
545        }
546    
547        /** {@inheritDoc} * */
548        public void refresh(final Object object, final LockMode lockMode) {
549            try {
550                getDelegate().refresh(object, lockMode);
551            } catch (HibernateException ex) {
552                throw handleException(ex);
553            }
554        }
555    
556        /** {@inheritDoc} * */
557        public void replicate(final Object object, final ReplicationMode replicationMode) {
558            try {
559                getDelegate().replicate(object, replicationMode);
560            } catch (HibernateException ex) {
561                throw handleException(ex);
562            }
563        }
564    
565        /** {@inheritDoc} * */
566        public void replicate(final String entityName, final Object object, final ReplicationMode replicationMode) {
567            try {
568                getDelegate().replicate(entityName, object, replicationMode);
569            } catch (HibernateException ex) {
570                throw handleException(ex);
571            }
572        }
573    
574        /** {@inheritDoc} * */
575        public Serializable save(final Object object) {
576            try {
577                return getDelegate().save(object);
578            } catch (HibernateException ex) {
579                throw handleException(ex);
580            }
581        }
582    
583        /** {@inheritDoc} * */
584        public Serializable save(final String entityName, final Object object) {
585            try {
586                return getDelegate().save(entityName, object);
587            } catch (HibernateException ex) {
588                throw handleException(ex);
589            }
590        }
591    
592        /** {@inheritDoc} * */
593        public void saveOrUpdate(final Object object) {
594            try {
595                getDelegate().saveOrUpdate(object);
596            } catch (HibernateException ex) {
597                throw handleException(ex);
598            }
599        }
600    
601        /** {@inheritDoc} * */
602        public void saveOrUpdate(final String entityName, final Object object) {
603            try {
604                getDelegate().saveOrUpdate(entityName, object);
605            } catch (HibernateException ex) {
606                throw handleException(ex);
607            }
608        }
609    
610        /** {@inheritDoc} * */
611        public void setCacheMode(final CacheMode cacheMode) {
612            try {
613                getDelegate().setCacheMode(cacheMode);
614            } catch (HibernateException ex) {
615                throw handleException(ex);
616            }
617        }
618    
619        /** {@inheritDoc} * */
620        public void setReadOnly(final Object entity, final boolean readOnly) {
621            try {
622                getDelegate().setReadOnly(entity, readOnly);
623            } catch (HibernateException ex) {
624                throw handleException(ex);
625            }
626        }
627    
628        /** {@inheritDoc} * */
629        public void setFlushMode(final FlushMode value) {
630            try {
631                getDelegate().setFlushMode(value);
632            } catch (HibernateException ex) {
633                throw handleException(ex);
634            }
635        }
636    
637        /** {@inheritDoc} * */
638        public void update(final Object object) {
639            try {
640                getDelegate().update(object);
641            } catch (HibernateException ex) {
642                throw handleException(ex);
643            }
644        }
645    
646        /** {@inheritDoc} * */
647        public void update(final String entityName, final Object object) {
648            try {
649                getDelegate().update(entityName, object);
650            } catch (HibernateException ex) {
651                throw handleException(ex);
652            }
653        }
654    
655    }