KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > hessian > 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.hessian;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.java.WorkDir;
33 import com.caucho.make.ClassDependency;
34 import com.caucho.vfs.MergePath;
35 import com.caucho.vfs.PersistentDependency;
36
37 import java.io.IOException JavaDoc;
38 import java.lang.reflect.Method JavaDoc;
39 import java.util.ArrayList JavaDoc;
40
41 /**
42  * Generator for stubs.
43  */

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

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

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

154       printMethod(prefix + method.getName(), method);
155     }
156     
157     printDependList(_dependList);
158
159     printFooter();
160   }
161
162   /**
163    * Prints the header for a HomeStub
164    */

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

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

289   void printFooter()
290     throws IOException JavaDoc
291   {
292     println();
293     println("public String toString()");
294     println("{");
295     pushDepth();
296     println("return \"[HessianStub " + _cl.getName() + " \" + _urlPath + \"]\";");
297     popDepth();
298     println("}");
299     
300     popDepth();
301     println("}");
302   }
303
304   /**
305    * Generates code for version changes.
306    */

307   protected void printVersionChange()
308     throws IOException JavaDoc
309   {
310     println("if (com.caucho.ejb.Version.getVersionId() != " +
311             com.caucho.ejb.Version.getVersionId() + ")");
312     println(" return true;");
313   }
314 }
315
Popular Tags