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.distrans;
019    
020    
021    
022    import javax.transaction.NotSupportedException;
023    import javax.transaction.SystemException;
024    import javax.transaction.UserTransaction;
025    import org.aopalliance.intercept.ConstructorInvocation;
026    import org.aopalliance.intercept.MethodInvocation;
027    import org.objectweb.jac.core.AspectComponent;
028    import org.objectweb.jac.core.Interaction;
029    import org.objectweb.jac.core.Wrapper;
030    import org.objectweb.jac.util.Log;
031    
032    /**
033     * This wrapper delimits the begining of a transaction.
034     * 
035     * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr>
036     * @version 1.0
037     */
038    public class BeginTransactionWrapper extends Wrapper {
039    
040            /** The transaction. */
041            private UserTransaction usertx;
042    
043            /**
044             * @param ac  the AC managing this wrapper
045             */
046            public BeginTransactionWrapper(AspectComponent ac) {
047                    super(ac);
048                    usertx = JOTMHelper.get().getUserTransaction();
049            }
050    
051            public Object invoke(MethodInvocation invocation) throws Throwable {
052            Interaction interaction = (Interaction) invocation;
053            try {
054                return _begin(interaction);
055            } catch (Exception e) {
056                Log.error("Error while beginning transaction");
057                e.printStackTrace();
058            }
059            return null;
060            }
061    
062            private Object _begin(Interaction interaction)
063                    throws NotSupportedException, SystemException {
064    
065                    usertx.begin();
066    
067                    return proceed(interaction);
068            }
069    
070        public Object construct(ConstructorInvocation invocation)
071            throws Throwable {
072            throw new Exception("This wrapper does not support constructor wrapping");
073        }
074    }