KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > gen > TransactionChain


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.ejb.gen;
30
31 import com.caucho.ejb.cfg.EjbMethod;
32 import com.caucho.java.JavaWriter;
33 import com.caucho.java.gen.CallChain;
34 import com.caucho.java.gen.FilterCallChain;
35 import com.caucho.util.L10N;
36
37 import java.io.IOException JavaDoc;
38
39 /**
40  * Generates the skeleton for a method call.
41  */

42 public class TransactionChain extends FilterCallChain {
43   private static final L10N L = new L10N(TransactionChain.class);
44
45
46   private int _xaType;
47   
48   public TransactionChain(CallChain next, int xaType)
49   {
50     super(next);
51     
52     _xaType = xaType;
53   }
54   
55   public static TransactionChain create(CallChain next, int xaType)
56   {
57     return new TransactionChain(next, xaType);
58   }
59   
60   /**
61    * Prints a call within the same JVM
62    *
63    * @param methodName the name of the method to call
64    * @param method the method to call
65    */

66   public void generateCall(JavaWriter out, String JavaDoc retType,
67                String JavaDoc var, String JavaDoc []args)
68     throws IOException JavaDoc
69   {
70     out.println("Thread thread = Thread.currentThread();");
71     out.println("ClassLoader oldLoader = thread.getContextClassLoader();");
72     
73     out.print("com.caucho.ejb.xa.TransactionContext trans");
74     
75     switch (_xaType) {
76     case EjbMethod.TRANS_SINGLE_READ:
77       out.println(" = _xaManager.beginSingleRead();");
78       break;
79       
80     case EjbMethod.TRANS_REQUIRES_NEW:
81       out.println(" = _xaManager.beginRequiresNew();");
82       break;
83     case EjbMethod.TRANS_BEAN:
84     case EjbMethod.TRANS_NOT_SUPPORTED:
85        out.println(" = _xaManager.suspend();");
86       break;
87     case EjbMethod.TRANS_NEVER:
88       out.println(" = _xaManager.beginNever();");
89       break;
90     case EjbMethod.TRANS_REQUIRED:
91       out.println(" = _xaManager.beginRequired();");
92       break;
93     case EjbMethod.TRANS_MANDATORY:
94       out.println(" = _xaManager.beginMandatory();");
95       break;
96     default:
97     case EjbMethod.TRANS_SUPPORTS:
98       out.println(" = _xaManager.beginSupports();");
99       break;
100     }
101
102     // out.println("Bean ptr = null;");
103

104     out.println("try {");
105     out.pushDepth();
106
107     out.println("thread.setContextClassLoader(_context._server.getClassLoader());");
108
109     super.generateCall(out, retType, var, args);
110     
111     out.popDepth();
112     out.println("} catch (RuntimeException e) {");
113     out.pushDepth();
114
115     /*
116     if (! out.isSession())
117       out.println("if (ptr != null) ptr._ejb_state = QEntity._CAUCHO_IS_DEAD;");
118     */

119
120     out.println("throw trans.setRollbackOnly(e);");
121     
122     out.popDepth();
123
124     out.println("} finally {");
125     out.pushDepth();
126     
127     out.println("thread.setContextClassLoader(oldLoader);");
128     
129     out.println("trans.commit();");
130
131     /*
132     if (out.isSession())
133       out.println("if (ptr != null) ptr._ejb_isActive = false;");
134     */

135
136     out.popDepth();
137     out.println("}");
138   }
139 }
140
Popular Tags