1 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 ; 37 38 41 public class TransactionCallChain extends FilterCallChain { 42 private static final L10N L = new L10N(TransactionCallChain.class); 43 44 private String _transaction = "Required"; 45 46 public TransactionCallChain(CallChain next, String 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 (L.l("'{0}' is an unknown transaction.\n'Required' are the valid transaction types.", 56 transaction)); 57 } 58 59 67 public void generateCall(JavaWriter out, String retVar, 68 String var, String []args) 69 throws IOException 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 (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 |