001    /*
002      Copyright (C) 2001-2003 Lionel Seinturier <Lionel.Seinturier@lip6.fr>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.hibernate;
019    
020    import net.sf.hibernate.HibernateException;
021    import net.sf.hibernate.Transaction;
022    
023    import org.aopalliance.intercept.ConstructorInvocation;
024    import org.aopalliance.intercept.MethodInvocation;
025    import org.objectweb.jac.core.AspectComponent;
026    import org.objectweb.jac.core.Interaction;
027    import org.objectweb.jac.core.Wrapper;
028    
029    /**
030     * This wrapper delimits the end of a persistent session.
031     * 
032     * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr>
033     * @version 1.0
034     */
035    public class EndPersistentSessionWrapper extends Wrapper {
036    
037        /** The gateway instance to Hibernate. */
038        private HibernateHelper hh = HibernateHelper.get();
039        
040        public EndPersistentSessionWrapper( AspectComponent ac ) {        
041            super(ac);
042        }
043        
044        /**
045         * Wrapping method around pointcuts
046         * where a persistent session ends.
047         */
048        public Object invoke( MethodInvocation invocation ) {
049            Interaction interaction = (Interaction) invocation;
050            
051            Object ret = proceed(interaction);
052            
053            try {
054                Transaction tx = hh.getTx();
055                            tx.commit();
056                return ret;
057                    } catch (HibernateException e1) {
058                            e1.printStackTrace();
059                            System.exit(1);
060                    }
061            return null;
062        }
063    
064        public Object construct(ConstructorInvocation invocation)
065            throws Throwable {
066            throw new Exception("This wrapper does not support constructor wrapping");
067        }
068    }