KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > idl > InterfaceBody


1 package org.jacorb.idl;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.io.PrintWriter JavaDoc;
24 import java.util.*;
25
26 /**
27  * @author Gerald Brose
28  * @version $Id: InterfaceBody.java,v 1.27 2004/05/06 12:39:58 nicolas Exp $
29  *
30  * directly known subclasses: ValueBody
31  */

32
33 public class InterfaceBody
34     extends IdlSymbol
35 {
36     public Vector v;
37     public Interface my_interface;
38     SymbolList inheritance_spec = null;
39     private Operation[] methods = null;
40     private boolean checking = false;
41     private String JavaDoc waitingName;
42
43     /** list of parse threads created and either active or still blocked */
44     public static Vector parseThreads = new Vector();
45
46     public class ParseThread
47         extends Thread JavaDoc
48     {
49
50         InterfaceBody b = null;
51         private boolean running = false;
52         private boolean incremented = false;
53
54         public ParseThread( InterfaceBody _b )
55         {
56             b = _b;
57             setDaemon( true );
58             parseThreads.addElement( this );
59             start();
60         }
61
62         public void run()
63         {
64             parser.set_pending( b.full_name() );
65             Object JavaDoc o = null;
66             for( Enumeration e = inheritance_spec.v.elements(); e.hasMoreElements(); )
67             {
68                 waitingName = ( (ScopedName)( e.nextElement() ) ).resolvedName();
69                 o = parser.get_pending( waitingName );
70                 if( o != null )
71                 {
72                     try
73                     {
74                         synchronized( o )
75                         {
76                             o.wait();
77                             running = true;
78                             parser.incActiveParseThreads();
79                             incremented = true;
80                         }
81                     }
82                     catch( InterruptedException JavaDoc ie )
83                     {
84                         logger.info( "ParseThread " + this + " interrupted!" );
85                     }
86                 }
87             }
88             b.internal_parse();
89             exitParseThread();
90         }
91
92         /**
93          * check whether this thread will eventually run
94          * @return true if the thread can run or is currently running
95          * false if it is still blocked or has just returned from run()
96          */

97
98         public synchronized boolean isRunnable()
99         {
100             boolean result = running || checkWaitCondition();
101             if( logger.isWarnEnabled() )
102                 logger.warn( "Thread is runnable: " + result );
103             return result;
104         }
105
106         private synchronized void exitParseThread()
107         {
108             parser.remove_pending( b.full_name() );
109             if( incremented )
110                 parser.decActiveParseThreads();
111             parseThreads.removeElement( this );
112             running = false;
113         }
114
115         /**
116          * @return true, if waiting condition is true,
117          * i.e., if thread still needs to wait.
118          */

119
120         private boolean checkWaitCondition()
121         {
122             return ( parser.get_pending( waitingName ) == null );
123         }
124
125     }
126
127
128     public InterfaceBody( int num )
129     {
130         super( num );
131         v = new Vector();
132     }
133
134     public void commit()
135     {
136     }
137
138
139     public void setEnclosingSymbol( IdlSymbol s )
140     {
141         if( enclosing_symbol != null && enclosing_symbol != s )
142             throw new RuntimeException JavaDoc( "Compiler Error: trying to reassign container for " + name );
143         enclosing_symbol = s;
144         for( Enumeration e = v.elements(); e.hasMoreElements(); )
145             ( (IdlSymbol)e.nextElement() ).setEnclosingSymbol( my_interface );
146     }
147
148     public void set_ancestors( SymbolList _inheritance_spec )
149     {
150         inheritance_spec = _inheritance_spec;
151     }
152
153
154     public void set_name( String JavaDoc n )
155     {
156         name = n;
157         for( Enumeration e = v.elements(); e.hasMoreElements(); )
158             ( (IdlSymbol)e.nextElement() ).setPackage( name );
159     }
160
161
162     public void setPackage( String JavaDoc s )
163     {
164         s = parser.pack_replace( s );
165         if( pack_name.length() > 0 )
166             pack_name = s + "." + pack_name;
167         else
168             pack_name = s;
169
170         for( Enumeration e = v.elements(); e.hasMoreElements(); )
171             ( (IdlSymbol)e.nextElement() ).setPackage( s );
172     }
173
174     public void addDefinition (Declaration d)
175     {
176         this.v.add (new Definition (d));
177     }
178
179     public void parse()
180     {
181         escapeName();
182
183         if( logger.isDebugEnabled() )
184             logger.debug( "Interface Body parse " + full_name() );
185
186         if( inheritance_spec != null )
187         {
188             Object JavaDoc o = null;
189             boolean pending = false;
190             for( Enumeration e = inheritance_spec.v.elements();
191                  e.hasMoreElements(); )
192             {
193                 ScopedName scoped_name = (ScopedName)e.nextElement();
194
195                 if( logger.isDebugEnabled() )
196                     logger.debug( "Trying to resolve " + scoped_name );
197
198
199                 o = parser.get_pending( scoped_name.resolvedName() );
200                 pending = pending || ( o != null );
201             }
202             ParseThread p = null;
203             if( pending )
204             {
205                 parser.set_pending( full_name() );
206                 p = new ParseThread( this );
207             }
208             else
209             {
210                 internal_parse();
211                 parser.remove_pending( full_name() );
212             }
213         }
214         else
215         {
216             internal_parse();
217             parser.remove_pending( full_name() );
218             if( logger.isDebugEnabled() )
219                 logger.debug( "Interface Body done parsing " + full_name() );
220         }
221     }
222
223     public void internal_parse()
224     {
225         if( logger.isDebugEnabled() )
226             logger.debug( "Interface Body internal_parse " + full_name() );
227
228         if( inheritance_spec != null )
229         {
230             try
231             {
232                 NameTable.inheritFrom( full_name(), inheritance_spec );
233             }
234             catch( NameAlreadyDefined nad )
235             {
236                 parser.fatal_error( "Name " + nad.getMessage() +
237                         " already defined in base interface(s)", token );
238             }
239         }
240         Definition d = null;
241         for( Enumeration e = v.elements(); e.hasMoreElements(); )
242         {
243             d = (Definition)e.nextElement();
244             Declaration dec = d.get_declaration();
245             if( is_pseudo )
246                 dec.set_pseudo();
247             dec.parse();
248         }
249     }
250
251     /**
252      * print definitions that appeared in an interface scope
253      * do not call print() in OpDecls and on Typedefs
254      */

255
256     public void print( PrintWriter JavaDoc ps )
257     {
258         if( ps != null )
259             throw new RuntimeException JavaDoc( "Compiler Error, interface body cannot be printed thus!" );
260
261         for( Enumeration e = v.elements(); e.hasMoreElements(); )
262         {
263             Declaration d = ( (Definition)e.nextElement() ).get_declaration();
264             if( !( d instanceof OpDecl ) )
265                 d.print( ps );
266         }
267     }
268
269     /** print signatures to the operations file */
270
271     public void printOperationSignatures( PrintWriter JavaDoc ps )
272     {
273         if( v.size() > 0 )
274         {
275             ps.println( "\t/* operations */" );
276         }
277
278         for( Enumeration e = v.elements(); e.hasMoreElements(); )
279         {
280             Definition d = (Definition)e.nextElement();
281             if( d.get_declaration() instanceof OpDecl )
282             {
283                 ( (OpDecl)d.get_declaration() ).printSignature( ps );
284             }
285             else if( d.get_declaration() instanceof AttrDecl )
286             {
287                 for( Enumeration m = ( (AttrDecl)d.get_declaration() ).getOperations();
288                      m.hasMoreElements(); )
289                 {
290                     ( (Operation)m.nextElement() ).printSignature( ps );
291                 }
292             }
293         }
294     }
295
296     /** print signatures to the operations file */
297
298     public void printConstants( PrintWriter JavaDoc ps )
299     {
300         if( v.size() > 0 )
301         {
302             ps.println( "\t/* constants */" );
303         }
304
305         for( Enumeration e = v.elements(); e.hasMoreElements(); )
306         {
307             Definition d = (Definition)e.nextElement();
308             if( d.get_declaration() instanceof ConstDecl )
309             {
310                 ( (ConstDecl)d.get_declaration() ).printContained( ps );
311             }
312         }
313     }
314
315     /** print only constant definitions to the interface file */
316
317     public void printInterfaceMethods( PrintWriter JavaDoc ps )
318     {
319         for( Enumeration e = v.elements(); e.hasMoreElements(); )
320         {
321             Definition d = (Definition)e.nextElement();
322             if( !( d.get_declaration() instanceof ConstDecl ) && is_pseudo() )
323             {
324                 ( (IdlSymbol)d ).print( ps );
325             }
326         }
327     }
328
329     public Operation[] getMethods()
330     {
331         if( methods == null )
332         {
333             Hashtable table = new Hashtable();
334             for( Enumeration e = v.elements(); e.hasMoreElements(); )
335             {
336                 Definition d = (Definition)e.nextElement();
337                 if( d.get_declaration() instanceof OpDecl )
338                 {
339                     table.put( ( (OpDecl)d.get_declaration() ).signature(), d.get_declaration() );
340                 }
341                 else if( d.get_declaration() instanceof AttrDecl )
342                 {
343                     for( Enumeration elems = ( (AttrDecl)d.get_declaration() ).getOperations();
344                          elems.hasMoreElements(); )
345                     {
346                         Operation op = (Operation)elems.nextElement();
347                         table.put( op.signature(), op );
348                     }
349                 }
350             }
351             for (Iterator i = my_interface.inheritanceSpec.v.iterator();
352                  i.hasNext(); )
353             {
354                 TypeSpec ts = ((ScopedName)i.next()).resolvedTypeSpec();
355                 if (ts instanceof ConstrTypeSpec)
356                 {
357                     Interface base = (Interface)((ConstrTypeSpec)ts).c_type_spec;
358                     Operation[] base_ops = base.getBody().getMethods();
359                     for( int j = 0; j < base_ops.length; j++ )
360                     {
361                         if( !table.contains( base_ops[ j ].signature() ) )
362                             table.put( base_ops[ j ].signature(), base_ops[ j ] );
363                     }
364
365                 }
366             }
367             Enumeration o = table.elements();
368             methods = new Operation[ table.size() ];
369             for( int i = 0; i < methods.length; i++ )
370                 methods[ i ] = (Operation)o.nextElement();
371         }
372         return methods;
373     }
374
375
376     /**
377      * Print methods to the stub file
378      */

379
380     public void printStubMethods( PrintWriter JavaDoc ps,
381                                   String JavaDoc classname,
382                                   boolean is_local,
383                                   boolean is_abstract)
384     {
385         Operation[] ops = getMethods();
386         for( int i = 0; i < ops.length; i++ )
387         {
388             ops[ i ].printMethod( ps, classname, is_local, is_abstract );
389         }
390
391         if ( parser.generate_ami_callback &&
392              !(my_interface instanceof ReplyHandler) )
393         {
394             for( int i = 0; i < ops.length; i++ )
395                 ops[ i ].print_sendc_Method( ps, classname );
396         }
397     }
398
399
400     /** print methods to the skeleton file */
401
402     public void printDelegatedMethods( PrintWriter JavaDoc ps )
403     {
404         Operation[] ops = getMethods();
405         if( ops.length > 0 )
406         {
407             for( int i = 0; i < ops.length; i++ )
408             {
409                 ops[ i ].printDelegatedMethod( ps );
410             }
411         }
412     }
413
414
415     /** print hash table that associates an operation string with an int */
416
417     public void printOperationsHash( PrintWriter JavaDoc ps )
418     {
419         Operation[] ops = getMethods();
420         if( ops.length <= 0 )
421             return;
422
423         String JavaDoc HASHTABLE = System.getProperty("java.version").startsWith ("1.1")
424                            ? "com.sun.java.util.collections.Hashtable"
425                            : "java.util.Hashtable";
426
427         ps.println( "\tstatic private final " + HASHTABLE
428                       + " m_opsHash = new " + HASHTABLE + "();" );
429         ps.println( "\tstatic" );
430         ps.println( "\t{" );
431
432         for( int i = 0; i < ops.length; i++ )
433         {
434             /* Some operation names have been escaped with "_" to
435                avoid name clashes with Java names. The operation name
436                on the wire is the original IDL name, however, so we
437                need to ask for the right name here. We need to take
438                care not to scramble up "_set_/_get" accessor methods!
439                (hence the check on instanceof OpDecl).
440                */

441
442             String JavaDoc name;
443             if( ops[ i ] instanceof OpDecl && ops[ i ].opName().startsWith( "_" ) )
444                 name = ops[ i ].opName().substring( 1 );
445             else
446                 name = ops[ i ].opName();
447
448             ps.println( "\t\tm_opsHash.put ( \"" + name + "\", new java.lang.Integer(" + i + "));" );
449         }
450
451         ps.println( "\t}" );
452     }
453
454     /** print methods for impl-based skeletons */
455
456     public void printSkelInvocations( PrintWriter JavaDoc ps )
457     {
458         Operation[] ops = getMethods();
459         if( ops.length <= 0 )
460         {
461             ps.println( "\t\tthrow new org.omg.CORBA.BAD_OPERATION(method + \" not found\");" );
462             return;
463         }
464         ps.println( "\t\t// quick lookup of operation" );
465         ps.println( "\t\tjava.lang.Integer opsIndex = (java.lang.Integer)m_opsHash.get ( method );" );
466         ps.println( "\t\tif ( null == opsIndex )" );
467         ps.println( "\t\t\tthrow new org.omg.CORBA.BAD_OPERATION(method + \" not found\");" );
468
469         ps.println( "\t\tswitch ( opsIndex.intValue() )" );
470         ps.println( "\t\t{" );
471
472         int nextIndex = 0;
473         for( int i = 0; i < ops.length; i++ )
474         {
475             String JavaDoc name;
476             if( ops[ i ] instanceof OpDecl && ops[ i ].opName().startsWith( "_" ) )
477                 name = ops[ i ].opName().substring( 1 );
478             else
479                 name = ops[ i ].opName();
480
481             ps.println( "\t\t\tcase " + nextIndex++ + ": // " + name );
482             ps.println( "\t\t\t{" );
483             ops[ i ].printInvocation( ps );
484             ps.println( "\t\t\t\tbreak;" );
485             ps.println( "\t\t\t}" );
486         }
487
488         ps.println( "\t\t}" );
489         ps.println( "\t\treturn _out;" );
490     }
491
492     void getIRInfo( Hashtable irInfoTable )
493     {
494         for( Enumeration e = v.elements(); e.hasMoreElements(); )
495         {
496             Definition d = (Definition)e.nextElement();
497             if( d.get_declaration() instanceof OpDecl )
498             {
499                 ( (OpDecl)d.get_declaration() ).getIRInfo( irInfoTable );
500             }
501             else if( d.get_declaration() instanceof AttrDecl )
502             {
503                 ( (AttrDecl)d.get_declaration() ).getIRInfo( irInfoTable );
504             }
505         }
506     }
507
508     /**
509      */

510
511     public void accept( IDLTreeVisitor visitor )
512     {
513         visitor.visitInterfaceBody( this );
514     }
515
516
517 }
518
Popular Tags