1 29 30 package com.caucho.ejb.gen; 31 32 import com.caucho.bytecode.JClass; 33 import com.caucho.bytecode.JMethod; 34 import com.caucho.java.JavaWriter; 35 import com.caucho.java.gen.BaseMethod; 36 import com.caucho.util.L10N; 37 38 import java.io.IOException ; 39 import java.util.Collection ; 40 import java.util.Set ; 41 42 45 public class AmberGetter extends BaseMethod { 46 private static L10N L = new L10N(AmberGetter.class); 47 48 private JMethod _method; 49 private String _implClassName; 50 private boolean _isReadOnly; 51 52 public AmberGetter(JMethod method, 53 String implClassName) 54 { 55 super(method); 56 57 _method = method; 58 _implClassName = implClassName; 59 } 60 61 64 public void setReadOnly(boolean isReadOnly) 65 { 66 _isReadOnly = isReadOnly; 67 } 68 69 74 public void generateCall(JavaWriter out, String []args) 75 throws IOException 76 { 77 JClass returnType = _method.getReturnType(); 78 79 if (! _isReadOnly) { 80 81 out.println("com.caucho.ejb.xa.TransactionContext xa = _xaManager.beginSingleRead();"); 82 out.println(); 83 84 out.println("try {"); 85 out.pushDepth(); 86 87 out.println("if (xa == null) {"); 88 out.pushDepth(); 89 } 90 91 out.print(returnType.getPrintName()); 92 out.print(" value = "); 93 out.print("((" + _implClassName + ") _context.__caucho_getAmber().loadEntity(0))." + _method.getName() + "("); 94 for (int i = 0; i < args.length; i++) { 95 if (i != 0) 96 out.print(", "); 97 out.print(args[i]); 98 } 99 out.println(");"); 100 101 if (returnType.isAssignableTo(Collection .class)) { 102 out.println("if (value == null)"); 103 out.println(" return value;"); 104 105 if (returnType.isAssignableTo(Set .class)) 106 out.println("return new java.util.HashSet(value);"); 107 else 108 out.println("return new java.util.ArrayList(value);"); 109 } 110 else { 111 out.println("return value;"); 112 } 113 114 if (! _isReadOnly) { 115 out.popDepth(); 116 out.println("} else {"); 117 out.println(" Bean ptr = _context._ejb_begin(xa, false, true);"); 118 out.print(" return ptr." + _method.getName() + "("); 119 for (int i = 0; i < args.length; i++) { 120 if (i != 0) 121 out.print(", "); 122 out.print(args[i]); 123 } 124 out.println(");"); 125 out.println("}"); 126 127 out.popDepth(); 128 out.println("} finally {"); 129 out.println(" if (xa != null) xa.commit();"); 130 out.println("}"); 131 } 132 } 133 } 134 | Popular Tags |