1 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 ; 38 39 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 66 public void generateCall(JavaWriter out, String retType, 67 String var, String []args) 68 throws IOException 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 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 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 135 136 out.popDepth(); 137 out.println("}"); 138 } 139 } 140 | Popular Tags |