KickJava   Java API By Example, From Geeks To Geeks.

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


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.java.JavaWriter;
34 import com.caucho.java.gen.ClassComponent;
35 import com.caucho.util.L10N;
36
37 import java.io.IOException JavaDoc;
38
39 /**
40  * Generates the skeleton for an Amber-based entity bean.
41  */

42 public class AmberBean extends EntityBean {
43   private final static L10N L = new L10N(AmberBean.class);
44   
45   public AmberBean(JClass ejbClass,
46            String JavaDoc contextClassName,
47            String JavaDoc implClassName)
48   {
49     super(ejbClass, contextClassName, implClassName);
50
51     addComponent(new FlushMethod());
52
53     if (hasMethod(ejbClass, "ejbLoad", new JClass[0])) {
54       addComponent(new LoadMethod());
55     }
56
57     boolean hasRemove = hasMethod(ejbClass, "ejbRemove", new JClass[0]);
58     
59     addComponent(new RemoveMethod(hasRemove));
60   }
61
62   /**
63    * Returns true for CMP.
64    */

65   protected boolean isCMP()
66   {
67     return true;
68   }
69
70   /**
71    * Generates the amber context stuff.
72    */

73   protected void generateContext(JavaWriter out)
74     throws IOException JavaDoc
75   {
76     super.generateContext(out);
77
78     out.println();
79     out.println("com.caucho.amber.entity.EntityItem _amber;");
80
81     out.println();
82     out.println("protected final void __caucho_setAmber(com.caucho.amber.entity.EntityItem amber)");
83     out.println("{");
84     out.println(" _amber = amber;");
85     out.println("}");
86     
87     out.println();
88     out.println("protected final com.caucho.amber.entity.EntityItem __caucho_getAmber()");
89     out.println("{");
90     out.println(" if (_amber != null)");
91     out.println(" return _amber;");
92     out.println(" else");
93     out.println(" throw new javax.ejb.EJBException(\"object no longer exists.\");");
94     out.println("}");
95   }
96
97   /**
98    * Generates the load code.
99    */

100   protected void generateStore(JavaWriter out)
101     throws IOException JavaDoc
102   {
103   }
104
105   /**
106    * Generates the load code.
107    */

108   protected void generateLoad(JavaWriter out)
109     throws IOException JavaDoc
110   {
111     out.println("if (doLoad) {");
112     out.println(" try {");
113     out.println(" ptr.__caucho_makePersistent(trans.getAmberConnection(), __caucho_getAmber());");
114     out.println(" ptr.__caucho_retrieve(trans.getAmberConnection());");
115
116     /*
117     if (hasMethod("ejbLoad", new Class[0]))
118       out.println(" ptr.ejbLoad();");
119     */

120       
121     out.println(" } catch (Exception e) { throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e); }");
122     // out.println(" ptr._ejb_state = QEntity._CAUCHO_IS_DIRTY;");
123
out.println(" ptr._ejb_state = QEntity._CAUCHO_IS_LOADED;");
124     out.println("}");
125   }
126
127   private static boolean hasMethod(Class JavaDoc cl, String JavaDoc name, Class JavaDoc []param)
128   {
129     try {
130       return cl.getMethod(name, param) != null;
131     } catch (Throwable JavaDoc e) {
132       return false;
133     }
134   }
135
136   static class FlushMethod extends ClassComponent {
137     public void generate(JavaWriter out)
138       throws IOException JavaDoc
139     {
140       out.println("protected void __caucho_flush_callback()");
141       out.println(" throws java.sql.SQLException");
142       out.println("{");
143       out.println(" ejbStore();");
144       out.println("}");
145     }
146   }
147
148   static class LoadMethod extends ClassComponent {
149     public void generate(JavaWriter out)
150       throws IOException JavaDoc
151     {
152       out.println("protected void __caucho_load_callback()");
153       out.println("{");
154       out.println(" try {");
155       out.println(" ejbLoad();");
156       out.println(" } catch (Throwable e) {");
157       out.println(" log.log(java.util.logging.Level.WARNING, e.toString(), e);");
158       out.println(" }");
159       out.println("}");
160     }
161   }
162
163   static class RemoveMethod extends ClassComponent {
164     private boolean _hasRemove;
165
166     RemoveMethod(boolean hasRemove)
167     {
168       _hasRemove = hasRemove;
169     }
170     
171     public void generate(JavaWriter out)
172       throws IOException JavaDoc
173     {
174       out.println("public void ejbRemove()");
175       out.println("{");
176       out.pushDepth();
177       
178       out.println("try {");
179       out.pushDepth();
180
181       if (_hasRemove)
182     out.println("super.ejbRemove();");
183
184       out.println("_ejb_state = com.caucho.ejb.entity.EntityObject._CAUCHO_IS_DEAD;");
185
186       out.println("_ejb_trans.getAmberConnection().delete(this);");
187
188       out.popDepth();
189       out.println("} catch (Throwable e) {");
190       out.println(" log.log(java.util.logging.Level.WARNING, e.toString(), e);");
191       out.println("}");
192
193       out.popDepth();
194       out.println("}");
195     }
196   }
197 }
198
Popular Tags