KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > distrans > EndTransactionWrapper


1 /*
2   Copyright (C) 2001-2003 Lionel Seinturier <Lionel.Seinturier@lip6.fr>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.distrans;
19
20 import javax.transaction.HeuristicMixedException JavaDoc;
21 import javax.transaction.HeuristicRollbackException JavaDoc;
22 import javax.transaction.RollbackException JavaDoc;
23 import javax.transaction.SystemException JavaDoc;
24 import javax.transaction.UserTransaction JavaDoc;
25
26 import org.aopalliance.intercept.ConstructorInvocation;
27 import org.aopalliance.intercept.MethodInvocation;
28 import org.objectweb.jac.core.AspectComponent;
29 import org.objectweb.jac.core.Interaction;
30 import org.objectweb.jac.core.Wrapper;
31
32 /**
33  * This wrapper delimits the end of a transaction.
34  * This is an abstract class that needs an decide() implementation
35  * to commit or rollback the transaction.
36  *
37  * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr>
38  * @version 1.0
39  */

40 public abstract class EndTransactionWrapper extends Wrapper {
41
42     /** The transaction. */
43     private UserTransaction JavaDoc usertx;
44     
45     public EndTransactionWrapper(AspectComponent ac) {
46         super(ac);
47         usertx = JOTMHelper.get().getUserTransaction();
48     }
49     
50     /**
51      * Method to decide whether the transaction is to be commited
52      * or rollbacked.
53      *
54      * @return true if commit, false if rollback
55      */

56     public abstract boolean decide();
57     
58     /* (non-Javadoc)
59      * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
60      */

61     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
62         Interaction interaction = (Interaction) invocation;
63         try {
64             return _end(interaction);
65         } catch (Exception JavaDoc e) {
66             e.printStackTrace();
67         }
68
69         return null;
70     }
71     
72     private Object JavaDoc _end(Interaction interaction)
73         throws SecurityException JavaDoc, RollbackException JavaDoc, HeuristicMixedException JavaDoc,
74         HeuristicRollbackException JavaDoc, SystemException JavaDoc
75     {
76         Object JavaDoc ret = proceed(interaction);
77         
78         if (decide()) {
79             usertx.commit();
80         }
81         else {
82             usertx.rollback();
83         }
84             
85         return ret;
86     }
87     
88     public Object JavaDoc construct(ConstructorInvocation invocation) throws Throwable JavaDoc {
89         throw new Exception JavaDoc("This wrapper does not support constructor wrapping");
90     }
91 }
92
Popular Tags