KickJava   Java API By Example, From Geeks To Geeks.

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


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.make.ClassDependency;
32 import com.caucho.util.IntMap;
33 import com.caucho.vfs.MergePath;
34 import com.caucho.vfs.Path;
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 import java.util.Iterator JavaDoc;
41
42 /**
43  * Skeleton generator code for both Home and Remote interfaces.
44  */

45 class BurlapSkeletonGenerator extends MarshalGenerator {
46   Class JavaDoc _cl;
47   String JavaDoc _objClass;
48   String JavaDoc _fullName;
49   String JavaDoc _pkg;
50   String JavaDoc _className;
51
52   protected int _unique;
53   
54   protected ArrayList JavaDoc<Class JavaDoc> _marshallClasses;
55   protected ArrayList JavaDoc<Class JavaDoc> _unmarshallClasses;
56   
57   protected ArrayList JavaDoc<Class JavaDoc> _marshallArrays;
58   protected ArrayList JavaDoc<Class JavaDoc> _unmarshallArrays;
59   protected Class JavaDoc _interfaceClass;
60   protected ArrayList JavaDoc<PersistentDependency> _dependList;
61
62   /**
63    * Creates a skeleton generator for a given bean configuration.
64    */

65   BurlapSkeletonGenerator(Class JavaDoc interfaceClass)
66   {
67     if (interfaceClass == null)
68       throw new IllegalArgumentException JavaDoc("interface can't be null");
69     
70     _interfaceClass = interfaceClass;
71     
72     _dependList = new ArrayList JavaDoc<PersistentDependency>();
73
74     MergePath mergePath = new MergePath();
75     mergePath.addClassPath(interfaceClass.getClassLoader());
76
77     setSearchPath(mergePath);
78
79     _dependList.add(new ClassDependency(interfaceClass));
80   }
81
82   /**
83    * Creates a home interface.
84    */

85   static Class JavaDoc generate(Class JavaDoc interfaceClass, Path workDir)
86     throws Exception JavaDoc
87   {
88     BurlapSkeletonGenerator gen = new BurlapSkeletonGenerator(interfaceClass);
89     gen.setClassDir(workDir);
90
91     gen.setFullClassName("_ejb." + interfaceClass.getName() + "__BurlapSkel");
92
93     Class JavaDoc cl = gen.preload();
94     if (cl != null)
95       return cl;
96
97     gen.generate();
98     return gen.compile();
99   }
100
101   /**
102    * Generates the Java source code for the home skeleton.
103    */

104   public void generateJava()
105     throws IOException JavaDoc
106   {
107     ArrayList JavaDoc<Method JavaDoc> methodList = new ArrayList JavaDoc<Method JavaDoc>();
108
109     Method JavaDoc []methods = _interfaceClass.getMethods();
110     for (int i = 0; i < methods.length; i++) {
111       String JavaDoc className = methods[i].getDeclaringClass().getName();
112       String JavaDoc methodName = methods[i].getName();
113       
114       methodList.add(methods[i]);
115     }
116     
117     printHeader();
118     
119     IntMap methodMap = printDispatcher(methodList);
120
121     printDependList(_dependList);
122     
123     printFooter(methodMap);
124   }
125
126   /**
127    * Prints the header of the generated class.
128    */

129   void printHeader()
130     throws IOException JavaDoc
131   {
132     if (getPackageName() != null)
133       println("package " + getPackageName() + ";");
134
135     println();
136     println("import java.io.*;");
137     println("import com.caucho.util.*;");
138     println("import com.caucho.burlap.*;");
139     println("import com.caucho.burlap.io.BurlapInput;");
140     println("import com.caucho.burlap.io.BurlapOutput;");
141     println("import " + _interfaceClass.getName() + ";");
142
143     println();
144     print("public class " + getClassName() + " extends com.caucho.ejb.burlap.BurlapSkeleton");
145     println(" {");
146     
147     pushDepth();
148
149     println("private " + _interfaceClass.getName() + " obj;");
150     println();
151     println("protected void _setObject(Object o)");
152     println("{");
153     println(" obj = (" + _interfaceClass.getName() + ") o;");
154     println("}");
155   }
156
157   /**
158    * Creates the dispatch code.
159    *
160    * @param methodMap a hash map from method names to methods.
161    */

162   protected IntMap printDispatcher(ArrayList JavaDoc<Method JavaDoc> methods)
163     throws IOException JavaDoc
164   {
165     IntMap map = new IntMap();
166     
167     println();
168     print("protected void _execute(CharBuffer method, BurlapInput in, BurlapOutput out)");
169     println(" throws Throwable");
170     println("{");
171     pushDepth();
172     println("switch (methods.get(method)) {");
173
174     int i = 0;
175     for (; i < methods.size(); i++) {
176       Method JavaDoc method = methods.get(i);
177
178       if (map.get(method.getName()) < 0)
179         map.put(method.getName(), i);
180       
181       map.put(mangleMethodName(method.getName(), method, false), i);
182
183       if (i > 0)
184         println();
185       println("case " + i + ":");
186       pushDepth();
187       println("{");
188       pushDepth();
189       
190       printUnmarshal(method.getName(), method.getParameterTypes(),
191                      method.getReturnType());
192
193       println("break;");
194       popDepth();
195       println("}");
196       popDepth();
197     }
198
199     println("default:");
200     println(" _executeUnknown(method, in, out);");
201     println(" break;");
202     println("}");
203     popDepth();
204     println("}");
205
206     return map;
207   }
208
209   /**
210    * Print the unmarshalling and marshalling code for a single method.
211    *
212    * @param method the reflected method to define code.
213    */

214   void printUnmarshal(String JavaDoc methodName, Class JavaDoc []param, Class JavaDoc retType)
215     throws IOException JavaDoc
216   {
217     for (int i = 0; i < param.length; i++) {
218       String JavaDoc var = "_arg" + i;
219       printClass(param[i]);
220       print(" " + var + " = ");
221       printUnmarshalType(param[i]);
222     }
223
224     println("in.completeCall();");
225
226     if (! retType.getName().equals("void")) {
227       printClass(retType);
228       print(" _ret = ");
229     }
230
231     printCall("obj", methodName, param);
232
233     println("out.startReply();");
234
235     if (! retType.equals(void.class))
236       printMarshalType(retType, "_ret");
237     else
238       println("out.writeNull();");
239     
240     println("out.completeReply();");
241   }
242
243   /**
244    * Prints a call to the underlying objects.
245    *
246    * @param objName the underlying object name
247    * @param method the underlying method
248    */

249   void printCall(String JavaDoc objName, String JavaDoc methodName, Class JavaDoc []param)
250     throws IOException JavaDoc
251   {
252     print(objName);
253     print(".");
254     print(methodName);
255     print("(");
256     for (int i = 0; i < param.length; i++) {
257       if (i != 0)
258         print(", ");
259       print("_arg" + i);
260     }
261     println(");");
262   }
263
264   /**
265    * Returns the externally callable method name.
266    */

267   String JavaDoc getMethodName(Method JavaDoc method)
268   {
269     return method.getName();
270   }
271
272   /**
273    * Prints the code at the end of the class to define the static constants.
274    */

275   protected void printFooter(IntMap methodMap)
276     throws IOException JavaDoc
277   {
278     println("static private IntMap methods = new IntMap();");
279     println("static {");
280     pushDepth();
281
282     Iterator JavaDoc keys = methodMap.iterator();
283     while (keys.hasNext()) {
284       String JavaDoc name = (String JavaDoc) keys.next();
285       int value = methodMap.get(name);
286       
287       println("methods.put(new CharBuffer(\"" + name + "\")," + value + ");");
288     }
289     popDepth();
290     println("}");
291     popDepth();
292     println("}");
293   }
294 }
295
Popular Tags