KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.util.Hashtable JavaDoc;
23 import java.util.Vector JavaDoc;
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 /**
35  * This wrapper wraps all JAC objects that can be part of a
36  * transaction in order to dispatch the transactions to local clones
37  * and to commit them. */

38
39 public class DispatchTransactionWrapper extends Wrapper {
40     static Logger logger = Logger.getLogger("transaction");
41
42     Hashtable JavaDoc clones = new Hashtable JavaDoc();
43     Hashtable JavaDoc originals = new Hashtable JavaDoc();
44
45     public DispatchTransactionWrapper(AspectComponent ac) {
46         super(ac);
47     }
48
49     /**
50      * Dispatches a call on a wrappee transaction-clone depending on
51      * the current transaction if any (if not, it performs a regular
52      * call). */

53
54     public Object JavaDoc dispatch(Interaction interaction) {
55         Integer JavaDoc id = (Integer JavaDoc)attr("Transaction.id");
56         Integer JavaDoc commit = (Integer JavaDoc)attr("Transaction.commit");
57         Integer JavaDoc rollback = (Integer JavaDoc)attr("Transaction.rollback");
58         // none transaction is active => local call
59
if( id == null ) {
60             return proceed(interaction);
61         } else if( commit != null ) {
62             if( commit.equals( id ) ) {
63                 // this transaction is committing...
64
return proceed(interaction);
65             }
66         } else if( rollback != null ) {
67             if( rollback.equals( id ) ) {
68                 // this transaction is rollbacking...
69
return proceed(interaction);
70             }
71         } else {
72             if( ! clones.containsKey(id) ) {
73                 logger.debug("creating a new clone for transaction "+id);
74                 // creates a new original object
75
originals.put(id,Wrapping.clone(interaction.wrappee));
76                 // creates a new clone
77
clones.put(id,Wrapping.clone(interaction.wrappee));
78                 // memorize that the object is part of the transaction
79
logger.debug(interaction.wrappee+" is part of a transaction");
80                 Vector JavaDoc affectedObjects = (Vector JavaDoc)attr("Transaction"+id+".affectedObjects");
81                 affectedObjects.add(interaction.wrappee);
82             }
83             logger.debug("delegating to the clone "+id);
84             // delegate to the transaction's clone
85
return interaction.invoke(clones.get(id));
86         }
87         return proceed(interaction);
88     }
89
90     /**
91      * Commits the transaction on the wrappee (role method).
92      *
93      * <p>This method copies the transaction-clone state to the
94      * original object.
95      *
96      * @param transactionId the transaction to be commited */

97
98     public void commit(Wrappee wrappee, Integer JavaDoc transactionId) throws Exception JavaDoc {
99         logger.debug("committing transaction "+transactionId+
100                      " on object "+wrappee);
101         if( clones.containsKey(transactionId) ) {
102             // remove the transaction state
103
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     /**
112      * Rollbacks the transaction on the wrappee (role method).
113      *
114      * @param transactionId the transaction to be rollbacked */

115
116     public void rollback(Wrappee wrappee, Integer JavaDoc transactionId) {
117         logger.debug("rollbacking transaction "+transactionId+
118                      " on object "+wrappee);
119         if( clones.containsKey(transactionId) ) {
120             // remove the transaction state
121
clones.remove(transactionId);
122             originals.remove(transactionId);
123         }
124     }
125
126     /* (non-Javadoc)
127      * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
128      */

129     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
130         // TODO Auto-generated method stub
131
throw new Exception JavaDoc("Unimplemented method: invoke(MethodInvocation invocation)");
132     }
133
134     /* (non-Javadoc)
135      * @see org.aopalliance.intercept.ConstructorInterceptor#construct(org.aopalliance.intercept.ConstructorInvocation)
136      */

137     public Object JavaDoc construct(ConstructorInvocation invocation) throws Throwable JavaDoc {
138         // TODO Auto-generated method stub
139
throw new Exception JavaDoc("Unimplemented method: construct(MethodInvocation invocation)");
140     }
141
142 }
143
144
145
Popular Tags