KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > iiop > IiopStubCompiler


1 /*
2  * Copyright (c) 1998-2000 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.iiop;
30
31 import com.caucho.java.AbstractGenerator;
32 import com.caucho.server.util.CauchoSystem;
33 import com.caucho.vfs.MergePath;
34 import com.caucho.vfs.Path;
35
36 import javax.ejb.EJBHome JavaDoc;
37 import javax.ejb.EJBObject JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.lang.reflect.Method JavaDoc;
40
41 public class IiopStubCompiler extends AbstractGenerator {
42   private Class JavaDoc _cl;
43   
44   public IiopStubCompiler(Class JavaDoc cl)
45   {
46     _cl = cl;
47
48     String JavaDoc name = cl.getName();
49
50     // XXX:
51
//name = "org.omg.stub." + name;
52

53     int p = name.lastIndexOf('.');
54     if (p > 0) {
55       String JavaDoc pkg = name.substring(0, p);
56       String JavaDoc tail = name.substring(p + 1);
57       setFullClassName(pkg + "._" + tail + "_Stub");
58     }
59     else
60       setFullClassName("_" + name + "_Stub");
61   }
62     
63   /**
64    * Starts generation of the Java code
65    */

66   public void generateJava()
67     throws Exception JavaDoc
68   {
69     printHeader();
70
71     Method JavaDoc []methods = _cl.getMethods();
72     for (int i = 0; i < methods.length; i++) {
73       Method JavaDoc method = methods[i];
74       
75       if (method.getDeclaringClass().isAssignableFrom(EJBHome JavaDoc.class) ||
76           method.getDeclaringClass().isAssignableFrom(EJBObject JavaDoc.class))
77         continue;
78
79       printMethod(method);
80     }
81     
82     printFooter();
83   }
84
85   /**
86    * Prints the header
87    */

88   private void printHeader()
89     throws IOException JavaDoc
90   {
91     println("/*");
92     println(" * Generated by Resin-EJB IiopStubCompiler");
93     println(" */");
94     println();
95     
96     if (getPackageName() != null)
97       println("package " + getPackageName() + ";");
98
99     println();
100     print("public class " + getClassName());
101     if (EJBHome JavaDoc.class.isAssignableFrom(_cl))
102       println(" extends com.caucho.iiop.client.IiopHomeStub");
103     else
104       println(" extends com.caucho.iiop.client.IiopStub");
105     println(" implements " + _cl.getName() + " {");
106     pushDepth();
107
108     println("private static final String[] _type_ids = {");
109     println(" \"RMI:" + _cl.getName() + ":0000000000000000\"");
110     println("};");
111     
112     println();
113     println("public String[] _ids()");
114     println("{");
115     println(" return _type_ids;");
116     println("}");
117   }
118
119   /**
120    * Prints a method
121    */

122   private void printMethod(Method JavaDoc method)
123     throws IOException JavaDoc
124   {
125     printMethodHeader(method);
126     println("{");
127     pushDepth();
128
129     println("org.omg.CORBA_2_3.portable.OutputStream os = null;");
130     println("try {");
131     pushDepth();
132     
133     println("os = (org.omg.CORBA_2_3.portable.OutputStream) _request(\"" + method.getName() + "\", true);");
134
135     Class JavaDoc []args = method.getParameterTypes();
136     for (int i = 0; i < args.length; i++) {
137       printSetValue(args[i], "a" + i);
138     }
139     
140     println("org.omg.CORBA_2_3.portable.InputStream is;");
141     println("is = (org.omg.CORBA_2_3.portable.InputStream) _invoke(os);");
142
143     Class JavaDoc returnType = method.getReturnType();
144     
145     if (returnType.equals(void.class)) {
146     }
147     else {
148       print("return ");
149       printGetValue(returnType);
150       println(";");
151     }
152     
153     popDepth();
154     println("} catch (org.omg.CORBA.portable.ApplicationException e) {");
155     // XXX: handle types
156
println(" org.omg.CORBA_2_3.portable.InputStream in = (org.omg.CORBA_2_3.portable.InputStream) e.getInputStream();");
157     println(" String name = in.read_string();");
158     println(" Throwable ex = (Throwable) in.read_value();");
159     for (Class JavaDoc ex : method.getExceptionTypes()) {
160       println(" if (ex instanceof " + ex.getName() + ")");
161       println(" throw (" + ex.getName() + ") ex;");
162     }
163     println(" throw new java.rmi.RemoteException(ex.getMessage(), ex);");
164     println("} catch (Exception e) {");
165     println(" e.printStackTrace();");
166     println(" throw new java.rmi.RemoteException(e.getMessage(), e);");
167     println("}");
168
169     popDepth();
170     println("}");
171   }
172
173   private void printGetValue(Class JavaDoc type)
174     throws IOException JavaDoc
175   {
176     if (type.equals(boolean.class))
177       print("is.read_boolean()");
178     else if (type.equals(byte.class))
179       print("is.read_octet()");
180     else if (type.equals(short.class))
181       print("is.read_short()");
182     else if (type.equals(int.class))
183       print("is.read_long()");
184     else if (type.equals(long.class))
185       print("is.read_longlong()");
186     else if (type.equals(float.class))
187       print("is.read_float()");
188     else if (type.equals(double.class))
189       print("is.read_double()");
190     else if (type.equals(char.class))
191       print("is.read_wchar()");
192     else if (javax.ejb.EJBObject JavaDoc.class.isAssignableFrom(type) ||
193          java.rmi.Remote JavaDoc.class.isAssignableFrom(type)) {
194       // Note that if sender/receiver have mismatched types, one
195
// possible result is a Java OOM
196
// ejb/114t
197
print("(");
198       printJavaClass(type);
199       print(") is.read_Object(");
200       printJavaClass(type);
201       print(".class)");
202     }
203     else {
204       print("(");
205       printJavaClass(type);
206       print(") is.read_value(");
207       printJavaClass(type);
208       print(".class)");
209     }
210   }
211
212   void printJavaClass(Class JavaDoc type)
213     throws IOException JavaDoc
214   {
215     if (type.isArray()) {
216       printJavaClass(type.getComponentType());
217       print("[]");
218     }
219     else
220       print(type.getName());
221   }
222
223   private void printSetValue(Class JavaDoc type, String JavaDoc value)
224     throws IOException JavaDoc
225   {
226     if (type.equals(boolean.class))
227       println("os.write_boolean(" + value + ");");
228     else if (type.equals(byte.class))
229       println("os.write_octet(" + value + ");");
230     else if (type.equals(short.class))
231       println("os.write_short(" + value + ");");
232     else if (type.equals(int.class))
233       println("os.write_long(" + value + ");");
234     else if (type.equals(long.class))
235       println("os.write_longlong(" + value + ");");
236     else if (type.equals(float.class))
237       println("os.write_float(" + value + ");");
238     else if (type.equals(double.class))
239       println("os.write_double(" + value + ");");
240     else if (type.equals(char.class))
241       println("os.write_wchar(" + value + ");");
242     else
243       println("os.write_value((java.io.Serializable) " + value + ");");
244   }
245   
246   /**
247    * Prints the footer
248    */

249   private void printFooter()
250     throws IOException JavaDoc
251   {
252     popDepth();
253     println("}");
254   }
255
256   public static Class JavaDoc create(String JavaDoc className)
257     throws Exception JavaDoc
258   {
259     Class JavaDoc cl = CauchoSystem.loadClass(className);
260
261     MergePath mergePath = new MergePath();
262     mergePath.addClassPath();
263
264     Path classPath = mergePath.lookup(className.replace('.', '/') + ".class");
265     classPath = ((MergePath) classPath).getBestPath();
266
267     Path dir = classPath.getParent();
268     int i = 0;
269     while ((i = className.indexOf('.', i + 1)) >= 0) {
270       dir = dir.getParent();
271     }
272     
273     dir.mkdirs();
274
275     IiopStubCompiler compiler = new IiopStubCompiler(cl);
276     compiler.setClassDir(dir);
277     Class JavaDoc gen = compiler.preload();
278     if (gen != null)
279       return gen;
280     
281     compiler.generate();
282     
283     return compiler.compile();
284   }
285
286   /**
287    * Compiles the stub
288    */

289   public static void main(String JavaDoc []argv)
290     throws Exception JavaDoc
291   {
292     String JavaDoc className = argv[0];
293
294     System.out.println(create(className));
295   }
296 }
297
Popular Tags