KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > compiler > StubCompiler


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop.compiler;
20
21 import gcc.generator.*;
22 import gcc.util.JavaClass;
23 import gcc.util.ProcessUtil;
24
25 import java.lang.reflect.Modifier;
26 import java.lang.reflect.Method;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.LinkedList;
30 import java.util.Iterator;
31
32 public class StubCompiler
33         extends Compiler
34 {
35     protected ValueTypeContext _vtc = new ValueTypeContext();
36     protected static JParameter _objInputVar = new JParameter( gcc.rmi.iiop.ObjectInputStream.class, "input");
37     protected static JParameter _objOutputVar = new JParameter( gcc.rmi.iiop.ObjectOutputStream.class, "output");
38
39     protected String _stubClassName = "";
40     protected Class _stubClass = null;
41
42     protected String _inStreamName = "getInputStream";
43     protected String _outStreamName = "getOutputStream";
44
45     public StubCompiler( Class riClass )
46     {
47         super( riClass );
48         init();
49     }
50
51     public StubCompiler( Class riClass, GenOptions go )
52     {
53         super( riClass, go );
54         init();
55     }
56
57     protected void init()
58     {
59         String className = _riClass.getName();
60         _stubClassName = JavaClass.addPackageSuffix(className, "iiop_stubs") + "_Stub";
61     }
62
63     protected void addMethod( JClass jc, JReturnType jrc, String name, JParameter[] jparms, Class[] excepts )
64     {
65 // java.lang.Object $key_1 = $getRequestKey();
66
// for (int $retry = 0; ; $retry++)
67
// {
68
// try
69
// {
70
// gcc.rmi.iiop.client.Connection $connection_2 = this.$connect();
71
// gcc.rmi.iiop.ObjectOutputStream $output_3 = $connection_2.getSimpleOutputStream(); // simple idl
72
// gcc.rmi.iiop.ObjectOutputStream $output_3 = $connection_2.getOutputStream(); // rmi-iiop
73
// $output_3.writeObject(type$1, p1);
74
// $connection_2.invoke(this, "_is_a", $key_1, $retry);
75
// gcc.rmi.iiop.ObjectInputStream $input_4 = $connection_2.getSimpleInputStream(); // simple idl
76
// gcc.rmi.iiop.ObjectInputStream $input_4 = $connection_2.getInputStream(); // rmi-iiop
77
// $connection_2.forget($key_1);
78
// $connection_2.close();
79
// java.lang.String $et_5 = $connection_2.getExceptionType();
80
// if ($et_5 != null)
81
// {
82
// throw gcc.rmi.iiop.SystemExceptionFactory.getException($connection_2.getException());
83
// }
84
// boolean $djc_result;
85
// $djc_result = $input_4.readBoolean();
86
// return $djc_result;
87
// }
88
// catch (gcc.rmi.iiop.client.RetryInvokeException $ex_6)
89
// {
90
// if ($retry == 3)
91
// {
92
// throw $ex_6.getRuntimeException();
93
// }
94
// }
95
// }
96

97         //JParameter jpID = new JParameter( java.lang.String.class, "id" );
98
JMethod jm = jc.newMethod( jrc, name, jparms, excepts );
99
100         JLocalVariable jlvKey = jm.newLocalVariable( Object.class, "$key", new JExpression( new JCodeStatement( "$getRequestKey()" ) ) );
101         JLocalVariable jlvRetry = jm.newLocalVariable( int.class, "$retry" );
102
103         JForStatement jfs = new JForStatement( new JCodeStatement( jlvRetry.getName() + " = 0" ),
104                                                new JExpression( new JCodeStatement( " ; ") ),
105                                                new JCodeStatement( jlvRetry.getName() + "++" ) );
106
107         jm.addStatement( jfs );
108
109         JTryCatchFinallyStatement tcfs = new JTryCatchFinallyStatement();
110         JTryStatement ts = tcfs.getTryStatement();
111
112         JBlockStatement jbs = (JBlockStatement)ts;
113
114         JLocalVariable jlvConn = jbs.newLocalVariable( gcc.rmi.iiop.client.Connection.class, "$conn" );
115         JLocalVariable jlvOutput = jbs.newLocalVariable( gcc.rmi.iiop.ObjectOutputStream.class, "$out" );
116         JLocalVariable jlvEt = jbs.newLocalVariable( java.lang.String.class, "$et" );
117         JLocalVariable jlvInput = null;
118
119         JLocalVariable jlvRc = null;
120         if (jrc != null && jrc.getType() != void.class)
121         {
122             jlvRc = jbs.newLocalVariable( jrc.getType(), "$rc" );
123             jlvInput = jbs.newLocalVariable( gcc.rmi.iiop.ObjectInputStream.class, "$in" );
124         }
125
126         jbs.addStatement( new JCodeStatement( jlvConn.getName() + " = this.$connect();" ) );
127         jbs.addStatement( new JCodeStatement( jlvOutput.getName() + " = " + jlvConn.getName() + "." + _outStreamName + "();" ) );
128
129         String writeMethod = null;
130         String writeCall = "";
131         for( int i=0; i<jparms.length; i++ )
132         {
133             writeMethod = getWriteMethod( jparms[i] );
134
135             if (writeMethod != null)
136             {
137                 writeCall = writeMethod + "( " + jparms[i].getName() + " )";
138             }
139             else
140             {
141                 writeCall = "writeObject( " + _vtc.getValueTypeVarName(jc,jparms[i]) + ", " + jparms[i].getName() + ")";
142             }
143
144             jbs.addStatement( new JCodeStatement( jlvOutput.getName() + "." + writeCall + ";") );
145         }
146
147         jbs.addStatement( new JCodeStatement( jlvConn.getName() + ".invoke(this, \"" + name + "\", " + jlvKey.getName() + ", $retry);" ) );
148         if (jlvRc != null)
149         {
150             jbs.addStatement( new JCodeStatement( jlvInput.getName() + " = " + jlvConn.getName() + "." + _inStreamName + "();" ) );
151         }
152         jbs.addStatement( new JCodeStatement( jlvConn.getName() + ".forget(" + jlvKey.getName() + ");" ) );
153         jbs.addStatement( new JCodeStatement( jlvConn.getName() + ".close();" ) );
154         jbs.addStatement( new JCodeStatement( jlvEt.getName() + " = " + jlvConn.getName() + ".getExceptionType();" ) );
155
156         JIfElseIfElseStatement jiefs = new JIfElseIfElseStatement( new JExpression( new JCodeStatement(jlvEt.getName() + " != null") ) );
157         JIfStatement jis = jiefs.getIfStatement();
158         jis.addStatement( new JCodeStatement( "throw gcc.rmi.iiop.SystemExceptionFactory.getException(" + jlvConn.getName() + ".getException());" ) );
159         jbs.addStatement( jiefs );
160
161         if (jlvRc != null)
162         {
163             String readMethod = getReadMethod( jlvRc );
164             String readCall = "";
165
166             if (readMethod != null)
167             {
168                 readCall = jlvInput.getName() + "." + readMethod + "()";
169             }
170             else
171             {
172                 readCall = "("+ jlvRc.getTypeDecl() + ")" + jlvInput.getName() + "." + "readObject( " + _vtc.getValueTypeVarName(jc,jlvRc) + ")";
173             }
174
175             jbs.addStatement( new JCodeStatement( jlvRc.getName() + " = " + readCall + ";" ) );
176             jbs.addStatement( new JCodeStatement( "return " + jlvRc.getName() + ";" ) );
177         }
178
179         ts.addStatement( jbs );
180
181         JVariable jv = new JVariable( gcc.rmi.iiop.client.RetryInvokeException.class, "$ex" );
182         JCatchStatement cs = tcfs.newCatch( jv );
183
184         jiefs = new JIfElseIfElseStatement( new JExpression( new JCodeStatement(jlvRetry.getName() + " == 3") ) );
185         jis = jiefs.getIfStatement();
186         jis.addStatement( new JCodeStatement( "throw " + jv.getName() + ".getRuntimeException();" ) );
187         cs.addStatement( jiefs );
188
189         jfs.addStatement( tcfs );
190     }
191
192     // public methods
193

194     public void addMethod_is_a( JClass jc )
195     {
196         JParameter jpID = new JParameter( java.lang.String.class, "id" );
197         addMethod( jc, new JReturnType(boolean.class),
198                        "_is_a",
199                        new JParameter[] { jpID },
200                        (Class[]) null );
201
202     }
203
204     public void addMethod( Method m, JClass jc )
205     {
206         String name = m.getName();
207         JParameter[] sparms = getMethodParms(m);
208
209         addMethod( jc, new JReturnType(m.getReturnType()),
210                                    name,
211                                    sparms,
212                                    m.getExceptionTypes() );
213
214     }
215
216     protected Method[] getMethods()
217     {
218         Method myMethods[] = _riClass.getDeclaredMethods();
219         Method myOpsMethods[] = null;
220         Class myInterfaces[] = _riClass.getInterfaces();
221
222         if (myInterfaces != null && myInterfaces.length > 0)
223         {
224             String opsName = _riClass.getName() + "Operations";
225
226             for( int i=0; i<myInterfaces.length; i++ )
227             {
228                 if (myInterfaces[i].getName().equals(opsName))
229                 {
230                     myOpsMethods = myInterfaces[i].getDeclaredMethods();
231                     break;
232                 }
233             }
234         }
235
236         Method m[] = null;
237
238         if (myOpsMethods == null)
239         {
240             m = myMethods;
241         }
242         else
243         {
244             m = new Method[ myMethods.length + myOpsMethods.length ];
245             System.arraycopy( myMethods, 0, m, 0, myMethods.length );
246             System.arraycopy( myOpsMethods, 0, m, myMethods.length, myOpsMethods.length );
247         }
248
249         return m;
250     }
251
252     public void generate()
253         throws Exception
254     {
255         _vtc.clear();
256
257         if (_simpleIDL)
258         {
259             _inStreamName = "getSimpleInputStream";
260             _outStreamName = "getSimpleOutputStream";
261         }
262
263         JavaGenerator jg = new JavaGenerator( _genOptions );
264
265         String className;
266         JPackage p = new JPackage( JavaClass.getNamePrefix(_stubClassName) );
267         className = JavaClass.getNameSuffix( _stubClassName );
268
269         JClass jc = p.newClass( className );
270         jc.addImport( "gcc.rmi.iiop", "ObjectRef" );
271         jc.setExtends( "ObjectRef" );
272         jc.addImplements( _riClass.getName() );
273
274         JField idsField = jc.newField( String[].class, "_ids", new JExpression( new JCodeStatement( "{ \"" + _riClass.getName() + "\", \"RMI:" + _riClass.getName() + ":0000000000000000\"}" ) ), true );
275
276         JConstructor jcCon = jc.newConstructor( (JParameter[]) null, (Class[]) null );
277         jcCon.addStatement( new JCodeStatement( "super();" ) );
278
279         addMethod_is_a( jc );
280
281         Method m[] = null;
282         m = getMethods();
283         for (int i=0; m != null && i<m.length; i++)
284         {
285             addMethod( m[i], jc );
286         }
287
288         jg.generate( p );
289     }
290
291     public void compile()
292         throws Exception
293     {
294         String className = _riClass.getName();
295         String stubClassName = JavaClass.addPackageSuffix(className, "iiop_stubs");
296         String stubPackage = JavaClass.getNamePrefix(stubClassName);
297
298         System.out.println( "Compiling Package: " + stubPackage );
299         System.out.println( "Compiling Stub: " + stubClassName );
300
301         String javac = "javac -d ../classes -classpath ../classes;D:/Dev/3rdparty.jag/ejb21/ejb-2_1-api.jar " + stubPackage.replace( '.', '/' ) + "/*.java";
302
303         ProcessUtil pu = ProcessUtil.getInstance();
304         pu.setEcho( System.out );
305         pu.run( javac, (String[])null, "./src" );
306     }
307
308     public Class getStubClass()
309     {
310         System.out.println( "StubCompiler.getStubClass(): riClass: " + _riClass );
311
312         if (_stubClass == null)
313         {
314             try
315             {
316                 _stubClass = Class.forName( _stubClassName );
317             }
318             catch( Exception e )
319             {
320                 e.printStackTrace();
321             }
322
323             try
324             {
325                 if( _stubClass == null )
326                 {
327                     generate();
328                     compile();
329                     _stubClass = Class.forName( _stubClassName );
330                 }
331             }
332             catch( Exception e )
333             {
334                 e.printStackTrace();
335             }
336         }
337
338         return _stubClass;
339     }
340
341     public static void main( String args[] )
342         throws Exception
343     {
344         boolean generate = false;
345         boolean compile = false;
346         boolean loadclass = false;
347         boolean simpleidl = false;
348         List interfaces = new LinkedList();
349         GenOptions go = new GenOptions();
350
351         go.setGenDir( "./" );
352         go.setOverwrite( false );
353         go.setVerbose( false );
354
355         for( int i=0; i<args.length; i++ )
356         {
357             if (args[i].equals( "-g" ))
358             {
359                 generate = true;
360             }
361             else if (args[i].equals( "-c" ))
362             {
363                 compile = true;
364             }
365             else if (args[i].equals( "-l" ))
366             {
367                 loadclass = true;
368             }
369             else if (args[i].equals( "-s" ))
370             {
371                 simpleidl = true;
372             }
373             else if (args[i].equals( "-d" ) && ((i+1) < args.length))
374             {
375                 go.setGenDir( args[++i] );
376             }
377             else if (args[i].equals( "-v" ))
378             {
379                 go.setVerbose( true );
380             }
381             else if (args[i].equals( "-o" ))
382             {
383                 go.setOverwrite( true );
384             }
385             else if (args[i].startsWith("-"))
386             {
387                 System.out.println( "Warning: Ignoring unrecognized options: '" + args[i] + "'" );
388             }
389             else
390             {
391                 interfaces.add( args[i] );
392             }
393         }
394
395         Iterator i = interfaces.iterator();
396         while( i != null && i.hasNext() )
397         {
398             String intfName = (String)i.next();
399
400             if (intfName.startsWith("RMI:"))
401             {
402                 simpleidl = false;
403                 intfName = intfName.substring( 4 );
404             }
405             else if (intfName.startsWith( "IDL:" ))
406             {
407                 simpleidl = true;
408                 intfName = intfName.substring( 4 );
409             }
410
411             Class riClass = Class.forName( intfName );
412             StubCompiler sg = new StubCompiler(riClass,go);
413             sg.setSimpleIDL( simpleidl );
414
415             if (generate)
416             {
417                 sg.generate();
418             }
419
420             if (compile)
421             {
422                 sg.compile();
423             }
424
425             if (loadclass)
426             {
427                 Class c = sg.getStubClass();
428                 System.out.println( "StubClass: " + c );
429             }
430         }
431
432         //sg.setSimpleIDL( true );
433
//sg.generate( "gcc.rmi.iiop.NameServiceOperations");
434
}
435
436 }
437
Popular Tags