1 17 18 package org.objectweb.jac.aspects.transaction; 19 20 21 import java.util.Iterator ; 22 import java.util.Vector ; 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 38 39 public class TransactionAC extends AspectComponent implements TransactionConf { 40 static Logger logger = Logger.getLogger("transaction"); 41 42 49 50 public TransactionAC() { 51 pointcut("ALL", "ALL", "ALL", 52 DispatchTransactionWrapper.class.getName(), 53 null, true); 54 } 55 56 public void defineTransactionalMethods(String classExpr, 57 String methodExpr, 58 String objectExpr) { 59 pointcut(objectExpr, classExpr, methodExpr, 60 new TransactionWrapper(this), "handleTransaction", null); 61 } 62 63 75 76 public class TransactionWrapper extends Wrapper { 77 79 int transactionCount=0; 80 81 public TransactionWrapper(AspectComponent ac) { 82 super(ac); 83 } 84 85 88 public Object handleTransaction(Interaction interaction) { 89 beginOfTransaction(interaction); 90 Object ret = proceed(interaction); 91 endOfTransaction(interaction); 92 return ret; 93 } 94 96 public void beginOfTransaction(Interaction interaction) { 97 logger.debug("begin of transactional method: "+interaction.method); 98 Integer id = new Integer (++transactionCount); 99 this.attrdef("Transaction.id",id); 100 this.attrdef("Transaction"+id+".affectedObjects",new Vector ()); 101 } 102 104 public void endOfTransaction(Interaction interaction) { 105 logger.debug("end of transactional method: "+interaction.method); 106 Integer id = (Integer )this.attr("Transaction.id"); 107 Vector v = (Vector )this.attr("Transaction"+id+".affectedObjects"); 108 this.attrdef("Transaction.commit",id); 109 Iterator it = v.iterator(); 110 while( it.hasNext() ) { 111 Wrappee w = (Wrappee)it.next(); 112 Wrapping.invokeRoleMethod(w,"commit",new Object [] {this.attr("Transaction.id")}); 113 } 114 this.attrdef("Transaction"+id+".affectedObjects",null); 115 } 116 119 public void catchException(Interaction interaction, Exception e) throws Exception { 120 logger.debug("exception in transactionnal method: "+interaction.method); 121 Integer id = (Integer )this.attr("Transaction.id"); 122 Vector v = (Vector )this.attr("Transaction"+id+".affectedObjects"); 123 this.attrdef("Transaction.rollback",id); 124 if( id != null && v != null ) { 125 Iterator it = v.iterator(); 126 while( it.hasNext() ) { 127 Wrappee w = (Wrappee)it.next(); 128 Wrapping.invokeRoleMethod(w,"rollback",new Object [] {this.attr("Transaction.id")}); 129 } 130 this.attrdef("Transaction"+id+".affectedObjects",null); 131 } 132 throw e; 133 } 134 135 138 public Object invoke(MethodInvocation invocation) throws Throwable { 139 return null; 141 } 142 143 146 public Object construct(ConstructorInvocation invocation) throws Throwable { 147 return null; 149 } 150 } 151 } 152 153 154 | Popular Tags |