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.ejb.cfg.EjbEntityBean; 35 import com.caucho.java.JavaWriter; 36 import com.caucho.java.gen.BaseClass; 37 import com.caucho.java.gen.BaseMethod; 38 import com.caucho.java.gen.ClassComponent; 39 import com.caucho.util.L10N; 40 41 import javax.ejb.EntityContext ; 42 import java.io.IOException ; 43 44 47 public class EntityBean extends ClassComponent { 48 private final static L10N L = new L10N(EntityBean.class); 49 50 private EjbEntityBean _bean; 51 private JClass _ejbClass; 52 protected String _implClassName; 53 protected String _contextClassName; 54 55 protected BaseClass _beanClass; 56 57 public EntityBean(JClass ejbClass, 58 String contextClassName, 59 String implClassName) 60 { 61 _ejbClass = ejbClass; 62 _contextClassName = contextClassName; 63 _implClassName = implClassName; 64 65 _beanClass = new BeanImpl(); 66 } 67 68 71 public void setBean(EjbEntityBean bean) 72 { 73 _bean = bean; 74 } 75 76 79 public EjbEntityBean getBean() 80 { 81 return _bean; 82 } 83 84 87 protected String getImplClassName() 88 { 89 return _implClassName; 90 } 91 92 95 protected boolean isCMP() 96 { 97 return false; 98 } 99 100 103 public void generate(JavaWriter out) 104 throws IOException 105 { 106 out.println("protected static final java.util.logging.Logger __caucho_log = java.util.logging.Logger.getLogger(\"" + _contextClassName + "\");"); 107 108 generateContext(out); 109 110 _beanClass.generate(out); 111 } 112 113 protected void generateContext(JavaWriter out) 114 throws IOException 115 { 116 String shortContextName = _contextClassName; 117 118 int p = shortContextName.lastIndexOf('.'); 119 if (p > 0) 120 shortContextName = shortContextName.substring(p + 1); 121 122 out.println(); 123 out.println("Bean _ejb_free;"); 124 out.println(); 125 out.println("public " + shortContextName + "(EntityServer server)"); 126 out.println("{"); 127 out.println(" super(server);"); 128 out.println("}"); 129 130 out.println(); 131 out.println("Bean _ejb_begin(TransactionContext trans, boolean isHome, boolean doLoad)"); 132 out.println("{"); 133 out.pushDepth(); 134 135 out.println("if (trans == null || isDead()) throw new IllegalStateException();"); 136 out.println(); 137 out.println("Bean ptr;"); 138 out.println("if (isHome)"); 139 out.println(" ptr = (Bean) trans.getEntity(_server, null);"); 140 out.println("else"); 141 out.println(" ptr = (Bean) trans.getEntity(_server, getPrimaryKey());"); 142 out.println("if (ptr != null)"); 143 out.println(" return ptr;"); 144 145 out.println("synchronized (this) {"); 147 out.println(" ptr = _ejb_free;"); 148 out.println(" _ejb_free = null;"); 149 out.println("}"); 150 151 out.println("if (ptr == null) {"); 152 out.pushDepth(); 153 out.println("ptr = new Bean(this);"); 154 155 if (BeanAssembler.hasMethod(_ejbClass, "ejbActivate", new JClass[0])) { 156 out.println("if (! isHome)"); 157 out.println(" try { ptr.ejbActivate(); } catch (Exception e) { throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e); }"); 158 } 159 out.popDepth(); 160 out.println("}"); 161 162 out.println(); 163 out.println("ptr._ejb_trans = trans;"); 164 out.println("trans.addObject(ptr);"); 165 166 generateLoad(out); 167 168 out.println("return ptr;"); 169 170 out.popDepth(); 171 out.println("}"); 172 173 174 176 out.println(); 177 out.println("public void _caucho_load()"); 178 out.println(" throws FinderException"); 179 out.println("{"); 180 out.pushDepth(); 181 out.println("TransactionContext trans = _server.getTransactionManager().beginSupports();"); 182 out.println("try {"); 183 out.pushDepth(); 184 185 out.println("Bean bean = _ejb_begin(trans, false, true);"); 186 187 out.popDepth(); 188 out.println("} catch (RuntimeException e) {"); 189 out.println(" trans.setRollbackOnly(e);"); 190 out.println(" throw FinderExceptionWrapper.create(e);"); 191 out.println("} finally {"); 192 out.println(" trans.commit();"); 193 out.println("}"); 194 out.popDepth(); 195 out.println("}"); 196 197 out.println(); 198 out.println("public void update()"); 199 out.println("{"); 200 out.println("}"); 201 202 out.println(); 203 out.println("public void _caucho_killCache()"); 204 out.println(" throws javax.ejb.RemoveException"); 205 out.println("{"); 206 out.println("}"); 207 208 generateDestroy(out); 209 } 210 211 214 protected void generateDestroy(JavaWriter out) 215 throws IOException 216 { 217 out.println(); 218 out.println("public void destroy()"); 219 out.println(" throws Exception"); 220 out.println("{"); 221 out.pushDepth(); 222 out.println("Bean bean;"); 223 out.println("synchronized (this) {"); 224 out.pushDepth(); 225 226 239 out.println("super.destroy();"); 240 out.println(); 241 out.println("bean = _ejb_free;"); 242 out.println("_ejb_free = null;"); 243 out.popDepth(); 244 out.println("}"); 245 246 out.println(); 247 out.println("if (bean != null) {"); 248 out.pushDepth(); 249 250 if (hasMethod("ejbPassivate", new JClass[0])) { 251 out.println("if (bean._ejb_state > QEntity._CAUCHO_IS_HOME)"); 252 out.println(" bean.ejbPassivate();"); 253 } 254 255 if (hasMethod("unsetEntityContext", new JClass[0])) 256 out.println("bean.unsetEntityContext();"); 257 258 out.popDepth(); 259 out.println("}"); 260 261 out.popDepth(); 262 out.println("}"); 263 } 264 265 268 void addMethod(BaseMethod method) 269 { 270 if (method != null) 271 _beanClass.addMethod(method); 272 } 273 274 277 void addComponent(ClassComponent component) 278 { 279 if (component != null) 280 _beanClass.addComponent(component); 281 } 282 283 286 protected boolean hasMethod(String methodName, JClass []paramTypes) 287 { 288 return BeanAssembler.hasMethod(_ejbClass, methodName, paramTypes); 289 } 290 291 294 protected boolean hasMethod(JClass ejbClass, String methodName, JClass []paramTypes) 295 { 296 return BeanAssembler.hasMethod(ejbClass, methodName, paramTypes); 297 } 298 299 302 protected void generateLoad(JavaWriter out) 303 throws IOException 304 { 305 if (hasMethod("ejbLoad", new JClass[0])) { 306 out.println("if (doLoad) {"); 307 out.println(" try {"); 308 if (hasMethod("ejbLoad", new JClass[0])) 309 out.println(" ptr.ejbLoad();"); 310 311 out.println(" } catch (Exception e) { throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e); }"); 312 out.println(" ptr._ejb_state = QEntity._CAUCHO_IS_LOADED;"); 314 out.println("}"); 315 } 316 } 317 318 class BeanImpl extends BaseClass { 319 BeanImpl() 320 { 321 super("Bean", _implClassName); 322 323 addInterfaceName("QEntity"); 324 325 setStatic(true); 326 } 327 328 public void generateClassContent(JavaWriter out) 329 throws IOException 330 { 331 out.println(); 332 out.println("protected final static java.util.logging.Logger __caucho_log = com.caucho.log.Log.open(" + _ejbClass.getName() + ".class);"); 333 out.println("private static int __caucho_dbg_id;"); 334 out.println("private final String __caucho_id;"); 335 336 out.println("Bean _ejb_next;"); 337 338 out.println(_contextClassName + " _ejb_context;"); 339 out.println("TransactionContext _ejb_trans;"); 340 out.println("byte _ejb_state;"); 341 out.println("byte _ejb_flags;"); 342 343 out.println(); 344 getBean().generateBeanPrologue(out); 345 346 out.println(); 347 out.println("Bean(" + _contextClassName + " context)"); 348 out.println("{"); 349 out.pushDepth(); 350 351 out.println("__caucho_id = \"\" + __caucho_dbg_id++ + \":" + _ejbClass.getName() + "\";"); 352 353 out.println("try {"); 354 out.println(" _ejb_context = context;"); 355 out.println(" _ejb_state = QEntity._CAUCHO_IS_HOME;"); 356 357 if (BeanAssembler.hasMethod(_ejbClass, "setEntityContext", 358 new JClass[] { 359 JClassLoader.systemForName(EntityContext .class.getName()) 360 })) { 361 out.println(" setEntityContext(context);"); 362 } 363 364 out.println("} catch (Exception e) {"); 365 out.println(" __caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);"); 366 out.println(" throw com.caucho.ejb.EJBExceptionWrapper.create(e);"); 367 out.println("}"); 368 369 out.popDepth(); 370 out.println("}"); 371 372 if (BeanAssembler.hasMethod(_ejbClass, "ejbActivate", new JClass[0])) { 373 out.println(); 374 out.println("public void ejbActivate()"); 375 out.println("{"); 376 out.println(" if (__caucho_log.isLoggable(java.util.logging.Level.FINE))"); 377 out.println(" __caucho_log.fine(__caucho_id + \":activate()\");"); 378 out.println(); 379 out.println(" _ejb_state = _CAUCHO_IS_ACTIVE;"); 380 384 out.println(" try {super.ejbActivate(); } catch (Exception e) { throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e); }"); 385 out.println("}"); 386 } 387 388 if (BeanAssembler.hasMethod(_ejbClass, "ejbPassivate", new JClass[0])) { 389 out.println(); 390 out.println("public void ejbPassivate()"); 391 out.println("{"); 392 out.println(" if (__caucho_log.isLoggable(java.util.logging.Level.FINE))"); 393 out.println(" __caucho_log.fine(__caucho_id + \":passivate()\");"); 394 out.println(" super.ejbPassivate();"); 395 out.println("}"); 396 } 397 398 out.println(); 399 out.println("public boolean _caucho_isMatch(com.caucho.ejb.AbstractServer server, Object primaryKey)"); 400 out.println("{"); 401 out.println(" if (server != _ejb_context.getServer())"); 402 out.println(" return false;"); 403 out.println(" Object key = _ejb_context._caucho_getPrimaryKey();"); 404 out.println(" return (primaryKey == key || primaryKey != null && primaryKey.equals(key));"); 405 out.println("}"); 406 407 generateSync(out); 408 generateBeforeCompletion(out); 409 generateAfterCompletion(out); 410 411 super.generateClassContent(out); 412 } 413 } 414 415 418 protected void generateSync(JavaWriter out) 419 throws IOException 420 { 421 out.println(); 422 out.println("public void _caucho_sync()"); 423 out.println("{"); 424 425 if (BeanAssembler.hasMethod(_ejbClass, "ejbStore", new JClass[0])) { 426 out.pushDepth(); 427 out.println("if (this._ejb_state >= _CAUCHO_IS_ACTIVE) {"); 428 out.println(" this._ejb_state = _CAUCHO_IS_LOADED;"); 430 out.println(" ejbStore();"); 431 out.println("}"); 432 out.popDepth(); 433 } 434 435 out.println("}"); 436 } 437 438 441 protected void generateBeforeCompletion(JavaWriter out) 442 throws IOException 443 { 444 out.println(); 445 out.println("public void _caucho_beforeCompletion(boolean isCommit)"); 446 out.println("{"); 447 448 if (BeanAssembler.hasMethod(_ejbClass, "ejbStore", new JClass[0])) { 449 out.pushDepth(); 450 451 generateStore(out); 452 453 out.popDepth(); 454 } 455 out.println("}"); 456 } 457 458 461 protected void generateAfterCompletion(JavaWriter out) 462 throws IOException 463 { 464 465 out.println(); 467 out.println("public void _caucho_afterCompletion(boolean isCommit)"); 468 out.println("{"); 469 out.pushDepth(); 470 out.println(_contextClassName + " cxt = (" + _contextClassName + ") _ejb_context;"); 471 out.println(); 472 out.println("boolean isDead = false;"); 473 474 if (isCMP()) { 475 out.println("if (isCommit)"); 476 out.println(" __caucho_afterCommit();"); 477 out.println("else"); 478 out.println(" __caucho_afterRollback();"); 479 } 480 481 out.println("if (! isCommit && _ejb_flags != 0) {"); 482 out.println(" _ejb_state = QEntity._CAUCHO_IS_DEAD;"); 483 out.println(" cxt.getEntityServer().removeCache(cxt.getPrimaryKey());"); 484 out.println("}"); 485 out.println("else if (_ejb_state == QEntity._CAUCHO_IS_REMOVED) {"); 486 out.println(" cxt.getEntityServer().removeCache(cxt.getPrimaryKey());"); 487 488 if (isCMP()) 489 out.println(" _ejb_context._amber = null;"); 490 491 out.println(" try {"); 492 out.println(" cxt.destroy();"); 493 out.println(" } catch (Exception e) {"); 494 out.println(" }"); 495 496 out.println("}"); 497 out.println("else {"); 498 out.pushDepth(); 499 500 if (isCMP()) { 501 out.println("if (_ejb_context._amber == null)"); 502 out.println(" _ejb_context._amber = __caucho_item;"); 503 504 _bean.generateAfterCommit(out); 505 } 506 out.println("_ejb_trans = null;"); 507 out.println("synchronized (this) {"); 508 out.println(" if (_ejb_context._ejb_free == null) {"); 509 out.println(" _ejb_context._ejb_free = this;"); 512 out.println(" return;"); 513 out.println(" }"); 514 out.println("}"); 515 516 if (hasMethod("ejbPassivate", new JClass[0])) { 517 out.println("if (_ejb_state > QEntity._CAUCHO_IS_HOME)"); 518 out.println(" ejbPassivate();"); 519 } 520 521 if (hasMethod("unsetEntityContext", new JClass[0])) { 522 out.println(); 523 out.println("unsetEntityContext();"); 524 } 525 526 out.popDepth(); 527 out.println("}"); 528 out.popDepth(); 529 out.println("}"); 530 } 531 532 535 protected void generateStore(JavaWriter out) 536 throws IOException 537 { 538 out.println("if (_ejb_state >= _CAUCHO_IS_LOADED && isCommit) {"); 539 out.println(" _ejb_state = _CAUCHO_IS_LOADED;"); 540 out.println(" ejbStore();"); 541 out.println("}"); 542 } 543 } 544 | Popular Tags |