1 29 30 package com.caucho.ejb.gen; 31 32 import com.caucho.bytecode.JClass; 33 import com.caucho.bytecode.JClassLoader; 34 import com.caucho.java.JavaWriter; 35 import com.caucho.java.gen.ClassComponent; 36 import com.caucho.util.L10N; 37 38 import javax.ejb.SessionContext ; 39 import java.io.IOException ; 40 41 44 public class SessionBean extends ClassComponent { 45 private static final L10N L = new L10N(SessionBean.class); 46 47 private JClass _ejbClass; 48 protected String _contextClassName; 49 50 public SessionBean(JClass ejbClass, String contextClassName) 51 { 52 _ejbClass = ejbClass; 53 _contextClassName = contextClassName; 54 } 55 56 public void generate(JavaWriter out) 57 throws IOException 58 { 59 generateContext(out); 60 61 generateNewInstance(out); 62 generateNewRemoteInstance(out); 63 64 generateBean(out); 65 } 66 67 protected void generateContext(JavaWriter out) 68 throws IOException 69 { 70 String shortContextName = _contextClassName; 71 int p = shortContextName.lastIndexOf('.'); 72 73 if (p > 0) 74 shortContextName = shortContextName.substring(p + 1); 75 76 out.println("protected static final java.util.logging.Logger __caucho_log = java.util.logging.Logger.getLogger(\"" + _contextClassName + "\");"); 77 out.println(); 78 out.println("com.caucho.ejb.xa.EjbTransactionManager _xaManager;"); 79 out.println("private Bean _freeBean;"); 80 out.println(); 81 out.println("public " + shortContextName + "(com.caucho.ejb.session.SessionServer server)"); 82 out.println("{"); 83 out.println(" super(server);"); 84 out.println(" _xaManager = server.getContainer().getTransactionManager();"); 85 out.println("}"); 86 87 out.println(); 88 out.println("Bean _ejb_begin(com.caucho.ejb.xa.TransactionContext trans)"); 89 out.println(" throws javax.ejb.EJBException"); 90 out.println("{"); 91 out.pushDepth(); 92 out.println("Bean bean;"); 93 out.println("synchronized (this) {"); 94 out.println(" bean = _freeBean;"); 95 out.println(" if (bean != null) {"); 96 out.println(" _freeBean = null;"); 97 if (_ejbClass.isAssignableTo(javax.ejb.SessionSynchronization .class)) 98 out.println(" trans.addSession(bean);"); 99 out.println(" return bean;"); 100 out.println(" }"); 101 out.println("}"); 102 out.println(); 103 out.println("throw new EJBException(\"session bean is not reentrant\");"); 104 out.popDepth(); 105 out.println("}"); 106 107 out.println(); 108 out.println("void _ejb_free(Bean bean)"); 109 out.println(" throws javax.ejb.EJBException"); 110 out.println("{"); 111 out.pushDepth(); 112 out.println("if (bean == null)"); 113 out.println(" return;"); 114 out.println(); 115 out.println("synchronized (this) {"); 116 out.println(" if (_freeBean == null) {"); 117 out.println(" _freeBean = bean;"); 118 out.println(" return;"); 119 out.println(" }"); 120 out.println("}"); 121 122 out.popDepth(); 123 out.println("}"); 124 125 out.println(); 126 out.println("public void destroy()"); 127 out.println("{"); 128 out.pushDepth(); 129 out.println("Bean ptr;"); 130 out.println("synchronized (this) {"); 131 out.println(" ptr = _freeBean;"); 132 out.println(" _freeBean = null;"); 133 out.println("}"); 134 135 if (hasMethod("ejbRemove", new JClass[0])) { 136 out.println(); 137 out.println("try {"); 138 out.println(" if (ptr != null)"); 139 out.println(" ptr.ejbRemove();"); 140 out.println("} catch (Throwable e) {"); 141 out.println(" __caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);"); 142 out.println("}"); 143 } 144 out.popDepth(); 145 out.println("}"); 146 } 147 148 protected void generateNewInstance(JavaWriter out) 149 throws IOException 150 { 151 out.println(); 152 out.println("protected Object _caucho_newInstance()"); 153 out.println("{"); 154 out.pushDepth(); 155 156 out.println(_contextClassName + " cxt = new " + _contextClassName + "(_server);"); 157 158 out.println("Bean bean = new Bean(cxt);"); 159 160 out.println("cxt._ejb_free(bean);"); 161 162 out.println(); 163 164 174 out.println("return cxt.getEJBLocalObject();"); 175 176 out.popDepth(); 177 out.println("}"); 178 } 179 180 protected void generateNewRemoteInstance(JavaWriter out) 181 throws IOException 182 { 183 out.println(); 184 out.println("protected Object _caucho_newRemoteInstance()"); 185 out.println("{"); 186 out.pushDepth(); 187 188 out.println(_contextClassName + " cxt = new " + _contextClassName + "(_server);"); 189 190 out.println("Bean bean = new Bean(cxt);"); 191 192 out.println("cxt._ejb_free(bean);"); 193 194 out.println(); 195 196 206 out.println("return cxt.getEJBObject();"); 207 208 out.popDepth(); 209 out.println("}"); 210 } 211 212 private void generateBean(JavaWriter out) 213 throws IOException 214 { 215 out.println(); 216 out.println("public static class Bean extends " + _ejbClass.getName() + " {"); 217 out.pushDepth(); 218 219 out.println(); 220 out.println("protected final static java.util.logging.Logger __caucho_log = com.caucho.log.Log.open(" + _ejbClass.getName() + ".class);"); 221 out.println("private static int __caucho_dbg_id;"); 222 out.println("private final String __caucho_id;"); 223 224 out.println(_contextClassName + " _ejb_context;"); 225 out.println("boolean _ejb_isActive;"); 226 227 out.println(); 228 out.println("Bean(" + _contextClassName + " context)"); 229 out.println("{"); 230 out.pushDepth(); 231 232 out.println("synchronized (" + _ejbClass.getName() + ".class) {"); 233 out.println(" __caucho_id = __caucho_dbg_id++ + \"-" + _ejbClass.getName() + "\";"); 234 out.println("}"); 235 out.println("__caucho_log.fine(__caucho_id + \":new()\");"); 236 237 out.println("try {"); 238 out.pushDepth(); 239 240 out.println("_ejb_context = context;"); 241 242 if (hasMethod("setSessionContext", new JClass[] { JClassLoader.systemForName(SessionContext .class.getName()) })) { 243 out.println("setSessionContext(context);"); 244 } 245 246 out.println(); 247 out.println("context.getServer().initInstance(this);"); 248 249 out.popDepth(); 250 out.println("} catch (RuntimeException e) {"); 251 out.println(" throw e;"); 252 out.println("} catch (Throwable e) {"); 253 out.println(" __caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);"); 254 out.println(" throw com.caucho.ejb.EJBExceptionWrapper.create(e);"); 255 out.println("}"); 256 257 out.popDepth(); 258 out.println("}"); 259 260 out.popDepth(); 261 out.println("}"); 262 } 263 264 267 protected boolean hasMethod(String methodName, JClass []paramTypes) 268 { 269 return BeanAssembler.hasMethod(_ejbClass, methodName, paramTypes); 270 } 271 } 272 | Popular Tags |