KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > transaction > enhancer > TransactionCallChain


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.transaction.enhancer;
30
31 import com.caucho.java.JavaWriter;
32 import com.caucho.java.gen.CallChain;
33 import com.caucho.java.gen.FilterCallChain;
34 import com.caucho.util.L10N;
35
36 import java.io.IOException JavaDoc;
37
38 /**
39  * Enhancing a method objects.
40  */

41 public class TransactionCallChain extends FilterCallChain {
42   private static final L10N L = new L10N(TransactionCallChain.class);
43   
44   private String JavaDoc _transaction = "Required";
45
46   public TransactionCallChain(CallChain next, String JavaDoc transaction)
47   {
48     super(next);
49     
50     if (transaction.equals("Required"))
51       _transaction = "Required";
52     else if (transaction.equals("RequiresNew"))
53       _transaction = "RequiresNew";
54     else
55       throw new IllegalArgumentException JavaDoc(L.l("'{0}' is an unknown transaction.\n'Required' are the valid transaction types.",
56                     transaction));
57   }
58
59   /**
60    * Generates the code for the method call.
61    *
62    * @param out the writer to the output stream.
63    * @param retVar the variable to hold the return value
64    * @param var the object to be called
65    * @param args the method arguments
66    */

67   public void generateCall(JavaWriter out, String JavaDoc retVar,
68                String JavaDoc var, String JavaDoc []args)
69     throws IOException JavaDoc
70   {
71     out.println("com.caucho.transaction.TransactionContainer _caucho_xa_c;");
72     out.println("_caucho_xa_c = com.caucho.transaction.TransactionContainer.getTransactionContainer();");
73     out.println("javax.transaction.Transaction _caucho_xa;");
74
75     if ("Required".equals(_transaction)) {
76       out.println("_caucho_xa = _caucho_xa_c.beginRequired();");
77     }
78     else if ("RequiresNew".equals(_transaction)) {
79       out.println("_caucho_xa = _caucho_xa_c.beginRequiresNew();");
80     }
81     else if ("Mandatory".equals(_transaction)) {
82       out.println("_caucho_xa_c.beginMandatory();");
83     }
84     else
85       throw new IllegalStateException JavaDoc(L.l("unknown transaction type '{0}'",
86                       _transaction));
87
88     out.println("try {");
89     out.pushDepth();
90
91     super.generateCall(out, retVar, var, args);
92       
93     out.popDepth();
94     out.println("} catch (RuntimeException e) {");
95     out.println(" _caucho_xa_c.setRollbackOnly(e);");
96     out.println(" throw e;");
97     out.pushDepth();
98
99     if ("Required".equals(_transaction)) {
100       out.println("} finally {");
101       out.println(" if (_caucho_xa == null)");
102       out.println(" _caucho_xa_c.commit(null);");
103       out.println("}");
104     }
105     else if ("RequiresNew".equals(_transaction)) {
106       out.println("} finally {");
107       out.println(" _caucho_xa_c.commit(_caucho_xa);");
108       out.println("}");
109     }
110     else if ("Mandatory".equals(_transaction)) {
111       out.println("}");
112     }
113
114     out.popDepth();
115     out.println("}");
116   }
117 }
118
Popular Tags