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    import javax.transaction.HeuristicMixedException;
021    import javax.transaction.HeuristicRollbackException;
022    import javax.transaction.RollbackException;
023    import javax.transaction.SystemException;
024    import javax.transaction.UserTransaction;
025    
026    import org.aopalliance.intercept.ConstructorInvocation;
027    import org.aopalliance.intercept.MethodInvocation;
028    import org.objectweb.jac.core.AspectComponent;
029    import org.objectweb.jac.core.Interaction;
030    import org.objectweb.jac.core.Wrapper;
031    
032    /**
033     * This wrapper delimits the end of a transaction.
034     * This is an abstract class that needs an decide() implementation
035     * to commit or rollback the transaction.
036     * 
037     * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr>
038     * @version 1.0
039     */
040    public abstract class EndTransactionWrapper extends Wrapper {
041    
042        /** The transaction. */
043        private UserTransaction usertx;
044        
045        public EndTransactionWrapper(AspectComponent ac) {
046            super(ac);
047            usertx = JOTMHelper.get().getUserTransaction();
048        }
049        
050        /**
051         * Method to decide whether the transaction is to be commited
052         * or rollbacked.
053         * 
054         * @return true if commit, false if rollback
055         */
056        public abstract boolean decide();
057        
058        /* (non-Javadoc)
059         * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
060         */
061        public Object invoke(MethodInvocation invocation) throws Throwable {
062            Interaction interaction = (Interaction) invocation;
063                    try {
064                            return _end(interaction);
065                    } catch (Exception e) {
066                            e.printStackTrace();
067                    }
068    
069            return null;
070        }
071        
072        private Object _end(Interaction interaction)
073            throws SecurityException, RollbackException, HeuristicMixedException,
074            HeuristicRollbackException, SystemException 
075        {
076            Object ret = proceed(interaction);
077            
078            if (decide()) {
079                usertx.commit();
080            }
081            else {
082                usertx.rollback();
083            }
084                
085            return ret;
086        }
087        
088        public Object construct(ConstructorInvocation invocation) throws Throwable {
089            throw new Exception("This wrapper does not support constructor wrapping");
090        }
091    }