1 17 18 package org.objectweb.jac.aspects.transaction; 19 20 21 22 import java.util.Hashtable ; 23 import java.util.Vector ; 24 import org.aopalliance.intercept.ConstructorInvocation; 25 import org.aopalliance.intercept.MethodInvocation; 26 import org.apache.log4j.Logger; 27 import org.objectweb.jac.core.AspectComponent; 28 import org.objectweb.jac.core.Interaction; 29 import org.objectweb.jac.core.Wrappee; 30 import org.objectweb.jac.core.Wrapper; 31 import org.objectweb.jac.core.Wrapping; 32 import org.objectweb.jac.util.Log; 33 34 38 39 public class DispatchTransactionWrapper extends Wrapper { 40 static Logger logger = Logger.getLogger("transaction"); 41 42 Hashtable clones = new Hashtable (); 43 Hashtable originals = new Hashtable (); 44 45 public DispatchTransactionWrapper(AspectComponent ac) { 46 super(ac); 47 } 48 49 53 54 public Object dispatch(Interaction interaction) { 55 Integer id = (Integer )attr("Transaction.id"); 56 Integer commit = (Integer )attr("Transaction.commit"); 57 Integer rollback = (Integer )attr("Transaction.rollback"); 58 if( id == null ) { 60 return proceed(interaction); 61 } else if( commit != null ) { 62 if( commit.equals( id ) ) { 63 return proceed(interaction); 65 } 66 } else if( rollback != null ) { 67 if( rollback.equals( id ) ) { 68 return proceed(interaction); 70 } 71 } else { 72 if( ! clones.containsKey(id) ) { 73 logger.debug("creating a new clone for transaction "+id); 74 originals.put(id,Wrapping.clone(interaction.wrappee)); 76 clones.put(id,Wrapping.clone(interaction.wrappee)); 78 logger.debug(interaction.wrappee+" is part of a transaction"); 80 Vector affectedObjects = (Vector )attr("Transaction"+id+".affectedObjects"); 81 affectedObjects.add(interaction.wrappee); 82 } 83 logger.debug("delegating to the clone "+id); 84 return interaction.invoke(clones.get(id)); 86 } 87 return proceed(interaction); 88 } 89 90 97 98 public void commit(Wrappee wrappee, Integer transactionId) throws Exception { 99 logger.debug("committing transaction "+transactionId+ 100 " on object "+wrappee); 101 if( clones.containsKey(transactionId) ) { 102 Wrappee clone = (Wrappee)clones.get(transactionId); 104 Wrappee original = (Wrappee)originals.get(transactionId); 105 clones.remove(transactionId); 106 originals.remove(transactionId); 107 Merging.merge(wrappee,original,clone); 108 } 109 } 110 111 115 116 public void rollback(Wrappee wrappee, Integer transactionId) { 117 logger.debug("rollbacking transaction "+transactionId+ 118 " on object "+wrappee); 119 if( clones.containsKey(transactionId) ) { 120 clones.remove(transactionId); 122 originals.remove(transactionId); 123 } 124 } 125 126 129 public Object invoke(MethodInvocation invocation) throws Throwable { 130 throw new Exception ("Unimplemented method: invoke(MethodInvocation invocation)"); 132 } 133 134 137 public Object construct(ConstructorInvocation invocation) throws Throwable { 138 throw new Exception ("Unimplemented method: construct(MethodInvocation invocation)"); 140 } 141 142 } 143 144 145 | Popular Tags |