KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > bean > AnnotatedTxPOJO


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.test.aop.bean;
23
24 import org.jboss.aspects.tx.Tx;
25 import org.jboss.aspects.txlock.TxSynchronized;
26 import org.jboss.aspects.tx.TxType;
27 import org.jboss.tm.TxUtils;
28
29 import javax.naming.InitialContext JavaDoc;
30 import javax.transaction.Transaction JavaDoc;
31 import javax.transaction.TransactionManager JavaDoc;
32
33 /**
34  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
35  * @version $Revision: 57757 $
36  */

37 @Tx (TxType.NOTSUPPORTED)
38 public class AnnotatedTxPOJO
39 {
40    TransactionManager JavaDoc tm;
41
42    public AnnotatedTxPOJO() throws Exception JavaDoc
43    {
44       tm = (TransactionManager JavaDoc)new InitialContext JavaDoc().lookup("java:/TransactionManager");
45    }
46
47    @Tx (TxType.NEVER)
48    public void never() {}
49
50    public void callNever() throws Exception JavaDoc
51    {
52       boolean exceptionThrown = false;
53       tm.begin();
54       try
55       {
56          never();
57       }
58       catch (Exception JavaDoc ex)
59       {
60          exceptionThrown = true;
61       }
62       tm.commit();
63       if (!exceptionThrown) throw new Exception JavaDoc("failed on mandatory no tx call");
64    }
65
66    @Tx (TxType.NOTSUPPORTED)
67    public void notsupported() throws Exception JavaDoc
68    {
69       if (tm.getTransaction() != null) throw new Exception JavaDoc("notsupported() method has tx set");
70    }
71
72    public void callNotSupported() throws Exception JavaDoc
73    {
74       tm.begin();
75       notsupported();
76       tm.commit();
77    }
78
79     @Tx (TxType.SUPPORTS)
80    public void supports(Transaction JavaDoc tx) throws Exception JavaDoc
81    {
82       Transaction JavaDoc tmTx = tm.getTransaction();
83       if (tx != tmTx) throw new Exception JavaDoc("supports didn't work");
84    }
85
86    public boolean hasActiveTransaction() throws Exception JavaDoc
87    {
88       Transaction JavaDoc tx = tm.getTransaction();
89       if (tx == null)
90       {
91          System.out.println("Transaction: is null");
92       } // end of if ()
93
else
94       {
95          System.out.println("Transaction: status " + tx.getStatus() + " of tx" + tx);
96       } // end of else
97

98       return TxUtils.isActive(tx);
99    }
100
101    public void callSupportsWithTx() throws Exception JavaDoc
102    {
103       tm.begin();
104       Transaction JavaDoc tx = tm.getTransaction();
105       supports(tx);
106       tm.commit();
107    }
108
109    public void callSupportsWithoutTx() throws Exception JavaDoc
110    {
111       supports(null);
112    }
113
114    @Tx (TxType.REQUIRED)
115    public void required() throws Exception JavaDoc
116    {
117       if (tm.getTransaction() == null) throw new Exception JavaDoc("rquired() method has no tx set");
118    }
119
120
121    @Tx (TxType.REQUIRESNEW)
122    public void requiresNew(Transaction JavaDoc tx) throws Exception JavaDoc
123    {
124       Transaction JavaDoc tmTx = tm.getTransaction();
125       if (tx == tmTx
126           || (tx != null && tx.equals(tmTx))) throw new Exception JavaDoc("transactions shouldn't be equal");
127       if (tmTx == null) throw new Exception JavaDoc("tx is null in RequiresNew");
128    }
129    public void callRequiresNew() throws Exception JavaDoc
130    {
131       tm.begin();
132       Transaction JavaDoc tx = tm.getTransaction();
133       requiresNew(tx);
134       tm.commit();
135    }
136
137    @Tx (TxType.MANDATORY)
138    public void mandatory() {}
139
140    public void callMandatoryNoTx() throws Exception JavaDoc
141    {
142       boolean exceptionThrown = false;
143       try
144       {
145          mandatory();
146       }
147       catch (Exception JavaDoc ex)
148       {
149          exceptionThrown = true;
150       }
151       if (!exceptionThrown) throw new Exception JavaDoc("failed on mandatory no tx call");
152    }
153
154    public void callMandatoryWithTx() throws Exception JavaDoc
155    {
156       tm.begin();
157       mandatory();
158       tm.commit();
159    }
160
161
162 }
163
164
Popular Tags