KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > ir > OperationDef


1 package org.jacorb.ir;
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.lang.reflect.*;
24 import java.util.*;
25
26 import org.omg.CORBA.ExceptionDefPOATie;
27 import org.omg.CORBA.INTF_REPOS JavaDoc;
28 import org.omg.PortableServer.POA JavaDoc;
29
30 import org.apache.avalon.framework.logger.Logger;
31
32 public class OperationDef
33     extends Contained
34     implements org.omg.CORBA.OperationDefOperations JavaDoc
35 {
36     private org.omg.CORBA.TypeCode JavaDoc result = null;
37     private org.omg.CORBA.IDLType JavaDoc result_def = null;
38     private org.omg.CORBA.ExceptionDef JavaDoc[] exceptions = null;
39     private org.omg.CORBA.ParameterDescription JavaDoc[] params = null;
40
41     private String JavaDoc[] contexts = new String JavaDoc[0];
42     private org.omg.CORBA.OperationMode JavaDoc mode;
43
44     private Method method;
45
46     /** the extra information on the operation that is provided in the
47         IRHelper */

48     private String JavaDoc opInfo;
49     private String JavaDoc returnTypeName;
50     private String JavaDoc[] paramTypeNames = new String JavaDoc[0];
51
52     private boolean defined = false;
53
54     private Logger logger;
55     private ClassLoader JavaDoc loader;
56     private POA JavaDoc poa;
57
58     public OperationDef( Method m,
59                          Class JavaDoc def_in,
60                          Class JavaDoc irHelper,
61                          org.omg.CORBA.InterfaceDef JavaDoc i_def,
62                          Logger logger,
63                          ClassLoader JavaDoc loader,
64                          POA JavaDoc poa)
65     {
66         this.logger = logger;
67         this.loader = loader;
68         this.poa = poa;
69
70         def_kind = org.omg.CORBA.DefinitionKind.dk_Operation;
71         name( m.getName());
72
73         if (def_in == null)
74         {
75            throw new INTF_REPOS JavaDoc ("Class argument null");
76         }
77         if (i_def == null)
78         {
79            throw new INTF_REPOS JavaDoc ("Idef argument null" );
80         }
81
82         id( RepositoryID.toRepositoryID(
83                 RepositoryID.className( i_def.id(), loader) + "/" + name(),
84                 false,
85                 loader));
86
87         version(id().substring( id().lastIndexOf(':')));
88         defined_in = i_def;
89         containing_repository = i_def.containing_repository();
90         String JavaDoc className = def_in.getName();
91         absolute_name = i_def.absolute_name() + "::" + name;
92         method = m;
93
94         if (this.logger.isDebugEnabled())
95         {
96             this.logger.debug("New OperationDef, name: " + name +
97                               " " + absolute_name);
98         }
99
100         Hashtable irInfo = null;
101         opInfo = null;
102         try
103         {
104             irInfo = (Hashtable)irHelper.getDeclaredField("irInfo").get(null);
105             opInfo = (String JavaDoc)irInfo.get( name() );
106         }
107         catch( Exception JavaDoc e )
108         {
109             logger.error("Caught Exception", e);
110         }
111
112         /* parse extra operation information that's in the opInfo string */
113
114         if( opInfo != null )
115         {
116             if( opInfo.endsWith("-oneway"))
117             {
118                 mode = org.omg.CORBA.OperationMode.OP_ONEWAY;
119             }
120
121             if( opInfo.indexOf("(") > 0 )
122                 returnTypeName = opInfo.substring(0,opInfo.indexOf("("));
123
124
125             StringTokenizer strtok =
126                 new StringTokenizer( opInfo.substring( opInfo.indexOf("(") + 1,
127                                                        opInfo.lastIndexOf(")")), ",");
128
129             paramTypeNames = new String JavaDoc[strtok.countTokens()];
130             for( int i = 0; i < paramTypeNames.length; i++ )
131             {
132                 String JavaDoc token = strtok.nextToken();
133
134                 paramTypeNames[i] = ( !token.equals(",") ? token : null );
135             }
136         }
137
138         if( mode == null )
139         {
140             mode = org.omg.CORBA.OperationMode.OP_NORMAL;
141         }
142
143
144         contexts = new String JavaDoc[0];
145     }
146
147     void define()
148     {
149         try
150         {
151             result =
152                 TypeCodeUtil.getTypeCode( method.getReturnType(),
153                                           this.loader,
154                                           null,
155                                           returnTypeName,
156                                           this.logger);
157
158             result_def = org.jacorb.ir.IDLType.create( result,
159                                                        containing_repository,
160                                                        this.logger,
161                                                        this.poa);
162         }
163         catch( Exception JavaDoc e )
164         {
165             logger.error("Caught Exception", e);
166         }
167
168         params = getParameterDescriptions();
169
170         Class JavaDoc [] ex_classes = method.getExceptionTypes();
171         Class JavaDoc uexc = null;
172         try
173         {
174             uexc = this.loader.loadClass("org.omg.CORBA.UserException");
175         }
176         catch ( ClassNotFoundException JavaDoc e1)
177         {
178             throw new INTF_REPOS JavaDoc(ErrorMsg.IR_Definition_Not_Found,
179                                                org.omg.CORBA.CompletionStatus.COMPLETED_NO);
180         }
181
182         Vector v = new Vector();
183         for( int ix = 0; ix < ex_classes.length; ix++ )
184         {
185             if( uexc.isAssignableFrom(ex_classes[ix]))
186             {
187                 try
188                 {
189                     ExceptionDef ex = new ExceptionDef( ex_classes[ ix ],
190                                                          defined_in,
191                                                          containing_repository,
192                                                          this.loader,
193                                                          this.poa,
194                                                          this.logger);
195                     org.omg.CORBA.ExceptionDef JavaDoc exRef =
196                         org.omg.CORBA.ExceptionDefHelper.narrow(
197                               this.poa.servant_to_reference(
198                                    new ExceptionDefPOATie ( ex )
199                                    )
200                               );
201
202                     v.addElement( exRef );
203                     ex.setReference( exRef );
204                 }
205                 catch( Exception JavaDoc e )
206                 {
207                     logger.error("Caught Exception", e);
208                 }
209             }
210         }
211
212         exceptions = new org.omg.CORBA.ExceptionDef JavaDoc[ v.size() ];
213         v.copyInto( exceptions );
214
215         defined = true;
216     }
217
218
219     org.omg.CORBA.ParameterDescription JavaDoc[] getParameterDescriptions()
220     {
221         org.omg.CORBA.TypeCode JavaDoc tc = null;
222         Class JavaDoc m_params[] = method.getParameterTypes();
223
224         org.omg.CORBA.ParameterDescription JavaDoc[] params =
225             new org.omg.CORBA.ParameterDescription JavaDoc[m_params.length];
226
227         if( paramTypeNames.length > 0 )
228         {
229             if (paramTypeNames.length != m_params.length)
230             {
231                 throw new INTF_REPOS JavaDoc ("Different parameter type numbers! " +
232                                       paramTypeNames.length + " vs. " + m_params.length +
233                                       " inforString: " + opInfo);
234             }
235         }
236
237
238         for( int i = 0; i < params.length; i++)
239         {
240             String JavaDoc name = "arg_" + i;
241             String JavaDoc paramInfo = null;
242             org.omg.CORBA.ParameterMode JavaDoc mode = null;
243             try
244             {
245                 String JavaDoc parameterTypeName = m_params[i].getName();
246
247                 if( paramTypeNames.length != 0 )
248                     paramInfo = paramTypeNames[i];
249
250                 if( ! parameterTypeName.endsWith("Holder") )
251                 {
252                     mode = org.omg.CORBA.ParameterMode.PARAM_IN;
253                     if( paramInfo != null && paramInfo.indexOf(' ') > 0 )
254                     {
255                         parameterTypeName =
256                             paramInfo.substring( paramInfo.indexOf(' ')+1);
257                         name =
258                             paramInfo.substring( paramInfo.indexOf(':')+1,
259                                                  paramInfo.indexOf(' '));
260                     }
261                 }
262                 else
263                 {
264                     if ( ! (paramInfo != null && (paramInfo.indexOf(' ') > 0)))
265                     {
266                         throw new INTF_REPOS JavaDoc ("No param info for " + parameterTypeName);
267                     }
268
269                     if( paramInfo.substring(0, (paramInfo.indexOf(' ')-1)).startsWith("inout:"))
270                         mode = org.omg.CORBA.ParameterMode.PARAM_INOUT;
271                     else
272                         mode = org.omg.CORBA.ParameterMode.PARAM_OUT;
273
274                     name = paramInfo.substring( paramInfo.indexOf(':')+1,
275                                                 paramInfo.indexOf(' '));
276
277                     parameterTypeName =
278                        parameterTypeName.substring(0, parameterTypeName.indexOf("Holder"));
279                 }
280
281
282                 if (this.logger.isDebugEnabled())
283                 {
284                     this.logger.debug("Operation " + name() + ", param #"+ i +
285                                       "name: " + name +
286                                       ", paramTypeName " + parameterTypeName +
287                                       paramInfo);
288                 }
289
290                 tc = TypeCodeUtil.getTypeCode( m_params[i],
291                                                this.loader,
292                                                null,
293                                                parameterTypeName,
294                                                this.logger);
295             }
296             catch ( Exception JavaDoc e )
297             {
298                 logger.error("Caught Exception", e);
299                 throw new INTF_REPOS JavaDoc( ErrorMsg.IR_Definition_Not_Found,
300                             org.omg.CORBA.CompletionStatus.COMPLETED_NO);
301             }
302             org.omg.CORBA.IDLType JavaDoc type_def =
303                 IDLType.create( tc, containing_repository,
304                                 this.logger, this.poa );
305             params[i] =
306                 new org.omg.CORBA.ParameterDescription JavaDoc( name, tc, type_def, mode);
307         }
308         return params;
309     }
310
311     public org.omg.CORBA.IDLType JavaDoc result_def()
312     {
313         if ( ! defined)
314         {
315             throw new INTF_REPOS JavaDoc ("OperationDef undefined");
316         }
317         if (result_def == null)
318         {
319             throw new INTF_REPOS JavaDoc ("Result def for op " + name () + " null");
320         }
321         return result_def;
322     }
323
324     public void result_def(org.omg.CORBA.IDLType JavaDoc a)
325     {
326         result_def = a;
327     }
328
329     public org.omg.CORBA.OperationMode JavaDoc mode()
330     {
331         return mode;
332     }
333
334     public void mode( org.omg.CORBA.OperationMode JavaDoc a)
335     {
336         mode = a;
337     }
338
339     public org.omg.CORBA.TypeCode JavaDoc result()
340     {
341         if ( ! defined)
342         {
343             throw new INTF_REPOS JavaDoc ("OperationDeg undefined");
344         }
345
346         return result;
347     }
348
349     public org.omg.CORBA.ParameterDescription JavaDoc[] params()
350     {
351         if( !defined )
352             define();
353         return params;
354     }
355
356     public void params(org.omg.CORBA.ParameterDescription JavaDoc[] a)
357     {
358         params = a;
359     }
360
361     public java.lang.String JavaDoc[] contexts()
362     {
363         if( !defined )
364             define();
365         return contexts;
366     }
367
368     public void contexts(java.lang.String JavaDoc[] a)
369     {
370         contexts = a;
371     }
372
373     public org.omg.CORBA.ExceptionDef JavaDoc[] exceptions()
374     {
375         if( !defined )
376             define();
377         return exceptions;
378     }
379
380     public void exceptions(org.omg.CORBA.ExceptionDef JavaDoc[] a)
381     {
382         exceptions = a;
383     }
384
385     public org.omg.CORBA.OperationDescription JavaDoc describe_operation()
386     {
387         if( !defined )
388             define();
389
390         org.omg.CORBA.ExceptionDescription JavaDoc ex_des[] =
391             new org.omg.CORBA.ExceptionDescription JavaDoc[exceptions.length];
392
393         for( int i = 0; i < exceptions.length; i++ )
394         {
395             org.omg.CORBA.ContainedPackage.Description cd = exceptions[i].describe();
396
397             if( cd.kind != org.omg.CORBA.DefinitionKind.dk_Exception )
398             {
399                 throw new INTF_REPOS JavaDoc( ErrorMsg.IR_Unexpected_Definition_Kind,
400                                                     org.omg.CORBA.CompletionStatus.COMPLETED_NO);
401             }
402             ex_des[i] = org.omg.CORBA.ExceptionDescriptionHelper.extract( cd.value );
403         }
404         return new org.omg.CORBA.OperationDescription JavaDoc(name,
405                                                       id,
406                                                       org.omg.CORBA.ContainedHelper.narrow(defined_in).id(),
407                                                       version,
408                                                       result, mode, contexts, params, ex_des);
409     }
410
411     // from IRObject
412

413     public void destroy()
414     {
415         throw new INTF_REPOS JavaDoc(
416                                            ErrorMsg.IR_Not_Implemented,
417                                            org.omg.CORBA.CompletionStatus.COMPLETED_NO);
418     }
419
420
421     // from Contained
422

423     public org.omg.CORBA.ContainedPackage.Description describe()
424     {
425         if ( ! defined)
426         {
427             throw new INTF_REPOS JavaDoc ("OperationDeg undefined");
428         }
429
430
431         org.omg.CORBA.Any JavaDoc a = orb.create_any();
432         org.omg.CORBA.OperationDescriptionHelper.insert( a, describe_operation() );
433         return new org.omg.CORBA.ContainedPackage.Description( org.omg.CORBA.DefinitionKind.dk_Operation, a);
434     }
435
436
437 }
438
Popular Tags