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 java.util.Iterator;
021    import java.util.List;
022    
023    import net.sf.hibernate.HibernateException;
024    import net.sf.hibernate.ObjectNotFoundException;
025    import net.sf.hibernate.Session;
026    
027    import org.aopalliance.intercept.ConstructorInvocation;
028    import org.aopalliance.intercept.MethodInvocation;
029    import org.objectweb.jac.core.AspectComponent;
030    import org.objectweb.jac.core.Interaction;
031    import org.objectweb.jac.core.NameRepository;
032    import org.objectweb.jac.core.Wrapper;
033    import org.objectweb.jac.util.Repository;
034    
035    /**
036     * This wrapper delimits the begining of a persistent session.
037     * 
038     * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr>
039     * @version 1.0
040     */
041    public class BeginPersistentSessionWrapper extends Wrapper {
042    
043        /** The gateway instance to Hibernate. */
044        private HibernateHelper hh = HibernateHelper.get();
045        
046        public BeginPersistentSessionWrapper( AspectComponent ac ) {
047            super(ac);
048        }
049        
050        /**
051         * Wrapping method around pointcuts
052         * where a persistent session begins.
053         */
054        public Object invoke( MethodInvocation invocation ) {
055            Interaction interaction = (Interaction) invocation;
056            try {
057                            return _begin(interaction);
058                    } catch (HibernateException e) {
059                            e.printStackTrace();
060                            System.exit(1);
061                    }
062            return null;
063        }
064        private Object _begin( Interaction interaction ) throws HibernateException {
065            
066            hh.openSessionAndBeginTx();
067            Session session = hh.getSession();
068                
069            /**
070             * Load all persistent objects.
071             * If they do not exist yet in the storage,
072             * create an entry for them (save).
073             */
074            HibernateAC hac = (HibernateAC) ac;
075            List pObjects = hac.getPersistentObjects();
076            for ( Iterator iter=pObjects.iterator() ; iter.hasNext() ; ) {
077                
078                String name = (String) iter.next();
079                Object object = names.getObject(name);
080                
081                try {
082                                    session.load(object,name);
083                } catch (ObjectNotFoundException e) {
084                    session.save(object,name);
085                            }
086                    }            
087                    
088            /**
089             * Proceed the interaction.
090             */
091            return proceed(interaction);
092        }
093        
094    
095        /**
096         * The reference towards the JAC name repository.
097         */
098        private Repository names = NameRepository.get();
099    
100        public Object construct(ConstructorInvocation invocation)
101            throws Throwable {
102            throw new Exception("This wrapper does not support constructor wrapping");
103        }
104    }