KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > burlap > StubGenerator


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.burlap;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.make.ClassDependency;
33 import com.caucho.vfs.MergePath;
34 import com.caucho.vfs.PersistentDependency;
35
36 import java.io.IOException JavaDoc;
37 import java.lang.reflect.Method JavaDoc;
38 import java.util.ArrayList JavaDoc;
39
40 /**
41  * Generator for stubs.
42  */

43 class StubGenerator extends MarshalGenerator {
44   private ArrayList JavaDoc<PersistentDependency> _dependList;
45   
46   StubGenerator()
47   {
48   }
49   
50   Class JavaDoc createHomeStub(Class JavaDoc cl)
51     throws ConfigException
52   {
53     return makeClient(cl, "__BurlapStub");
54   }
55   
56   Class JavaDoc createObjectStub(Class JavaDoc cl)
57     throws ConfigException
58   {
59     return makeClient(cl, "__BurlapStub");
60   }
61   
62   Class JavaDoc createStub(Class JavaDoc cl)
63     throws ConfigException
64   {
65     return makeClient(cl, "__BurlapStub");
66   }
67
68   /**
69    * Creates a client stub.
70    *
71    * @param cl the remote interface of the stub
72    * @param genSuffix the suffix for the generated class
73    */

74   Class JavaDoc makeClient(Class JavaDoc cl, String JavaDoc genSuffix)
75     throws ConfigException
76   {
77     _cl = cl;
78
79     try {
80       setFullClassName("_ejb." + cl.getName() + genSuffix);
81
82       if (cl.getClassLoader() != null)
83         setParentLoader(cl.getClassLoader());
84     
85       MergePath mergePath = new MergePath();
86       if (cl.getClassLoader() != null)
87         mergePath.addClassPath(cl.getClassLoader());
88       else
89         mergePath.addClassPath(Thread.currentThread().getContextClassLoader());
90       setSearchPath(mergePath);
91
92       _dependList = new ArrayList JavaDoc<PersistentDependency>();
93
94       _dependList.add(new ClassDependency(cl));
95
96       Class JavaDoc stubClass = preload();
97       if (stubClass != null)
98         return stubClass;
99
100       generate();
101     
102       return compile();
103     } catch (Exception JavaDoc e) {
104       throw new ConfigException(e);
105     }
106   }
107
108   public void generateJava()
109     throws IOException JavaDoc
110   {
111     generateJava(_cl.getMethods());
112   }
113
114
115   /**
116    * Generates the Java source.
117    *
118    * @param methods the methods to generate
119    */

120   private void generateJava(Method JavaDoc []methods)
121     throws IOException JavaDoc
122   {
123     if (javax.ejb.EJBObject JavaDoc.class.isAssignableFrom(_cl))
124       printHeader("ObjectStub");
125     else if (javax.ejb.EJBHome JavaDoc.class.isAssignableFrom(_cl))
126       printHeader("HomeStub");
127     else
128       printHeader("ObjectStub");
129
130     // println("public String getBurlapType()");
131
println("public String getHessianType()");
132     println("{");
133     println(" return \"" + _cl.getName() + "\";");
134     println("}");
135
136     for (int i = 0; i < methods.length; i++) {
137       Method JavaDoc method = methods[i];
138       Class JavaDoc declaringClass = method.getDeclaringClass();
139       String JavaDoc prefix = "";
140
141       if (declaringClass.getName().startsWith("javax.ejb."))
142         prefix = "_ejb_";
143
144       Class JavaDoc []exns = method.getExceptionTypes();
145       for (int j = 0; j < exns.length; j++) {
146         if (exns[j].isAssignableFrom(java.rmi.RemoteException JavaDoc.class)) {
147           printMethod(prefix + method.getName(), method);
148           break;
149         }
150       }
151     }
152     
153     printDependList(_dependList);
154
155     printFooter();
156   }
157
158   /**
159    * Prints the header for a HomeStub
160    */

161   void printHeader(String JavaDoc stubClassName)
162     throws IOException JavaDoc
163   {
164     if (getPackageName() != null)
165       println("package " + getPackageName() + ";");
166
167     println();
168     println("import java.io.*;");
169     println("import java.rmi.*;");
170     println("import com.caucho.vfs.*;");
171     println("import com.caucho.util.*;");
172     println("import com.caucho.ejb.burlap.*;");
173     println("import com.caucho.burlap.io.*;");
174     println("import " + _cl.getName() + ";");
175     
176     println();
177     println("public class " + getClassName() + " extends " + stubClassName);
178     print(" implements " + _cl.getName());
179
180     println(" {");
181     pushDepth();
182   }
183
184   /**
185    * Generates the code for a remote stub method.
186    *
187    * @param name the name of the remote
188    * @param method the reflected object for the method
189    */

190   void printMethod(String JavaDoc name, Method JavaDoc method)
191     throws IOException JavaDoc
192   {
193     Class JavaDoc ret = method.getReturnType();
194     Class JavaDoc []params = method.getParameterTypes();
195
196     printMethodDeclaration(name, method);
197     
198     println("{");
199     pushDepth();
200
201     println("BurlapWriter out = _burlap_openWriter();");
202     println("try {");
203     pushDepth();
204
205     String JavaDoc mangleName = mangleMethodName(method.getName(), method, false);
206     
207     println("out.startCall(\"" + mangleName + "\");");
208
209     for (int i = 0; i < params.length; i++)
210       printMarshalType(params[i], "_arg" + i);
211
212     println("BurlapInput in = out.doCall();");
213
214     if (! void.class.equals(ret)) {
215       printClass(ret);
216       println(" _ret;");
217       print("_ret = ");
218       printUnmarshalType(ret);
219     }
220     else {
221       println("in.readNull();");
222     }
223
224     println("in.completeReply();");
225
226     println("_burlap_freeWriter(out);");
227     println("out = null;");
228
229     if (! void.class.equals(ret))
230       println("return _ret;");
231     
232     popDepth();
233
234     Class JavaDoc []exn = method.getExceptionTypes();
235
236     boolean hasThrowable = false;
237     boolean hasRuntimeException = false;
238
239     loop:
240     for (int i = 0; i < exn.length; i++) {
241       for (int j = 0; j < i; j++) {
242         if (exn[j].isAssignableFrom(exn[i]))
243           continue loop;
244       }
245       
246       if (! hasThrowable) {
247         println("} catch (" + exn[i].getName() + " e) {");
248         println(" throw e;");
249       }
250       
251       if (exn[i].equals(Throwable JavaDoc.class)) {
252         hasThrowable = true;
253         hasRuntimeException = true;
254       }
255       
256       if (exn[i].equals(Exception JavaDoc.class))
257         hasRuntimeException = true;
258       if (exn[i].equals(RuntimeException JavaDoc.class))
259         hasRuntimeException = true;
260     }
261
262     if (! hasRuntimeException) {
263       println("} catch (RuntimeException e) {");
264       println(" throw e;");
265     }
266     
267     if (! hasThrowable) {
268       println("} catch (Throwable e) {");
269       println(" throw new RemoteException(\"stub exception\", e);");
270     }
271     println("} finally {");
272     println(" if (out != null) out.close();");
273     println("}");
274
275     popDepth();
276     println("}");
277   }
278
279   /**
280    * Prints the class footer for the generated stub.
281    */

282   void printFooter()
283     throws IOException JavaDoc
284   {
285     println();
286     println("public String toString()");
287     println("{");
288     pushDepth();
289     println("return \"BurlapStub[" + _cl.getName() + ",\" + _urlPath + \"]\";");
290     popDepth();
291     println("}");
292     
293     popDepth();
294     println("}");
295   }
296
297   /**
298    * Generates code for version changes.
299    */

300   protected void printVersionChange()
301     throws IOException JavaDoc
302   {
303     println("if (com.caucho.ejb.Version.getVersionId() != " +
304             com.caucho.ejb.Version.getVersionId() + ")");
305     println(" return true;");
306   }
307 }
308
Popular Tags