KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > tx > TxInterceptor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aspects.tx;
23
24 import javax.transaction.Transaction JavaDoc;
25 import javax.transaction.TransactionManager JavaDoc;
26 import org.jboss.aop.advice.Interceptor;
27 import org.jboss.aop.joinpoint.Invocation;
28 import org.jboss.logging.Logger;
29
30 /**
31  * This interceptor handles transactions for AOP
32  *
33  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
34  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
35  * @version $Revision: 57070 $
36  */

37 public class TxInterceptor
38 {
39    private static final Logger log = Logger.getLogger(TxInterceptor.class);
40    private static final TxTimeoutReader txTimeoutReader = TxTimeoutReaderFactory.getTxTimeoutReader();
41
42    public static class Never implements Interceptor
43    {
44       protected TransactionManager JavaDoc tm;
45       protected TxPolicy policy;
46       protected int timeout;
47       
48       public Never(TransactionManager JavaDoc tm, TxPolicy policy)
49       {
50          this(tm, policy, -1);
51       }
52
53       public Never(TransactionManager JavaDoc tm, TxPolicy policy, int timeout)
54       {
55          this.tm = tm;
56          this.policy = policy;
57          this.timeout = timeout;
58       }
59
60       public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
61       {
62          if (tm.getTransaction() != null)
63          {
64             throw new IllegalStateException JavaDoc("Transaction present on server in Never call");
65          } // end of if ()
66
return policy.invokeInNoTx(invocation);
67       }
68
69       public String JavaDoc getName()
70       {
71          return this.getClass().getName();
72       }
73    }
74
75    public static class NotSupported implements Interceptor
76    {
77       protected TransactionManager JavaDoc tm;
78       protected TxPolicy policy;
79       protected int timeout;
80       
81       public NotSupported(TransactionManager JavaDoc tm, TxPolicy policy)
82       {
83          this(tm, policy, -1);
84       }
85
86       public NotSupported(TransactionManager JavaDoc tm, TxPolicy policy, int timeout)
87       {
88          this.tm = tm;
89          this.policy = policy;
90          this.timeout = timeout;
91       }
92
93       public Object JavaDoc invoke(Invocation invocation)
94               throws Throwable JavaDoc
95       {
96          Transaction JavaDoc tx = tm.getTransaction();
97          if (tx != null)
98          {
99             tm.suspend();
100             try
101             {
102                return policy.invokeInNoTx(invocation);
103             }
104             finally
105             {
106                tm.resume(tx);
107             }
108
109          }
110          else
111          {
112             return policy.invokeInNoTx(invocation);
113          }
114       }
115
116       public String JavaDoc getName()
117       {
118          return this.getClass().getName();
119       }
120    }
121
122    public static class Supports implements Interceptor
123    {
124       protected TransactionManager JavaDoc tm;
125       protected TxPolicy policy;
126       protected int timeout;
127       
128       public Supports(TransactionManager JavaDoc tm, TxPolicy policy)
129       {
130          this(tm, policy, -1);
131       }
132
133       public Supports(TransactionManager JavaDoc tm, TxPolicy policy, int timeout)
134       {
135          this.tm = tm;
136          this.policy = policy;
137          this.timeout = timeout;
138       }
139
140       public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
141       {
142          if (tm.getTransaction() == null)
143          {
144             return policy.invokeInNoTx(invocation);
145          }
146          else
147          {
148             return policy.invokeInCallerTx(invocation, tm.getTransaction());
149          }
150       }
151
152       public String JavaDoc getName()
153       {
154          return this.getClass().getName();
155       }
156    }
157
158    public static class Required implements Interceptor
159    {
160       private static final Logger log = Logger.getLogger(Required.class);
161       
162       protected TransactionManager JavaDoc tm;
163       protected TxPolicy policy;
164       protected int timeout;
165       
166       public Required(TransactionManager JavaDoc tm, TxPolicy policy)
167       {
168          this(tm, policy, -1);
169       }
170
171       public Required(TransactionManager JavaDoc tm, TxPolicy policy, int timeout)
172       {
173          this.tm = tm;
174          this.policy = policy;
175          this.timeout = timeout;
176       }
177
178       public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
179       {
180          int oldTimeout = txTimeoutReader.getTransactionTimeOut(tm);
181
182          try
183          {
184             if (timeout != -1 && tm != null)
185             {
186                tm.setTransactionTimeout(timeout);
187             }
188                
189             Transaction JavaDoc tx = tm.getTransaction();
190             
191             if (tx == null)
192             {
193                return policy.invokeInOurTx(invocation, tm);
194             }
195             else
196             {
197                return policy.invokeInCallerTx(invocation, tx);
198             }
199          }
200          finally
201          {
202             if (tm != null)
203             {
204                tm.setTransactionTimeout(oldTimeout);
205             }
206          }
207       }
208
209       public String JavaDoc getName()
210       {
211          return this.getClass().getName();
212       }
213    }
214
215    public static class RequiresNew implements Interceptor
216    {
217       protected TransactionManager JavaDoc tm;
218       protected TxPolicy policy;
219       protected int timeout;
220       
221       public RequiresNew(TransactionManager JavaDoc tm, TxPolicy policy)
222       {
223          this(tm, policy, -1);
224       }
225
226       public RequiresNew(TransactionManager JavaDoc tm, TxPolicy policy, int timeout)
227       {
228          this.tm = tm;
229          this.policy = policy;
230          this.timeout = timeout;
231       }
232
233       public String JavaDoc getName()
234       {
235          return this.getClass().getName();
236       }
237
238       public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
239       {
240          int oldTimeout = txTimeoutReader.getTransactionTimeOut(tm);
241
242          try
243          {
244             if (timeout != -1 && tm != null)
245             {
246                tm.setTransactionTimeout(timeout);
247             }
248             
249             Transaction JavaDoc tx = tm.getTransaction();
250             if (tx != null)
251             {
252                tm.suspend();
253                try
254                {
255                   return policy.invokeInOurTx(invocation, tm);
256                }
257                finally
258                {
259                   tm.resume(tx);
260                }
261             }
262             else
263             {
264                return policy.invokeInOurTx(invocation, tm);
265             }
266          }
267          finally
268          {
269             if (tm != null)
270             {
271                tm.setTransactionTimeout(oldTimeout);
272             }
273          }
274       }
275    }
276
277    public static class Mandatory implements Interceptor
278    {
279       protected TransactionManager JavaDoc tm;
280       protected TxPolicy policy;
281       protected int timeout;
282       
283       public Mandatory(TransactionManager JavaDoc tm, TxPolicy policy)
284       {
285          this(tm, policy, -1);
286       }
287
288       public Mandatory(TransactionManager JavaDoc tm, TxPolicy policy, int timeout)
289       {
290          this.tm = tm;
291          this.policy = policy;
292          this.timeout = timeout;
293       }
294
295       public String JavaDoc getName()
296       {
297          return this.getClass().getName();
298       }
299
300       public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
301       {
302          Transaction JavaDoc tx = tm.getTransaction();
303          if (tx == null)
304          {
305             policy.throwMandatory(invocation);
306          }
307          return policy.invokeInCallerTx(invocation, tx);
308       }
309
310    }
311 }
312
Popular Tags