KickJava   Java API By Example, From Geeks To Geeks.

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


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.java.JavaWriter;
35 import com.caucho.java.gen.ClassComponent;
36 import com.caucho.util.L10N;
37
38 import javax.ejb.SessionContext JavaDoc;
39 import java.io.IOException JavaDoc;
40
41 /**
42  * Generates the skeleton for a session bean.
43  */

44 public class SessionBean extends ClassComponent {
45   private static final L10N L = new L10N(SessionBean.class);
46
47   private JClass _ejbClass;
48   protected String JavaDoc _contextClassName;
49   
50   public SessionBean(JClass ejbClass, String JavaDoc contextClassName)
51   {
52     _ejbClass = ejbClass;
53     _contextClassName = contextClassName;
54   }
55   
56   public void generate(JavaWriter out)
57     throws IOException JavaDoc
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 JavaDoc
69   {
70     String JavaDoc 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 JavaDoc.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 JavaDoc
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     /*
165     Class retType = getReturnType();
166     if ("RemoteHome".equals(_prefix))
167       out.println("return (" + retType.getName() + ") cxt.getRemoteView();");
168     else if ("LocalHome".equals(_prefix))
169       out.println("return (" + retType.getName() + ") cxt.getLocalView();");
170     else
171       throw new IOException(L.l("trying to create unknown type {0}",
172                 _prefix));
173     */

174     out.println("return cxt.getEJBLocalObject();");
175     
176     out.popDepth();
177     out.println("}");
178   }
179
180   protected void generateNewRemoteInstance(JavaWriter out)
181     throws IOException JavaDoc
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     /*
197     Class retType = getReturnType();
198     if ("RemoteHome".equals(_prefix))
199       out.println("return (" + retType.getName() + ") cxt.getRemoteView();");
200     else if ("LocalHome".equals(_prefix))
201       out.println("return (" + retType.getName() + ") cxt.getLocalView();");
202     else
203       throw new IOException(L.l("trying to create unknown type {0}",
204                 _prefix));
205     */

206     out.println("return cxt.getEJBObject();");
207     
208     out.popDepth();
209     out.println("}");
210   }
211
212   private void generateBean(JavaWriter out)
213     throws IOException JavaDoc
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 JavaDoc.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   /**
265    * Returns true if the method is implemented.
266    */

267   protected boolean hasMethod(String JavaDoc methodName, JClass []paramTypes)
268   {
269     return BeanAssembler.hasMethod(_ejbClass, methodName, paramTypes);
270   }
271 }
272
Popular Tags