KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > gen > EntityBean


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 /**
45  * Generates the skeleton for a session bean.
46  */

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 JavaDoc _implClassName;
53   protected String JavaDoc _contextClassName;
54
55   protected BaseClass _beanClass;
56   
57   public EntityBean(JClass ejbClass,
58             String JavaDoc contextClassName,
59             String JavaDoc implClassName)
60   {
61     _ejbClass = ejbClass;
62     _contextClassName = contextClassName;
63     _implClassName = implClassName;
64
65     _beanClass = new BeanImpl();
66   }
67
68   /**
69    * Sets the bean.
70    */

71   public void setBean(EjbEntityBean bean)
72   {
73     _bean = bean;
74   }
75
76   /**
77    * Gets the bean.
78    */

79   public EjbEntityBean getBean()
80   {
81     return _bean;
82   }
83
84   /**
85    * Returns the implementation class name.
86    */

87   protected String JavaDoc getImplClassName()
88   {
89     return _implClassName;
90   }
91
92   /**
93    * Returns true for CMP.
94    */

95   protected boolean isCMP()
96   {
97     return false;
98   }
99
100   /**
101    * Generates the general entity information.
102    */

103   public void generate(JavaWriter out)
104     throws IOException JavaDoc
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 JavaDoc
115   {
116     String JavaDoc 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     // Otherwise create the bean
146
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     // XXX: need to optimize when no other transaction
175

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   /**
212    * Generates the cleanup code when the object is removed.
213    */

214   protected void generateDestroy(JavaWriter out)
215     throws IOException JavaDoc
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     /*
227     if (_localClass != null) {
228       println("if (viewLocal != null)");
229       println(" viewLocal.destroy();");
230       println("viewLocal = null;");
231     }
232     
233     if (_remoteClass != null) {
234       println("if (viewRemote != null)");
235       println(" viewRemote.destroy();");
236       println("viewRemote = null;");
237     }
238     */

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   /**
266    * Adds a bean method.
267    */

268   void addMethod(BaseMethod method)
269   {
270     if (method != null)
271       _beanClass.addMethod(method);
272   }
273
274   /**
275    * Adds a bean component.
276    */

277   void addComponent(ClassComponent component)
278   {
279     if (component != null)
280       _beanClass.addComponent(component);
281   }
282
283   /**
284    * Returns true if the method is implemented.
285    */

286   protected boolean hasMethod(String JavaDoc methodName, JClass []paramTypes)
287   {
288     return BeanAssembler.hasMethod(_ejbClass, methodName, paramTypes);
289   }
290
291   /**
292    * Returns true if the method is implemented.
293    */

294   protected boolean hasMethod(JClass ejbClass, String JavaDoc methodName, JClass []paramTypes)
295   {
296     return BeanAssembler.hasMethod(ejbClass, methodName, paramTypes);
297   }
298
299   /**
300    * Generates the load code.
301    */

302   protected void generateLoad(JavaWriter out)
303     throws IOException JavaDoc
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_DIRTY;");
313
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 JavaDoc
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 JavaDoc.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     /*
381       if (getContainerManagedPersistence())
382       out.println(" _caucho_setPrimaryKey(_ejb_context.getPrimaryKey());");
383     */

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   /**
416    * Generates the sync code.
417    */

418   protected void generateSync(JavaWriter out)
419     throws IOException JavaDoc
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       // if (! getContainerManagedPersistence())
429
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   /**
439    * Generates the before transaction completion code.
440    */

441   protected void generateBeforeCompletion(JavaWriter out)
442     throws IOException JavaDoc
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   /**
459    * Generates the after transaction completion code.
460    */

461   protected void generateAfterCompletion(JavaWriter out)
462     throws IOException JavaDoc
463   {
464
465     // Frees the bean after it's done with the transaction.
466
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     // ejb/0252
510
// out.println(" __caucho_expire();"); // ejb/0aje
511
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   /**
533    * Generates the store code.
534    */

535   protected void generateStore(JavaWriter out)
536     throws IOException JavaDoc
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