KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > compiler > StubCompiler


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop.compiler;
19
20 import java.lang.reflect.Method JavaDoc;
21 import java.io.File JavaDoc;
22 import java.util.*;
23
24 import org.apache.geronimo.interop.generator.*;
25 import org.apache.geronimo.interop.util.JavaClass;
26 import org.apache.geronimo.interop.util.ProcessUtil;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 public class StubCompiler extends Compiler JavaDoc {
31     private final Log log = LogFactory.getLog(StubCompiler.class);
32
33     private ValueTypeContext vtc = new ValueTypeContext();
34
35     private String JavaDoc inStreamName = "getInputStream";
36     private String JavaDoc outStreamName = "getOutputStream";
37
38     private HashMap packages = new HashMap();
39
40     public StubCompiler(GenOptions go, ClassLoader JavaDoc cl) {
41         super(go, cl);
42     }
43
44     protected void addMethod(JClass jc, String JavaDoc iiopMethodName, JReturnType jrc,
45                              String JavaDoc name, JParameter[] jparms, Class JavaDoc[] excepts) {
46         //
47
// Method Template:
48
//
49
// java.lang.Object $key_1 = $getRequestKey();
50
// for (int $retry = 0; ; $retry++)
51
// {
52
// try
53
// {
54
// org.apache.geronimo.interop.rmi.iiop.client.Connection $connection_2 = this.$connect();
55
// org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream $output_3 = $connection_2.getSimpleOutputStream(); // simple idl
56
// org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream $output_3 = $connection_2.getOutputStream(); // rmi-iiop
57
// $output_3.writeObject(type$1, p1);
58
// $connection_2.invoke(this, "_is_a", $key_1, $retry);
59
// org.apache.geronimo.interop.rmi.iiop.ObjectInputStream $input_4 = $connection_2.getSimpleInputStream(); // simple idl
60
// org.apache.geronimo.interop.rmi.iiop.ObjectInputStream $input_4 = $connection_2.getInputStream(); // rmi-iiop
61
// $connection_2.forget($key_1);
62
// $connection_2.close();
63
// java.lang.String $et_5 = $connection_2.getExceptionType();
64
// if ($et_5 != null)
65
// {
66
// throw org.apache.geronimo.interop.rmi.iiop.SystemExceptionFactory.getException($connection_2.getException());
67
// }
68
// boolean $djc_result;
69
// $djc_result = $input_4.readBoolean();
70
// return $djc_result;
71
// }
72
// catch (org.apache.geronimo.interop.rmi.iiop.client.RetryInvokeException $ex_6)
73
// {
74
// if ($retry == 3)
75
// {
76
// throw $ex_6.getRuntimeException();
77
// }
78
// }
79
// }
80

81         //JParameter jpID = new JParameter( java.lang.String.class, "id" );
82
JMethod jm = jc.newMethod(jrc, name, jparms, excepts);
83
84         JLocalVariable jlvKey = jm.newLocalVariable(Object JavaDoc.class, "$key", new JExpression(new JCodeStatement("$getRequestKey()")));
85         JLocalVariable jlvRetry = jm.newLocalVariable(int.class, "$retry");
86
87         JForStatement jfs = new JForStatement(new JCodeStatement(jlvRetry.getName() + " = 0"),
88                                               new JExpression(new JCodeStatement(" ; ")),
89                                               new JCodeStatement(jlvRetry.getName() + "++"));
90
91         jm.addStatement(jfs);
92
93         JTryCatchFinallyStatement tcfs = new JTryCatchFinallyStatement();
94         JTryStatement ts = tcfs.getTryStatement();
95
96         JBlockStatement jbs = (JBlockStatement) ts;
97
98         JLocalVariable jlvConn = jbs.newLocalVariable(org.apache.geronimo.interop.rmi.iiop.client.Connection.class, "$conn");
99         JLocalVariable jlvOutput = jbs.newLocalVariable(org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream.class, "$out");
100         JLocalVariable jlvEt = jbs.newLocalVariable(java.lang.String JavaDoc.class, "$et");
101         JLocalVariable jlvInput = null;
102
103         JLocalVariable jlvRc = null;
104         if (jrc != null && jrc.getType() != void.class) {
105             jlvRc = jbs.newLocalVariable(jrc.getType(), "$rc");
106             jlvInput = jbs.newLocalVariable(org.apache.geronimo.interop.rmi.iiop.ObjectInputStream.class, "$in");
107         }
108
109         jbs.addStatement(new JCodeStatement(jlvConn.getName() + " = this.$connect();"));
110         jbs.addStatement(new JCodeStatement(jlvOutput.getName() + " = " + jlvConn.getName() + "." + outStreamName + "();"));
111
112         String JavaDoc writeMethod = null;
113         String JavaDoc writeCall = "";
114         for (int i = 0; i < jparms.length; i++) {
115             writeMethod = getWriteMethod(jparms[i]);
116
117             if (writeMethod != null) {
118                 writeCall = writeMethod + "( " + jparms[i].getName() + " )";
119             } else {
120                 writeCall = "writeObject( " + vtc.getValueTypeVarName(jc, jparms[i]) + ", " + jparms[i].getName() + ")";
121             }
122
123             jbs.addStatement(new JCodeStatement(jlvOutput.getName() + "." + writeCall + ";"));
124         }
125
126         jbs.addStatement(new JCodeStatement(jlvConn.getName() + ".invoke(this, \"" + iiopMethodName + "\", " + jlvKey.getName() + ", $retry);"));
127         if (jlvRc != null) {
128             jbs.addStatement(new JCodeStatement(jlvInput.getName() + " = " + jlvConn.getName() + "." + inStreamName + "();"));
129         }
130         jbs.addStatement(new JCodeStatement(jlvConn.getName() + ".forget(" + jlvKey.getName() + ");"));
131         jbs.addStatement(new JCodeStatement(jlvConn.getName() + ".close();"));
132         jbs.addStatement(new JCodeStatement(jlvEt.getName() + " = " + jlvConn.getName() + ".getExceptionType();"));
133
134         JIfElseIfElseStatement jiefs = new JIfElseIfElseStatement(new JExpression(new JCodeStatement(jlvEt.getName() + " != null")));
135         JIfStatement jis = jiefs.getIfStatement();
136         jis.addStatement(new JCodeStatement("throw org.apache.geronimo.interop.rmi.iiop.SystemExceptionFactory.getException(" + jlvConn.getName() + ".getException());"));
137         jbs.addStatement(jiefs);
138
139         if (jlvRc != null) {
140             String JavaDoc readMethod = getReadMethod(jlvRc);
141             String JavaDoc readCall = "";
142
143             if (readMethod != null) {
144                 readCall = jlvInput.getName() + "." + readMethod + "()";
145             } else {
146                 readCall = "(" + jlvRc.getTypeDecl() + ")" + jlvInput.getName() + "." + "readObject( " + vtc.getValueTypeVarName(jc, jlvRc) + ")";
147             }
148
149             jbs.addStatement(new JCodeStatement(jlvRc.getName() + " = " + readCall + ";"));
150             jbs.addStatement(new JCodeStatement("return " + jlvRc.getName() + ";"));
151         }
152
153         ts.addStatement(jbs);
154
155         JVariable jv = new JVariable(org.apache.geronimo.interop.rmi.iiop.client.RetryInvokeException.class, "$ex");
156         JCatchStatement cs = tcfs.newCatch(jv);
157
158         jiefs = new JIfElseIfElseStatement(new JExpression(new JCodeStatement(jlvRetry.getName() + " == 3")));
159         jis = jiefs.getIfStatement();
160         jis.addStatement(new JCodeStatement("throw " + jv.getName() + ".getRuntimeException();"));
161         cs.addStatement(jiefs);
162
163         jfs.addStatement(tcfs);
164     }
165
166     protected void addMethod_is_a(JClass jc) {
167         JParameter jpID = new JParameter(java.lang.String JavaDoc.class, "id");
168         addMethod(jc, "_is_a", new JReturnType(boolean.class),
169                   "_is_a",
170                   new JParameter[]{jpID},
171                   (Class JavaDoc[]) null);
172
173     }
174
175     protected void addMethod(MethodOverload mo, JClass jc) {
176         Method JavaDoc m = mo.method;
177
178         String JavaDoc name = m.getName();
179         JParameter[] sparms = getMethodParms(m);
180
181         addMethod(jc, mo.iiop_name, new JReturnType(m.getReturnType()),
182                   name,
183                   sparms,
184                   m.getExceptionTypes());
185
186     }
187
188     public void generate() throws GenException {
189         GenOptions go = getGenOptions();
190         List interfaces = go.getInterfaces();
191         Iterator intf = null;
192
193         if (interfaces != null) {
194             intf = interfaces.iterator();
195         }
196
197         JavaGenerator jg = new JavaGenerator(genOptions);
198
199         if (go.isSimpleIdl()) {
200             inStreamName = "getSimpleInputStream";
201             outStreamName = "getSimpleOutputStream";
202         } else {
203             inStreamName = "getInputStream";
204             outStreamName = "getOutputStream";
205         }
206
207         String JavaDoc riClassName = "";
208         Class JavaDoc riClass = null;
209         String JavaDoc stubClassName = "";
210         JPackage pkg = null;
211
212         while (intf != null && intf.hasNext() ) {
213             // Clear the value type cache.
214
vtc.clear();
215
216             riClassName = (String JavaDoc)intf.next();
217             stubClassName = JavaClass.addPackageSuffix(riClassName, "iiop_stubs") + "_Stub";
218
219             try {
220                 riClass = getClassLoader().loadClass( riClassName );
221             } catch (Exception JavaDoc ex) {
222                 throw new GenException( "Generate Stubs Failed:", ex );
223             }
224
225             String JavaDoc pkgName = JavaClass.getNamePrefix(stubClassName);
226             pkg = (JPackage) packages.get( pkgName );
227             if (pkg == null)
228             {
229                 pkg = new JPackage( pkgName );
230                 packages.put( pkgName, pkg );
231             }
232
233             String JavaDoc className = JavaClass.getNameSuffix(stubClassName);
234             JClass jc = pkg.newClass(className);
235             jc.addImport("org.apache.geronimo.interop.rmi.iiop", "ObjectRef");
236             jc.setExtends("ObjectRef");
237             jc.addImplements(riClass.getName());
238
239             JConstructor jcCon = jc.newConstructor((JParameter[]) null, (Class JavaDoc[]) null);
240             jcCon.addStatement(new JCodeStatement("super();"));
241
242             addMethod_is_a(jc);
243
244             Method JavaDoc m[] = getMethods( riClass, go.isSimpleIdl());
245             MethodOverload mo[] = null;
246             mo = getMethodOverloads( m );
247             for (int i = 0; mo != null && i < mo.length; i++) {
248                 addMethod( mo[i], jc );
249             }
250         }
251
252         Set pkgSet = packages.keySet();
253         Iterator pkgIt = pkgSet.iterator();
254         String JavaDoc stubPkg = "";
255
256         while (pkgIt.hasNext())
257         {
258             stubPkg = (String JavaDoc) pkgIt.next();
259             pkg = (JPackage)packages.get(stubPkg);
260             System.out.println("Generating Package: " + stubPkg);
261             jg.generate(pkg);
262         }
263     }
264
265     public void compile()
266             throws Exception JavaDoc {
267
268         Set pkg = packages.keySet();
269         Iterator pkgIt = pkg.iterator();
270         String JavaDoc stubPkg = "";
271
272         /*
273          * Each of the packages were generated under go.getGenSrcDir().
274          *
275          * Go through all the packages and run the compiler on *.java
276          */

277
278         GenOptions go = getGenOptions();
279         String JavaDoc classpath = adjustPath(go.getClasspath());
280         String JavaDoc srcpath = adjustPath(go.getGenSrcDir());
281
282         String JavaDoc filesToCompile = "";
283         String JavaDoc javacCmd = "";
284
285         while (pkgIt.hasNext())
286         {
287             stubPkg = (String JavaDoc) pkgIt.next();
288             stubPkg = stubPkg.replace( '.', File.separatorChar );
289             filesToCompile = adjustPath(go.getGenSrcDir() + File.separator + stubPkg + File.separator + "*.java");
290
291             System.out.println("Compiling Package: " + filesToCompile);
292
293             javacCmd = "javac -d " + go.getGenClassDir() +
294                             ( go.isCompileDebug() ? " -g" : "" ) +
295                             " -classpath " + classpath + " " +
296                             " -sourcepath " + srcpath + " " + filesToCompile;
297
298             System.out.println( "Lauching: " + javacCmd );
299
300             ProcessUtil pu = ProcessUtil.getInstance();
301             pu.setEcho(System.out);
302             pu.run(javacCmd, (String JavaDoc[]) null, "./" );
303         }
304     }
305
306     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
307         GenOptions go = null;
308
309         try
310         {
311             go = new GenOptions( "./stubs", args );
312         }
313         catch( GenWarning gw )
314         {
315             gw.printStackTrace();
316         }
317
318         ClassLoader JavaDoc cl = ClassLoader.getSystemClassLoader();
319         StubCompiler sg = new StubCompiler( go, cl );
320
321         if (go.isGenerate()) {
322             sg.generate();
323         }
324
325         if (go.isCompile()) {
326             sg.compile();
327         }
328     }
329 }
330
Popular Tags