KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > transaction > TransactionAC


1 /*
2   Copyright (C) 2001 Renaud Pawlak
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.transaction;
19
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Vector JavaDoc;
23 import org.aopalliance.intercept.ConstructorInvocation;
24 import org.aopalliance.intercept.MethodInvocation;
25 import org.apache.log4j.Logger;
26 import org.objectweb.jac.core.AspectComponent;
27 import org.objectweb.jac.core.Interaction;
28 import org.objectweb.jac.core.Wrappee;
29 import org.objectweb.jac.core.Wrapper;
30 import org.objectweb.jac.core.Wrapping;
31 import org.objectweb.jac.util.Log;
32
33 /**
34  * This aspect component handle the transaction aspect within JAC
35  * applications.
36  *
37  * @author Renaud Pawlak */

38
39 public class TransactionAC extends AspectComponent implements TransactionConf {
40     static Logger logger = Logger.getLogger("transaction");
41
42    /**
43     * The default constructor. Wraps every application objects with a
44     * <code>DispatchTransactionWrapper</code> instance.
45     *
46     * @see DispatchTransactionWrapper
47     * @see DispatchTransactionWrapper#dispatch
48     */

49
50    public TransactionAC() {
51       pointcut("ALL", "ALL", "ALL",
52                DispatchTransactionWrapper.class.getName(),
53                null, true);
54    }
55
56    public void defineTransactionalMethods(String JavaDoc classExpr,
57                                           String JavaDoc methodExpr,
58                                           String JavaDoc objectExpr) {
59       pointcut(objectExpr, classExpr, methodExpr,
60                new TransactionWrapper(this), "handleTransaction", null);
61    }
62
63    /**
64     * This wrapper defines the transaction wrapper.
65     *
66     * <p>It defines a wrapping method that starts a transaction when a
67     * method is called and that commits it when the method returns
68     * normally.
69     *
70     * <code>Within the transaction, the cloning and the dispatching is
71     * ensured by an external
72     * <code>DispatchTransactionWrapper</code>.
73     *
74     * @see DispatchTransactionWrapper */

75
76    public class TransactionWrapper extends Wrapper {
77       /** The current transaction count (increments on each
78           transaction). */

79       int transactionCount=0;
80
81       public TransactionWrapper(AspectComponent ac) {
82          super(ac);
83       }
84
85       /**
86        * This wrapping method wraps a transactional method.
87        */

88       public Object JavaDoc handleTransaction(Interaction interaction) {
89          beginOfTransaction(interaction);
90          Object JavaDoc ret = proceed(interaction);
91          endOfTransaction(interaction);
92          return ret;
93       }
94       /**
95        * Initializes the transaction. */

96       public void beginOfTransaction(Interaction interaction) {
97          logger.debug("begin of transactional method: "+interaction.method);
98          Integer JavaDoc id = new Integer JavaDoc(++transactionCount);
99          this.attrdef("Transaction.id",id);
100          this.attrdef("Transaction"+id+".affectedObjects",new Vector JavaDoc());
101       }
102       /**
103        * Ends the transaction (when success). */

104       public void endOfTransaction(Interaction interaction) {
105          logger.debug("end of transactional method: "+interaction.method);
106          Integer JavaDoc id = (Integer JavaDoc)this.attr("Transaction.id");
107          Vector JavaDoc v = (Vector JavaDoc)this.attr("Transaction"+id+".affectedObjects");
108          this.attrdef("Transaction.commit",id);
109          Iterator JavaDoc it = v.iterator();
110          while( it.hasNext() ) {
111             Wrappee w = (Wrappee)it.next();
112             Wrapping.invokeRoleMethod(w,"commit",new Object JavaDoc[] {this.attr("Transaction.id")});
113          }
114          this.attrdef("Transaction"+id+".affectedObjects",null);
115       }
116       /**
117        * Handles an exception within a transaction (rollback).
118        */

119       public void catchException(Interaction interaction, Exception JavaDoc e) throws Exception JavaDoc {
120          logger.debug("exception in transactionnal method: "+interaction.method);
121          Integer JavaDoc id = (Integer JavaDoc)this.attr("Transaction.id");
122          Vector JavaDoc v = (Vector JavaDoc)this.attr("Transaction"+id+".affectedObjects");
123          this.attrdef("Transaction.rollback",id);
124          if( id != null && v != null ) {
125             Iterator JavaDoc it = v.iterator();
126             while( it.hasNext() ) {
127                Wrappee w = (Wrappee)it.next();
128                Wrapping.invokeRoleMethod(w,"rollback",new Object JavaDoc[] {this.attr("Transaction.id")});
129             }
130             this.attrdef("Transaction"+id+".affectedObjects",null);
131          }
132          throw e;
133       }
134
135     /* (non-Javadoc)
136      * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
137      */

138     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
139         // TODO Auto-generated method stub
140
return null;
141     }
142
143     /* (non-Javadoc)
144      * @see org.aopalliance.intercept.ConstructorInterceptor#construct(org.aopalliance.intercept.ConstructorInvocation)
145      */

146     public Object JavaDoc construct(ConstructorInvocation invocation) throws Throwable JavaDoc {
147         // TODO Auto-generated method stub
148
return null;
149     }
150    }
151 }
152
153
154
Popular Tags